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

Side by Side Diff: base/logging.h

Issue 3527004: Revert 61142 - 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 // 371 #define CHECK(condition) \
393 // We make sure CHECK et al. always evaluates their arguments, as 372 LOG_IF(FATAL, !(condition)) << "Check failed: " #condition ". "
394 // doing CHECK(FunctionWithSideEffect()) is a common idiom.
395 //
396 // TODO(akalin): Fix the problem where if the min log level is >
397 // FATAL, CHECK() et al. won't terminate the program.
398 #define CHECK(condition) \
399 LAZY_STREAM(LOG_STREAM(FATAL), !(condition)) \
400 << "Check failed: " #condition ". "
401 373
402 #define PCHECK(condition) \ 374 #define PCHECK(condition) \
403 LAZY_STREAM(PLOG_STREAM(FATAL), !(condition)) \ 375 PLOG_IF(FATAL, !(condition)) << "Check failed: " #condition ". "
404 << "Check failed: " #condition ". "
405 376
406 // A container for a string pointer which can be evaluated to a bool - 377 // A container for a string pointer which can be evaluated to a bool -
407 // true iff the pointer is NULL. 378 // true iff the pointer is NULL.
408 struct CheckOpString { 379 struct CheckOpString {
409 CheckOpString(std::string* str) : str_(str) { } 380 CheckOpString(std::string* str) : str_(str) { }
410 // No destructor: if str_ is non-NULL, we're about to LOG(FATAL), 381 // No destructor: if str_ is non-NULL, we're about to LOG(FATAL),
411 // so there's no point in cleaning up str_. 382 // so there's no point in cleaning up str_.
412 operator bool() const { return str_ != NULL; } 383 operator bool() const { return str_ != NULL; }
413 std::string* str_; 384 std::string* str_;
414 }; 385 };
(...skipping 20 matching lines...) Expand all
435 extern template std::string* MakeCheckOpString<unsigned long, unsigned int>( 406 extern template std::string* MakeCheckOpString<unsigned long, unsigned int>(
436 const unsigned long&, const unsigned int&, const char* names); 407 const unsigned long&, const unsigned int&, const char* names);
437 extern template std::string* MakeCheckOpString<unsigned int, unsigned long>( 408 extern template std::string* MakeCheckOpString<unsigned int, unsigned long>(
438 const unsigned int&, const unsigned long&, const char* names); 409 const unsigned int&, const unsigned long&, const char* names);
439 extern template std::string* MakeCheckOpString<std::string, std::string>( 410 extern template std::string* MakeCheckOpString<std::string, std::string>(
440 const std::string&, const std::string&, const char* name); 411 const std::string&, const std::string&, const char* name);
441 #endif 412 #endif
442 413
443 // Helper macro for binary operators. 414 // Helper macro for binary operators.
444 // 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.
445 // 416 #define CHECK_OP(name, op, val1, val2) \
446 // TODO(akalin): Rewrite this so that constructs like if (...) 417 if (logging::CheckOpString _result = \
447 // CHECK_EQ(...) else { ... } work properly. 418 logging::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
448 #define CHECK_OP(name, op, val1, val2) \
449 if (logging::CheckOpString _result = \
450 logging::Check##name##Impl((val1), (val2), \
451 #val1 " " #op " " #val2)) \
452 logging::LogMessage(__FILE__, __LINE__, _result).stream() 419 logging::LogMessage(__FILE__, __LINE__, _result).stream()
453 420
454 // Helper functions for string comparisons. 421 // Helper functions for string comparisons.
455 // To avoid bloat, the definitions are in logging.cc. 422 // To avoid bloat, the definitions are in logging.cc.
456 //
457 // TODO(akalin): Actually have the implementations in logging.cc, or
458 // remove these.
459 #define DECLARE_CHECK_STROP_IMPL(func, expected) \ 423 #define DECLARE_CHECK_STROP_IMPL(func, expected) \
460 std::string* Check##func##expected##Impl(const char* s1, \ 424 std::string* Check##func##expected##Impl(const char* s1, \
461 const char* s2, \ 425 const char* s2, \
462 const char* names); 426 const char* names);
463 DECLARE_CHECK_STROP_IMPL(strcmp, true) 427 DECLARE_CHECK_STROP_IMPL(strcmp, true)
464 DECLARE_CHECK_STROP_IMPL(strcmp, false) 428 DECLARE_CHECK_STROP_IMPL(strcmp, false)
465 DECLARE_CHECK_STROP_IMPL(_stricmp, true) 429 DECLARE_CHECK_STROP_IMPL(_stricmp, true)
466 DECLARE_CHECK_STROP_IMPL(_stricmp, false) 430 DECLARE_CHECK_STROP_IMPL(_stricmp, false)
467 #undef DECLARE_CHECK_STROP_IMPL 431 #undef DECLARE_CHECK_STROP_IMPL
468 432
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 #define ENABLE_DCHECK 1 477 #define ENABLE_DCHECK 1
514 478
515 #else 479 #else
516 // Otherwise, we're a debug build so enable DLOGs and DCHECKs. 480 // Otherwise, we're a debug build so enable DLOGs and DCHECKs.
517 #define ENABLE_DLOG 1 481 #define ENABLE_DLOG 1
518 #define ENABLE_DCHECK 1 482 #define ENABLE_DCHECK 1
519 #endif 483 #endif
520 484
521 // Definitions for DLOG et al. 485 // Definitions for DLOG et al.
522 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
523 // DEBUG_MODE is for uses like 530 // DEBUG_MODE is for uses like
524 // if (DEBUG_MODE) foo.CheckThatFoo(); 531 // if (DEBUG_MODE) foo.CheckThatFoo();
525 // instead of 532 // instead of
526 // #ifndef NDEBUG 533 // #ifndef NDEBUG
527 // foo.CheckThatFoo(); 534 // foo.CheckThatFoo();
528 // #endif 535 // #endif
529 // 536 //
530 // We tie its state to ENABLE_DLOG. 537 // We tie its state to ENABLE_DLOG.
531 enum { DEBUG_MODE = ENABLE_DLOG }; 538 enum { DEBUG_MODE = ENABLE_DLOG };
532 539
533 #undef ENABLE_DLOG 540 #undef ENABLE_DLOG
534 541
535 #define DLOG_IS_ON(severity) (::logging::DEBUG_MODE && LOG_IS_ON(severity)) 542 // Definitions for DCHECK et al.
536 543
537 #define DLOG(severity) \ 544 // This macro can be followed by a sequence of stream parameters in
538 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity)) 545 // non-debug mode. The DCHECK and friends macros use this so that
539 #define DLOG_IF(severity, condition) \ 546 // the expanded expression DCHECK(foo) << "asdf" is still syntactically
540 LAZY_STREAM(LOG_STREAM(severity), DLOG_IS_ON(severity) && (condition)) 547 // valid, even though the expression will get optimized away.
541 548 #define DCHECK_EAT_STREAM_PARAMETERS \
542 #define DLOG_ASSERT(condition) \ 549 logging::LogMessage(__FILE__, __LINE__).stream()
543 DLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition ". "
544
545 #if defined(OS_WIN)
546 #define DLOG_GETLASTERROR(severity) \
547 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity), DLOG_IS_ON(severity))
548 #define DLOG_GETLASTERROR_MODULE(severity, module) \
549 LAZY_STREAM(LOG_GETLASTERROR_STREAM(severity, module), \
550 DLOG_IS_ON(severity))
551 #elif defined(OS_POSIX)
552 #define DLOG_ERRNO(severity) \
553 LAZY_STREAM(LOG_ERRNO_STREAM(severity), DLOG_IS_ON(severity))
554 #endif
555
556 #define DPLOG(severity) \
557 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity))
558 #define DPLOG_IF(severity, condition) \
559 LAZY_STREAM(PLOG_STREAM(severity), DLOG_IS_ON(severity) && (condition))
560
561 #define DVLOG(verboselevel) DLOG_IF(INFO, VLOG_IS_ON(verboselevel))
562 #define DVLOG_IF(verboselevel, condition) \
563 DLOG_IF(INFO, VLOG_IS_ON(verboselevel) && (condition))
564
565 // Definitions for DCHECK et al.
566 550
567 #if ENABLE_DCHECK 551 #if ENABLE_DCHECK
568 552
569 #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.
570 592
571 // 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.
572 extern bool g_enable_dcheck; 594 extern bool g_enable_dcheck;
573 #define DCHECK_IS_ON() (::logging::g_enable_dcheck) 595 #define DCHECK(condition) \
574 #define DCHECK_SEVERITY ERROR_REPORT 596 !logging::g_enable_dcheck ? void (0) : \
575 const LogSeverity LOG_DCHECK = LOG_ERROR_REPORT; 597 LOG_IF(ERROR_REPORT, !(condition)) << "Check failed: " #condition ". "
576 598
577 #else // defined(NDEBUG) 599 #define DPCHECK(condition) \
578 600 !logging::g_enable_dcheck ? void (0) : \
579 // On a regular debug build, we want to have DCHECKS enabled. 601 PLOG_IF(ERROR_REPORT, !(condition)) << "Check failed: " #condition ". "
580 #define DCHECK_IS_ON() (true)
581 #define DCHECK_SEVERITY FATAL
582 const LogSeverity LOG_DCHECK = LOG_FATAL;
583
584 #endif // defined(NDEBUG)
585
586 #else // ENABLE_DCHECK
587
588 #define DCHECK_IS_ON() (false)
589 #define DCHECK_SEVERITY FATAL
590 const LogSeverity LOG_DCHECK = LOG_FATAL;
591
592 #endif // ENABLE_DCHECK
593
594 // Unlike CHECK et al., DCHECK et al. *does* evaluate their arguments
595 // lazily.
596
597 #define DCHECK(condition) \
598 DLOG_IF(DCHECK_SEVERITY, DCHECK_IS_ON() && !(condition)) \
599 << "Check failed: " #condition ". "
600
601 #define DPCHECK(condition) \
602 DPLOG_IF(DCHECK_SEVERITY, DCHECK_IS_ON() && !(condition)) \
603 << "Check failed: " #condition ". "
604 602
605 // Helper macro for binary operators. 603 // Helper macro for binary operators.
606 // 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.
607 #define DCHECK_OP(name, op, val1, val2) \ 605 #define DCHECK_OP(name, op, val1, val2) \
608 if (DLOG_IS_ON(DCHECK_SEVERITY)) \ 606 if (logging::g_enable_dcheck) \
609 if (logging::CheckOpString _result = \ 607 if (logging::CheckOpString _result = \
610 logging::Check##name##Impl((val1), (val2), \ 608 logging::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
611 #val1 " " #op " " #val2)) \ 609 logging::LogMessage(__FILE__, __LINE__, logging::LOG_ERROR_REPORT, \
612 logging::LogMessage( \ 610 _result).stream()
613 __FILE__, __LINE__, ::logging::LOG_DCHECK, \ 611
614 _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
615 625
616 // 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
617 // 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
618 // must have operator<<(ostream, ...) defined. 628 // must have operator<<(ostream, ...) defined.
619 // 629 //
620 // You may append to the error message like so: 630 // You may append to the error message like so:
621 // DCHECK_NE(1, 2) << ": The world must be ending!"; 631 // DCHECK_NE(1, 2) << ": The world must be ending!";
622 // 632 //
623 // 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
624 // 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
625 // legal here. In particular, the arguments may be temporary expressions 635 // legal here. In particular, the arguments may be temporary expressions
626 // 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,
627 // for example: 637 // for example:
628 // DCHECK_EQ(string("abc")[1], 'b'); 638 // DCHECK_EQ(string("abc")[1], 'b');
629 // 639 //
630 // 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
631 // 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
632 // type of the desired pointer. 642 // type of the desired pointer.
633 643
634 #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2) 644 #define DCHECK_EQ(val1, val2) DCHECK_OP(EQ, ==, val1, val2)
635 #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2) 645 #define DCHECK_NE(val1, val2) DCHECK_OP(NE, !=, val1, val2)
636 #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2) 646 #define DCHECK_LE(val1, val2) DCHECK_OP(LE, <=, val1, val2)
637 #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2) 647 #define DCHECK_LT(val1, val2) DCHECK_OP(LT, < , val1, val2)
638 #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2) 648 #define DCHECK_GE(val1, val2) DCHECK_OP(GE, >=, val1, val2)
639 #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2) 649 #define DCHECK_GT(val1, val2) DCHECK_OP(GT, > , val1, val2)
640 650
641 // Helper macro for string comparisons. 651 #else // ENABLE_DCHECK
642 // Don't use this macro directly in your code, use DCHECK_STREQ et al below.
643 #define DCHECK_STROP(func, op, expected, s1, s2) \
644 if (DCHECK_IS_ON()) CHECK_STROP(func, op, expected, s1, s2)
645 652
646 // String (char*) equality/inequality checks. 653 // In order to avoid variable unused warnings for code that only uses a
647 // CASE versions are case-insensitive. 654 // variable in a CHECK, we make sure to use the macro arguments.
648 //
649 // Note that "s1" and "s2" may be temporary strings which are destroyed
650 // by the compiler at the end of the current "full expression"
651 // (e.g. DCHECK_STREQ(Foo().c_str(), Bar().c_str())).
652 655
653 #define DCHECK_STREQ(s1, s2) DCHECK_STROP(strcmp, ==, true, s1, s2) 656 #define DCHECK(condition) \
654 #define DCHECK_STRNE(s1, s2) DCHECK_STROP(strcmp, !=, false, s1, s2) 657 while (false && (condition)) DCHECK_EAT_STREAM_PARAMETERS
655 #define DCHECK_STRCASEEQ(s1, s2) DCHECK_STROP(_stricmp, ==, true, s1, s2)
656 #define DCHECK_STRCASENE(s1, s2) DCHECK_STROP(_stricmp, !=, false, s1, s2)
657 658
658 #define DCHECK_INDEX(I,A) DCHECK(I < (sizeof(A)/sizeof(A[0]))) 659 #define DPCHECK(condition) \
659 #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
660 696
661 // Helper functions for CHECK_OP macro. 697 // Helper functions for CHECK_OP macro.
662 // The (int, int) specialization works around the issue that the compiler 698 // The (int, int) specialization works around the issue that the compiler
663 // 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
664 // unnamed enum type - see comment below. 700 // unnamed enum type - see comment below.
665 #define DEFINE_CHECK_OP_IMPL(name, op) \ 701 #define DEFINE_CHECK_OP_IMPL(name, op) \
666 template <class t1, class t2> \ 702 template <class t1, class t2> \
667 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, \
668 const char* names) { \ 704 const char* names) { \
669 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
902 #elif NOTIMPLEMENTED_POLICY == 4 938 #elif NOTIMPLEMENTED_POLICY == 4
903 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG 939 #define NOTIMPLEMENTED() LOG(ERROR) << NOTIMPLEMENTED_MSG
904 #elif NOTIMPLEMENTED_POLICY == 5 940 #elif NOTIMPLEMENTED_POLICY == 5
905 #define NOTIMPLEMENTED() do {\ 941 #define NOTIMPLEMENTED() do {\
906 static int count = 0;\ 942 static int count = 0;\
907 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\ 943 LOG_IF(ERROR, 0 == count++) << NOTIMPLEMENTED_MSG;\
908 } while(0) 944 } while(0)
909 #endif 945 #endif
910 946
911 #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