// Copyright 2006 Google Inc. All Rights Reserved. // Author: Maxim Lifantsev // // Logging routines that do not allocate any memory and acquire any // locks, and can therefore be used by low-level memory allocation // and synchronization code. #ifndef BASE_RAW_LOGGING_H__ #define BASE_RAW_LOGGING_H__ @ac_google_start_namespace@ #include "glog/log_severity.h" #include "glog/vlog_is_on.h" // This is similar to LOG(severity) << format... and VLOG(level) << format.., // but // * it is to be used ONLY by low-level modules that can't use normal LOG() // * it is desiged to be a low-level logger that does not allocate any // memory and does not need any locks, hence: // * it logs straight and ONLY to STDERR w/o buffering // * it uses an explicit format and arguments list // * it will silently chop off really long message strings // Usage example: // RAW_LOG(ERROR, "Failed foo with %i: %s", status, error); // RAW_VLOG(3, "status is %i", status); // These will print an almost standard log lines like this to stderr only: // E0821 211317 file.cc:123] RAW: Failed foo with 22: bad_file // I0821 211317 file.cc:142] RAW: status is 20 #define RAW_LOG(severity, ...) \ do { \ @ac_google_namespace@::RawLog__(@ac_google_namespace@::severity, __FILE__, __LINE__, __VA_ARGS__); \ } while (0) #define RAW_VLOG(verboselevel, ...) \ do { \ if (VLOG_IS_ON(verboselevel)) { \ @ac_google_namespace@::RawLog__(@ac_google_namespace@::INFO, __FILE__, __LINE__, __VA_ARGS__); \ } \ } while (0) // Similar to CHECK(condition) << message, // but for low-level modules: we use only RAW_LOG that does not allocate memory. // We do not want to provide args list here to encourage this usage: // if (!cond) RAW_LOG(FATAL, "foo ...", hard_to_compute_args); // so that the args are not computed when not needed. #define RAW_CHECK(condition, message) \ do { \ if (!(condition)) { \ RAW_LOG(FATAL, "Check %s failed: %s", #condition, message); \ } \ } while (0) // Debug versions of RAW_LOG and RAW_CHECK #ifndef NDEBUG #define RAW_DLOG(severity, ...) RAW_LOG(severity, __VA_ARGS__) #define RAW_DCHECK(condition, message) RAW_CHECK(condition, message) #else // NDEBUG #define RAW_DLOG(severity, ...) \ while (false) \ RAW_LOG(severity, __VA_ARGS__) #define RAW_DCHECK(condition, message) \ while (false) \ RAW_CHECK(condition, message) #endif // NDEBUG // Helper function to implement RAW_LOG and RAW_VLOG // Logs format... at "severity" level, reporting it // as called from file:line. // This does not allocate memory or acquire locks. void RawLog__(LogSeverity severity, const char* file, int line, const char* format, ...) @ac_cv___attribute___printf_4_5@; // Hack to propagate time information into this module so that // this module does not have to directly call localtime_r(), // which could allocate memory. extern "C" struct ::tm; void RawLog__SetLastTime(const struct ::tm& t); @ac_google_end_namespace@ #endif // BASE_RAW_LOGGING_H__