2 // Copyright (C) 1999 and onwards Google, Inc.
6 // This file contains #include information about logging-related stuff.
7 // Pretty much everybody needs to #include this file so that they can
8 // log various happenings.
17 #if @ac_cv_have_unistd_h@
30 // Annoying stuff for windows -- makes sure clients can import these functions
31 #ifndef GOOGLE_GLOG_DLL_DECL
33 # define GOOGLE_GLOG_DLL_DECL __declspec(dllimport)
35 # define GOOGLE_GLOG_DLL_DECL
39 // We care a lot about number of bits things take up. Unfortunately,
40 // systems define their bit-specific ints in a lot of different ways.
41 // We use our own way, and have a typedef to get there.
42 // Note: these commands below may look like "#if 1" or "#if 0", but
43 // that's because they were constructed that way at ./configure time.
44 // Look at logging.h.in to see how they're calculated (based on your config).
45 #if @ac_cv_have_stdint_h@
46 #include <stdint.h> // the normal place uint16_t is defined
48 #if @ac_cv_have_systypes_h@
49 #include <sys/types.h> // the normal place u_int16_t is defined
51 #if @ac_cv_have_inttypes_h@
52 #include <inttypes.h> // a third place for uint16_t or u_int16_t
55 #if @ac_cv_have_libgflags@
56 #include <gflags/gflags.h>
59 @ac_google_start_namespace@
61 #if @ac_cv_have_uint16_t@ // the C99 format
62 typedef int32_t int32;
63 typedef uint32_t uint32;
64 typedef int64_t int64;
65 typedef uint64_t uint64;
66 #elif @ac_cv_have_u_int16_t@ // the BSD format
67 typedef int32_t int32;
68 typedef u_int32_t uint32;
69 typedef int64_t int64;
70 typedef u_int64_t uint64;
71 #elif @ac_cv_have___uint16@ // the windows (vc7) format
72 typedef __int32 int32;
73 typedef unsigned __int32 uint32;
74 typedef __int64 int64;
75 typedef unsigned __int64 uint64;
77 #error Do not know how to define a 32-bit integer quantity on your system
80 @ac_google_end_namespace@
82 // The global value of GOOGLE_STRIP_LOG. All the messages logged to
83 // LOG(XXX) with severity less than GOOGLE_STRIP_LOG will not be displayed.
84 // If it can be determined at compile time that the message will not be
85 // printed, the statement will be compiled out.
87 // Example: to strip out all INFO and WARNING messages, use the value
88 // of 2 below. To make an exception for WARNING messages from a single
89 // file, add "#define GOOGLE_STRIP_LOG 1" to that file _before_ including
91 #ifndef GOOGLE_STRIP_LOG
92 #define GOOGLE_STRIP_LOG 0
95 // GCC can be told that a certain branch is not likely to be taken (for
96 // instance, a CHECK failure), and use that information in static analysis.
97 // Giving it this information can help it optimize for the common case in
98 // the absence of better information (ie. -fprofile-arcs).
100 #ifndef GOOGLE_PREDICT_BRANCH_NOT_TAKEN
101 #if @ac_cv_have___builtin_expect@
102 #define GOOGLE_PREDICT_BRANCH_NOT_TAKEN(x) (__builtin_expect(x, 0))
104 #define GOOGLE_PREDICT_BRANCH_NOT_TAKEN(x) x
108 // Make a bunch of macros for logging. The way to log things is to stream
109 // things to LOG(<a particular severity level>). E.g.,
111 // LOG(INFO) << "Found " << num_cookies << " cookies";
113 // You can capture log messages in a string, rather than reporting them
116 // vector<string> errors;
117 // LOG_STRING(ERROR, &errors) << "Couldn't parse cookie #" << cookie_num;
119 // This pushes back the new error onto 'errors'; if given a NULL pointer,
120 // it reports the error via LOG(ERROR).
122 // You can also do conditional logging:
124 // LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
126 // You can also do occasional logging (log every n'th occurrence of an
129 // LOG_EVERY_N(INFO, 10) << "Got the " << COUNTER << "th cookie";
131 // The above will cause log messages to be output on the 1st, 11th, 21st, ...
132 // times it is executed. Note that the special COUNTER value is used to
133 // identify which repetition is happening.
135 // You can also do occasional conditional logging (log every n'th
136 // occurrence of an event, when condition is satisfied):
138 // LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << COUNTER
139 // << "th big cookie";
141 // You can log messages the first N times your code executes a line. E.g.
143 // LOG_FIRST_N(INFO, 20) << "Got the " << COUNTER << "th cookie";
145 // Outputs log messages for the first 20 times it is executed.
147 // Analogous SYSLOG, SYSLOG_IF, and SYSLOG_EVERY_N macros are available.
148 // These log to syslog as well as to the normal logs. If you use these at
149 // all, you need to be aware that syslog can drastically reduce performance,
150 // especially if it is configured for remote logging! Don't use these
151 // unless you fully understand this and have a concrete need to use them.
152 // Even then, try to minimize your use of them.
154 // There are also "debug mode" logging macros like the ones above:
156 // DLOG(INFO) << "Found cookies";
158 // DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
160 // DLOG_EVERY_N(INFO, 10) << "Got the " << COUNTER << "th cookie";
162 // All "debug mode" logging is compiled away to nothing for non-debug mode
167 // LOG_ASSERT(assertion);
168 // DLOG_ASSERT(assertion);
170 // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
172 // There are "verbose level" logging macros. They look like
174 // VLOG(1) << "I'm printed when you run the program with --v=1 or more";
175 // VLOG(2) << "I'm printed when you run the program with --v=2 or more";
177 // These always log at the INFO log level (when they log at all).
178 // The verbose logging can also be turned on module-by-module. For instance,
179 // --vmodule=mapreduce=2,file=1,gfs*=3 --v=0
181 // a. VLOG(2) and lower messages to be printed from mapreduce.{h,cc}
182 // b. VLOG(1) and lower messages to be printed from file.{h,cc}
183 // c. VLOG(3) and lower messages to be printed from files prefixed with "gfs"
184 // d. VLOG(0) and lower messages to be printed from elsewhere
186 // The wildcarding functionality shown by (c) supports both '*' (match
187 // 0 or more characters) and '?' (match any single character) wildcards.
189 // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
191 // if (VLOG_IS_ON(2)) {
192 // // do some logging preparation and logging
193 // // that can't be accomplished with just VLOG(2) << ...;
196 // There are also VLOG_IF, VLOG_EVERY_N and VLOG_IF_EVERY_N "verbose level"
197 // condition macros for sample cases, when some extra computation and
198 // preparation for logs is not needed.
199 // VLOG_IF(1, (size > 1024))
200 // << "I'm printed when size is more than 1024 and when you run the "
201 // "program with --v=1 or more";
202 // VLOG_EVERY_N(1, 10)
203 // << "I'm printed every 10th occurrence, and when you run the program "
204 // "with --v=1 or more. Present occurence is " << COUNTER;
205 // VLOG_IF_EVERY_N(1, (size > 1024), 10)
206 // << "I'm printed on every 10th occurence of case when size is more "
207 // " than 1024, when you run the program with --v=1 or more. ";
208 // "Present occurence is " << COUNTER;
210 // The supported severity levels for macros that allow you to specify one
211 // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL.
212 // Note that messages of a given severity are logged not only in the
213 // logfile for that severity, but also in all logfiles of lower severity.
214 // E.g., a message of severity FATAL will be logged to the logfiles of
215 // severity FATAL, ERROR, WARNING, and INFO.
217 // There is also the special severity of DFATAL, which logs FATAL in
218 // debug mode, ERROR in normal mode.
220 // Very important: logging a message at the FATAL severity level causes
221 // the program to terminate (after the message is logged).
223 // Unless otherwise specified, logs will be written to the filename
224 // "<program name>.<hostname>.<user name>.log.<severity level>.", followed
225 // by the date, time, and pid (you can't prevent the date, time, and pid
226 // from being in the filename).
228 // The logging code takes two flags:
229 // --v=# set the verbose level
230 // --logtostderr log all the messages to stderr instead of to logfiles
232 #ifndef DECLARE_VARIABLE
233 #define MUST_UNDEF_GFLAGS_DECLARE_MACROS
234 #define DECLARE_VARIABLE(type, name, tn) \
235 namespace FLAG__namespace_do_not_use_directly_use_DECLARE_##tn##_instead { \
236 extern GOOGLE_GLOG_DLL_DECL type FLAGS_##name; \
238 using FLAG__namespace_do_not_use_directly_use_DECLARE_##tn##_instead::FLAGS_##name
240 // bool specialization
241 #define DECLARE_bool(name) \
242 DECLARE_VARIABLE(bool, name, bool)
244 // int32 specialization
245 #define DECLARE_int32(name) \
246 DECLARE_VARIABLE(@ac_google_namespace@::int32, name, int32)
248 // Special case for string, because we have to specify the namespace
249 // std::string, which doesn't play nicely with our FLAG__namespace hackery.
250 #define DECLARE_string(name) \
251 namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead { \
252 extern GOOGLE_GLOG_DLL_DECL std::string FLAGS_##name; \
254 using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name
257 // Set whether log messages go to stderr instead of logfiles
258 DECLARE_bool(logtostderr);
260 // Set how important a log message should be to avoid buffering
261 DECLARE_int32(logbuflevel);
263 // Log suppression level: messages logged at a lower level than this
265 DECLARE_int32(minloglevel);
267 // If specified, logfiles are written into this directory instead of the
268 // default logging directory.
269 DECLARE_string(log_dir);
271 DECLARE_int32(v); // in vlog_is_on.cc
273 DECLARE_int32(stderrthreshold);
274 DECLARE_bool(alsologtostderr);
276 #ifdef MUST_UNDEF_GFLAGS_DECLARE_MACROS
277 #undef MUST_UNDEF_GFLAGS_DECLARE_MACROS
278 #undef DECLARE_VARIABLE
281 #undef DECLARE_string
284 // Log messages below the GOOGLE_STRIP_LOG level will be compiled away for
285 // security reasons. See LOG(severtiy) below.
287 // A few definitions of macros that don't generate much code. Since
288 // LOG(INFO) and its ilk are used all over our code, it's
289 // better to have compact code for these operations.
291 #if 0 >= GOOGLE_STRIP_LOG
292 #define COMPACT_GOOGLE_LOG_INFO @ac_google_namespace@::LogMessage(__FILE__, __LINE__)
294 #define COMPACT_GOOGLE_LOG_INFO @ac_google_namespace@::NullStream()
297 #if 1 >= GOOGLE_STRIP_LOG
298 #define COMPACT_GOOGLE_LOG_WARNING @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::WARNING)
300 #define COMPACT_GOOGLE_LOG_WARNING @ac_google_namespace@::NullStream()
303 #if 2 >= GOOGLE_STRIP_LOG
304 #define COMPACT_GOOGLE_LOG_ERROR @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::ERROR)
306 #define COMPACT_GOOGLE_LOG_ERROR @ac_google_namespace@::NullStream()
309 #if 3 >= GOOGLE_STRIP_LOG
310 #define COMPACT_GOOGLE_LOG_FATAL @ac_google_namespace@::LogMessageFatal(__FILE__, __LINE__)
312 #define COMPACT_GOOGLE_LOG_FATAL @ac_google_namespace@::NullStreamFatal()
315 // For DFATAL, we want to use LogMessage (as opposed to
316 // LogMessageFatal), to be consistent with the original behavior.
318 #define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_ERROR
319 #elif 3 >= GOOGLE_STRIP_LOG
320 #define COMPACT_GOOGLE_LOG_DFATAL @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::FATAL)
322 #define COMPACT_GOOGLE_LOG_DFATAL @ac_google_namespace@::NullStreamFatal()
325 #define GOOGLE_LOG_INFO(counter) @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::INFO, counter, &@ac_google_namespace@::LogMessage::SendToLog)
326 #define SYSLOG_INFO(counter) \
327 @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::INFO, counter, \
328 &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
329 #define GOOGLE_LOG_WARNING(counter) \
330 @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::WARNING, counter, \
331 &@ac_google_namespace@::LogMessage::SendToLog)
332 #define SYSLOG_WARNING(counter) \
333 @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::WARNING, counter, \
334 &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
335 #define GOOGLE_LOG_ERROR(counter) \
336 @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::ERROR, counter, \
337 &@ac_google_namespace@::LogMessage::SendToLog)
338 #define SYSLOG_ERROR(counter) \
339 @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::ERROR, counter, \
340 &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
341 #define GOOGLE_LOG_FATAL(counter) \
342 @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::FATAL, counter, \
343 &@ac_google_namespace@::LogMessage::SendToLog)
344 #define SYSLOG_FATAL(counter) \
345 @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::FATAL, counter, \
346 &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
347 #define GOOGLE_LOG_DFATAL(counter) \
348 @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::DFATAL_LEVEL, counter, \
349 &@ac_google_namespace@::LogMessage::SendToLog)
350 #define SYSLOG_DFATAL(counter) \
351 @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::DFATAL_LEVEL, counter, \
352 &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
354 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) || defined(__CYGWIN32__)
355 // A very useful logging macro to log windows errors:
356 #define LOG_SYSRESULT(result) \
357 if (FAILED(result)) { \
358 LPTSTR message = NULL; \
359 LPTSTR msg = reinterpret_cast<LPTSTR>(&message); \
360 DWORD message_length = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | \
361 FORMAT_MESSAGE_FROM_SYSTEM, \
362 0, result, 0, msg, 100, NULL); \
363 if (message_length > 0) { \
364 @ac_google_namespace@::LogMessage(__FILE__, __LINE__, ERROR, 0, \
365 &@ac_google_namespace@::LogMessage::SendToLog).stream() << message; \
366 LocalFree(message); \
371 // We use the preprocessor's merging operator, "##", so that, e.g.,
372 // LOG(INFO) becomes the token GOOGLE_LOG_INFO. There's some funny
373 // subtle difference between ostream member streaming functions (e.g.,
374 // ostream::operator<<(int) and ostream non-member streaming functions
375 // (e.g., ::operator<<(ostream&, string&): it turns out that it's
376 // impossible to stream something like a string directly to an unnamed
377 // ostream. We employ a neat hack by calling the stream() member
378 // function of LogMessage which seems to avoid the problem.
379 #define LOG(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
380 #define SYSLOG(severity) SYSLOG_ ## severity(0).stream()
382 @ac_google_start_namespace@
384 // They need the definitions of integer types.
385 #include "glog/log_severity.h"
386 #include "glog/vlog_is_on.h"
388 // Initialize google's logging library. You will see the program name
389 // specified by argv0 in log outputs.
390 GOOGLE_GLOG_DLL_DECL void InitGoogleLogging(const char* argv0);
392 // Install a function which will be called after LOG(FATAL).
393 GOOGLE_GLOG_DLL_DECL void InstallFailureFunction(void (*fail_func)());
395 class LogSink; // defined below
397 // If a non-NULL sink pointer is given, we push this message to that sink.
398 // We then do normal LOG(severity) logging as well.
399 // This is useful for capturing messages and passing/storing them
400 // somewhere more specific than the global log of the process.
403 // LogSeverity severity;
404 // The cast is to disambiguate NULL arguments.
405 #define LOG_TO_SINK(sink, severity) \
406 @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::severity, \
407 static_cast<@ac_google_namespace@::LogSink*>(sink)).stream()
409 // If a non-NULL pointer is given, we push the message onto the end
410 // of a vector of strings; otherwise, we report it with LOG(severity).
411 // This is handy for capturing messages and perhaps passing them back
412 // to the caller, rather than reporting them immediately.
414 // LogSeverity severity;
415 // vector<string> *outvec;
416 // The cast is to disambiguate NULL arguments.
417 #define LOG_STRING(severity, outvec) \
418 @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::severity, \
419 static_cast<std::vector<std::string>*>(outvec)).stream()
421 #define LOG_IF(severity, condition) \
422 !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
423 #define SYSLOG_IF(severity, condition) \
424 !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & SYSLOG(severity)
426 #define LOG_ASSERT(condition) \
427 LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition
428 #define SYSLOG_ASSERT(condition) \
429 SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition
431 // CHECK dies with a fatal error if condition is not true. It is *not*
432 // controlled by NDEBUG, so the check will be executed regardless of
433 // compilation mode. Therefore, it is safe to do things like:
434 // CHECK(fp->Write(x) == 4)
435 #define CHECK(condition) \
436 LOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN(!(condition))) \
437 << "Check failed: " #condition " "
439 // A container for a string pointer which can be evaluated to a bool -
440 // true iff the pointer is NULL.
441 struct CheckOpString {
442 CheckOpString(std::string* str) : str_(str) { }
443 // No destructor: if str_ is non-NULL, we're about to LOG(FATAL),
444 // so there's no point in cleaning up str_.
445 operator bool() const {
446 return GOOGLE_PREDICT_BRANCH_NOT_TAKEN(str_ != NULL);
451 // Function is overloaded for integral types to allow static const
452 // integrals declared in classes and not defined to be used as arguments to
453 // CHECK* macros. It's not encouraged though.
455 inline const T& GetReferenceableValue(const T& t) { return t; }
456 inline char GetReferenceableValue(char t) { return t; }
457 inline unsigned char GetReferenceableValue(unsigned char t) { return t; }
458 inline signed char GetReferenceableValue(signed char t) { return t; }
459 inline short GetReferenceableValue(short t) { return t; }
460 inline unsigned short GetReferenceableValue(unsigned short t) { return t; }
461 inline int GetReferenceableValue(int t) { return t; }
462 inline unsigned int GetReferenceableValue(unsigned int t) { return t; }
463 inline long GetReferenceableValue(long t) { return t; }
464 inline unsigned long GetReferenceableValue(unsigned long t) { return t; }
465 inline long long GetReferenceableValue(long long t) { return t; }
466 inline unsigned long long GetReferenceableValue(unsigned long long t) {
470 // This is a dummy class to define the following operator.
471 struct DummyClassToDefineOperator {};
473 @ac_google_end_namespace@
475 // Define global operator<< to declare using ::operator<<.
476 // This declaration will allow use to use CHECK macros for user
477 // defined classes which have operator<< (e.g., stl_logging.h).
478 inline std::ostream& operator<<(
479 std::ostream& out, const google::DummyClassToDefineOperator& dummy) {
483 @ac_google_start_namespace@
485 // Build the error message string.
486 template<class t1, class t2>
487 std::string* MakeCheckOpString(const t1& v1, const t2& v2, const char* names) {
488 // It means that we cannot use stl_logging if compiler doesn't
489 // support using expression for operator.
490 // TODO(hamaji): Figure out a way to fix.
491 #if @ac_cv_cxx_using_operator@
495 ss << names << " (" << v1 << " vs. " << v2 << ")";
496 return new std::string(ss.str(), ss.pcount());
499 // Helper functions for CHECK_OP macro.
500 // The (int, int) specialization works around the issue that the compiler
501 // will not instantiate the template version of the function on values of
502 // unnamed enum type - see comment below.
503 #define DEFINE_CHECK_OP_IMPL(name, op) \
504 template <class t1, class t2> \
505 inline std::string* Check##name##Impl(const t1& v1, const t2& v2, \
506 const char* names) { \
507 if (v1 op v2) return NULL; \
508 else return MakeCheckOpString(v1, v2, names); \
510 inline std::string* Check##name##Impl(int v1, int v2, const char* names) { \
511 return Check##name##Impl<int, int>(v1, v2, names); \
514 // Use _EQ, _NE, _LE, etc. in case the file including base/logging.h
515 // provides its own #defines for the simpler names EQ, NE, LE, etc.
516 // This happens if, for example, those are used as token names in a
518 DEFINE_CHECK_OP_IMPL(_EQ, ==)
519 DEFINE_CHECK_OP_IMPL(_NE, !=)
520 DEFINE_CHECK_OP_IMPL(_LE, <=)
521 DEFINE_CHECK_OP_IMPL(_LT, < )
522 DEFINE_CHECK_OP_IMPL(_GE, >=)
523 DEFINE_CHECK_OP_IMPL(_GT, > )
524 #undef DEFINE_CHECK_OP_IMPL
526 // Helper macro for binary operators.
527 // Don't use this macro directly in your code, use CHECK_EQ et al below.
529 #if defined(STATIC_ANALYSIS)
530 // Only for static analysis tool to know that it is equivalent to assert
531 #define CHECK_OP_LOG(name, op, val1, val2, log) CHECK((val1) op (val2))
532 #elif !defined(NDEBUG)
533 // In debug mode, avoid constructing CheckOpStrings if possible,
534 // to reduce the overhead of CHECK statments by 2x.
535 // Real DCHECK-heavy tests have seen 1.5x speedups.
537 // The meaning of "string" might be different between now and
538 // when this macro gets invoked (e.g., if someone is experimenting
539 // with other string implementations that get defined after this
540 // file is included). Save the current meaning now and use it
542 typedef std::string _Check_string;
543 #define CHECK_OP_LOG(name, op, val1, val2, log) \
544 while (@ac_google_namespace@::_Check_string* _result = \
545 @ac_google_namespace@::Check##name##Impl((val1), (val2), #val1 " " #op " " #val2)) \
546 log(__FILE__, __LINE__, @ac_google_namespace@::CheckOpString(_result)).stream()
548 // In optimized mode, use CheckOpString to hint to compiler that
549 // the while condition is unlikely.
550 #define CHECK_OP_LOG(name, op, val1, val2, log) \
551 while (@ac_google_namespace@::CheckOpString _result = \
552 @ac_google_namespace@::Check##name##Impl(GetReferenceableValue(val1), \
553 GetReferenceableValue(val2), \
554 #val1 " " #op " " #val2)) \
555 log(__FILE__, __LINE__, _result).stream()
556 #endif // STATIC_ANALYSIS, !NDEBUG
558 #define CHECK_OP(name, op, val1, val2) \
559 CHECK_OP_LOG(name, op, val1, val2, @ac_google_namespace@::LogMessageFatal)
561 // Equality/Inequality checks - compare two values, and log a FATAL message
562 // including the two values when the result is not as expected. The values
563 // must have operator<<(ostream, ...) defined.
565 // You may append to the error message like so:
566 // CHECK_NE(1, 2) << ": The world must be ending!";
568 // We are very careful to ensure that each argument is evaluated exactly
569 // once, and that anything which is legal to pass as a function argument is
570 // legal here. In particular, the arguments may be temporary expressions
571 // which will end up being destroyed at the end of the apparent statement,
573 // CHECK_EQ(string("abc")[1], 'b');
575 // WARNING: These don't compile correctly if one of the arguments is a pointer
576 // and the other is NULL. To work around this, simply static_cast NULL to the
577 // type of the desired pointer.
579 #define CHECK_EQ(val1, val2) CHECK_OP(_EQ, ==, val1, val2)
580 #define CHECK_NE(val1, val2) CHECK_OP(_NE, !=, val1, val2)
581 #define CHECK_LE(val1, val2) CHECK_OP(_LE, <=, val1, val2)
582 #define CHECK_LT(val1, val2) CHECK_OP(_LT, < , val1, val2)
583 #define CHECK_GE(val1, val2) CHECK_OP(_GE, >=, val1, val2)
584 #define CHECK_GT(val1, val2) CHECK_OP(_GT, > , val1, val2)
586 // Check that the input is non NULL. This very useful in constructor
587 // initializer lists.
589 #define CHECK_NOTNULL(val) \
590 @ac_google_namespace@::CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
592 // Helper functions for string comparisons.
593 // To avoid bloat, the definitions are in logging.cc.
594 #define DECLARE_CHECK_STROP_IMPL(func, expected) \
595 GOOGLE_GLOG_DLL_DECL std::string* Check##func##expected##Impl( \
596 const char* s1, const char* s2, const char* names);
597 DECLARE_CHECK_STROP_IMPL(strcmp, true)
598 DECLARE_CHECK_STROP_IMPL(strcmp, false)
599 DECLARE_CHECK_STROP_IMPL(strcasecmp, true)
600 DECLARE_CHECK_STROP_IMPL(strcasecmp, false)
601 #undef DECLARE_CHECK_STROP_IMPL
603 // Helper macro for string comparisons.
604 // Don't use this macro directly in your code, use CHECK_STREQ et al below.
605 #define CHECK_STROP(func, op, expected, s1, s2) \
606 while (@ac_google_namespace@::CheckOpString _result = \
607 @ac_google_namespace@::Check##func##expected##Impl((s1), (s2), \
608 #s1 " " #op " " #s2)) \
609 LOG(FATAL) << *_result.str_
612 // String (char*) equality/inequality checks.
613 // CASE versions are case-insensitive.
615 // Note that "s1" and "s2" may be temporary strings which are destroyed
616 // by the compiler at the end of the current "full expression"
617 // (e.g. CHECK_STREQ(Foo().c_str(), Bar().c_str())).
619 #define CHECK_STREQ(s1, s2) CHECK_STROP(strcmp, ==, true, s1, s2)
620 #define CHECK_STRNE(s1, s2) CHECK_STROP(strcmp, !=, false, s1, s2)
621 #define CHECK_STRCASEEQ(s1, s2) CHECK_STROP(strcasecmp, ==, true, s1, s2)
622 #define CHECK_STRCASENE(s1, s2) CHECK_STROP(strcasecmp, !=, false, s1, s2)
624 #define CHECK_INDEX(I,A) CHECK(I < (sizeof(A)/sizeof(A[0])))
625 #define CHECK_BOUND(B,A) CHECK(B <= (sizeof(A)/sizeof(A[0])))
627 #define CHECK_DOUBLE_EQ(val1, val2) \
629 CHECK_LE((val1), (val2)+0.000000000000001L); \
630 CHECK_GE((val1), (val2)-0.000000000000001L); \
633 #define CHECK_NEAR(val1, val2, margin) \
635 CHECK_LE((val1), (val2)+(margin)); \
636 CHECK_GE((val1), (val2)-(margin)); \
639 // perror()..googly style!
641 // PLOG() and PLOG_IF() and PCHECK() behave exactly like their LOG* and
642 // CHECK equivalents with the addition that they postpend a description
643 // of the current state of errno to their output lines.
645 #define PLOG(severity) GOOGLE_PLOG(severity, 0).stream()
647 #define GOOGLE_PLOG(severity, counter) \
648 @ac_google_namespace@::ErrnoLogMessage( \
649 __FILE__, __LINE__, @ac_google_namespace@::severity, counter, \
650 &@ac_google_namespace@::LogMessage::SendToLog)
652 #define PLOG_IF(severity, condition) \
653 !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & PLOG(severity)
655 // A CHECK() macro that postpends errno if the condition is false. E.g.
657 // if (poll(fds, nfds, timeout) == -1) { PCHECK(errno == EINTR); ... }
658 #define PCHECK(condition) \
659 PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN(!(condition))) \
660 << "Check failed: " #condition " "
662 // A CHECK() macro that lets you assert the success of a function that
663 // returns -1 and sets errno in case of an error. E.g.
665 // CHECK_ERR(mkdir(path, 0700));
669 // int fd = open(filename, flags); CHECK_ERR(fd) << ": open " << filename;
670 #define CHECK_ERR(invocation) \
671 PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1)) \
674 // Use macro expansion to create, for each use of LOG_EVERY_N(), static
675 // variables with the __LINE__ expansion as part of the variable name.
676 #define LOG_EVERY_N_VARNAME(base, line) LOG_EVERY_N_VARNAME_CONCAT(base, line)
677 #define LOG_EVERY_N_VARNAME_CONCAT(base, line) base ## line
679 #define LOG_OCCURRENCES LOG_EVERY_N_VARNAME(occurrences_, __LINE__)
680 #define LOG_OCCURRENCES_MOD_N LOG_EVERY_N_VARNAME(occurrences_mod_n_, __LINE__)
682 #define SOME_KIND_OF_LOG_EVERY_N(severity, n, what_to_do) \
683 static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
685 if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
686 if (LOG_OCCURRENCES_MOD_N == 1) \
687 @ac_google_namespace@::LogMessage( \
688 __FILE__, __LINE__, @ac_google_namespace@::severity, LOG_OCCURRENCES, \
689 &what_to_do).stream()
691 #define SOME_KIND_OF_LOG_IF_EVERY_N(severity, condition, n, what_to_do) \
692 static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
695 ((LOG_OCCURRENCES_MOD_N=(LOG_OCCURRENCES_MOD_N + 1) % n) == (1 % n))) \
696 @ac_google_namespace@::LogMessage( \
697 __FILE__, __LINE__, @ac_google_namespace@::severity, LOG_OCCURRENCES, \
698 &what_to_do).stream()
700 #define SOME_KIND_OF_PLOG_EVERY_N(severity, n, what_to_do) \
701 static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
703 if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
704 if (LOG_OCCURRENCES_MOD_N == 1) \
705 @ac_google_namespace@::ErrnoLogMessage( \
706 __FILE__, __LINE__, @ac_google_namespace@::severity, LOG_OCCURRENCES, \
707 &what_to_do).stream()
709 #define SOME_KIND_OF_LOG_FIRST_N(severity, n, what_to_do) \
710 static int LOG_OCCURRENCES = 0; \
711 if (LOG_OCCURRENCES <= n) \
713 if (LOG_OCCURRENCES <= n) \
714 @ac_google_namespace@::LogMessage( \
715 __FILE__, __LINE__, @ac_google_namespace@::severity, LOG_OCCURRENCES, \
716 &what_to_do).stream()
718 #define LOG_EVERY_N(severity, n) \
719 SOME_KIND_OF_LOG_EVERY_N(severity, (n), @ac_google_namespace@::LogMessage::SendToLog)
721 #define SYSLOG_EVERY_N(severity, n) \
722 SOME_KIND_OF_LOG_EVERY_N(severity, (n), @ac_google_namespace@::LogMessage::SendToSyslogAndLog)
724 #define PLOG_EVERY_N(severity, n) \
725 SOME_KIND_OF_PLOG_EVERY_N(severity, (n), @ac_google_namespace@::LogMessage::SendToLog)
727 #define LOG_FIRST_N(severity, n) \
728 SOME_KIND_OF_LOG_FIRST_N(severity, (n), @ac_google_namespace@::LogMessage::SendToLog)
730 #define LOG_IF_EVERY_N(severity, condition, n) \
731 SOME_KIND_OF_LOG_IF_EVERY_N(severity, (condition), (n), @ac_google_namespace@::LogMessage::SendToLog)
733 // We want the special COUNTER value available for LOG_EVERY_X()'ed messages
734 enum PRIVATE_Counter {COUNTER};
737 // Plus some debug-logging macros that get compiled to nothing for production
741 #define DLOG(severity) LOG(severity)
742 #define DLOG_IF(severity, condition) LOG_IF(severity, condition)
743 #define DLOG_EVERY_N(severity, n) LOG_EVERY_N(severity, n)
744 #define DLOG_IF_EVERY_N(severity, condition, n) \
745 LOG_IF_EVERY_N(severity, condition, n)
746 #define DLOG_ASSERT(condition) LOG_ASSERT(condition)
748 // debug-only checking. not executed in NDEBUG mode.
749 #define DCHECK(condition) CHECK(condition)
750 #define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
751 #define DCHECK_NE(val1, val2) CHECK_NE(val1, val2)
752 #define DCHECK_LE(val1, val2) CHECK_LE(val1, val2)
753 #define DCHECK_LT(val1, val2) CHECK_LT(val1, val2)
754 #define DCHECK_GE(val1, val2) CHECK_GE(val1, val2)
755 #define DCHECK_GT(val1, val2) CHECK_GT(val1, val2)
756 #define DCHECK_STREQ(str1, str2) CHECK_STREQ(str1, str2)
757 #define DCHECK_STRCASEEQ(str1, str2) CHECK_STRCASEEQ(str1, str2)
758 #define DCHECK_STRNE(str1, str2) CHECK_STRNE(str1, str2)
759 #define DCHECK_STRCASENE(str1, str2) CHECK_STRCASENE(str1, str2)
763 #define DLOG(severity) \
764 true ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
766 #define DLOG_IF(severity, condition) \
767 (true || !(condition)) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
769 #define DLOG_EVERY_N(severity, n) \
770 true ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
772 #define DLOG_IF_EVERY_N(severity, condition, n) \
773 (true || !(condition))? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
775 #define DLOG_ASSERT(condition) \
776 true ? (void) 0 : LOG_ASSERT(condition)
778 #define DCHECK(condition) \
782 #define DCHECK_EQ(val1, val2) \
786 #define DCHECK_NE(val1, val2) \
790 #define DCHECK_LE(val1, val2) \
794 #define DCHECK_LT(val1, val2) \
798 #define DCHECK_GE(val1, val2) \
802 #define DCHECK_GT(val1, val2) \
806 #define DCHECK_STREQ(str1, str2) \
808 CHECK_STREQ(str1, str2)
810 #define DCHECK_STRCASEEQ(str1, str2) \
812 CHECK_STRCASEEQ(str1, str2)
814 #define DCHECK_STRNE(str1, str2) \
816 CHECK_STRNE(str1, str2)
818 #define DCHECK_STRCASENE(str1, str2) \
820 CHECK_STRCASENE(str1, str2)
825 // Log only in verbose mode.
827 #define VLOG(verboselevel) LOG_IF(INFO, VLOG_IS_ON(verboselevel))
829 #define VLOG_IF(verboselevel, condition) \
830 LOG_IF(INFO, (condition) && VLOG_IS_ON(verboselevel))
832 #define VLOG_EVERY_N(verboselevel, n) \
833 LOG_IF_EVERY_N(INFO, VLOG_IS_ON(verboselevel), n)
835 #define VLOG_IF_EVERY_N(verboselevel, condition, n) \
836 LOG_IF_EVERY_N(INFO, (condition) && VLOG_IS_ON(verboselevel), n)
839 // This class more or less represents a particular log message. You
840 // create an instance of LogMessage and then stream stuff to it.
841 // When you finish streaming to it, ~LogMessage is called and the
842 // full message gets streamed to the appropriate destination.
844 // You shouldn't actually use LogMessage's constructor to log things,
845 // though. You should use the LOG() macro (and variants thereof)
847 class GOOGLE_GLOG_DLL_DECL LogMessage {
850 // Passing kNoLogPrefix for the line number disables the
851 // log-message prefix. Useful for using the LogMessage
852 // infrastructure as a printing utility. See also the --log_prefix
853 // flag for controlling the log-message prefix on an
854 // application-wide basis.
858 // LogStream inherit from non-DLL-exported class (std::ostrstream)
859 // and VC++ produces a warning for this situation.
860 // However, MSDN says "C4275 can be ignored in Microsoft Visual C++
861 // 2005 if you are deriving from a type in the Standard C++ Library"
862 // http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx
863 // Let's just ignore the warning.
865 # pragma warning(disable: 4275)
867 class GOOGLE_GLOG_DLL_DECL LogStream : public std::ostrstream {
869 # pragma warning(default: 4275)
872 LogStream(char *buf, int len, int ctr)
873 : ostrstream(buf, len),
878 int ctr() const { return ctr_; }
879 void set_ctr(int ctr) { ctr_ = ctr; }
880 LogStream* self() const { return self_; }
883 int ctr_; // Counter hack (for the LOG_EVERY_X() macro)
884 LogStream *self_; // Consistency check hack
888 // icc 8 requires this typedef to avoid an internal compiler error.
889 typedef void (LogMessage::*SendMethod)();
891 LogMessage(const char* file, int line, LogSeverity severity, int ctr,
892 SendMethod send_method);
894 // Two special constructors that generate reduced amounts of code at
895 // LOG call sites for common cases.
897 // Used for LOG(INFO): Implied are:
898 // severity = INFO, ctr = 0, send_method = &LogMessage::SendToLog.
900 // Using this constructor instead of the more complex constructor above
901 // saves 19 bytes per call site.
902 LogMessage(const char* file, int line);
904 // Used for LOG(severity) where severity != INFO. Implied
905 // are: ctr = 0, send_method = &LogMessage::SendToLog
907 // Using this constructor instead of the more complex constructor above
908 // saves 17 bytes per call site.
909 LogMessage(const char* file, int line, LogSeverity severity);
911 // Constructor to also log this message to a specified sink (if not NULL).
912 // Implied are: ctr = 0, send_method = &LogMessage::SendToSinkAndLog.
913 LogMessage(const char* file, int line, LogSeverity severity, LogSink* sink);
915 // Constructor where we also give a vector<string> pointer
916 // for storing the messages (if the pointer is not NULL).
917 // Implied are: ctr = 0, send_method = &LogMessage::SaveOrSendToLog.
918 LogMessage(const char* file, int line, LogSeverity severity,
919 std::vector<std::string>* outvec);
921 // A special constructor used for check failures
922 LogMessage(const char* file, int line, const CheckOpString& result);
926 // Flush a buffered message to the sink set in the constructor. Always
927 // called by the destructor, it may also be called from elsewhere if
928 // needed. Only the first call is actioned; any later ones are ignored.
931 // An arbitrary limit on the length of a single log message. This
932 // is so that streaming can be done more efficiently.
933 static const size_t kMaxLogMessageLen;
935 // Theses should not be called directly outside of logging.*,
936 // only passed as SendMethod arguments to other LogMessage methods:
937 void SendToLog(); // Actually dispatch to the logs
938 void SendToSyslogAndLog(); // Actually dispatch to syslog and the logs
940 // Call abort() or similar to perform LOG(FATAL) crash.
941 static void Fail() @ac_cv___attribute___noreturn@;
943 std::ostream& stream() { return data_->stream_; }
945 int preserved_errno() const { return data_->preserved_errno_; }
947 // Must be called without the log_mutex held. (L < log_mutex)
948 static int64 num_messages(int severity);
952 // Fully internal SendMethod cases:
953 void SendToSinkAndLog(); // Send to sink if provided and dispatch to the logs
954 void SaveOrSendToLog(); // Save to stringvec if provided, else to logs
956 struct LogMessageData;
958 void Init(const char* file, int line, LogSeverity severity,
959 void (LogMessage::*send_method)());
961 LogMessageData* GetMessageData(int preserved_errno, LogSeverity, int ctr);
963 // Counts of messages sent at each priority:
964 static int64 num_messages_[NUM_SEVERITIES]; // under log_mutex
966 // We keep the data in a separate struct so that each instance of
967 // LogMessage uses less stack space.
968 struct GOOGLE_GLOG_DLL_DECL LogMessageData {
969 // ORDER DEPENDENCY: preserved_errno_ comes before buf_ comes before
970 // message_text_ comes before stream_
971 int preserved_errno_; // preserved errno
973 char* message_text_; // Complete message text (points to selected buffer)
975 const char severity_; // What level is this LogMessage logged at?
976 int line_; // line number where logging call is.
977 void (LogMessage::*send_method_)(); // Call this in destructor to send
978 union { // At most one of these is used: union to keep the size low.
979 LogSink* sink_; // NULL or sink to send message to
980 std::vector<std::string>* outvec_; // NULL or vector to push message onto
982 time_t timestamp_; // Time of creation of LogMessage
983 struct ::tm tm_time_; // Time of creation of LogMessage
984 size_t num_prefix_chars_; // How many chars of "prefix" for this message?
985 size_t num_chars_to_log_; // How many chars of msg to send to log?
986 size_t num_chars_to_syslog_; // How many chars of msg to send to syslog?
988 const char* basename_; // basename of the file which called LOG.
989 const char* fullname_; // full name (including directory)
990 // of the file which called LOG.
992 bool has_been_flushed_; // False if data has not yet been flushed.
994 LogMessageData(int preserved_errno, LogSeverity severity, int ctr);
998 LogMessageData(const LogMessageData&);
999 void operator=(const LogMessageData&);
1002 static LogMessageData fatal_message_data_;
1004 LogMessageData* allocated_;
1005 LogMessageData* data_;
1007 friend class LogDestination;
1009 LogMessage(const LogMessage&);
1010 void operator=(const LogMessage&);
1013 // This class happens to be thread-hostile because all instances share
1014 // a single data buffer, but since it can only be created just before
1015 // the process dies, we don't worry so much.
1016 class GOOGLE_GLOG_DLL_DECL LogMessageFatal : public LogMessage {
1018 LogMessageFatal(const char* file, int line);
1019 LogMessageFatal(const char* file, int line, const CheckOpString& result);
1020 ~LogMessageFatal() @ac_cv___attribute___noreturn@;
1023 // A non-macro interface to the log facility; (useful
1024 // when the logging level is not a compile-time constant).
1025 inline void LogAtLevel(int const log_level, std::string const &msg) {
1026 LogMessage(__FILE__, __LINE__, log_level).stream() << msg;
1029 // A small helper for CHECK_NOTNULL().
1030 template <typename T>
1031 T* CheckNotNull(const char *file, int line, const char *names, T* t) {
1033 LogMessageFatal(file, line, new std::string(names));
1038 // Allow folks to put a counter in the LOG_EVERY_X()'ed messages. This
1039 // only works if ostream is a LogStream. If the ostream is not a
1040 // LogStream you'll get an assert saying as much at runtime.
1041 GOOGLE_GLOG_DLL_DECL std::ostream& operator<<(std::ostream &os,
1042 const PRIVATE_Counter&);
1045 // Derived class for PLOG*() above.
1046 class GOOGLE_GLOG_DLL_DECL ErrnoLogMessage : public LogMessage {
1049 ErrnoLogMessage(const char* file, int line, LogSeverity severity, int ctr,
1050 void (LogMessage::*send_method)());
1052 // Postpends ": strerror(errno) [errno]".
1056 ErrnoLogMessage(const ErrnoLogMessage&);
1057 void operator=(const ErrnoLogMessage&);
1061 // This class is used to explicitly ignore values in the conditional
1062 // logging macros. This avoids compiler warnings like "value computed
1063 // is not used" and "statement has no effect".
1065 class GOOGLE_GLOG_DLL_DECL LogMessageVoidify {
1067 LogMessageVoidify() { }
1068 // This has to be an operator with a precedence lower than << but
1070 void operator&(std::ostream&) { }
1074 // Flushes all log files that contains messages that are at least of
1075 // the specified severity level. Thread-safe.
1076 GOOGLE_GLOG_DLL_DECL void FlushLogFiles(LogSeverity min_severity);
1078 // Flushes all log files that contains messages that are at least of
1079 // the specified severity level. Thread-hostile because it ignores
1080 // locking -- used for catastrophic failures.
1081 GOOGLE_GLOG_DLL_DECL void FlushLogFilesUnsafe(LogSeverity min_severity);
1084 // Set the destination to which a particular severity level of log
1085 // messages is sent. If base_filename is "", it means "don't log this
1086 // severity". Thread-safe.
1088 GOOGLE_GLOG_DLL_DECL void SetLogDestination(LogSeverity severity,
1089 const char* base_filename);
1092 // Set the basename of the symlink to the latest log file at a given
1093 // severity. If symlink_basename is empty, do not make a symlink. If
1094 // you don't call this function, the symlink basename is the
1095 // invocation name of the program. Thread-safe.
1097 GOOGLE_GLOG_DLL_DECL void SetLogSymlink(LogSeverity severity,
1098 const char* symlink_basename);
1101 // Used to send logs to some other kind of destination
1102 // Users should subclass LogSink and override send to do whatever they want.
1103 // Implementations must be thread-safe because a shared instance will
1104 // be called from whichever thread ran the LOG(XXX) line.
1105 class GOOGLE_GLOG_DLL_DECL LogSink {
1109 // Sink's logging logic (message_len is such as to exclude '\n' at the end).
1110 // This method can't use LOG() or CHECK() as logging system mutex(s) are held
1111 // during this call.
1112 virtual void send(LogSeverity severity, const char* full_filename,
1113 const char* base_filename, int line,
1114 const struct ::tm* tm_time,
1115 const char* message, size_t message_len) = 0;
1117 // Redefine this to implement waiting for
1118 // the sink's logging logic to complete.
1119 // It will be called after each send() returns,
1120 // but before that LogMessage exits or crashes.
1121 // By default this function does nothing.
1122 // Using this function one can implement complex logic for send()
1123 // that itself involves logging; and do all this w/o causing deadlocks and
1124 // inconsistent rearrangement of log messages.
1125 // E.g. if a LogSink has thread-specific actions, the send() method
1126 // can simply add the message to a queue and wake up another thread that
1127 // handles real logging while itself making some LOG() calls;
1128 // WaitTillSent() can be implemented to wait for that logic to complete.
1129 // See our unittest for an example.
1130 virtual void WaitTillSent();
1132 // Returns the normal text output of the log message.
1133 // Can be useful to implement send().
1134 static std::string ToString(LogSeverity severity, const char* file, int line,
1135 const struct ::tm* tm_time,
1136 const char* message, size_t message_len);
1139 // Add or remove a LogSink as a consumer of logging data. Thread-safe.
1140 GOOGLE_GLOG_DLL_DECL void AddLogSink(LogSink *destination);
1141 GOOGLE_GLOG_DLL_DECL void RemoveLogSink(LogSink *destination);
1144 // Specify an "extension" added to the filename specified via
1145 // SetLogDestination. This applies to all severity levels. It's
1146 // often used to append the port we're listening on to the logfile
1147 // name. Thread-safe.
1149 GOOGLE_GLOG_DLL_DECL void SetLogFilenameExtension(
1150 const char* filename_extension);
1153 // Make it so that all log messages of at least a particular severity
1154 // are logged to stderr (in addition to logging to the usual log
1155 // file(s)). Thread-safe.
1157 GOOGLE_GLOG_DLL_DECL void SetStderrLogging(LogSeverity min_severity);
1160 // Make it so that all log messages go only to stderr. Thread-safe.
1162 GOOGLE_GLOG_DLL_DECL void LogToStderr();
1165 // Make it so that all log messages of at least a particular severity are
1166 // logged via email to a list of addresses (in addition to logging to the
1167 // usual log file(s)). The list of addresses is just a string containing
1168 // the email addresses to send to (separated by spaces, say). Thread-safe.
1170 GOOGLE_GLOG_DLL_DECL void SetEmailLogging(LogSeverity min_severity,
1171 const char* addresses);
1173 // A simple function that sends email. dest is a commma-separated
1174 // list of addressess. Thread-safe.
1175 GOOGLE_GLOG_DLL_DECL bool SendEmail(const char *dest,
1176 const char *subject, const char *body);
1178 GOOGLE_GLOG_DLL_DECL const std::vector<std::string>& GetLoggingDirectories();
1180 // For tests only: Clear the internal [cached] list of logging directories to
1181 // force a refresh the next time GetLoggingDirectories is called.
1183 void TestOnly_ClearLoggingDirectoriesList();
1185 // Returns a set of existing temporary directories, which will be a
1186 // subset of the directories returned by GetLogginDirectories().
1188 GOOGLE_GLOG_DLL_DECL void GetExistingTempDirectories(
1189 std::vector<std::string>* list);
1191 // Print any fatal message again -- useful to call from signal handler
1192 // so that the last thing in the output is the fatal message.
1193 // Thread-hostile, but a race is unlikely.
1194 GOOGLE_GLOG_DLL_DECL void ReprintFatalMessage();
1196 // Truncate a log file that may be the append-only output of multiple
1197 // processes and hence can't simply be renamed/reopened (typically a
1198 // stdout/stderr). If the file "path" is > "limit" bytes, copy the
1199 // last "keep" bytes to offset 0 and truncate the rest. Since we could
1200 // be racing with other writers, this approach has the potential to
1201 // lose very small amounts of data. For security, only follow symlinks
1202 // if the path is /proc/self/fd/*
1203 GOOGLE_GLOG_DLL_DECL void TruncateLogFile(const char *path,
1204 int64 limit, int64 keep);
1206 // Truncate stdout and stderr if they are over the value specified by
1207 // --max_log_size; keep the final 1MB. This function has the same
1208 // race condition as TruncateLogFile.
1209 GOOGLE_GLOG_DLL_DECL void TruncateStdoutStderr();
1211 // Return the string representation of the provided LogSeverity level.
1213 GOOGLE_GLOG_DLL_DECL const char* GetLogSeverityName(LogSeverity severity);
1215 // ---------------------------------------------------------------------
1216 // Implementation details that are not useful to most clients
1217 // ---------------------------------------------------------------------
1219 // A Logger is the interface used by logging modules to emit entries
1220 // to a log. A typical implementation will dump formatted data to a
1221 // sequence of files. We also provide interfaces that will forward
1222 // the data to another thread so that the invoker never blocks.
1223 // Implementations should be thread-safe since the logging system
1224 // will write to them from multiple threads.
1228 class GOOGLE_GLOG_DLL_DECL Logger {
1232 // Writes "message[0,message_len-1]" corresponding to an event that
1233 // occurred at "timestamp". If "force_flush" is true, the log file
1234 // is flushed immediately.
1236 // The input message has already been formatted as deemed
1237 // appropriate by the higher level logging facility. For example,
1238 // textual log messages already contain timestamps, and the
1239 // file:linenumber header.
1240 virtual void Write(bool force_flush,
1242 const char* message,
1243 int message_len) = 0;
1245 // Flush any buffered messages
1246 virtual void Flush() = 0;
1248 // Get the current LOG file size.
1249 // The returned value is approximate since some
1250 // logged data may not have been flushed to disk yet.
1251 virtual uint32 LogSize() = 0;
1254 // Get the logger for the specified severity level. The logger
1255 // remains the property of the logging module and should not be
1256 // deleted by the caller. Thread-safe.
1257 extern GOOGLE_GLOG_DLL_DECL Logger* GetLogger(LogSeverity level);
1259 // Set the logger for the specified severity level. The logger
1260 // becomes the property of the logging module and should not
1261 // be deleted by the caller. Thread-safe.
1262 extern GOOGLE_GLOG_DLL_DECL void SetLogger(LogSeverity level, Logger* logger);
1266 // glibc has traditionally implemented two incompatible versions of
1267 // strerror_r(). There is a poorly defined convention for picking the
1268 // version that we want, but it is not clear whether it even works with
1269 // all versions of glibc.
1270 // So, instead, we provide this wrapper that automatically detects the
1271 // version that is in use, and then implements POSIX semantics.
1272 // N.B. In addition to what POSIX says, we also guarantee that "buf" will
1273 // be set to an empty string, if this function failed. This means, in most
1274 // cases, you do not need to check the error code and you can directly
1275 // use the value of "buf". It will never have an undefined value.
1276 GOOGLE_GLOG_DLL_DECL int posix_strerror_r(int err, char *buf, size_t len);
1279 // A class for which we define operator<<, which does nothing.
1280 class GOOGLE_GLOG_DLL_DECL NullStream : public LogMessage::LogStream {
1282 // Initialize the LogStream so the messages can be written somewhere
1283 // (they'll never be actually displayed). This will be needed if a
1284 // NullStream& is implicitly converted to LogStream&, in which case
1285 // the overloaded NullStream::operator<< will not be invoked.
1286 NullStream() : LogMessage::LogStream(message_buffer_, 1, 0) { }
1287 NullStream &stream() { return *this; }
1289 // A very short buffer for messages (which we discard anyway). This
1290 // will be needed if NullStream& converted to LogStream& (e.g. as a
1291 // result of a conditional expression).
1292 char message_buffer_[2];
1295 // Do nothing. This operator is inline, allowing the message to be
1296 // compiled away. The message will not be compiled away if we do
1297 // something like (flag ? LOG(INFO) : LOG(ERROR)) << message; when
1298 // SKIP_LOG=WARNING. In those cases, NullStream will be implicitly
1299 // converted to LogStream and the message will be computed and then
1300 // quietly discarded.
1302 inline NullStream& operator<<(NullStream &str, const T &value) { return str; }
1304 // Similar to NullStream, but aborts the program (without stack
1305 // trace), like LogMessageFatal.
1306 class GOOGLE_GLOG_DLL_DECL NullStreamFatal : public NullStream {
1308 @ac_cv___attribute___noreturn@ ~NullStreamFatal() { _exit(1); }
1311 // Install a signal handler that will dump signal information and a stack
1312 // trace when the program crashes on certain signals. We'll install the
1313 // signal handler for the following signals.
1315 // SIGSEGV, SIGILL, SIGFPE, SIGABRT, SIGBUS, and SIGTERM.
1317 // By default, the signal handler will write the failure dump to the
1318 // standard error. You can customize the destination by installing your
1319 // own writer function by InstallFailureWriter() below.
1321 // Note on threading:
1323 // The function should be called before threads are created, if you want
1324 // to use the failure signal handler for all threads. The stack trace
1325 // will be shown only for the thread that receives the signal. In other
1326 // words, stack traces of other threads won't be shown.
1327 GOOGLE_GLOG_DLL_DECL void InstallFailureSignalHandler();
1329 // Installs a function that is used for writing the failure dump. "data"
1330 // is the pointer to the beginning of a message to be written, and "size"
1331 // is the size of the message. You should not expect the data is
1332 // terminated with '\0'.
1333 GOOGLE_GLOG_DLL_DECL void InstallFailureWriter(
1334 void (*writer)(const char* data, int size));
1336 @ac_google_end_namespace@
1338 #endif // _LOGGING_H_