1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Logging and checks. Avoids a dependency on base. 6 // 7 // LOG(tag) prints messages. Tags are INFO, WARNING, ERROR and FATAL. 8 // INFO prints to stdout, the others to stderr. FATAL aborts after printing. 9 // 10 // LOG_IF(tag, predicate) logs if predicate evaluates to true, else silent. 11 // 12 // VLOG(level) logs INFO messages where level is less than or equal to the 13 // verbosity level set with SetVerbose(). 14 // 15 // VLOG_IF(level, predicate) logs INFO if predicate evaluates to true, 16 // else silent. 17 // 18 // CHECK(predicate) logs a FATAL error if predicate is false. 19 // NOTREACHED() always aborts. 20 // Log streams can be changed with SetStreams(). Logging is not thread-safe. 21 // 22 23 #ifndef TOOLS_RELOCATION_PACKER_SRC_DEBUG_H_ 24 #define TOOLS_RELOCATION_PACKER_SRC_DEBUG_H_ 25 26 #include <limits.h> 27 #include <ostream> 28 #include <sstream> 29 30 namespace relocation_packer { 31 32 class Logger { 33 public: 34 enum Severity {INFO = 0, WARNING, ERROR, FATAL}; 35 36 // Construct a new message logger. Prints if level is less than or 37 // equal to the level set with SetVerbose() and predicate is true. 38 // |severity| is an enumerated severity. 39 // |level| is the verbosity level. 40 // |predicate| controls if the logger prints or is silent. 41 Logger(Severity severity, int level, bool predicate); 42 43 // On destruction, flush and print the strings accumulated in stream_. 44 ~Logger(); 45 46 // Return the stream for this logger. GetStream()47 std::ostream& GetStream() { return stream_; } 48 49 // Set verbosity level. Messages with a level less than or equal to 50 // this level are printed, others are discarded. Static, not thread-safe. SetVerbose(int level)51 static void SetVerbose(int level) { max_level_ = level; } 52 53 // Set info and error logging streams. Static, not thread-safe. SetStreams(std::ostream * info_stream,std::ostream * error_stream)54 static void SetStreams(std::ostream* info_stream, 55 std::ostream* error_stream) { 56 info_stream_ = info_stream; 57 error_stream_ = error_stream; 58 } 59 60 // Reset to initial state. 61 static void Reset(); 62 63 private: 64 // Message severity, verbosity level, and predicate. 65 Severity severity_; 66 int level_; 67 bool predicate_; 68 69 // String stream, accumulates message text. 70 std::ostringstream stream_; 71 72 // Verbosity for INFO messages. Not thread-safe. 73 static int max_level_; 74 75 // Logging streams. Not thread-safe. 76 static std::ostream* info_stream_; 77 static std::ostream* error_stream_; 78 }; 79 80 } // namespace relocation_packer 81 82 // Make logging severities visible globally. 83 typedef relocation_packer::Logger::Severity LogSeverity; 84 using LogSeverity::INFO; 85 using LogSeverity::WARNING; 86 using LogSeverity::ERROR; 87 using LogSeverity::FATAL; 88 89 // LOG(severity) prints a message with the given severity, and aborts if 90 // severity is FATAL. LOG_IF(severity, predicate) does the same but only if 91 // predicate is true. INT_MIN is guaranteed to be less than or equal to 92 // any verbosity level. 93 #define LOG(severity) \ 94 (relocation_packer::Logger(severity, INT_MIN, true).GetStream()) 95 #define LOG_IF(severity, predicate) \ 96 (relocation_packer::Logger(severity, INT_MIN, (predicate)).GetStream()) 97 98 // VLOG(level) prints its message as INFO if level is less than or equal to 99 // the current verbosity level. 100 #define VLOG(level) \ 101 (relocation_packer::Logger(INFO, (level), true).GetStream()) 102 #define VLOG_IF(level, predicate) \ 103 (relocation_packer::Logger(INFO, (level), (predicate)).GetStream()) 104 105 // CHECK(predicate) fails with a FATAL log message if predicate is false. 106 #define CHECK(predicate) (LOG_IF(FATAL, !(predicate)) \ 107 << __FILE__ << ":" << __LINE__ << ": " \ 108 << __FUNCTION__ << ": CHECK '" #predicate "' failed") 109 110 // NOTREACHED() always fails with a FATAL log message. 111 #define NOTREACHED(_) (LOG(FATAL) \ 112 << __FILE__ << ":" << __LINE__ << ": " \ 113 << __FUNCTION__ << ": NOTREACHED() hit") 114 115 #endif // TOOLS_RELOCATION_PACKER_SRC_DEBUG_H_ 116