OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_LOGGING_H_ | 5 #ifndef BASE_LOGGING_H_ |
6 #define BASE_LOGGING_H_ | 6 #define BASE_LOGGING_H_ |
7 | 7 |
8 #include <cassert> | 8 #include <cassert> |
9 #include <string> | 9 #include <string> |
10 #include <cstring> | 10 #include <cstring> |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 // DPLOG(ERROR) << "Couldn't do foo"; | 120 // DPLOG(ERROR) << "Couldn't do foo"; |
121 // PLOG_IF(ERROR, cond) << "Couldn't do foo"; | 121 // PLOG_IF(ERROR, cond) << "Couldn't do foo"; |
122 // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; | 122 // DPLOG_IF(ERROR, cond) << "Couldn't do foo"; |
123 // PCHECK(condition) << "Couldn't do foo"; | 123 // PCHECK(condition) << "Couldn't do foo"; |
124 // DPCHECK(condition) << "Couldn't do foo"; | 124 // DPCHECK(condition) << "Couldn't do foo"; |
125 // | 125 // |
126 // which append the last system error to the message in string form (taken from | 126 // which append the last system error to the message in string form (taken from |
127 // GetLastError() on Windows and errno on POSIX). | 127 // GetLastError() on Windows and errno on POSIX). |
128 // | 128 // |
129 // The supported severity levels for macros that allow you to specify one | 129 // The supported severity levels for macros that allow you to specify one |
130 // are (in increasing order of severity) INFO, WARNING, ERROR, ERROR_REPORT, | 130 // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL. |
131 // and FATAL. | |
132 // | 131 // |
133 // Very important: logging a message at the FATAL severity level causes | 132 // Very important: logging a message at the FATAL severity level causes |
134 // the program to terminate (after the message is logged). | 133 // the program to terminate (after the message is logged). |
135 // | 134 // |
136 // Note the special severity of ERROR_REPORT only available/relevant in normal | 135 // There is the special severity of DFATAL, which logs FATAL in debug mode, |
137 // mode, which displays error dialog without terminating the program. There is | 136 // ERROR in normal mode. |
138 // no error dialog for severity ERROR or below in normal mode. | |
139 // | |
140 // There is also the special severity of DFATAL, which logs FATAL in | |
141 // debug mode, ERROR in normal mode. | |
142 | 137 |
143 namespace logging { | 138 namespace logging { |
144 | 139 |
145 // TODO(avi): do we want to do a unification of character types here? | 140 // TODO(avi): do we want to do a unification of character types here? |
146 #if defined(OS_WIN) | 141 #if defined(OS_WIN) |
147 typedef wchar_t PathChar; | 142 typedef wchar_t PathChar; |
148 #else | 143 #else |
149 typedef char PathChar; | 144 typedef char PathChar; |
150 #endif | 145 #endif |
151 | 146 |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 // Dialogs are not shown by default. | 264 // Dialogs are not shown by default. |
270 BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs); | 265 BASE_EXPORT void SetShowErrorDialogs(bool enable_dialogs); |
271 | 266 |
272 // Sets the Log Assert Handler that will be used to notify of check failures. | 267 // Sets the Log Assert Handler that will be used to notify of check failures. |
273 // The default handler shows a dialog box and then terminate the process, | 268 // The default handler shows a dialog box and then terminate the process, |
274 // however clients can use this function to override with their own handling | 269 // however clients can use this function to override with their own handling |
275 // (e.g. a silent one for Unit Tests) | 270 // (e.g. a silent one for Unit Tests) |
276 typedef void (*LogAssertHandlerFunction)(const std::string& str); | 271 typedef void (*LogAssertHandlerFunction)(const std::string& str); |
277 BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler); | 272 BASE_EXPORT void SetLogAssertHandler(LogAssertHandlerFunction handler); |
278 | 273 |
279 // Sets the Log Report Handler that will be used to notify of check failures | |
280 // in non-debug mode. The default handler shows a dialog box and continues | |
281 // the execution, however clients can use this function to override with their | |
282 // own handling. | |
283 typedef void (*LogReportHandlerFunction)(const std::string& str); | |
284 BASE_EXPORT void SetLogReportHandler(LogReportHandlerFunction handler); | |
285 | |
286 // Sets the Log Message Handler that gets passed every log message before | 274 // Sets the Log Message Handler that gets passed every log message before |
287 // it's sent to other log destinations (if any). | 275 // it's sent to other log destinations (if any). |
288 // Returns true to signal that it handled the message and the message | 276 // Returns true to signal that it handled the message and the message |
289 // should not be sent to other log destinations. | 277 // should not be sent to other log destinations. |
290 typedef bool (*LogMessageHandlerFunction)(int severity, | 278 typedef bool (*LogMessageHandlerFunction)(int severity, |
291 const char* file, int line, size_t message_start, const std::string& str); | 279 const char* file, int line, size_t message_start, const std::string& str); |
292 BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler); | 280 BASE_EXPORT void SetLogMessageHandler(LogMessageHandlerFunction handler); |
293 BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler(); | 281 BASE_EXPORT LogMessageHandlerFunction GetLogMessageHandler(); |
294 | 282 |
295 typedef int LogSeverity; | 283 typedef int LogSeverity; |
296 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity | 284 const LogSeverity LOG_VERBOSE = -1; // This is level 1 verbosity |
297 // Note: the log severities are used to index into the array of names, | 285 // Note: the log severities are used to index into the array of names, |
298 // see log_severity_names. | 286 // see log_severity_names. |
299 const LogSeverity LOG_INFO = 0; | 287 const LogSeverity LOG_INFO = 0; |
300 const LogSeverity LOG_WARNING = 1; | 288 const LogSeverity LOG_WARNING = 1; |
301 const LogSeverity LOG_ERROR = 2; | 289 const LogSeverity LOG_ERROR = 2; |
302 const LogSeverity LOG_ERROR_REPORT = 3; | 290 const LogSeverity LOG_FATAL = 3; |
303 const LogSeverity LOG_FATAL = 4; | 291 const LogSeverity LOG_NUM_SEVERITIES = 4; |
304 const LogSeverity LOG_NUM_SEVERITIES = 5; | |
305 | 292 |
306 // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode | 293 // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode |
307 #ifdef NDEBUG | 294 #ifdef NDEBUG |
308 const LogSeverity LOG_DFATAL = LOG_ERROR; | 295 const LogSeverity LOG_DFATAL = LOG_ERROR; |
309 #else | 296 #else |
310 const LogSeverity LOG_DFATAL = LOG_FATAL; | 297 const LogSeverity LOG_DFATAL = LOG_FATAL; |
311 #endif | 298 #endif |
312 | 299 |
313 // A few definitions of macros that don't generate much code. These are used | 300 // A few definitions of macros that don't generate much code. These are used |
314 // by LOG() and LOG_IF, etc. Since these are used all over our code, it's | 301 // by LOG() and LOG_IF, etc. Since these are used all over our code, it's |
315 // better to have compact code for these operations. | 302 // better to have compact code for these operations. |
316 #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \ | 303 #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \ |
317 logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__) | 304 logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__) |
318 #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \ | 305 #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \ |
319 logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__) | 306 logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__) |
320 #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \ | 307 #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \ |
321 logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__) | 308 logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__) |
322 #define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \ | |
323 logging::ClassName(__FILE__, __LINE__, \ | |
324 logging::LOG_ERROR_REPORT , ##__VA_ARGS__) | |
325 #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \ | 309 #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \ |
326 logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__) | 310 logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__) |
327 #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \ | 311 #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \ |
328 logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__) | 312 logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__) |
329 | 313 |
330 #define COMPACT_GOOGLE_LOG_INFO \ | 314 #define COMPACT_GOOGLE_LOG_INFO \ |
331 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) | 315 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) |
332 #define COMPACT_GOOGLE_LOG_WARNING \ | 316 #define COMPACT_GOOGLE_LOG_WARNING \ |
333 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) | 317 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) |
334 #define COMPACT_GOOGLE_LOG_ERROR \ | 318 #define COMPACT_GOOGLE_LOG_ERROR \ |
335 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) | 319 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) |
336 #define COMPACT_GOOGLE_LOG_ERROR_REPORT \ | |
337 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage) | |
338 #define COMPACT_GOOGLE_LOG_FATAL \ | 320 #define COMPACT_GOOGLE_LOG_FATAL \ |
339 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) | 321 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) |
340 #define COMPACT_GOOGLE_LOG_DFATAL \ | 322 #define COMPACT_GOOGLE_LOG_DFATAL \ |
341 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) | 323 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) |
342 | 324 |
343 #if defined(OS_WIN) | 325 #if defined(OS_WIN) |
344 // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets | 326 // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets |
345 // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us | 327 // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us |
346 // to keep using this syntax, we define this macro to do the same thing | 328 // to keep using this syntax, we define this macro to do the same thing |
347 // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that | 329 // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that |
348 // the Windows SDK does for consistency. | 330 // the Windows SDK does for consistency. |
349 #define ERROR 0 | 331 #define ERROR 0 |
350 #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \ | 332 #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \ |
351 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__) | 333 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__) |
352 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR | 334 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR |
353 // Needed for LOG_IS_ON(ERROR). | 335 // Needed for LOG_IS_ON(ERROR). |
354 const LogSeverity LOG_0 = LOG_ERROR; | 336 const LogSeverity LOG_0 = LOG_ERROR; |
355 #endif | 337 #endif |
356 | 338 |
357 // As special cases, we can assume that LOG_IS_ON(ERROR_REPORT) and | 339 // As special cases, we can assume that LOG_IS_ON(FATAL) always holds. Also, |
358 // LOG_IS_ON(FATAL) always hold. Also, LOG_IS_ON(DFATAL) always holds | 340 // LOG_IS_ON(DFATAL) always holds in debug mode. In particular, CHECK()s will |
359 // in debug mode. In particular, CHECK()s will always fire if they | 341 // always fire if they fail. |
360 // fail. | |
361 #define LOG_IS_ON(severity) \ | 342 #define LOG_IS_ON(severity) \ |
362 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel()) | 343 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel()) |
363 | 344 |
364 // We can't do any caching tricks with VLOG_IS_ON() like the | 345 // We can't do any caching tricks with VLOG_IS_ON() like the |
365 // google-glog version since it requires GCC extensions. This means | 346 // google-glog version since it requires GCC extensions. This means |
366 // that using the v-logging functions in conjunction with --vmodule | 347 // that using the v-logging functions in conjunction with --vmodule |
367 // may be slow. | 348 // may be slow. |
368 #define VLOG_IS_ON(verboselevel) \ | 349 #define VLOG_IS_ON(verboselevel) \ |
369 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__)) | 350 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__)) |
370 | 351 |
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
917 #elif NOTIMPLEMENTED_POLICY == 5 | 898 #elif NOTIMPLEMENTED_POLICY == 5 |
918 #define NOTIMPLEMENTED() do {\ | 899 #define NOTIMPLEMENTED() do {\ |
919 static bool logged_once = false;\ | 900 static bool logged_once = false;\ |
920 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ | 901 LOG_IF(ERROR, !logged_once) << NOTIMPLEMENTED_MSG;\ |
921 logged_once = true;\ | 902 logged_once = true;\ |
922 } while(0);\ | 903 } while(0);\ |
923 EAT_STREAM_PARAMETERS | 904 EAT_STREAM_PARAMETERS |
924 #endif | 905 #endif |
925 | 906 |
926 #endif // BASE_LOGGING_H_ | 907 #endif // BASE_LOGGING_H_ |
OLD | NEW |