Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: base/logging.h

Issue 3606003: Revert 61127 - Made logging macros evaluate its stream arguments lazily.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 10 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/leak_tracker.h ('k') | base/logging.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 #include <cstring> 10 #include <cstring>
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 void SetLogMessageHandler(LogMessageHandlerFunction handler); 254 void SetLogMessageHandler(LogMessageHandlerFunction handler);
255 255
256 typedef int LogSeverity; 256 typedef int LogSeverity;
257 const LogSeverity LOG_INFO = 0; 257 const LogSeverity LOG_INFO = 0;
258 const LogSeverity LOG_WARNING = 1; 258 const LogSeverity LOG_WARNING = 1;
259 const LogSeverity LOG_ERROR = 2; 259 const LogSeverity LOG_ERROR = 2;
260 const LogSeverity LOG_ERROR_REPORT = 3; 260 const LogSeverity LOG_ERROR_REPORT = 3;
261 const LogSeverity LOG_FATAL = 4; 261 const LogSeverity LOG_FATAL = 4;
262 const LogSeverity LOG_NUM_SEVERITIES = 5; 262 const LogSeverity LOG_NUM_SEVERITIES = 5;
263 263
264 // LOG_DFATAL is LOG_FATAL in debug mode, ERROR in normal mode 264 // LOG_DFATAL_LEVEL is LOG_FATAL in debug mode, ERROR in normal mode
265 #ifdef NDEBUG 265 #ifdef NDEBUG
266 const LogSeverity LOG_DFATAL = LOG_ERROR; 266 const LogSeverity LOG_DFATAL_LEVEL = LOG_ERROR;
267 #else 267 #else
268 const LogSeverity LOG_DFATAL = LOG_FATAL; 268 const LogSeverity LOG_DFATAL_LEVEL = LOG_FATAL;
269 #endif 269 #endif
270 270
271 // A few definitions of macros that don't generate much code. These are used 271 // A few definitions of macros that don't generate much code. These are used
272 // by LOG() and LOG_IF, etc. Since these are used all over our code, it's 272 // by LOG() and LOG_IF, etc. Since these are used all over our code, it's
273 // better to have compact code for these operations. 273 // better to have compact code for these operations.
274 #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \ 274 #define COMPACT_GOOGLE_LOG_EX_INFO(ClassName, ...) \
275 logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__) 275 logging::ClassName(__FILE__, __LINE__, logging::LOG_INFO , ##__VA_ARGS__)
276 #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \ 276 #define COMPACT_GOOGLE_LOG_EX_WARNING(ClassName, ...) \
277 logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__) 277 logging::ClassName(__FILE__, __LINE__, logging::LOG_WARNING , ##__VA_ARGS__)
278 #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \ 278 #define COMPACT_GOOGLE_LOG_EX_ERROR(ClassName, ...) \
279 logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__) 279 logging::ClassName(__FILE__, __LINE__, logging::LOG_ERROR , ##__VA_ARGS__)
280 #define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \ 280 #define COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(ClassName, ...) \
281 logging::ClassName(__FILE__, __LINE__, \ 281 logging::ClassName(__FILE__, __LINE__, \
282 logging::LOG_ERROR_REPORT , ##__VA_ARGS__) 282 logging::LOG_ERROR_REPORT , ##__VA_ARGS__)
283 #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \ 283 #define COMPACT_GOOGLE_LOG_EX_FATAL(ClassName, ...) \
284 logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__) 284 logging::ClassName(__FILE__, __LINE__, logging::LOG_FATAL , ##__VA_ARGS__)
285 #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \ 285 #define COMPACT_GOOGLE_LOG_EX_DFATAL(ClassName, ...) \
286 logging::ClassName(__FILE__, __LINE__, logging::LOG_DFATAL , ##__VA_ARGS__) 286 logging::ClassName(__FILE__, __LINE__, \
287 logging::LOG_DFATAL_LEVEL , ##__VA_ARGS__)
287 288
288 #define COMPACT_GOOGLE_LOG_INFO \ 289 #define COMPACT_GOOGLE_LOG_INFO \
289 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage) 290 COMPACT_GOOGLE_LOG_EX_INFO(LogMessage)
290 #define COMPACT_GOOGLE_LOG_WARNING \ 291 #define COMPACT_GOOGLE_LOG_WARNING \
291 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage) 292 COMPACT_GOOGLE_LOG_EX_WARNING(LogMessage)
292 #define COMPACT_GOOGLE_LOG_ERROR \ 293 #define COMPACT_GOOGLE_LOG_ERROR \
293 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage) 294 COMPACT_GOOGLE_LOG_EX_ERROR(LogMessage)
294 #define COMPACT_GOOGLE_LOG_ERROR_REPORT \ 295 #define COMPACT_GOOGLE_LOG_ERROR_REPORT \
295 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage) 296 COMPACT_GOOGLE_LOG_EX_ERROR_REPORT(LogMessage)
296 #define COMPACT_GOOGLE_LOG_FATAL \ 297 #define COMPACT_GOOGLE_LOG_FATAL \
297 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage) 298 COMPACT_GOOGLE_LOG_EX_FATAL(LogMessage)
298 #define COMPACT_GOOGLE_LOG_DFATAL \ 299 #define COMPACT_GOOGLE_LOG_DFATAL \
299 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage) 300 COMPACT_GOOGLE_LOG_EX_DFATAL(LogMessage)
300 301
301 // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets 302 // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
302 // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us 303 // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
303 // to keep using this syntax, we define this macro to do the same thing 304 // to keep using this syntax, we define this macro to do the same thing
304 // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that 305 // as COMPACT_GOOGLE_LOG_ERROR, and also define ERROR the same way that
305 // the Windows SDK does for consistency. 306 // the Windows SDK does for consistency.
306 #define ERROR 0 307 #define ERROR 0
307 #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \ 308 #define COMPACT_GOOGLE_LOG_EX_0(ClassName, ...) \
308 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__) 309 COMPACT_GOOGLE_LOG_EX_ERROR(ClassName , ##__VA_ARGS__)
309 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR 310 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
310 // Needed for LOG_IS_ON(ERROR).
311 const LogSeverity LOG_0 = LOG_ERROR;
312
313 #define LOG_IS_ON(severity) \
314 ((::logging::LOG_ ## severity) >= ::logging::GetMinLogLevel())
315
316 // We can't do any caching tricks with VLOG_IS_ON() like the
317 // google-glog version since it requires GCC extensions. This means
318 // that using the v-logging functions in conjunction with --vmodule
319 // may be slow.
320 #define VLOG_IS_ON(verboselevel) \
321 ((verboselevel) <= ::logging::GetVlogLevel(__FILE__))
322
323 // Helper macro which avoids evaluating the arguments to a stream if
324 // the condition doesn't hold.
325 #define LAZY_STREAM(stream, condition) \
326 !(condition) ? (void) 0 : ::logging::LogMessageVoidify() & (stream)
327 311
328 // We use the preprocessor's merging operator, "##", so that, e.g., 312 // We use the preprocessor's merging operator, "##", so that, e.g.,
329 // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny 313 // LOG(INFO) becomes the token COMPACT_GOOGLE_LOG_INFO. There's some funny
330 // subtle difference between ostream member streaming functions (e.g., 314 // subtle difference between ostream member streaming functions (e.g.,
331 // ostream::operator<<(int) and ostream non-member streaming functions 315 // ostream::operator<<(int) and ostream non-member streaming functions
332 // (e.g., ::operator<<(ostream&, string&): it turns out that it's 316 // (e.g., ::operator<<(ostream&, string&): it turns out that it's
333 // impossible to stream something like a string directly to an unnamed 317 // impossible to stream something like a string directly to an unnamed
334 // ostream. We employ a neat hack by calling the stream() member 318 // ostream. We employ a neat hack by calling the stream() member
335 // function of LogMessage which seems to avoid the problem. 319 // function of LogMessage which seems to avoid the problem.
336 #define LOG_STREAM(severity) COMPACT_GOOGLE_LOG_ ## severity.stream() 320 //
321 // We can't do any caching tricks with VLOG_IS_ON() like the
322 // google-glog version since it requires GCC extensions. This means
323 // that using the v-logging functions in conjunction with --vmodule
324 // may be slow.
325 #define VLOG_IS_ON(verboselevel) \
326 (logging::GetVlogLevel(__FILE__) >= (verboselevel))
337 327
338 #define LOG(severity) LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity)) 328 #define LOG(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
339 #define LOG_IF(severity, condition) \
340 LAZY_STREAM(LOG_STREAM(severity), LOG_IS_ON(severity) && (condition))
341
342 #define SYSLOG(severity) LOG(severity) 329 #define SYSLOG(severity) LOG(severity)
343 #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition)
344
345 #define VLOG(verboselevel) LOG_IF(INFO, VLOG_IS_ON(verboselevel)) 330 #define VLOG(verboselevel) LOG_IF(INFO, VLOG_IS_ON(verboselevel))
346 #define VLOG_IF(verboselevel, condition) \
347 LOG_IF(INFO, VLOG_IS_ON(verboselevel) && (condition))
348 331
349 // TODO(akalin): Add more VLOG variants, e.g. VPLOG. 332 // TODO(akalin): Add more VLOG variants, e.g. VPLOG.
350 333
334 #define LOG_IF(severity, condition) \
335 !(condition) ? (void) 0 : logging::LogMessageVoidify() & LOG(severity)
336 #define SYSLOG_IF(severity, condition) LOG_IF(severity, condition)
337 #define VLOG_IF(verboselevel, condition) \
338 LOG_IF(INFO, (condition) && VLOG_IS_ON(verboselevel))
339
351 #define LOG_ASSERT(condition) \ 340 #define LOG_ASSERT(condition) \
352 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " 341 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
353 #define SYSLOG_ASSERT(condition) \ 342 #define SYSLOG_ASSERT(condition) \
354 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". " 343 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
355 344
356 #if defined(OS_WIN) 345 #if defined(OS_WIN)
357 #define LOG_GETLASTERROR_STREAM(severity) \ 346 #define LOG_GETLASTERROR(severity) \
358 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ 347 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
359 ::logging::GetLastSystemErrorCode()).stream() 348 ::logging::GetLastSystemErrorCode()).stream()
360 #define LOG_GETLASTERROR(severity) \ 349 #define LOG_GETLASTERROR_MODULE(severity, module) \
361 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), LOG_IS_ON(severity))
362 #define LOG_GETLASTERROR_MODULE_STREAM(severity, module) \
363 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \ 350 COMPACT_GOOGLE_LOG_EX_ ## severity(Win32ErrorLogMessage, \
364 ::logging::GetLastSystemErrorCode(), module).stream() 351 ::logging::GetLastSystemErrorCode(), module).stream()
365 #define LOG_GETLASTERROR_MODULE(severity, module) \ 352 // PLOG is the usual error logging macro for each platform.
366 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \ 353 #define PLOG(severity) LOG_GETLASTERROR(severity)
367 LOG_IS_ON(severity)) 354 #define DPLOG(severity) DLOG_GETLASTERROR(severity)
368 // PLOG_STREAM is used by PLOG, which is the usual error logging macro
369 // for each platform.
370 #define PLOG_STREAM(severity) LOG_GETLASTERROR_STREAM(severity)
371 #elif defined(OS_POSIX) 355 #elif defined(OS_POSIX)
372 #define LOG_ERRNO_STREAM(severity) \ 356 #define LOG_ERRNO(severity) \
373 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \ 357 COMPACT_GOOGLE_LOG_EX_ ## severity(ErrnoLogMessage, \
374 ::logging::GetLastSystemErrorCode()).stream() 358 ::logging::GetLastSystemErrorCode()).stream()
375 #define LOG_ERRNO(severity) \ 359 // PLOG is the usual error logging macro for each platform.
376 LAZY_STREAM(LOG_ERRNO_STREAM(severity), LOG_IS_ON(severity)) 360 #define PLOG(severity) LOG_ERRNO(severity)
377 // PLOG_STREAM is used by PLOG, which is the usual error logging macro 361 #define DPLOG(severity) DLOG_ERRNO(severity)
378 // for each platform.
379 #define PLOG_STREAM(severity) LOG_ERRNO_STREAM(severity)
380 // TODO(tschmelcher): Should we add OSStatus logging for Mac? 362 // TODO(tschmelcher): Should we add OSStatus logging for Mac?
381 #endif 363 #endif
382 364
383 #define PLOG(severity) \
384 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity))
385
386 #define PLOG_IF(severity, condition) \ 365 #define PLOG_IF(severity, condition) \
387 LAZY_STREAM(PLOG_STREAM(severity), LOG_IS_ON(severity) && (condition)) 366 !(condition) ? (void) 0 : logging::LogMessageVoidify() & PLOG(severity)
388 367
389 // CHECK dies with a fatal error if condition is not true. It is *not* 368 // CHECK dies with a fatal error if condition is not true. It is *not*
390 // controlled by NDEBUG, so the check will be executed regardless of 369 // controlled by NDEBUG, so the check will be executed regardless of
391 // compilation mode. 370 // compilation mode.
392 #define CHECK(condition) \ 371 #define CHECK(condition) \
393 LOG_IF(FATAL, !(condition)) << "Check failed: " #condition ". " 372 LOG_IF(FATAL, !(condition)) << "Check failed: " #condition ". "
394 373
395 #define PCHECK(condition) \ 374 #define PCHECK(condition) \
396 PLOG_IF(FATAL, !(condition)) << "Check failed: " #condition ". " 375 PLOG_IF(FATAL, !(condition)) << "Check failed: " #condition ". "
397 376
(...skipping 29 matching lines...) Expand all
427 extern template std::string* MakeCheckOpString<unsigned long, unsigned int>( 406 extern template std::string* MakeCheckOpString<unsigned long, unsigned int>(
428 const unsigned long&, const unsigned int&, const char* names); 407 const unsigned long&, const unsigned int&, const char* names);
429 extern template std::string* MakeCheckOpString<unsigned int, unsigned long>( 408 extern template std::string* MakeCheckOpString<unsigned int, unsigned long>(
430 const unsigned int&, const unsigned long&, const char* names); 409 const unsigned int&, const unsigned long&, const char* names);
431 extern template std::string* MakeCheckOpString<std::string, std::string>( 410 extern template std::string* MakeCheckOpString<std::string, std::string>(
432 const std::string&, const std::string&, const char* name); 411 const std::string&, const std::string&, const char* name);
433 #endif 412 #endif
434 413
435 // Helper macro for binary operators. 414 // Helper macro for binary operators.
436 // Don't use this macro directly in your code, use CHECK_EQ et al below. 415 // Don't use this macro directly in your code, use CHECK_EQ et al below.
437 // 416 #define CHECK_OP(name, op, val1, val2) \
438 // We don't bother evaluating the arguments lazily since the only way 417 if (logging::CheckOpString _result = \
439 // they wouldn't need to get evaluated was if the user set the logging 418 logging::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
440 // level > LOG_FATAL, which is pretty uncommon. 419 logging::LogMessage(__FILE__, __LINE__, _result).stream()
441 //
442 // TODO(akalin): Rewrite this so that constructs like if (...)
443 // CHECK_EQ(...) else { ... } work properly.
444 #define CHECK_OP(name, op, val1, val2) \
445 if (LOG_IS_ON(FATAL)) \
446 if (logging::CheckOpString _result = \
447 logging::Check##name##Impl((val1), (val2), \
448 #val1 " " #op " " #val2)) \
449 logging::LogMessage(__FILE__, __LINE__, _result).stream()
450 420
451 // Helper functions for string comparisons. 421 // Helper functions for string comparisons.
452 // To avoid bloat, the definitions are in logging.cc. 422 // To avoid bloat, the definitions are in logging.cc.
453 //
454 // TODO(akalin): Actually have the implementations in logging.cc, or
455 // remove these.
456 #define DECLARE_CHECK_STROP_IMPL(func, expected) \ 423 #define DECLARE_CHECK_STROP_IMPL(func, expected) \
457 std::string* Check##func##expected##Impl(const char* s1, \ 424 std::string* Check##func##expected##Impl(const char* s1, \
458 const char* s2, \ 425 const char* s2, \
459 const char* names); 426 const char* names);
460 DECLARE_CHECK_STROP_IMPL(strcmp, true) 427 DECLARE_CHECK_STROP_IMPL(strcmp, true)
461 DECLARE_CHECK_STROP_IMPL(strcmp, false) 428 DECLARE_CHECK_STROP_IMPL(strcmp, false)
462 DECLARE_CHECK_STROP_IMPL(_stricmp, true) 429 DECLARE_CHECK_STROP_IMPL(_stricmp, true)
463 DECLARE_CHECK_STROP_IMPL(_stricmp, false) 430 DECLARE_CHECK_STROP_IMPL(_stricmp, false)
464 #undef DECLARE_CHECK_STROP_IMPL 431 #undef DECLARE_CHECK_STROP_IMPL
465 432
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 #define ENABLE_DCHECK 1 477 #define ENABLE_DCHECK 1
511 478
512 #else 479 #else
513 // Otherwise, we're a debug build so enable DLOGs and DCHECKs. 480 // Otherwise, we're a debug build so enable DLOGs and DCHECKs.
514 #define ENABLE_DLOG 1 481 #define ENABLE_DLOG 1
515 #define ENABLE_DCHECK 1 482 #define ENABLE_DCHECK 1
516 #endif 483 #endif
517 484
518 // Definitions for DLOG et al. 485 // Definitions for DLOG et al.
519 486
487 #if ENABLE_DLOG
488
489 #define DLOG(severity) LOG(severity)
490 #define DLOG_IF(severity, condition) LOG_IF(severity, condition)
491 #define DLOG_ASSERT(condition) LOG_ASSERT(condition)
492
493 #if defined(OS_WIN)
494 #define DLOG_GETLASTERROR(severity) LOG_GETLASTERROR(severity)
495 #define DLOG_GETLASTERROR_MODULE(severity, module) \
496 LOG_GETLASTERROR_MODULE(severity, module)
497 #elif defined(OS_POSIX)
498 #define DLOG_ERRNO(severity) LOG_ERRNO(severity)
499 #endif
500
501 #define DPLOG_IF(severity, condition) PLOG_IF(severity, condition)
502
503 #else // ENABLE_DLOG
504
505 #define DLOG(severity) \
506 true ? (void) 0 : logging::LogMessageVoidify() & LOG(severity)
507
508 #define DLOG_IF(severity, condition) \
509 true ? (void) 0 : logging::LogMessageVoidify() & LOG(severity)
510
511 #define DLOG_ASSERT(condition) \
512 true ? (void) 0 : LOG_ASSERT(condition)
513
514 #if defined(OS_WIN)
515 #define DLOG_GETLASTERROR(severity) \
516 true ? (void) 0 : logging::LogMessageVoidify() & LOG_GETLASTERROR(severity)
517 #define DLOG_GETLASTERROR_MODULE(severity, module) \
518 true ? (void) 0 : logging::LogMessageVoidify() & \
519 LOG_GETLASTERROR_MODULE(severity, module)
520 #elif defined(OS_POSIX)
521 #define DLOG_ERRNO(severity) \
522 true ? (void) 0 : logging::LogMessageVoidify() & LOG_ERRNO(severity)
523 #endif
524
525 #define DPLOG_IF(severity, condition) \
526 true ? (void) 0 : logging::LogMessageVoidify() & PLOG(severity)
527
528 #endif // ENABLE_DLOG
529
520 // DEBUG_MODE is for uses like 530 // DEBUG_MODE is for uses like
521 // if (DEBUG_MODE) foo.CheckThatFoo(); 531 // if (DEBUG_MODE) foo.CheckThatFoo();
522 // instead of 532 // instead of
523 // #ifndef NDEBUG 533 // #ifndef NDEBUG
524 // foo.CheckThatFoo(); 534 // foo.CheckThatFoo();
525 // #endif 535 // #endif
526 // 536 //
527 // We tie its state to ENABLE_DLOG. 537 // We tie its state to ENABLE_DLOG.
528 enum { DEBUG_MODE = ENABLE_DLOG }; 538 enum { DEBUG_MODE = ENABLE_DLOG };
529 539
530 #undef ENABLE_DLOG 540 #undef ENABLE_DLOG
531 541
532 #define DLOG_IS_ON(severity) (::logging::DEBUG_MODE && LOG_IS_ON(severity)) 542 // Definitions for DCHECK et al.
533 543
534 #define DLOG(severity) \ 544 // This macro can be followed by a sequence of stream parameters in
535 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity)) 545 // non-debug mode. The DCHECK and friends macros use this so that
536 #define DLOG_IF(severity, condition) \ 546 // the expanded expression DCHECK(foo) << "asdf" is still syntactically
537 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity) && (condition)) 547 // valid, even though the expression will get optimized away.
538 548 #define DCHECK_EAT_STREAM_PARAMETERS \
539 #define DLOG_ASSERT(condition) \ 549 logging::LogMessage(__FILE__, __LINE__).stream()
540 DLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
541
542 #if defined(OS_WIN)
543 #define DLOG_GETLASTERROR(severity) \
544 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity))
545 #define DLOG_GETLASTERROR_MODULE(severity, module) \
546 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
547 DLOG_IS_ON(severity))
548 #elif defined(OS_POSIX)
549 #define DLOG_ERRNO(severity) \
550 LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity))
551 #endif
552
553 #define DPLOG(severity) \
554 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
555 #define DPLOG_IF(severity, condition) \
556 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity) && (condition))
557
558 #define DVLOG(verboselevel) DLOG_IF(INFO, VLOG_IS_ON(verboselevel))
559 #define DVLOG_IF(verboselevel, condition) \
560 DLOG_IF(INFO, VLOG_IS_ON(verboselevel) && (condition))
561
562 // Definitions for DCHECK et al.
563 550
564 #if ENABLE_DCHECK 551 #if ENABLE_DCHECK
565 552
566 #if defined(NDEBUG) 553 #ifndef NDEBUG
554 // On a regular debug build, we want to have DCHECKS enabled.
555
556 #define DCHECK(condition) CHECK(condition)
557 #define DPCHECK(condition) PCHECK(condition)
558
559 // Helper macro for binary operators.
560 // Don't use this macro directly in your code, use DCHECK_EQ et al below.
561 #define DCHECK_OP(name, op, val1, val2) \
562 if (logging::CheckOpString _result = \
563 logging::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
564 logging::LogMessage(__FILE__, __LINE__, _result).stream()
565
566 // Helper macro for string comparisons.
567 // Don't use this macro directly in your code, use CHECK_STREQ et al below.
568 #define DCHECK_STROP(func, op, expected, s1, s2) \
569 while (CheckOpString _result = \
570 logging::Check##func##expected##Impl((s1), (s2), \
571 #s1 " " #op " " #s2)) \
572 LOG(FATAL) << *_result.str_
573
574 // String (char*) equality/inequality checks.
575 // CASE versions are case-insensitive.
576 //
577 // Note that "s1" and "s2" may be temporary strings which are destroyed
578 // by the compiler at the end of the current "full expression"
579 // (e.g. DCHECK_STREQ(Foo().c_str(), Bar().c_str())).
580
581 #define DCHECK_STREQ(s1, s2) DCHECK_STROP(strcmp, ==, true, s1, s2)
582 #define DCHECK_STRNE(s1, s2) DCHECK_STROP(strcmp, !=, false, s1, s2)
583 #define DCHECK_STRCASEEQ(s1, s2) DCHECK_STROP(_stricmp, ==, true, s1, s2)
584 #define DCHECK_STRCASENE(s1, s2) DCHECK_STROP(_stricmp, !=, false, s1, s2)
585
586 #define DCHECK_INDEX(I,A) DCHECK(I < (sizeof(A)/sizeof(A[0])))
587 #define DCHECK_BOUND(B,A) DCHECK(B <= (sizeof(A)/sizeof(A[0])))
588
589 #else // NDEBUG
590 // On a regular release build we want to be able to enable DCHECKS through the
591 // command line.
567 592
568 // Set to true in InitLogging when we want to enable the dchecks in release. 593 // Set to true in InitLogging when we want to enable the dchecks in release.
569 extern bool g_enable_dcheck; 594 extern bool g_enable_dcheck;
570 #define DCHECK_IS_ON() (::logging::g_enable_dcheck) 595 #define DCHECK(condition) \
571 #define DCHECK_SEVERITY ERROR_REPORT 596 !logging::g_enable_dcheck ? void (0) : \
572 const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT; 597 LOG_IF(ERROR_REPORT, !(condition)) << "Check failed: " #condition ". "
573 598
574 #else // defined(NDEBUG) 599 #define DPCHECK(condition) \
575 600 !logging::g_enable_dcheck ? void (0) : \
576 // On a regular debug build, we want to have DCHECKS enabled. 601 PLOG_IF(ERROR_REPORT, !(condition)) << "Check failed: " #condition ". "
577 #define DCHECK_IS_ON() (true)
578 #define DCHECK_SEVERITY FATAL
579 const LogSeverity LOG_DCHECK = LOG_FATAL;
580
581 #endif // defined(NDEBUG)
582
583 #else // ENABLE_DCHECK
584
585 #define DCHECK_IS_ON() (false)
586 #define DCHECK_SEVERITY FATAL
587 const LogSeverity LOG_DCHECK = LOG_FATAL;
588
589 #endif // ENABLE_DCHECK
590
591 // TODO(akalin): Change to use ERROR_REPORT.
592
593 #define DCHECK(condition) \
594 DLOG_IF(DCHECK_SEVERITY, DCHECK_IS_ON() && !(condition)) \
595 << "Check failed: " #condition ". "
596
597 #define DPCHECK(condition) \
598 DPLOG_IF(DCHECK_SEVERITY, DCHECK_IS_ON() && !(condition)) \
599 << "Check failed: " #condition ". "
600 602
601 // Helper macro for binary operators. 603 // Helper macro for binary operators.
602 // Don't use this macro directly in your code, use DCHECK_EQ et al below. 604 // Don't use this macro directly in your code, use DCHECK_EQ et al below.
603 #define DCHECK_OP(name, op, val1, val2) \ 605 #define DCHECK_OP(name, op, val1, val2) \
604 if (DLOG_IS_ON(DCHECK_SEVERITY)) \ 606 if (logging::g_enable_dcheck) \
605 if (logging::CheckOpString _result = \ 607 if (logging::CheckOpString _result = \
606 logging::Check##name##Impl((val1), (val2), \ 608 logging::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
607 #val1 " " #op " " #val2)) \ 609 logging::LogMessage(__FILE__, __LINE__, logging::LOG_ERROR_REPORT, \
608 logging::LogMessage( \ 610 _result).stream()
609 __FILE__, __LINE__, ::logging::LOG_DCHECK, \ 611
610 _result).stream() 612 #define DCHECK_STREQ(str1, str2) \
613 while (false) DCHECK_EAT_STREAM_PARAMETERS
614
615 #define DCHECK_STRCASEEQ(str1, str2) \
616 while (false) DCHECK_EAT_STREAM_PARAMETERS
617
618 #define DCHECK_STRNE(str1, str2) \
619 while (false) DCHECK_EAT_STREAM_PARAMETERS
620
621 #define DCHECK_STRCASENE(str1, str2) \
622 while (false) DCHECK_EAT_STREAM_PARAMETERS
623
624 #endif // NDEBUG
611 625
612 // Equality/Inequality checks - compare two values, and log a LOG_FATAL message 626 // Equality/Inequality checks - compare two values, and log a LOG_FATAL message
613 // including the two values when the result is not as expected. The values 627 // including the two values when the result is not as expected. The values
614 // must have operator<<(ostream, ...) defined. 628 // must have operator<<(ostream, ...) defined.
615 // 629 //
616 // You may append to the error message like so: 630 // You may append to the error message like so:
617 // DCHECK_NE(1, 2) << ": The world must be ending!"; 631 // DCHECK_NE(1, 2) << ": The world must be ending!";
618 // 632 //
619 // We are very careful to ensure that each argument is evaluated exactly 633 // We are very careful to ensure that each argument is evaluated exactly
620 // once, and that anything which is legal to pass as a function argument is 634 // once, and that anything which is legal to pass as a function argument is
621 // legal here. In particular, the arguments may be temporary expressions 635 // legal here. In particular, the arguments may be temporary expressions
622 // which will end up being destroyed at the end of the apparent statement, 636 // which will end up being destroyed at the end of the apparent statement,
623 // for example: 637 // for example:
624 // DCHECK_EQ(string("abc")[1], 'b'); 638 // DCHECK_EQ(string("abc")[1], 'b');
625 // 639 //
626 // WARNING: These may not compile correctly if one of the arguments is a pointer 640 // WARNING: These may not compile correctly if one of the arguments is a pointer
627 // and the other is NULL. To work around this, simply static_cast NULL to the 641 // and the other is NULL. To work around this, simply static_cast NULL to the
628 // type of the desired pointer. 642 // type of the desired pointer.
629 643
630 #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2) 644 #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
631 #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2) 645 #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
632 #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2) 646 #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
633 #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2) 647 #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2)
634 #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2) 648 #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
635 #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2) 649 #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2)
636 650
637 // Helper macro for string comparisons. 651 #else // ENABLE_DCHECK
638 // Don't use this macro directly in your code, use DCHECK_STREQ et al below.
639 #define DCHECK_STROP(func, op, expected, s1, s2) \
640 if (DCHECK_IS_ON()) CHECK_STROP(func, op, expected, s1, s2)
641 652
642 // String (char*) equality/inequality checks. 653 // In order to avoid variable unused warnings for code that only uses a
643 // CASE versions are case-insensitive. 654 // variable in a CHECK, we make sure to use the macro arguments.
644 //
645 // Note that "s1" and "s2" may be temporary strings which are destroyed
646 // by the compiler at the end of the current "full expression"
647 // (e.g. DCHECK_STREQ(Foo().c_str(), Bar().c_str())).
648 655
649 #define DCHECK_STREQ(s1, s2) DCHECK_STROP(strcmp, ==, true, s1, s2) 656 #define DCHECK(condition) \
650 #define DCHECK_STRNE(s1, s2) DCHECK_STROP(strcmp, !=, false, s1, s2) 657 while (false && (condition)) DCHECK_EAT_STREAM_PARAMETERS
651 #define DCHECK_STRCASEEQ(s1, s2) DCHECK_STROP(_stricmp, ==, true, s1, s2)
652 #define DCHECK_STRCASENE(s1, s2) DCHECK_STROP(_stricmp, !=, false, s1, s2)
653 658
654 #define DCHECK_INDEX(I,A) DCHECK(I < (sizeof(A)/sizeof(A[0]))) 659 #define DPCHECK(condition) \
655 #define DCHECK_BOUND(B,A) DCHECK(B <= (sizeof(A)/sizeof(A[0]))) 660 while (false && (condition)) DCHECK_EAT_STREAM_PARAMETERS
661
662 #define DCHECK_EQ(val1, val2) \
663 while (false && (val1) == (val2)) DCHECK_EAT_STREAM_PARAMETERS
664
665 #define DCHECK_NE(val1, val2) \
666 while (false && (val1) == (val2)) DCHECK_EAT_STREAM_PARAMETERS
667
668 #define DCHECK_LE(val1, val2) \
669 while (false && (val1) == (val2)) DCHECK_EAT_STREAM_PARAMETERS
670
671 #define DCHECK_LT(val1, val2) \
672 while (false && (val1) == (val2)) DCHECK_EAT_STREAM_PARAMETERS
673
674 #define DCHECK_GE(val1, val2) \
675 while (false && (val1) == (val2)) DCHECK_EAT_STREAM_PARAMETERS
676
677 #define DCHECK_GT(val1, val2) \
678 while (false && (val1) == (val2)) DCHECK_EAT_STREAM_PARAMETERS
679
680 #define DCHECK_STREQ(str1, str2) \
681 while (false && (str1) == (str2)) DCHECK_EAT_STREAM_PARAMETERS
682
683 #define DCHECK_STRCASEEQ(str1, str2) \
684 while (false && (str1) == (str2)) DCHECK_EAT_STREAM_PARAMETERS
685
686 #define DCHECK_STRNE(str1, str2) \
687 while (false && (str1) == (str2)) DCHECK_EAT_STREAM_PARAMETERS
688
689 #define DCHECK_STRCASENE(str1, str2) \
690 while (false && (str1) == (str2)) DCHECK_EAT_STREAM_PARAMETERS
691
692 #endif // ENABLE_DCHECK
693 #undef ENABLE_DCHECK
694
695 #undef DCHECK_EAT_STREAM_PARAMETERS
656 696
657 // Helper functions for CHECK_OP macro. 697 // Helper functions for CHECK_OP macro.
658 // The (int, int) specialization works around the issue that the compiler 698 // The (int, int) specialization works around the issue that the compiler
659 // will not instantiate the template version of the function on values of 699 // will not instantiate the template version of the function on values of
660 // unnamed enum type - see comment below. 700 // unnamed enum type - see comment below.
661 #define DEFINE_CHECK_OP_IMPL(name, op) \ 701 #define DEFINE_CHECK_OP_IMPL(name, op) \
662 template <class t1, class t2> \ 702 template <class t1, class t2> \
663 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \ 703 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
664 const char* names) { \ 704 const char* names) { \
665 if (v1 op v2) return NULL; \ 705 if (v1 op v2) return NULL; \
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
898 #elif NOTIMPLEMENTED_POLICY == 4 938 #elif NOTIMPLEMENTED_POLICY == 4
899 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG 939 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
900 #elif NOTIMPLEMENTED_POLICY == 5 940 #elif NOTIMPLEMENTED_POLICY == 5
901 #define NOTIMPLEMENTED() do {\ 941 #define NOTIMPLEMENTED() do {\
902 static int count = 0;\ 942 static int count = 0;\
903 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\ 943 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\
904 } while(0) 944 } while(0)
905 #endif 945 #endif
906 946
907 #endif // BASE_LOGGING_H_ 947 #endif // BASE_LOGGING_H_
OLDNEW
« no previous file with comments | « base/leak_tracker.h ('k') | base/logging.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698