debug.h revision 116680a4aac90f2aa7413d9095a592090648e557
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 <algorithm> 28#include <ostream> 29#include <sstream> 30 31namespace relocation_packer { 32 33class Logger { 34 public: 35 enum Severity {INFO = 0, WARNING, ERROR, FATAL}; 36 37 // Construct a new message logger. Prints if level is less than or 38 // equal to the level set with SetVerbose() and predicate is true. 39 // |severity| is an enumerated severity. 40 // |level| is the verbosity level. 41 // |predicate| controls if the logger prints or is silent. 42 Logger(Severity severity, int level, bool predicate); 43 44 // On destruction, flush and print the strings accumulated in stream_. 45 ~Logger(); 46 47 // Return the stream for this logger. 48 std::ostream& GetStream() { return stream_; } 49 50 // Set verbosity level. Messages with a level less than or equal to 51 // this level are printed, others are discarded. Static, not thread-safe. 52 static void SetVerbose(int level) { max_level_ = level; } 53 54 // Set info and error logging streams. Static, not thread-safe. 55 static void SetStreams(std::ostream* info_stream, 56 std::ostream* error_stream) { 57 info_stream_ = info_stream; 58 error_stream_ = error_stream; 59 } 60 61 // Reset to initial state. 62 static void Reset(); 63 64 private: 65 // Message severity, verbosity level, and predicate. 66 Severity severity_; 67 int level_; 68 bool predicate_; 69 70 // String stream, accumulates message text. 71 std::ostringstream stream_; 72 73 // Verbosity for INFO messages. Not thread-safe. 74 static int max_level_; 75 76 // Logging streams. Not thread-safe. 77 static std::ostream* info_stream_; 78 static std::ostream* error_stream_; 79}; 80 81} // namespace relocation_packer 82 83// Make logging severities visible globally. 84typedef relocation_packer::Logger::Severity LogSeverity; 85const LogSeverity INFO = relocation_packer::Logger::INFO; 86const LogSeverity WARNING = relocation_packer::Logger::WARNING; 87const LogSeverity ERROR = relocation_packer::Logger::ERROR; 88const LogSeverity FATAL = relocation_packer::Logger::FATAL; 89 90// LOG(severity) prints a message with the given severity, and aborts if 91// severity is FATAL. LOG_IF(severity, predicate) does the same but only if 92// predicate is true. INT_MIN is guaranteed to be less than or equal to 93// any verbosity level. 94#define LOG(severity) \ 95 (relocation_packer::Logger(severity, INT_MIN, true).GetStream()) 96#define LOG_IF(severity, predicate) \ 97 (relocation_packer::Logger(severity, INT_MIN, (predicate)).GetStream()) 98 99// VLOG(level) prints its message as INFO if level is less than or equal to 100// the current verbosity level. 101#define VLOG(level) \ 102 (relocation_packer::Logger(INFO, (level), true).GetStream()) 103#define VLOG_IF(level, predicate) \ 104 (relocation_packer::Logger(INFO, (level), (predicate)).GetStream()) 105 106// CHECK(predicate) fails with a FATAL log message if predicate is false. 107#define CHECK(predicate) (LOG_IF(FATAL, !(predicate)) \ 108 << __FILE__ << ":" << __LINE__ << ": " \ 109 << __FUNCTION__ << ": CHECK '" #predicate "' failed") 110 111// NOTREACHED() always fails with a FATAL log message. 112#define NOTREACHED(_) (LOG(FATAL) \ 113 << __FILE__ << ":" << __LINE__ << ": " \ 114 << __FUNCTION__ << ": NOTREACHED() hit") 115 116#endif // TOOLS_RELOCATION_PACKER_SRC_DEBUG_H_ 117