From cca6113667e10b1f3405dbe3b1bd3d983f9cf420 Mon Sep 17 00:00:00 2001 From: Gaurav Jain Date: Wed, 9 Oct 2019 07:16:28 -0700 Subject: [PATCH] Add DVLOG macro to discard logs on user builds PiperOrigin-RevId: 273740308 --- tensorflow/core/platform/default/logging.h | 20 ++++++++++++++++++-- tensorflow/core/platform/logging_test.cc | 2 ++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/tensorflow/core/platform/default/logging.h b/tensorflow/core/platform/default/logging.h index 9ca8b12286a..58934cb8340 100644 --- a/tensorflow/core/platform/default/logging.h +++ b/tensorflow/core/platform/default/logging.h @@ -42,7 +42,7 @@ namespace internal { class LogMessage : public std::basic_ostringstream { public: LogMessage(const char* fname, int line, int severity); - ~LogMessage(); + ~LogMessage() override; // Returns the minimum log level for VLOG statements. // E.g., if MinVLogLevel() is 2, then VLOG(2) statements will produce output, @@ -81,7 +81,14 @@ struct Voidifier { class LogMessageFatal : public LogMessage { public: LogMessageFatal(const char* file, int line) TF_ATTRIBUTE_COLD; - TF_ATTRIBUTE_NORETURN ~LogMessageFatal(); + TF_ATTRIBUTE_NORETURN ~LogMessageFatal() override; +}; + +// LogMessageNull supports the DVLOG macro by simply dropping any log messages. +class LogMessageNull : public std::basic_ostringstream { + public: + LogMessageNull() {} + ~LogMessageNull() override {} }; #define _TF_LOG_INFO \ @@ -123,6 +130,15 @@ class LogMessageFatal : public LogMessage { ::tensorflow::internal::LogMessage(__FILE__, __LINE__, \ tensorflow::INFO) +// `DVLOG` behaves like `VLOG` in debug mode (i.e. `#ifndef NDEBUG`). +// Otherwise, it compiles away and does nothing. +#ifndef NDEBUG +#define DVLOG VLOG +#else +#define DVLOG(verbose_level) \ + while (false && (verbose_level) > 0) ::tensorflow::internal::LogMessageNull() +#endif + // CHECK dies with a fatal error if condition is not true. It is *not* // controlled by NDEBUG, so the check will be executed regardless of // compilation mode. Therefore, it is safe to do things like: diff --git a/tensorflow/core/platform/logging_test.cc b/tensorflow/core/platform/logging_test.cc index f395f6419d1..75da1a4e484 100644 --- a/tensorflow/core/platform/logging_test.cc +++ b/tensorflow/core/platform/logging_test.cc @@ -24,6 +24,8 @@ TEST(Logging, Log) { LOG(ERROR) << "Error message"; VLOG(1) << "A VLOG message"; VLOG(2) << "A higher VLOG message"; + DVLOG(1) << "A DVLOG message"; + DVLOG(2) << "A higher DVLOG message"; } TEST(Logging, CheckChecks) {