15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef _LOGGING_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define _LOGGING_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <errno.h>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string.h>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <time.h>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <strstream>
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef COMPILER_MSVC
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <unistd.h>   // for _exit()
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/port.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/commandlineflags.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/crash.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/dynamic_annotations.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/macros.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/stl_decl_msvc.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/log_severity.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/vlog_is_on.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "global_strip_options.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Make a bunch of macros for logging.  The way to log things is to stream
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// things to LOG(<a particular severity level>).  E.g.,
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LOG(INFO) << "Found " << num_cookies << " cookies";
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// You can capture log messages in a string, rather than reporting them
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// immediately:
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   vector<string> errors;
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LOG_STRING(ERROR, &errors) << "Couldn't parse cookie #" << cookie_num;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This pushes back the new error onto 'errors'; if given a NULL pointer,
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// it reports the error via LOG(ERROR).
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// You can also do conditional logging:
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// You can also do occasional logging (log every n'th occurrence of an
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// event):
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LOG_EVERY_N(INFO, 10) << "Got the " << COUNTER << "th cookie";
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The above will cause log messages to be output on the 1st, 11th, 21st, ...
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// times it is executed.  Note that the special COUNTER value is used to
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// identify which repetition is happening.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// You can also do occasional conditional logging (log every n'th
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// occurrence of an event, when condition is satisfied):
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << COUNTER
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                                           << "th big cookie";
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// You can log messages the first N times your code executes a line. E.g.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LOG_FIRST_N(INFO, 20) << "Got the " << COUNTER << "th cookie";
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Outputs log messages for the first 20 times it is executed.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Analogous SYSLOG, SYSLOG_IF, and SYSLOG_EVERY_N macros are available.
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These log to syslog as well as to the normal logs.  If you use these at
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// all, you need to be aware that syslog can drastically reduce performance,
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// especially if it is configured for remote logging!  Don't use these
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// unless you fully understand this and have a concrete need to use them.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Even then, try to minimize your use of them.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// There are also "debug mode" logging macros like the ones above:
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   DLOG(INFO) << "Found cookies";
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   DLOG_EVERY_N(INFO, 10) << "Got the " << COUNTER << "th cookie";
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// All "debug mode" logging is compiled away to nothing for non-debug mode
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// compiles.
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We also have
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LOG_ASSERT(assertion);
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   DLOG_ASSERT(assertion);
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We also override the standard 'assert' to use 'DLOG_ASSERT'.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// There are "verbose level" logging macros.  They look like
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   VLOG(1) << "I'm printed when you run the program with --v=1 or more";
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   VLOG(2) << "I'm printed when you run the program with --v=2 or more";
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// These always log at the INFO log level (when they log at all).
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The verbose logging can also be turned on module-by-module.  For instance,
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    --vmodule=recordio=2,file=1,gfs*=3 --v=0
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// will cause:
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   a. VLOG(2) and lower messages to be printed from recordio.{h,cc}
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   b. VLOG(1) and lower messages to be printed from google2file
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   c. VLOG(3) and lower messages to be printed from files prefixed with "gfs"
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   d. VLOG(0) and lower messages to be printed from elsewhere
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The wildcarding functionality shown by (c) supports both '*' (match
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 0 or more characters) and '?' (match any single character) wildcards.
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   if (VLOG_IS_ON(2)) {
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     // do some logging preparation and logging
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     // that can't be accomplished with just VLOG(2) << ...;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   }
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// There are also VLOG_IF, VLOG_EVERY_N and VLOG_IF_EVERY_N "verbose level"
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// condition macros for sample cases, when some extra computation and
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// preparation for logs is not needed.
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   VLOG_IF(1, (size > 1024))
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//      << "I'm printed when size is more than 1024 and when you run the "
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//         "program with --v=1 or more";
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   VLOG_EVERY_N(1, 10)
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//      << "I'm printed every 10th occurrence, and when you run the program "
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//         "with --v=1 or more. Present occurence is " << COUNTER;
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   VLOG_IF_EVERY_N(1, (size > 1024), 10)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//      << "I'm printed on every 10th occurence of case when size is more "
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//         " than 1024, when you run the program with --v=1 or more. ";
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//         "Present occurence is " << COUNTER;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// [MLOG is OBSOLETE - use the more convenient VLOG(n) macros]
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// There is also an MLOG option that enables module-level logging.  MLOG
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// is associated with a specific flag by defining a MODULE_FLAG macro.
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Other than this, it behaves like VLOG.  Example:
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    DEFINE_int32(dnsverbose, 0, "Verbose level for DNS module");
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    #define MODULE_FLAG FLAGS_dnsverbose
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    MLOG(1) << "I'm printed when you run with --dnsverbose=1 or more";
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The supported severity levels for macros that allow you to specify one
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL.
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that messages of a given severity are logged not only in the
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// logfile for that severity, but also in all logfiles of lower severity.
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// E.g., a message of severity FATAL will be logged to the logfiles of
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// severity FATAL, ERROR, WARNING, and INFO.
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// There is also the special severity of DFATAL, which logs FATAL in
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// debug mode, ERROR in normal mode.
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Very important: logging a message at the FATAL severity level causes
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the program to terminate (after the message is logged).
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Unless otherwise specified, logs will be written to the filename
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "<program name>.<hostname>.<user name>.log.<severity level>.", followed
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// by the date, time, and pid (you can't prevent the date, time, and pid
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// from being in the filename).
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The logging code takes two flags:
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     --v=#           set the verbose level
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     --logtostderr   log all the messages to stderr instead of to logfiles
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LOG LINE PREFIX FORMAT
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Log lines have this form:
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg...
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// where the fields are defined as follows:
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   L                A single character, representing the log level
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                    (eg 'I' for INFO)
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   mm               The month (zero padded; ie May is '05')
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   dd               The day (zero padded)
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   hh:mm:ss.uuuuuu  Time in hours, minutes and fractional seconds
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   threadid         The space-padded thread ID as returned by GetTID()
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                    (this matches the PID on Linux)
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   file             The file name
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   line             The line number
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   msg              The user-supplied message
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Example:
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   I1103 11:57:31.739339 24395 google.cc:2341] Command line: ./some_prog
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   I1103 11:57:31.739403 24395 google.cc:2342] Process id 24395
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NOTE: although the microseconds are useful for comparing events on
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a single machine, clocks on different machines may not be well
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// synchronized.  Hence, use caution when comparing the low bits of
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// timestamps from different machines.
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Set whether log messages go to stderr instead of logfiles
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_bool(logtostderr);
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Set whether log messages go to stderr in addition to logfiles.
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_bool(alsologtostderr);
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Log messages at a level >= this flag are automatically sent to
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// stderr in addition to log files.
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_int32(stderrthreshold);
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Set whether the log prefix should be prepended to each line of output.
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_bool(log_prefix);
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Log messages at a level <= this flag are buffered.
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Log messages at a higher level are flushed immediately.
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_int32(logbuflevel);
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Sets the maximum number of seconds which logs may be buffered for.
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_int32(logbufsecs);
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Should Google1 logging be turned on?
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_bool(logging);
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Log suppression level: messages logged at a lower level than this
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// are suppressed.
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_int32(minloglevel);
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If specified, logfiles are written into this directory instead of the
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// default logging directory.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_string(log_dir);
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Sets the path of the directory into which to put additional links
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to the log files.
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_string(log_link);
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Sets the maximum log file size (in MB).
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_int32(max_log_size);
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Should log IO be directed to a background thread?  This flag has no
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// effect unless //thread/logger:logger is linked into the binary.
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_bool(threaded_logging);
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Set to cause StatusMessage() to write status to ./STATUS file.
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_bool(status_messages_to_status_file);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Sets whether to avoid logging to the disk if the disk is full.
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_bool(stop_logging_if_full_disk);
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Log messages below the STRIP_LOG level will be compiled away for
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// security reasons. See LOG(severtiy) below. STRIP_LOG is defined in
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// //base/global_strip_log.h
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A few definitions of macros that don't generate much code.  Since
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LOG(INFO) and its ilk are used all over our code, it's
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// better to have compact code for these operations.
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if STRIP_LOG == 0
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_INFO LogMessage(__FILE__, __LINE__)
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_TO_STRING_INFO(message) LogMessage(__FILE__, __LINE__, INFO, \
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                               message)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_INFO NullStream()
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_TO_STRING_INFO(message) NullStream()
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if STRIP_LOG <= 1
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_WARNING LogMessage(__FILE__, __LINE__, WARNING)
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_TO_STRING_WARNING(message) LogMessage(__FILE__, __LINE__, \
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                  WARNING, message)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_WARNING NullStream()
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_TO_STRING_WARNING(message) NullStream()
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if STRIP_LOG <= 2
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_ERROR LogMessage(__FILE__, __LINE__, ERROR)
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_TO_STRING_ERROR(message) LogMessage(__FILE__, __LINE__, ERROR, \
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                message)
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_ERROR NullStream()
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_TO_STRING_ERROR(message) NullStream()
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if STRIP_LOG <= 3
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_FATAL LogMessageFatal(__FILE__, __LINE__)
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_QFATAL LogMessageQuietlyFatal(__FILE__, __LINE__)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_TO_STRING_FATAL(message) LogMessage(__FILE__, __LINE__, FATAL, \
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                message)
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_FATAL NullStreamFatal()
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_QFATAL NullStreamFatal()
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_TO_STRING_FATAL(message) NullStreamFatal()
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For DFATAL, we want to use LogMessage (as opposed to
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LogMessageFatal), to be consistent with the original behavior.
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef NDEBUG
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_ERROR
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif STRIP_LOG <= 3
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_DFATAL LogMessage(__FILE__, __LINE__, FATAL)
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPACT_GOOGLE_LOG_DFATAL NullStreamFatal()
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define GOOGLE_LOG_INFO(counter) \
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(__FILE__, __LINE__, INFO, counter, &LogMessage::SendToLog)
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SYSLOG_INFO(counter) \
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(__FILE__, __LINE__, INFO, counter, \
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  &LogMessage::SendToSyslogAndLog)
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define GOOGLE_LOG_WARNING(counter)  \
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(__FILE__, __LINE__, WARNING, counter, &LogMessage::SendToLog)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SYSLOG_WARNING(counter)  \
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(__FILE__, __LINE__, WARNING, counter, \
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  &LogMessage::SendToSyslogAndLog)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define GOOGLE_LOG_ERROR(counter)  \
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(__FILE__, __LINE__, ERROR, counter, &LogMessage::SendToLog)
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SYSLOG_ERROR(counter)  \
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(__FILE__, __LINE__, ERROR, counter, \
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  &LogMessage::SendToSyslogAndLog)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define GOOGLE_LOG_FATAL(counter) \
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(__FILE__, __LINE__, FATAL, counter, &LogMessage::SendToLog)
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SYSLOG_FATAL(counter) \
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(__FILE__, __LINE__, FATAL, counter, \
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  &LogMessage::SendToSyslogAndLog)
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define GOOGLE_LOG_DFATAL(counter) \
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(__FILE__, __LINE__, DFATAL_LEVEL, counter, &LogMessage::SendToLog)
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SYSLOG_DFATAL(counter) \
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(__FILE__, __LINE__, DFATAL_LEVEL, counter, \
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  &LogMessage::SendToSyslogAndLog)
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef OS_WINDOWS
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A very useful logging macro to log windows errors:
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_SYSRESULT(result) \
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (FAILED(result)) { \
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LPTSTR message = NULL; \
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LPTSTR msg = reinterpret_cast<LPTSTR>(&message); \
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DWORD message_length = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | \
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         FORMAT_MESSAGE_FROM_SYSTEM, \
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         0, result, 0, msg, 100, NULL); \
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (message_length > 0) { \
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LogMessage(__FILE__, __LINE__, ERROR, 0, \
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 &LogMessage::SendToLog).stream() << message; \
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LocalFree(message); \
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } \
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We use the preprocessor's merging operator, "##", so that, e.g.,
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// LOG(INFO) becomes the token GOOGLE_LOG_INFO.  There's some funny
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// subtle difference between ostream member streaming functions (e.g.,
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ostream::operator<<(int) and ostream non-member streaming functions
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (e.g., ::operator<<(ostream&, string&): it turns out that it's
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// impossible to stream something like a string directly to an unnamed
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ostream. We employ a neat hack by calling the stream() member
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// function of LogMessage which seems to avoid the problem.
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SYSLOG(severity) SYSLOG_ ## severity(0).stream()
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A convenient shorthand
3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LG LOG(INFO)
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class LogSink;  // defined below
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If a non-NULL sink pointer is given, we push this message to that sink.
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For LOG_TO_SINK we then do normal LOG(severity) logging as well.
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is useful for capturing messages and passing/storing them
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// somewhere more specific than the global log of the process.
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Argument types:
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LogSink* sink;
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LogSeverity severity;
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The cast is to disambiguate NULL arguments.
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_TO_SINK(sink, severity) \
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(__FILE__, __LINE__, severity, \
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             static_cast<LogSink*>(sink), true).stream()
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_TO_SINK_BUT_NOT_TO_LOGFILE(sink, severity) \
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(__FILE__, __LINE__, severity, \
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             static_cast<LogSink*>(sink), false).stream()
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If a non-NULL string pointer is given, we write this message to that string.
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We then do normal LOG(severity) logging as well.
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is useful for capturing messages and storing them somewhere more
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// specific than the global log of the process.
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Argument types:
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   string* message;
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LogSeverity severity;
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The cast is to disambiguate NULL arguments.
3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NOTE: LOG(severity) expands to LogMessage().stream() for the specified
3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// severity.
3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_TO_STRING(severity, message) \
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LOG_TO_STRING_##severity(static_cast<string*>(message)).stream()
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// If a non-NULL pointer is given, we push the message onto the end
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of a vector of strings; otherwise, we report it with LOG(severity).
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This is handy for capturing messages and perhaps passing them back
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to the caller, rather than reporting them immediately.
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Argument types:
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   LogSeverity severity;
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   vector<string> *outvec;
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The cast is to disambiguate NULL arguments.
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_STRING(severity, outvec) \
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LOG_TO_STRING_##severity(static_cast<vector<string>*>(outvec)).stream()
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_IF(severity, condition) \
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  !(condition) ? (void) 0 : LogMessageVoidify() & LOG(severity)
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SYSLOG_IF(severity, condition) \
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  !(condition) ? (void) 0 : LogMessageVoidify() & SYSLOG(severity)
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_ASSERT(condition)  \
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SYSLOG_ASSERT(condition) \
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CHECK dies with a fatal error if condition is not true.  It is *not*
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// controlled by NDEBUG, so the check will be executed regardless of
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// compilation mode.  Therefore, it is safe to do things like:
4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    CHECK(fp->Write(x) == 4)
4105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK(condition)  \
4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LOG_IF(FATAL, PREDICT_FALSE(!(condition))) \
4125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             << "Check failed: " #condition " "
4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// QCHECK is a quiet version of CHECK. It has all of the same properties,
4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// except that when it dies it simply prints out this message and doesn't
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// dump a giant stack trace, etc. This is good for tests like sanity-checking
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// user inputs, where your own failure message is really the only thing you
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// need or want to display.
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK(condition)  \
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      LOG_IF(QFATAL, PREDICT_FALSE(!(condition))) \
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             << "Check failed: " #condition " "
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A container for a string pointer which can be evaluated to a bool -
4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// true iff the pointer is NULL.
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct CheckOpString {
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CheckOpString(string* str) : str_(str) { }
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // No destructor: if str_ is non-NULL, we're about to LOG(FATAL),
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // so there's no point in cleaning up str_.
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  operator bool() const { return PREDICT_FALSE(str_ != NULL); }
4305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string* str_;
4315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
4325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Function is overloaded for integral types to allow static const
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// integrals declared in classes and not defined to be used as arguments to
4355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CHECK* macros. It's not encouraged though.
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <class T>
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline const T&       GetReferenceableValue(const T&           t) { return t; }
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline char           GetReferenceableValue(char               t) { return t; }
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline unsigned char  GetReferenceableValue(unsigned char      t) { return t; }
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline signed char    GetReferenceableValue(signed char        t) { return t; }
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline short          GetReferenceableValue(short              t) { return t; }
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline unsigned short GetReferenceableValue(unsigned short     t) { return t; }
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline int            GetReferenceableValue(int                t) { return t; }
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline unsigned int   GetReferenceableValue(unsigned int       t) { return t; }
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline long           GetReferenceableValue(long               t) { return t; }
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline unsigned long  GetReferenceableValue(unsigned long      t) { return t; }
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline long long      GetReferenceableValue(long long          t) { return t; }
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline unsigned long long GetReferenceableValue(unsigned long long t) {
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return t;
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Build the error message string.
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<class t1, class t2>
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  strstream ss;
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ss << names << " (" << v1 << " vs. " << v2 << ")";
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return new string(ss.str(), ss.pcount());
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Helper functions for CHECK_OP macro.
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The (int, int) specialization works around the issue that the compiler
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// will not instantiate the template version of the function on values of
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// unnamed enum type - see comment below.
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DEFINE_CHECK_OP_IMPL(name, op) \
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  template <class t1, class t2> \
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  inline string* Check##name##Impl(const t1& v1, const t2& v2, \
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const char* names) { \
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (v1 op v2) return NULL; \
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    else return MakeCheckOpString(v1, v2, names); \
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } \
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  inline string* Check##name##Impl(int v1, int v2, const char* names) { \
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return Check##name##Impl<int, int>(v1, v2, names); \
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use _EQ, _NE, _LE, etc. in case the file including base/logging.h
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// provides its own #defines for the simpler names EQ, NE, LE, etc.
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This happens if, for example, those are used as token names in a
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// yacc grammar.
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DEFINE_CHECK_OP_IMPL(_EQ, ==)
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DEFINE_CHECK_OP_IMPL(_NE, !=)
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DEFINE_CHECK_OP_IMPL(_LE, <=)
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DEFINE_CHECK_OP_IMPL(_LT, < )
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DEFINE_CHECK_OP_IMPL(_GE, >=)
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DEFINE_CHECK_OP_IMPL(_GT, > )
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef DEFINE_CHECK_OP_IMPL
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Helper macro for binary operators.
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Don't use this macro directly in your code, use CHECK_EQ et al below.
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if defined(STATIC_ANALYSIS)
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Only for static analysis tool to know that it is equivalent to assert
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_OP_LOG(name, op, val1, val2, log) CHECK((val1) op (val2))
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#elif !defined(NDEBUG)
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In debug mode, avoid constructing CheckOpStrings if possible,
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// to reduce the overhead of CHECK statments by 2x.
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Real DCHECK-heavy tests have seen 1.5x speedups.
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The meaning of "string" might be different between now and
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// when this macro gets invoked (e.g., if someone is experimenting
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with other string implementations that get defined after this
5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// file is included).  Save the current meaning now and use it
5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// in the macro.
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)typedef string _Check_string;
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_OP_LOG(name, op, val1, val2, log) \
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (_Check_string* _result = \
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         Check##name##Impl(GetReferenceableValue(val1), \
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           GetReferenceableValue(val2), \
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           #val1 " " #op " " #val2)) \
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    log(__FILE__, __LINE__, CheckOpString(_result)).stream()
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In optimized mode, use CheckOpString to hint to compiler that
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the while condition is unlikely.
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_OP_LOG(name, op, val1, val2, log) \
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (CheckOpString _result = \
5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         Check##name##Impl(GetReferenceableValue(val1), \
5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           GetReferenceableValue(val2), \
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           #val1 " " #op " " #val2)) \
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    log(__FILE__, __LINE__, _result).stream()
5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // STATIC_ANALYSIS, !NDEBUG
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#if STRIP_LOG <= 3
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_OP(name, op, val1, val2) \
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK_OP_LOG(name, op, val1, val2, LogMessageFatal)
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_OP(name, op, val1, val2) \
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK_OP_LOG(name, op, val1, val2, NullStreamFatal)
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // STRIP_LOG <= 3
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_OP(name, op, val1, val2) \
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK_OP_LOG(name, op, val1, val2, LogMessageQuietlyFatal)
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Equality/Inequality checks - compare two values, and log a FATAL message
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// including the two values when the result is not as expected.  The values
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// must have operator<<(ostream, ...) defined.
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// You may append to the error message like so:
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   CHECK_NE(1, 2) << ": The world must be ending!";
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We are very careful to ensure that each argument is evaluated exactly
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// once, and that anything which is legal to pass as a function argument is
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// legal here.  In particular, the arguments may be temporary expressions
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// which will end up being destroyed at the end of the apparent statement,
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// for example:
5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   CHECK_EQ(string("abc")[1], 'b');
5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// WARNING: These don't compile correctly if one of the arguments is a pointer
5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and the other is NULL. To work around this, simply static_cast NULL to the
5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// type of the desired pointer.
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_EQ(val1, val2) CHECK_OP(_EQ, ==, val1, val2)
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_NE(val1, val2) CHECK_OP(_NE, !=, val1, val2)
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_LE(val1, val2) CHECK_OP(_LE, <=, val1, val2)
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_LT(val1, val2) CHECK_OP(_LT, < , val1, val2)
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_GE(val1, val2) CHECK_OP(_GE, >=, val1, val2)
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_GT(val1, val2) CHECK_OP(_GT, > , val1, val2)
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_EQ(val1, val2) QCHECK_OP(_EQ, ==, val1, val2)
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_NE(val1, val2) QCHECK_OP(_NE, !=, val1, val2)
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_LE(val1, val2) QCHECK_OP(_LE, <=, val1, val2)
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_LT(val1, val2) QCHECK_OP(_LT, < , val1, val2)
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_GE(val1, val2) QCHECK_OP(_GE, >=, val1, val2)
5615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_GT(val1, val2) QCHECK_OP(_GT, > , val1, val2)
5625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Check that the input is non NULL.  This very useful in constructor
5655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// initializer lists.
5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_NOTNULL(val) \
5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Helper functions for string comparisons.
5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// To avoid bloat, the definitions are in logging.cc.
5725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DECLARE_CHECK_STROP_IMPL(func, expected) \
5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  string* Check##func##expected##Impl(const char* s1, const char* s2, \
5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      const char* names);
5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_CHECK_STROP_IMPL(strcmp, true)
5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_CHECK_STROP_IMPL(strcmp, false)
5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_CHECK_STROP_IMPL(strcasecmp, true)
5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)DECLARE_CHECK_STROP_IMPL(strcasecmp, false)
5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef DECLARE_CHECK_STROP_IMPL
5805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Helper macro for string comparisons.
5825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Don't use this macro directly in your code, use CHECK_STREQ et al below.
5835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_STROP(func, op, expected, s1, s2) \
5845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (CheckOpString _result = \
5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         Check##func##expected##Impl((s1), (s2), \
5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     #s1 " " #op " " #s2)) \
5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(FATAL) << *_result.str_
5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_STROP(func, op, expected, s1, s2) \
5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (CheckOpString _result = \
5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)         Check##func##expected##Impl((s1), (s2), \
5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     #s1 " " #op " " #s2)) \
5925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LOG(QFATAL) << *_result.str_
5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// String (char*) equality/inequality checks.
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CASE versions are case-insensitive.
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that "s1" and "s2" may be temporary strings which are destroyed
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// by the compiler at the end of the current "full expression"
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (e.g. CHECK_STREQ(Foo().c_str(), Bar().c_str())).
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_STREQ(s1, s2) CHECK_STROP(strcmp, ==, true, s1, s2)
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_STRNE(s1, s2) CHECK_STROP(strcmp, !=, false, s1, s2)
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_STRCASEEQ(s1, s2) CHECK_STROP(strcasecmp, ==, true, s1, s2)
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_STRCASENE(s1, s2) CHECK_STROP(strcasecmp, !=, false, s1, s2)
6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_INDEX(I,A) CHECK(I < (sizeof(A)/sizeof(A[0])))
6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_BOUND(B,A) CHECK(B <= (sizeof(A)/sizeof(A[0])))
6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_STREQ(s1, s2) QCHECK_STROP(strcmp, ==, true, s1, s2)
6115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_STRNE(s1, s2) QCHECK_STROP(strcmp, !=, false, s1, s2)
6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_STRCASEEQ(s1, s2) QCHECK_STROP(strcasecmp, ==, true, s1, s2)
6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_STRCASENE(s1, s2) QCHECK_STROP(strcasecmp, !=, false, s1, s2)
6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_INDEX(I,A) QCHECK(I < (sizeof(A)/sizeof(A[0])))
6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define QCHECK_BOUND(B,A) QCHECK(B <= (sizeof(A)/sizeof(A[0])))
6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Likely to be deprecated; instead use
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   CHECK(MathUtil::NearByMargin(x, y))
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (or another similar function from util/math/mathutil.h).
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_DOUBLE_EQ(val1, val2)              \
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  do {                                           \
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_LE((val1), (val2)+0.000000000000001L); \
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_GE((val1), (val2)-0.000000000000001L); \
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } while (0)
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Likely to be deprecated; instead use
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   CHECK(MathUtil::WithinMargin(x, y, margin))
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (or another similar function from util/math/mathutil.h).
6305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_NEAR(val1, val2, margin)           \
6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  do {                                           \
6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_LE((val1), (val2)+(margin));           \
6335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_GE((val1), (val2)-(margin));           \
6345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } while (0)
6355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// perror()..googly style!
6375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PLOG() and PLOG_IF() and PCHECK() behave exactly like their LOG* and
6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CHECK equivalents with the addition that they postpend a description
6405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of the current state of errno to their output lines.
6415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PLOG(severity) GOOGLE_PLOG(severity, 0).stream()
6435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define GOOGLE_PLOG(severity, counter)  \
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ErrnoLogMessage(__FILE__, __LINE__, severity, counter, \
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                  &LogMessage::SendToLog)
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PLOG_IF(severity, condition) \
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  !(condition) ? (void) 0 : LogMessageVoidify() & PLOG(severity)
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A CHECK() macro that postpends errno if the condition is false. E.g.
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// if (poll(fds, nfds, timeout) == -1) { PCHECK(errno == EINTR); ... }
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PCHECK(condition)  \
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PLOG_IF(FATAL, PREDICT_FALSE(!(condition))) \
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              << "Check failed: " #condition " "
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A CHECK() macro that lets you assert the success of a function that
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// returns -1 and sets errno in case of an error. E.g.
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// CHECK_ERR(mkdir(path, 0700));
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// or
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// int fd = open(filename, flags); CHECK_ERR(fd) << ": open " << filename;
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHECK_ERR(invocation)                                          \
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)PLOG_IF(FATAL, PREDICT_FALSE((invocation) == -1)) << #invocation
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use macro expansion to create, for each use of LOG_EVERY_N(), static
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// variables with the __LINE__ expansion as part of the variable name.
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_EVERY_N_VARNAME(base, line) LOG_EVERY_N_VARNAME_CONCAT(base, line)
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_EVERY_N_VARNAME_CONCAT(base, line) base ## line
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_OCCURRENCES LOG_EVERY_N_VARNAME(occurrences_, __LINE__)
6755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_OCCURRENCES_MOD_N LOG_EVERY_N_VARNAME(occurrences_mod_n_, __LINE__)
6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SOME_KIND_OF_LOG_EVERY_N(severity, n, what_to_do) \
6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ++LOG_OCCURRENCES; \
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (LOG_OCCURRENCES_MOD_N == 1) \
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LogMessage(__FILE__, __LINE__, severity, LOG_OCCURRENCES, \
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               &what_to_do).stream()
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SOME_KIND_OF_LOG_IF_EVERY_N(severity, condition, n, what_to_do) \
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ANNOTATE_BENIGN_RACE(&LOG_OCCURRENCES, "logging"); \
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ANNOTATE_BENIGN_RACE(&LOG_OCCURRENCES_MOD_N, "logging"); \
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ++LOG_OCCURRENCES; \
6905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (condition && \
6915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ((LOG_OCCURRENCES_MOD_N=(LOG_OCCURRENCES_MOD_N + 1) % n) == (1 % n))) \
6925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LogMessage(__FILE__, __LINE__, severity, LOG_OCCURRENCES, \
6935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 &what_to_do).stream()
6945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SOME_KIND_OF_PLOG_EVERY_N(severity, n, what_to_do) \
6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ANNOTATE_BENIGN_RACE(&LOG_OCCURRENCES, "logging"); \
6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ANNOTATE_BENIGN_RACE(&LOG_OCCURRENCES_MOD_N, "logging"); \
6995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ++LOG_OCCURRENCES; \
7005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
7015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (LOG_OCCURRENCES_MOD_N == 1) \
7025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ErrnoLogMessage(__FILE__, __LINE__, severity, LOG_OCCURRENCES, \
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    &what_to_do).stream()
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SOME_KIND_OF_LOG_FIRST_N(severity, n, what_to_do) \
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int LOG_OCCURRENCES = 0; \
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ANNOTATE_BENIGN_RACE(&LOG_OCCURRENCES, "logging"); \
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (LOG_OCCURRENCES <= n) \
7095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ++LOG_OCCURRENCES; \
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (LOG_OCCURRENCES <= n) \
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LogMessage(__FILE__, __LINE__, severity, LOG_OCCURRENCES, \
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)               &what_to_do).stream()
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_EVERY_N(severity, n) \
7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  COMPILE_ASSERT(severity < NUM_SEVERITIES, \
7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                 INVALID_REQUESTED_LOG_SEVERITY); \
7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SOME_KIND_OF_LOG_EVERY_N(severity, (n), LogMessage::SendToLog)
7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define SYSLOG_EVERY_N(severity, n) \
7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SOME_KIND_OF_LOG_EVERY_N(severity, (n), LogMessage::SendToSyslogAndLog)
7215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define PLOG_EVERY_N(severity, n) \
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SOME_KIND_OF_PLOG_EVERY_N(severity, (n), LogMessage::SendToLog)
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_FIRST_N(severity, n) \
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SOME_KIND_OF_LOG_FIRST_N(severity, (n), LogMessage::SendToLog)
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define LOG_IF_EVERY_N(severity, condition, n) \
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SOME_KIND_OF_LOG_IF_EVERY_N(severity, (condition), (n), LogMessage::SendToLog)
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// We want the special COUNTER value available for LOG_EVERY_X()'ed messages
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)enum PRIVATE_Counter {COUNTER};
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Plus some debug-logging macros that get compiled to nothing for production
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NDEBUG
7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DLOG(severity) LOG(severity)
7405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DVLOG(verboselevel) VLOG(verboselevel)
7415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DLOG_IF(severity, condition) LOG_IF(severity, condition)
7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DLOG_EVERY_N(severity, n) LOG_EVERY_N(severity, n)
7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DLOG_IF_EVERY_N(severity, condition, n) \
7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LOG_IF_EVERY_N(severity, condition, n)
7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DLOG_ASSERT(condition) LOG_ASSERT(condition)
7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// debug-only checking.  not executed in NDEBUG mode.
7485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK(condition) CHECK(condition)
7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_NE(val1, val2) CHECK_NE(val1, val2)
7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_LE(val1, val2) CHECK_LE(val1, val2)
7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_LT(val1, val2) CHECK_LT(val1, val2)
7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_GE(val1, val2) CHECK_GE(val1, val2)
7545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_GT(val1, val2) CHECK_GT(val1, val2)
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_STREQ(str1, str2) CHECK_STREQ(str1, str2)
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_STRCASEEQ(str1, str2) CHECK_STRCASEEQ(str1, str2)
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_STRNE(str1, str2) CHECK_STRNE(str1, str2)
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_STRCASENE(str1, str2) CHECK_STRCASENE(str1, str2)
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#else  // NDEBUG
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DLOG(severity) \
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  true ? (void) 0 : LogMessageVoidify() & LOG(severity)
7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DVLOG(verboselevel) \
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  (true || !VLOG_IS_ON(verboselevel)) ?\
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    (void) 0 : LogMessageVoidify() & LOG(INFO)
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DLOG_IF(severity, condition) \
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  (true || !(condition)) ? (void) 0 : LogMessageVoidify() & LOG(severity)
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DLOG_EVERY_N(severity, n) \
7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  true ? (void) 0 : LogMessageVoidify() & LOG(severity)
7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DLOG_IF_EVERY_N(severity, condition, n) \
7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  (true || !(condition))? (void) 0 : LogMessageVoidify() & LOG(severity)
7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DLOG_ASSERT(condition) \
7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  true ? (void) 0 : LOG_ASSERT(condition)
7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK(condition) \
7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (false) \
7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK(condition)
7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_EQ(val1, val2) \
7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (false) \
7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_EQ(val1, val2)
7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_NE(val1, val2) \
7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (false) \
7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_NE(val1, val2)
7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_LE(val1, val2) \
7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (false) \
7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_LE(val1, val2)
7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_LT(val1, val2) \
7985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (false) \
7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_LT(val1, val2)
8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_GE(val1, val2) \
8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (false) \
8035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_GE(val1, val2)
8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_GT(val1, val2) \
8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (false) \
8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_GT(val1, val2)
8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_STREQ(str1, str2) \
8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (false) \
8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_STREQ(str1, str2)
8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_STRCASEEQ(str1, str2) \
8145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (false) \
8155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_STRCASEEQ(str1, str2)
8165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_STRNE(str1, str2) \
8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (false) \
8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_STRNE(str1, str2)
8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DCHECK_STRCASENE(str1, str2) \
8225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (false) \
8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_STRCASENE(str1, str2)
8245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // NDEBUG
8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Log only in verbose mode.
8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define VLOG(verboselevel) LOG_IF(INFO, VLOG_IS_ON(verboselevel))
8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define VLOG_IF(verboselevel, condition) \
8335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LOG_IF(INFO, (condition) && VLOG_IS_ON(verboselevel))
8345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define VLOG_EVERY_N(verboselevel, n) \
8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LOG_IF_EVERY_N(INFO, VLOG_IS_ON(verboselevel), n)
8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define VLOG_IF_EVERY_N(verboselevel, condition, n) \
8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LOG_IF_EVERY_N(INFO, (condition) && VLOG_IS_ON(verboselevel), n)
8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// [MLOG is OBSOLETE - use the more convenient VLOG(n) macros]
8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Log only when a module-specific value (MODULE_FLAG) has a specific
8445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// value.  MODULE_FLAG must be a macro that evaluates to the name of
8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the flag that you wish to use.  You should '#define MODULE_FLAG
8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// <variable name>' before using this macro.  (For example:
8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       #define MODULE_FLAG FLAGS_dnsverbose
8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MLOG(verboselevel) LOG_IF(INFO, MODULE_FLAG >= (verboselevel))
8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Redefine the standard assert to use our nice log files
8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#undef assert
8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define assert(x) DLOG_ASSERT(x)
8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
8555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class more or less represents a particular log message.  You
8565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// create an instance of LogMessage and then stream stuff to it.
8575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// When you finish streaming to it, ~LogMessage is called and the
8585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// full message gets streamed to the appropriate destination.
8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
8605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// You shouldn't actually use LogMessage's constructor to log things,
8615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// though.  You should use the LOG() macro (and variants thereof)
8625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// above.
8635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class LogMessage {
8645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
8655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum {
8665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Passing kNoLogPrefix for the line number disables the
8675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // log-message prefix. Useful for using the LogMessage
8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // infrastructure as a printing utility. See also the --log_prefix
8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // flag for controlling the log-message prefix on an
8705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // application-wide basis.
8715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kNoLogPrefix = -1
8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
8735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class LogStream : public ostrstream {
8755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  public:
8765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LogStream(char *buf, int len, int ctr)
8775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : ostrstream(buf, len),
8785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        ctr_(ctr) {
8795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      self_ = this;
8805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
8815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int ctr() const { return ctr_; }
8835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void set_ctr(int ctr) { ctr_ = ctr; }
8845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LogStream* self() const { return self_; }
8855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  private:
8875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    int ctr_;  // Counter hack (for the LOG_EVERY_X() macro)
8885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    LogStream *self_;  // Consistency check hack
8895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public:
8925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // icc 8 requires this typedef to avoid an internal compiler error.
8935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef void (LogMessage::*SendMethod)();
8945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(const char* file, int line, LogSeverity severity, int ctr,
8965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             SendMethod send_method);
8975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Two special constructors that generate reduced amounts of code at
8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // LOG call sites for common cases.
9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used for LOG(INFO): Implied are:
9025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // severity = INFO, ctr = 0, send_method = &LogMessage::SendToLog.
9035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
9045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Using this constructor instead of the more complex constructor above
9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // saves 19 bytes per call site.
9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(const char* file, int line);
9075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used for LOG(severity) where severity != INFO.  Implied
9095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // are: ctr = 0, send_method = &LogMessage::SendToLog
9105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
9115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Using this constructor instead of the more complex constructor above
9125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // saves 17 bytes per call site.
9135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(const char* file, int line, LogSeverity severity);
9145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Constructor to log this message to a specified sink (if not NULL).
9165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Implied are: ctr = 0, send_method = &LogMessage::SendToSinkAndLog if
9175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // also_send_to_log is true, send_method = &LogMessage::SendToSink otherwise.
9185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(const char* file, int line, LogSeverity severity, LogSink* sink,
9195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             bool also_send_to_log);
9205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Constructor where we also give a vector<string> pointer
9225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for storing the messages (if the pointer is not NULL).
9235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Implied are: ctr = 0, send_method = &LogMessage::SaveOrSendToLog.
9245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(const char* file, int line, LogSeverity severity,
9255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             vector<string>* outvec);
9265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Constructor where we also give a string pointer for storing the
9285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // message (if the pointer is not NULL).  Implied are: ctr = 0,
9295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // send_method = &LogMessage::WriteToStringAndLog.
9305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(const char* file, int line, LogSeverity severity,
9315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)             string* message);
9325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A special constructor used for check failures
9345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  LogMessage(const char* file, int line, const CheckOpString& result);
9355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~LogMessage();
9375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Flush a buffered message to the sink set in the constructor.  Always
9395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // called by the destructor, it may also be called from elsewhere if
9405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // needed.  Only the first call is actioned; any later ones are ignored.
9415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Flush();
9425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // An arbitrary limit on the length of a single log message.  This
9445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is so that streaming can be done more efficiently.
9455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const size_t kMaxLogMessageLen;
9465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Theses should not be called directly outside of logging.*,
9485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // only passed as SendMethod arguments to other LogMessage methods:
9495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SendToLog();  // Actually dispatch to the logs
9505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SendToSyslogAndLog();  // Actually dispatch to syslog and the logs
9515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Call abort() or similar to perform LOG(FATAL) crash.
9535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Writes current stack trace to stderr.
9545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void Fail() ATTRIBUTE_NORETURN;
9555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Same as Fail(), but without writing out the stack trace.
9575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It is assumed that the caller has already generated and
9585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // written the trace as appropriate.
9595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void FailWithoutStackTrace() ATTRIBUTE_NORETURN;
9605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Similar to FailWithoutStackTrace(), but without abort()ing.
9625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Terminates the process with error exit code.
9635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void FailQuietly() ATTRIBUTE_NORETURN;
9645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ostream& stream() { return *(data_->stream_); }
9665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int preserved_errno() const { return data_->preserved_errno_; }
9685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Must be called without the log_mutex held.  (L < log_mutex)
9705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static int64 num_messages(int severity);
9715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)private:
9735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Fully internal SendMethod cases:
9745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SendToSinkAndLog();  // Send to sink if provided and dispatch to the logs
9755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SendToSink();  // Send to sink if provided, do nothing otherwise.
9765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Write to string if provided and dispatch to the logs.
9785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void WriteToStringAndLog();
9795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SaveOrSendToLog();  // Save to stringvec if provided, else to logs
9815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Init(const char* file, int line, LogSeverity severity,
9835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            void (LogMessage::*send_method)());
9845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
9855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used to fill in crash information during LOG(FATAL) failures.
9865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RecordCrashReason(base::CrashReason* reason);
9875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
988