diff options
Diffstat (limited to 'icing/util/logging.h')
-rw-r--r-- | icing/util/logging.h | 109 |
1 files changed, 4 insertions, 105 deletions
diff --git a/icing/util/logging.h b/icing/util/logging.h index cbe1102..9d598fe 100644 --- a/icing/util/logging.h +++ b/icing/util/logging.h @@ -15,115 +15,14 @@ #ifndef ICING_UTIL_LOGGING_H_ #define ICING_UTIL_LOGGING_H_ -#include <atomic> -#include <cstdint> -#include <string> +#include "icing/text_classifier/lib3/utils/base/logging.h" -#include "icing/proto/debug.pb.h" - -// This header provides base/logging.h style macros, ICING_LOG and ICING_VLOG, -// for logging in various platforms. The macros use __android_log_write on -// Android, and log to stdout/stderr on others. It also provides a function -// SetLoggingLevel to control the log severity level for ICING_LOG and verbosity -// for ICING_VLOG. namespace icing { namespace lib { -// Whether we should log according to the current logging level. -// The function will always return false when verbosity is negative. -bool ShouldLog(LogSeverity::Code severity, int16_t verbosity = 0); - -// Set the minimal logging priority to be enabled, and the verbose level to see -// from the logs. -// Return false if priority is set higher than VERBOSE but verbosity is not 0. -// The function will always return false when verbosity is negative. -bool SetLoggingLevel(LogSeverity::Code priority, int16_t verbosity = 0); - -// A tiny code footprint string stream for assembling log messages. -struct LoggingStringStream { - LoggingStringStream& stream() { return *this; } - - std::string message; -}; - -template <typename T> -inline LoggingStringStream& operator<<(LoggingStringStream& stream, - const T& entry) { - stream.message.append(std::to_string(entry)); - return stream; -} - -template <typename T> -inline LoggingStringStream& operator<<(LoggingStringStream& stream, - T* const entry) { - stream.message.append( - std::to_string(reinterpret_cast<const uint64_t>(entry))); - return stream; -} - -inline LoggingStringStream& operator<<(LoggingStringStream& stream, - const char* message) { - stream.message.append(message); - return stream; -} - -inline LoggingStringStream& operator<<(LoggingStringStream& stream, - const std::string& message) { - stream.message.append(message); - return stream; -} - -inline LoggingStringStream& operator<<(LoggingStringStream& stream, - std::string_view message) { - stream.message.append(message); - return stream; -} - -template <typename T1, typename T2> -inline LoggingStringStream& operator<<(LoggingStringStream& stream, - const std::pair<T1, T2>& entry) { - stream << "(" << entry.first << ", " << entry.second << ")"; - return stream; -} - -// The class that does all the work behind our ICING_LOG(severity) macros. Each -// ICING_LOG(severity) << obj1 << obj2 << ...; logging statement creates a -// LogMessage temporary object containing a stringstream. Each operator<< adds -// info to that stringstream and the LogMessage destructor performs the actual -// logging. The reason this works is that in C++, "all temporary objects are -// destroyed as the last step in evaluating the full-expression that (lexically) -// contains the point where they were created." For more info, see -// http://en.cppreference.com/w/cpp/language/lifetime. Hence, the destructor is -// invoked after the last << from that logging statement. -class LogMessage { - public: - LogMessage(LogSeverity::Code severity, uint16_t verbosity, - std::string_view tag, const char* file_name, int line_number) - __attribute__((noinline)); - - ~LogMessage() __attribute__((noinline)); - - // Returns the stream associated with the logger object. - LoggingStringStream& stream() { return stream_; } - - private: - const LogSeverity::Code severity_; - const uint16_t verbosity_; - const std::string tag_; - - // Stream that "prints" all info into a string (not to a file). We construct - // here the entire logging message and next print it in one operation. - LoggingStringStream stream_; -}; - -#define ICING_VLOG(verbose_level) \ - ::icing::lib::LogMessage(::icing::lib::LogSeverity::VERBOSE, verbose_level, \ - "icing", __FILE__, __LINE__) \ - .stream() -#define ICING_LOG(severity) \ - ::icing::lib::LogMessage(::icing::lib::LogSeverity::severity, \ - /*verbosity=*/0, "icing", __FILE__, __LINE__) \ - .stream() +// TODO(b/146903474) Add verbose level control +#define ICING_VLOG(verbose_level) TC3_VLOG(verbose_level) +#define ICING_LOG(severity) TC3_LOG(severity) } // namespace lib } // namespace icing |