From 49bb08c055889f90d380d5c28ca7506e02c7e44a Mon Sep 17 00:00:00 2001 From: Vo Van Nghia Date: Sat, 1 Aug 2020 16:04:53 +0700 Subject: [PATCH] Add c interface for `core/platform:logging` --- tensorflow/c/BUILD | 11 ++++++++ tensorflow/c/logging.cc | 59 +++++++++++++++++++++++++++++++++++++++++ tensorflow/c/logging.h | 42 +++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 tensorflow/c/logging.cc create mode 100644 tensorflow/c/logging.h diff --git a/tensorflow/c/BUILD b/tensorflow/c/BUILD index 410fc22069f..5f64c43dfd3 100644 --- a/tensorflow/c/BUILD +++ b/tensorflow/c/BUILD @@ -213,6 +213,17 @@ tf_cuda_library( alwayslink = 1, ) +cc_library( + name = "logging", + srcs = ["logging.cc"], + hdrs = ["logging.h"], + deps = [ + ":c_api", + "//tensorflow/core/platform:logging", + "//tensorflow/core/platform:stringprintf", + ], +) + tf_cuda_library( name = "tf_status_internal", hdrs = [ diff --git a/tensorflow/c/logging.cc b/tensorflow/c/logging.cc new file mode 100644 index 00000000000..bf6bf069fff --- /dev/null +++ b/tensorflow/c/logging.cc @@ -0,0 +1,59 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ +#include "tensorflow/c/logging.h" + +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/platform/stringprintf.h" + +static ::tensorflow::string BuildMessage(const char* fmt, va_list args) { + ::tensorflow::string message; + ::tensorflow::strings::Appendv(&message, fmt, args); + return message; +} + +void TF_Log(TF_LogLevel level, const char* fmt, ...) { + if (level < TF_INFO || level > TF_FATAL) return; + va_list args; + va_start(args, fmt); + auto message = BuildMessage(fmt, args); + switch (level) { + case TF_INFO: + LOG(INFO) << message; + break; + case TF_WARNING: + LOG(WARNING) << message; + break; + case TF_ERROR: + LOG(ERROR) << message; + break; + case TF_FATAL: + LOG(FATAL) << message; + break; + } +} + +void TF_VLog(int level, const char* fmt, ...) { + va_list args; + va_start(args, fmt); + auto message = BuildMessage(fmt, args); + VLOG(level) << message; +} + +void TF_DVLog(int level, const char* fmt, ...) { + va_list args; + va_start(args, fmt); + auto message = BuildMessage(fmt, args); + DVLOG(level) << message; +} diff --git a/tensorflow/c/logging.h b/tensorflow/c/logging.h new file mode 100644 index 00000000000..ad97cbf8c8a --- /dev/null +++ b/tensorflow/c/logging.h @@ -0,0 +1,42 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +==============================================================================*/ +#ifndef TENSORFLOW_C_LOGGING_H_ +#define TENSORFLOW_C_LOGGING_H_ + +#include "tensorflow/c/c_api.h" + +// -------------------------------------------------------------------------- +// C API for tensorflow::Logging. + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum TF_LogLevel { + TF_INFO = 0, + TF_WARNING = 1, + TF_ERROR = 2, + TF_FATAL = 3, +} TF_LogLevel; + +TF_CAPI_EXPORT extern void TF_Log(TF_LogLevel level, const char* fmt, ...); +TF_CAPI_EXPORT extern void TF_VLog(int level, const char* fmt, ...); +TF_CAPI_EXPORT extern void TF_DVLog(int level, const char* fmt, ...); + +#ifdef __cplusplus +} +#endif + +#endif // TENSORFLOW_C_LOGGING_H_