[AiS] Mac AiS on two lines
[chromium-blink-merge.git] / tools / relocation_packer / src / debug.h
blob99db94862c5c11df68e53b4bbb4eed9dceb02355
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.
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.
12 // VLOG(level) logs INFO messages where level is less than or equal to the
13 // verbosity level set with SetVerbose().
15 // VLOG_IF(level, predicate) logs INFO if predicate evaluates to true,
16 // else silent.
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.
23 #ifndef TOOLS_RELOCATION_PACKER_SRC_DEBUG_H_
24 #define TOOLS_RELOCATION_PACKER_SRC_DEBUG_H_
26 #include <limits.h>
27 #include <ostream>
28 #include <sstream>
30 namespace relocation_packer {
32 class Logger {
33 public:
34 enum Severity {INFO = 0, WARNING, ERROR, FATAL};
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);
43 // On destruction, flush and print the strings accumulated in stream_.
44 ~Logger();
46 // Return the stream for this logger.
47 std::ostream& GetStream() { return stream_; }
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.
51 static void SetVerbose(int level) { max_level_ = level; }
53 // Set info and error logging streams. Static, not thread-safe.
54 static void SetStreams(std::ostream* info_stream,
55 std::ostream* error_stream) {
56 info_stream_ = info_stream;
57 error_stream_ = error_stream;
60 // Reset to initial state.
61 static void Reset();
63 private:
64 // Message severity, verbosity level, and predicate.
65 Severity severity_;
66 int level_;
67 bool predicate_;
69 // String stream, accumulates message text.
70 std::ostringstream stream_;
72 // Verbosity for INFO messages. Not thread-safe.
73 static int max_level_;
75 // Logging streams. Not thread-safe.
76 static std::ostream* info_stream_;
77 static std::ostream* error_stream_;
80 } // namespace relocation_packer
82 // Make logging severities visible globally.
83 typedef relocation_packer::Logger::Severity LogSeverity;
84 static const LogSeverity INFO = LogSeverity::INFO;
85 static const LogSeverity WARNING = LogSeverity::WARNING;
86 static const LogSeverity ERROR = LogSeverity::ERROR;
87 static const LogSeverity FATAL = LogSeverity::FATAL;
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())
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())
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")
110 // NOTREACHED() always fails with a FATAL log message.
111 #define NOTREACHED(_) (LOG(FATAL) \
112 << __FILE__ << ":" << __LINE__ << ": " \
113 << __FUNCTION__ << ": NOTREACHED() hit")
115 #endif // TOOLS_RELOCATION_PACKER_SRC_DEBUG_H_