Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
logging.h
Go to the documentation of this file.
1 
19 #ifndef ION_BASE_LOGGING_H_
20 #define ION_BASE_LOGGING_H_
21 
22 #include <functional>
23 #include <memory>
24 #include <sstream>
25 #include <string>
26 
27 #include "ion/port/logging.h"
28 
36 
37 namespace ion {
38 namespace base {
39 
47 ION_API void SetLogEntryWriter(port::LogEntryWriter* writer);
48 
50 ION_API port::LogEntryWriter* GetLogEntryWriter();
51 
54 ION_API port::LogEntryWriter* GetDefaultLogEntryWriter();
55 
60 ION_API void SetBreakHandler(const std::function<void()>& break_handler);
61 
63 ION_API void RestoreDefaultBreakHandler();
64 
68 namespace logging_internal {
69 
73 class ION_API Logger {
74  public:
75  Logger(const char* file_name, int line_number, port::LogSeverity severity);
76  ~Logger();
77 
79  std::ostream& GetStream();
80 
82  static const std::string CheckMessage(const char* check_string,
83  const char* expr_string);
84 
85  private:
86  port::LogSeverity severity_;
87  std::ostringstream stream_;
88 };
89 
93 class ION_API NullLogger {
94  public:
97 
100  explicit NullLogger(port::LogSeverity severity);
102 
104  std::ostream& GetStream();
105 };
106 
110 class ION_API SingleLogger {
111  public:
112  SingleLogger(const char* file_name, int line_number,
113  port::LogSeverity severity);
114  ~SingleLogger();
115 
117  std::ostream& GetStream();
118 
121  static void ClearMessages();
122 
123  private:
124  static bool HasLoggedMessageAt(const char* file_name, int line_number);
125 
126  std::unique_ptr<Logger> logger_;
127 };
128 
133 class ION_API ThrottledLogger {
134  public:
135  ThrottledLogger(const char* file_name, int line_number,
136  port::LogSeverity severity, float seconds);
137  ~ThrottledLogger();
138 
140  std::ostream& GetStream();
141 
142  private:
143  std::unique_ptr<Logger> logger_;
144 };
145 
146 } // namespace logging_internal
147 } // namespace base
148 } // namespace ion
149 
155 
157 #if ION_PRODUCTION
158 
160 #define ION_LOG(severity) \
161  ::ion::base::logging_internal::NullLogger(::ion::port::severity).GetStream()
162 
164 #define ION_IGNORE_LOG(severity) \
165  ::ion::base::logging_internal::NullLogger(::ion::port::severity).GetStream()
166 
168 #define ION_LOG_ONCE(severity) \
169  ::ion::base::logging_internal::NullLogger(::ion::port::severity).GetStream()
170 
172 #define ION_LOG_EVERY_N_SEC(severity, time) \
173  ::ion::base::logging_internal::NullLogger(::ion::port::severity).GetStream()
174 
175 #else
176 
178 #define ION_LOG(severity) \
179  ::ion::base::logging_internal::Logger(__FILE__, __LINE__, \
180  ::ion::port::severity).GetStream()
181 
183 #define ION_IGNORE_LOG(severity) \
184  ::ion::base::logging_internal::NullLogger().GetStream()
185 
187 #define ION_LOG_ONCE(severity) \
188  ::ion::base::logging_internal::SingleLogger( \
189  __FILE__, __LINE__, ::ion::port::severity).GetStream()
190 
192 #define ION_LOG_EVERY_N_SEC(severity, time) \
193  ::ion::base::logging_internal::ThrottledLogger( \
194  __FILE__, __LINE__, ::ion::port::severity, time).GetStream()
195 
196 #endif
197 
199 
214 
216 #define LOG(severity) ION_LOG(severity)
217 
219 #define LOG_ONCE(severity) ION_LOG_ONCE(severity)
220 
223 #define LOG_EVERY_N_SEC(severity, time) ION_LOG_EVERY_N_SEC(severity, time)
224 #if ION_DEBUG
225 #define DLOG(severity) ION_LOG(severity)
226 #define DLOG_ONCE(severity) ION_LOG_ONCE(severity)
227 #define DLOG_EVERY_N_SEC(severity, time) ION_LOG_EVERY_N_SEC(severity, time)
228 #else
229 #define DLOG(severity) ION_IGNORE_LOG(severity)
231 
233 #define DLOG_ONCE(severity) ION_IGNORE_LOG(severity)
234 
236 #define DLOG_EVERY_N_SEC(severity, time) ION_IGNORE_LOG(severity)
237 #endif
238 
245 #define ION_LOG_CHECK_MESSAGE(severity, check_type, expr) \
246  LOG(severity) << ::ion::base::logging_internal::Logger::CheckMessage( \
247  check_type, #expr)
248 
249 #define ION_CHECK(expr) \
250  if (expr) \
251  ; \
252  else \
253  ION_LOG_CHECK_MESSAGE(FATAL, "CHECK", #expr)
254 
256 #if ION_DEBUG
257 #define ION_DCHECK(expr) \
258  if (expr) \
259  ; \
260  else \
261  ION_LOG_CHECK_MESSAGE(DFATAL, "DCHECK", #expr)
262 #else
263 #if defined(ION_PLATFORM_WINDOWS)
292 #define ION_DCHECK(expr) \
293  (true) ? \
294  (void)(expr) : \
295  ::ion::base::logging_internal::NullLogger().GetStream()
296 #else // defined(ION_PLATFORM_WINDOWS)
297 #define ION_DCHECK(expr) \
298  if (true || (false && (expr))) \
299  ; \
300  else \
301  ::ion::base::logging_internal::NullLogger().GetStream()
302 #endif // defined(ION_PLATFORM_WINDOWS)
303 #endif // ION_DEBUG
304 
305 #define ION_CHECK_OP(op, val1, val2) \
306  ION_CHECK((val1) op (val2)) << "(" << (val1) << " " << #op /* NOLINT */ \
307  << " " << (val2) << ")\n"
308 #define ION_DCHECK_OP(op, val1, val2) \
309  ION_DCHECK((val1) op (val2)) << "(" << (val1) << " " << #op /* NOLINT */ \
310  << " " << (val2) << ")\n"
311 
323 #define CHECK(expr) ION_CHECK(expr)
324 #define CHECK_EQ(val1, val2) ION_CHECK_OP(==, val1, val2) // NOLINT
325 #define CHECK_NE(val1, val2) ION_CHECK_OP(!=, val1, val2) // NOLINT
326 #define CHECK_LE(val1, val2) ION_CHECK_OP(<=, val1, val2) // NOLINT
327 #define CHECK_LT(val1, val2) ION_CHECK_OP(<, val1, val2) // NOLINT
328 #define CHECK_GE(val1, val2) ION_CHECK_OP(>=, val1, val2) // NOLINT
329 #define CHECK_GT(val1, val2) ION_CHECK_OP(>, val1, val2) // NOLINT
330 
331 #define DCHECK(expr) ION_DCHECK(expr)
332 #define DCHECK_EQ(val1, val2) ION_DCHECK_OP(==, val1, val2) // NOLINT
333 #define DCHECK_NE(val1, val2) ION_DCHECK_OP(!=, val1, val2) // NOLINT
334 #define DCHECK_LE(val1, val2) ION_DCHECK_OP(<=, val1, val2) // NOLINT
335 #define DCHECK_LT(val1, val2) ION_DCHECK_OP(<, val1, val2) // NOLINT
336 #define DCHECK_GE(val1, val2) ION_DCHECK_OP(>=, val1, val2) // NOLINT
337 #define DCHECK_GT(val1, val2) ION_DCHECK_OP(>, val1, val2) // NOLINT
338 
339 namespace ion {
340 namespace base {
341 namespace logging_internal {
342 
345 template <typename T>
346 T& CheckNotNullCommon(const char* expr_string, T& t) { // NOLINT
347  if (t == NULL) {
348  ION_LOG(FATAL) << Logger::CheckMessage("CHECK_NOTNULL", expr_string);
349  }
350  return t;
351 }
352 
353 template <typename T>
354 T* CheckNotNull(const char* expr_string, T* t) { // NOLINT
355  return CheckNotNullCommon(expr_string, t);
356 }
357 
358 template <typename T>
359 T& CheckNotNull(const char* expr_string, T& t) { // NOLINT
360  return CheckNotNullCommon(expr_string, t);
361 }
362 
370 template <typename T>
371 const T& CheckNotNull(const char* expr_string, const T& t) { // NOLINT
372  return CheckNotNullCommon(expr_string, t);
373 }
374 
375 } // namespace logging_internal
376 } // namespace base
377 } // namespace ion
378 
382 #define CHECK_NOTNULL(val) \
383  ::ion::base::logging_internal::CheckNotNull(\
384  "'" #val "' Must be non NULL", (val))
385 
387 
388 #endif // ION_BASE_LOGGING_H_
This class prints a message only if the passed file_name and line_number has not printed a message in...
Definition: logging.h:133
static const std::string CheckMessage(const char *check_string, const char *expr_string)
Returns a message that can be used in CHECK or DCHECK output.
Definition: logging.cc:134
#define ION_LOG(severity)
Completely disable logging in production builds.
Definition: logging.h:178
port::LogEntryWriter * GetDefaultLogEntryWriter()
Returns the log-writer that messages will be logged to if if another is not explicitly specified via ...
Definition: logging.cc:197
LogSeverity
Definition: logging.h:26
T & CheckNotNullCommon(const char *expr_string, T &t)
Helpers for CHECK_NOTNULL().
Definition: logging.h:346
T * CheckNotNull(const char *expr_string, T *t)
Definition: logging.h:354
NullLogger()
Constructs a NullLogger that does nothing.
Definition: logging.h:96
port::LogEntryWriter * GetLogEntryWriter()
Returns the log-writer that messages are currently logged to.
Definition: logging.cc:192
This class is used to disable logging, while still allowing for log messages to contain '<<' expres...
Definition: logging.h:93
This class prints a message only the first time it is created for the passed file_name and line_numbe...
Definition: logging.h:110
void RestoreDefaultBreakHandler()
Restores the default break handler (port::BreakAndAbort).
Definition: logging.cc:211
void SetLogEntryWriter(port::LogEntryWriter *w)
Public functions.
Definition: logging.cc:188
void SetBreakHandler(const std::function< void()> &break_handler)
Sets a custom break handler that gets invoked by Logger::CheckMessage.
Definition: logging.cc:205
This class is used for regular logging.
Definition: logging.h:73