allow to use __declspec(noreturn)
[platform/upstream/glog.git] / src / glog / logging.h.in
1 // Copyright (c) 1999, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Ray Sidney
31 //
32 // This file contains #include information about logging-related stuff.
33 // Pretty much everybody needs to #include this file so that they can
34 // log various happenings.
35 //
36 #ifndef _LOGGING_H_
37 #define _LOGGING_H_
38
39 #include <errno.h>
40 #include <string.h>
41 #include <time.h>
42 #include <iosfwd>
43 #include <ostream>
44 #include <sstream>
45 #include <string>
46 #if @ac_cv_have_unistd_h@
47 # include <unistd.h>
48 #endif
49 #include <vector>
50
51 #if defined(_MSC_VER)
52 #define GLOG_MSVC_PUSH_DISABLE_WARNING(n) __pragma(warning(push)) \
53                                      __pragma(warning(disable:n))
54 #define GLOG_MSVC_POP_WARNING() __pragma(warning(pop))
55 #else
56 #define GLOG_MSVC_PUSH_DISABLE_WARNING(n)
57 #define GLOG_MSVC_POP_WARNING()
58 #endif
59
60 // Annoying stuff for windows -- makes sure clients can import these functions
61 #ifndef GOOGLE_GLOG_DLL_DECL
62 # if defined(_WIN32) && !defined(__CYGWIN__)
63 #   define GOOGLE_GLOG_DLL_DECL  __declspec(dllimport)
64 # else
65 #   define GOOGLE_GLOG_DLL_DECL
66 # endif
67 #endif
68
69 // We care a lot about number of bits things take up.  Unfortunately,
70 // systems define their bit-specific ints in a lot of different ways.
71 // We use our own way, and have a typedef to get there.
72 // Note: these commands below may look like "#if 1" or "#if 0", but
73 // that's because they were constructed that way at ./configure time.
74 // Look at logging.h.in to see how they're calculated (based on your config).
75 #if @ac_cv_have_stdint_h@
76 #include <stdint.h>             // the normal place uint16_t is defined
77 #endif
78 #if @ac_cv_have_systypes_h@
79 #include <sys/types.h>          // the normal place u_int16_t is defined
80 #endif
81 #if @ac_cv_have_inttypes_h@
82 #include <inttypes.h>           // a third place for uint16_t or u_int16_t
83 #endif
84
85 #if @ac_cv_have_libgflags@
86 #include <gflags/gflags.h>
87 #endif
88
89 @ac_google_start_namespace@
90
91 #if @ac_cv_have_uint16_t@      // the C99 format
92 typedef int32_t int32;
93 typedef uint32_t uint32;
94 typedef int64_t int64;
95 typedef uint64_t uint64;
96 #elif @ac_cv_have_u_int16_t@   // the BSD format
97 typedef int32_t int32;
98 typedef u_int32_t uint32;
99 typedef int64_t int64;
100 typedef u_int64_t uint64;
101 #elif @ac_cv_have___uint16@    // the windows (vc7) format
102 typedef __int32 int32;
103 typedef unsigned __int32 uint32;
104 typedef __int64 int64;
105 typedef unsigned __int64 uint64;
106 #else
107 #error Do not know how to define a 32-bit integer quantity on your system
108 #endif
109
110 @ac_google_end_namespace@
111
112 // The global value of GOOGLE_STRIP_LOG. All the messages logged to
113 // LOG(XXX) with severity less than GOOGLE_STRIP_LOG will not be displayed.
114 // If it can be determined at compile time that the message will not be
115 // printed, the statement will be compiled out.
116 //
117 // Example: to strip out all INFO and WARNING messages, use the value
118 // of 2 below. To make an exception for WARNING messages from a single
119 // file, add "#define GOOGLE_STRIP_LOG 1" to that file _before_ including
120 // base/logging.h
121 #ifndef GOOGLE_STRIP_LOG
122 #define GOOGLE_STRIP_LOG 0
123 #endif
124
125 // GCC can be told that a certain branch is not likely to be taken (for
126 // instance, a CHECK failure), and use that information in static analysis.
127 // Giving it this information can help it optimize for the common case in
128 // the absence of better information (ie. -fprofile-arcs).
129 //
130 #ifndef GOOGLE_PREDICT_BRANCH_NOT_TAKEN
131 #if @ac_cv_have___builtin_expect@
132 #define GOOGLE_PREDICT_BRANCH_NOT_TAKEN(x) (__builtin_expect(x, 0))
133 #define GOOGLE_PREDICT_FALSE(x) (__builtin_expect(x, 0))
134 #define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
135 #else
136 #define GOOGLE_PREDICT_BRANCH_NOT_TAKEN(x) x
137 #define GOOGLE_PREDICT_FALSE(x) x
138 #define GOOGLE_PREDICT_TRUE(x) x
139 #endif
140 #endif
141
142 // Make a bunch of macros for logging.  The way to log things is to stream
143 // things to LOG(<a particular severity level>).  E.g.,
144 //
145 //   LOG(INFO) << "Found " << num_cookies << " cookies";
146 //
147 // You can capture log messages in a string, rather than reporting them
148 // immediately:
149 //
150 //   vector<string> errors;
151 //   LOG_STRING(ERROR, &errors) << "Couldn't parse cookie #" << cookie_num;
152 //
153 // This pushes back the new error onto 'errors'; if given a NULL pointer,
154 // it reports the error via LOG(ERROR).
155 //
156 // You can also do conditional logging:
157 //
158 //   LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
159 //
160 // You can also do occasional logging (log every n'th occurrence of an
161 // event):
162 //
163 //   LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
164 //
165 // The above will cause log messages to be output on the 1st, 11th, 21st, ...
166 // times it is executed.  Note that the special google::COUNTER value is used
167 // to identify which repetition is happening.
168 //
169 // You can also do occasional conditional logging (log every n'th
170 // occurrence of an event, when condition is satisfied):
171 //
172 //   LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER
173 //                                           << "th big cookie";
174 //
175 // You can log messages the first N times your code executes a line. E.g.
176 //
177 //   LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie";
178 //
179 // Outputs log messages for the first 20 times it is executed.
180 //
181 // Analogous SYSLOG, SYSLOG_IF, and SYSLOG_EVERY_N macros are available.
182 // These log to syslog as well as to the normal logs.  If you use these at
183 // all, you need to be aware that syslog can drastically reduce performance,
184 // especially if it is configured for remote logging!  Don't use these
185 // unless you fully understand this and have a concrete need to use them.
186 // Even then, try to minimize your use of them.
187 //
188 // There are also "debug mode" logging macros like the ones above:
189 //
190 //   DLOG(INFO) << "Found cookies";
191 //
192 //   DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
193 //
194 //   DLOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
195 //
196 // All "debug mode" logging is compiled away to nothing for non-debug mode
197 // compiles.
198 //
199 // We also have
200 //
201 //   LOG_ASSERT(assertion);
202 //   DLOG_ASSERT(assertion);
203 //
204 // which is syntactic sugar for {,D}LOG_IF(FATAL, assert fails) << assertion;
205 //
206 // There are "verbose level" logging macros.  They look like
207 //
208 //   VLOG(1) << "I'm printed when you run the program with --v=1 or more";
209 //   VLOG(2) << "I'm printed when you run the program with --v=2 or more";
210 //
211 // These always log at the INFO log level (when they log at all).
212 // The verbose logging can also be turned on module-by-module.  For instance,
213 //    --vmodule=mapreduce=2,file=1,gfs*=3 --v=0
214 // will cause:
215 //   a. VLOG(2) and lower messages to be printed from mapreduce.{h,cc}
216 //   b. VLOG(1) and lower messages to be printed from file.{h,cc}
217 //   c. VLOG(3) and lower messages to be printed from files prefixed with "gfs"
218 //   d. VLOG(0) and lower messages to be printed from elsewhere
219 //
220 // The wildcarding functionality shown by (c) supports both '*' (match
221 // 0 or more characters) and '?' (match any single character) wildcards.
222 //
223 // There's also VLOG_IS_ON(n) "verbose level" condition macro. To be used as
224 //
225 //   if (VLOG_IS_ON(2)) {
226 //     // do some logging preparation and logging
227 //     // that can't be accomplished with just VLOG(2) << ...;
228 //   }
229 //
230 // There are also VLOG_IF, VLOG_EVERY_N and VLOG_IF_EVERY_N "verbose level"
231 // condition macros for sample cases, when some extra computation and
232 // preparation for logs is not needed.
233 //   VLOG_IF(1, (size > 1024))
234 //      << "I'm printed when size is more than 1024 and when you run the "
235 //         "program with --v=1 or more";
236 //   VLOG_EVERY_N(1, 10)
237 //      << "I'm printed every 10th occurrence, and when you run the program "
238 //         "with --v=1 or more. Present occurence is " << google::COUNTER;
239 //   VLOG_IF_EVERY_N(1, (size > 1024), 10)
240 //      << "I'm printed on every 10th occurence of case when size is more "
241 //         " than 1024, when you run the program with --v=1 or more. ";
242 //         "Present occurence is " << google::COUNTER;
243 //
244 // The supported severity levels for macros that allow you to specify one
245 // are (in increasing order of severity) INFO, WARNING, ERROR, and FATAL.
246 // Note that messages of a given severity are logged not only in the
247 // logfile for that severity, but also in all logfiles of lower severity.
248 // E.g., a message of severity FATAL will be logged to the logfiles of
249 // severity FATAL, ERROR, WARNING, and INFO.
250 //
251 // There is also the special severity of DFATAL, which logs FATAL in
252 // debug mode, ERROR in normal mode.
253 //
254 // Very important: logging a message at the FATAL severity level causes
255 // the program to terminate (after the message is logged).
256 //
257 // Unless otherwise specified, logs will be written to the filename
258 // "<program name>.<hostname>.<user name>.log.<severity level>.", followed
259 // by the date, time, and pid (you can't prevent the date, time, and pid
260 // from being in the filename).
261 //
262 // The logging code takes two flags:
263 //     --v=#           set the verbose level
264 //     --logtostderr   log all the messages to stderr instead of to logfiles
265
266 // LOG LINE PREFIX FORMAT
267 //
268 // Log lines have this form:
269 //
270 //     Lmmdd hh:mm:ss.uuuuuu threadid file:line] msg...
271 //
272 // where the fields are defined as follows:
273 //
274 //   L                A single character, representing the log level
275 //                    (eg 'I' for INFO)
276 //   mm               The month (zero padded; ie May is '05')
277 //   dd               The day (zero padded)
278 //   hh:mm:ss.uuuuuu  Time in hours, minutes and fractional seconds
279 //   threadid         The space-padded thread ID as returned by GetTID()
280 //                    (this matches the PID on Linux)
281 //   file             The file name
282 //   line             The line number
283 //   msg              The user-supplied message
284 //
285 // Example:
286 //
287 //   I1103 11:57:31.739339 24395 google.cc:2341] Command line: ./some_prog
288 //   I1103 11:57:31.739403 24395 google.cc:2342] Process id 24395
289 //
290 // NOTE: although the microseconds are useful for comparing events on
291 // a single machine, clocks on different machines may not be well
292 // synchronized.  Hence, use caution when comparing the low bits of
293 // timestamps from different machines.
294
295 #ifndef DECLARE_VARIABLE
296 #define MUST_UNDEF_GFLAGS_DECLARE_MACROS
297 #define DECLARE_VARIABLE(type, shorttype, name, tn)                     \
298   namespace fL##shorttype {                                             \
299     extern GOOGLE_GLOG_DLL_DECL type FLAGS_##name;                      \
300   }                                                                     \
301   using fL##shorttype::FLAGS_##name
302
303 // bool specialization
304 #define DECLARE_bool(name) \
305   DECLARE_VARIABLE(bool, B, name, bool)
306
307 // int32 specialization
308 #define DECLARE_int32(name) \
309   DECLARE_VARIABLE(@ac_google_namespace@::int32, I, name, int32)
310
311 // Special case for string, because we have to specify the namespace
312 // std::string, which doesn't play nicely with our FLAG__namespace hackery.
313 #define DECLARE_string(name)                                            \
314   namespace fLS {                                                       \
315     extern GOOGLE_GLOG_DLL_DECL std::string& FLAGS_##name;              \
316   }                                                                     \
317   using fLS::FLAGS_##name
318 #endif
319
320 // Set whether log messages go to stderr instead of logfiles
321 DECLARE_bool(logtostderr);
322
323 // Set whether log messages go to stderr in addition to logfiles.
324 DECLARE_bool(alsologtostderr);
325
326 // Set color messages logged to stderr (if supported by terminal).
327 DECLARE_bool(colorlogtostderr);
328
329 // Log messages at a level >= this flag are automatically sent to
330 // stderr in addition to log files.
331 DECLARE_int32(stderrthreshold);
332
333 // Set whether the log prefix should be prepended to each line of output.
334 DECLARE_bool(log_prefix);
335
336 // Log messages at a level <= this flag are buffered.
337 // Log messages at a higher level are flushed immediately.
338 DECLARE_int32(logbuflevel);
339
340 // Sets the maximum number of seconds which logs may be buffered for.
341 DECLARE_int32(logbufsecs);
342
343 // Log suppression level: messages logged at a lower level than this
344 // are suppressed.
345 DECLARE_int32(minloglevel);
346
347 // If specified, logfiles are written into this directory instead of the
348 // default logging directory.
349 DECLARE_string(log_dir);
350
351 // Sets the path of the directory into which to put additional links
352 // to the log files.
353 DECLARE_string(log_link);
354
355 DECLARE_int32(v);  // in vlog_is_on.cc
356
357 // Sets the maximum log file size (in MB).
358 DECLARE_int32(max_log_size);
359
360 // Sets whether to avoid logging to the disk if the disk is full.
361 DECLARE_bool(stop_logging_if_full_disk);
362
363 #ifdef MUST_UNDEF_GFLAGS_DECLARE_MACROS
364 #undef MUST_UNDEF_GFLAGS_DECLARE_MACROS
365 #undef DECLARE_VARIABLE
366 #undef DECLARE_bool
367 #undef DECLARE_int32
368 #undef DECLARE_string
369 #endif
370
371 // Log messages below the GOOGLE_STRIP_LOG level will be compiled away for
372 // security reasons. See LOG(severtiy) below.
373
374 // A few definitions of macros that don't generate much code.  Since
375 // LOG(INFO) and its ilk are used all over our code, it's
376 // better to have compact code for these operations.
377
378 #if GOOGLE_STRIP_LOG == 0
379 #define COMPACT_GOOGLE_LOG_INFO @ac_google_namespace@::LogMessage( \
380       __FILE__, __LINE__)
381 #define LOG_TO_STRING_INFO(message) @ac_google_namespace@::LogMessage( \
382       __FILE__, __LINE__, @ac_google_namespace@::GLOG_INFO, message)
383 #else
384 #define COMPACT_GOOGLE_LOG_INFO @ac_google_namespace@::NullStream()
385 #define LOG_TO_STRING_INFO(message) @ac_google_namespace@::NullStream()
386 #endif
387
388 #if GOOGLE_STRIP_LOG <= 1
389 #define COMPACT_GOOGLE_LOG_WARNING @ac_google_namespace@::LogMessage( \
390       __FILE__, __LINE__, @ac_google_namespace@::GLOG_WARNING)
391 #define LOG_TO_STRING_WARNING(message) @ac_google_namespace@::LogMessage( \
392       __FILE__, __LINE__, @ac_google_namespace@::GLOG_WARNING, message)
393 #else
394 #define COMPACT_GOOGLE_LOG_WARNING @ac_google_namespace@::NullStream()
395 #define LOG_TO_STRING_WARNING(message) @ac_google_namespace@::NullStream()
396 #endif
397
398 #if GOOGLE_STRIP_LOG <= 2
399 #define COMPACT_GOOGLE_LOG_ERROR @ac_google_namespace@::LogMessage( \
400       __FILE__, __LINE__, @ac_google_namespace@::GLOG_ERROR)
401 #define LOG_TO_STRING_ERROR(message) @ac_google_namespace@::LogMessage( \
402       __FILE__, __LINE__, @ac_google_namespace@::GLOG_ERROR, message)
403 #else
404 #define COMPACT_GOOGLE_LOG_ERROR @ac_google_namespace@::NullStream()
405 #define LOG_TO_STRING_ERROR(message) @ac_google_namespace@::NullStream()
406 #endif
407
408 #if GOOGLE_STRIP_LOG <= 3
409 #define COMPACT_GOOGLE_LOG_FATAL @ac_google_namespace@::LogMessageFatal( \
410       __FILE__, __LINE__)
411 #define LOG_TO_STRING_FATAL(message) @ac_google_namespace@::LogMessage( \
412       __FILE__, __LINE__, @ac_google_namespace@::GLOG_FATAL, message)
413 #else
414 #define COMPACT_GOOGLE_LOG_FATAL @ac_google_namespace@::NullStreamFatal()
415 #define LOG_TO_STRING_FATAL(message) @ac_google_namespace@::NullStreamFatal()
416 #endif
417
418 // For DFATAL, we want to use LogMessage (as opposed to
419 // LogMessageFatal), to be consistent with the original behavior.
420 #ifdef NDEBUG
421 #define COMPACT_GOOGLE_LOG_DFATAL COMPACT_GOOGLE_LOG_ERROR
422 #elif GOOGLE_STRIP_LOG <= 3
423 #define COMPACT_GOOGLE_LOG_DFATAL @ac_google_namespace@::LogMessage( \
424       __FILE__, __LINE__, @ac_google_namespace@::GLOG_FATAL)
425 #else
426 #define COMPACT_GOOGLE_LOG_DFATAL @ac_google_namespace@::NullStreamFatal()
427 #endif
428
429 #define GOOGLE_LOG_INFO(counter) @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::GLOG_INFO, counter, &@ac_google_namespace@::LogMessage::SendToLog)
430 #define SYSLOG_INFO(counter) \
431   @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::GLOG_INFO, counter, \
432   &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
433 #define GOOGLE_LOG_WARNING(counter)  \
434   @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::GLOG_WARNING, counter, \
435   &@ac_google_namespace@::LogMessage::SendToLog)
436 #define SYSLOG_WARNING(counter)  \
437   @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::GLOG_WARNING, counter, \
438   &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
439 #define GOOGLE_LOG_ERROR(counter)  \
440   @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::GLOG_ERROR, counter, \
441   &@ac_google_namespace@::LogMessage::SendToLog)
442 #define SYSLOG_ERROR(counter)  \
443   @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::GLOG_ERROR, counter, \
444   &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
445 #define GOOGLE_LOG_FATAL(counter) \
446   @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::GLOG_FATAL, counter, \
447   &@ac_google_namespace@::LogMessage::SendToLog)
448 #define SYSLOG_FATAL(counter) \
449   @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::GLOG_FATAL, counter, \
450   &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
451 #define GOOGLE_LOG_DFATAL(counter) \
452   @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::DFATAL_LEVEL, counter, \
453   &@ac_google_namespace@::LogMessage::SendToLog)
454 #define SYSLOG_DFATAL(counter) \
455   @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::DFATAL_LEVEL, counter, \
456   &@ac_google_namespace@::LogMessage::SendToSyslogAndLog)
457
458 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__) || defined(__CYGWIN32__)
459 // A very useful logging macro to log windows errors:
460 #define LOG_SYSRESULT(result) \
461   if (FAILED(HRESULT_FROM_WIN32(result))) { \
462     LPSTR message = NULL; \
463     LPSTR msg = reinterpret_cast<LPSTR>(&message); \
464     DWORD message_length = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | \
465                          FORMAT_MESSAGE_FROM_SYSTEM, \
466                          0, result, 0, msg, 100, NULL); \
467     if (message_length > 0) { \
468       @ac_google_namespace@::LogMessage(__FILE__, __LINE__, @ac_google_namespace@::GLOG_ERROR, 0, \
469           &@ac_google_namespace@::LogMessage::SendToLog).stream() \
470           << reinterpret_cast<const char*>(message); \
471       LocalFree(message); \
472     } \
473   }
474 #endif
475
476 // We use the preprocessor's merging operator, "##", so that, e.g.,
477 // LOG(INFO) becomes the token GOOGLE_LOG_INFO.  There's some funny
478 // subtle difference between ostream member streaming functions (e.g.,
479 // ostream::operator<<(int) and ostream non-member streaming functions
480 // (e.g., ::operator<<(ostream&, string&): it turns out that it's
481 // impossible to stream something like a string directly to an unnamed
482 // ostream. We employ a neat hack by calling the stream() member
483 // function of LogMessage which seems to avoid the problem.
484 #define LOG(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
485 #define SYSLOG(severity) SYSLOG_ ## severity(0).stream()
486
487 @ac_google_start_namespace@
488
489 // They need the definitions of integer types.
490 #include "glog/log_severity.h"
491 #include "glog/vlog_is_on.h"
492
493 // Initialize google's logging library. You will see the program name
494 // specified by argv0 in log outputs.
495 GOOGLE_GLOG_DLL_DECL void InitGoogleLogging(const char* argv0);
496
497 // Shutdown google's logging library.
498 GOOGLE_GLOG_DLL_DECL void ShutdownGoogleLogging();
499
500 // Install a function which will be called after LOG(FATAL).
501 GOOGLE_GLOG_DLL_DECL void InstallFailureFunction(void (*fail_func)());
502
503 class LogSink;  // defined below
504
505 // If a non-NULL sink pointer is given, we push this message to that sink.
506 // For LOG_TO_SINK we then do normal LOG(severity) logging as well.
507 // This is useful for capturing messages and passing/storing them
508 // somewhere more specific than the global log of the process.
509 // Argument types:
510 //   LogSink* sink;
511 //   LogSeverity severity;
512 // The cast is to disambiguate NULL arguments.
513 #define LOG_TO_SINK(sink, severity) \
514   @ac_google_namespace@::LogMessage(                                    \
515       __FILE__, __LINE__,                                               \
516       @ac_google_namespace@::GLOG_ ## severity,                         \
517       static_cast<@ac_google_namespace@::LogSink*>(sink), true).stream()
518 #define LOG_TO_SINK_BUT_NOT_TO_LOGFILE(sink, severity)                  \
519   @ac_google_namespace@::LogMessage(                                    \
520       __FILE__, __LINE__,                                               \
521       @ac_google_namespace@::GLOG_ ## severity,                         \
522       static_cast<@ac_google_namespace@::LogSink*>(sink), false).stream()
523
524 // If a non-NULL string pointer is given, we write this message to that string.
525 // We then do normal LOG(severity) logging as well.
526 // This is useful for capturing messages and storing them somewhere more
527 // specific than the global log of the process.
528 // Argument types:
529 //   string* message;
530 //   LogSeverity severity;
531 // The cast is to disambiguate NULL arguments.
532 // NOTE: LOG(severity) expands to LogMessage().stream() for the specified
533 // severity.
534 #define LOG_TO_STRING(severity, message) \
535   LOG_TO_STRING_##severity(static_cast<string*>(message)).stream()
536
537 // If a non-NULL pointer is given, we push the message onto the end
538 // of a vector of strings; otherwise, we report it with LOG(severity).
539 // This is handy for capturing messages and perhaps passing them back
540 // to the caller, rather than reporting them immediately.
541 // Argument types:
542 //   LogSeverity severity;
543 //   vector<string> *outvec;
544 // The cast is to disambiguate NULL arguments.
545 #define LOG_STRING(severity, outvec) \
546   LOG_TO_STRING_##severity(static_cast<vector<string>*>(outvec)).stream()
547
548 #define LOG_IF(severity, condition) \
549   !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
550 #define SYSLOG_IF(severity, condition) \
551   !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & SYSLOG(severity)
552
553 #define LOG_ASSERT(condition)  \
554   LOG_IF(FATAL, !(condition)) << "Assert failed: " #condition
555 #define SYSLOG_ASSERT(condition) \
556   SYSLOG_IF(FATAL, !(condition)) << "Assert failed: " #condition
557
558 // CHECK dies with a fatal error if condition is not true.  It is *not*
559 // controlled by NDEBUG, so the check will be executed regardless of
560 // compilation mode.  Therefore, it is safe to do things like:
561 //    CHECK(fp->Write(x) == 4)
562 #define CHECK(condition)  \
563       LOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN(!(condition))) \
564              << "Check failed: " #condition " "
565
566 // A container for a string pointer which can be evaluated to a bool -
567 // true iff the pointer is NULL.
568 struct CheckOpString {
569   CheckOpString(std::string* str) : str_(str) { }
570   // No destructor: if str_ is non-NULL, we're about to LOG(FATAL),
571   // so there's no point in cleaning up str_.
572   operator bool() const {
573     return GOOGLE_PREDICT_BRANCH_NOT_TAKEN(str_ != NULL);
574   }
575   std::string* str_;
576 };
577
578 // Function is overloaded for integral types to allow static const
579 // integrals declared in classes and not defined to be used as arguments to
580 // CHECK* macros. It's not encouraged though.
581 template <class T>
582 inline const T&       GetReferenceableValue(const T&           t) { return t; }
583 inline char           GetReferenceableValue(char               t) { return t; }
584 inline unsigned char  GetReferenceableValue(unsigned char      t) { return t; }
585 inline signed char    GetReferenceableValue(signed char        t) { return t; }
586 inline short          GetReferenceableValue(short              t) { return t; }
587 inline unsigned short GetReferenceableValue(unsigned short     t) { return t; }
588 inline int            GetReferenceableValue(int                t) { return t; }
589 inline unsigned int   GetReferenceableValue(unsigned int       t) { return t; }
590 inline long           GetReferenceableValue(long               t) { return t; }
591 inline unsigned long  GetReferenceableValue(unsigned long      t) { return t; }
592 inline long long      GetReferenceableValue(long long          t) { return t; }
593 inline unsigned long long GetReferenceableValue(unsigned long long t) {
594   return t;
595 }
596
597 // This is a dummy class to define the following operator.
598 struct DummyClassToDefineOperator {};
599
600 @ac_google_end_namespace@
601
602 // Define global operator<< to declare using ::operator<<.
603 // This declaration will allow use to use CHECK macros for user
604 // defined classes which have operator<< (e.g., stl_logging.h).
605 inline std::ostream& operator<<(
606     std::ostream& out, const google::DummyClassToDefineOperator&) {
607   return out;
608 }
609
610 @ac_google_start_namespace@
611
612 // This formats a value for a failing CHECK_XX statement.  Ordinarily,
613 // it uses the definition for operator<<, with a few special cases below.
614 template <typename T>
615 inline void MakeCheckOpValueString(std::ostream* os, const T& v) {
616   (*os) << v;
617 }
618
619 // Overrides for char types provide readable values for unprintable
620 // characters.
621 template <> GOOGLE_GLOG_DLL_DECL
622 void MakeCheckOpValueString(std::ostream* os, const char& v);
623 template <> GOOGLE_GLOG_DLL_DECL
624 void MakeCheckOpValueString(std::ostream* os, const signed char& v);
625 template <> GOOGLE_GLOG_DLL_DECL
626 void MakeCheckOpValueString(std::ostream* os, const unsigned char& v);
627
628 // Build the error message string. Specify no inlining for code size.
629 template <typename T1, typename T2>
630 std::string* MakeCheckOpString(const T1& v1, const T2& v2, const char* exprtext)
631     @ac_cv___attribute___noinline@;
632
633 namespace base {
634 namespace internal {
635
636 // If "s" is less than base_logging::INFO, returns base_logging::INFO.
637 // If "s" is greater than base_logging::FATAL, returns
638 // base_logging::ERROR.  Otherwise, returns "s".
639 LogSeverity NormalizeSeverity(LogSeverity s);
640
641 }  // namespace internal
642
643 // A helper class for formatting "expr (V1 vs. V2)" in a CHECK_XX
644 // statement.  See MakeCheckOpString for sample usage.  Other
645 // approaches were considered: use of a template method (e.g.,
646 // base::BuildCheckOpString(exprtext, base::Print<T1>, &v1,
647 // base::Print<T2>, &v2), however this approach has complications
648 // related to volatile arguments and function-pointer arguments).
649 class GOOGLE_GLOG_DLL_DECL CheckOpMessageBuilder {
650  public:
651   // Inserts "exprtext" and " (" to the stream.
652   explicit CheckOpMessageBuilder(const char *exprtext);
653   // Deletes "stream_".
654   ~CheckOpMessageBuilder();
655   // For inserting the first variable.
656   std::ostream* ForVar1() { return stream_; }
657   // For inserting the second variable (adds an intermediate " vs. ").
658   std::ostream* ForVar2();
659   // Get the result (inserts the closing ")").
660   std::string* NewString();
661
662  private:
663   std::ostringstream *stream_;
664 };
665
666 }  // namespace base
667
668 template <typename T1, typename T2>
669 std::string* MakeCheckOpString(const T1& v1, const T2& v2, const char* exprtext) {
670   base::CheckOpMessageBuilder comb(exprtext);
671   MakeCheckOpValueString(comb.ForVar1(), v1);
672   MakeCheckOpValueString(comb.ForVar2(), v2);
673   return comb.NewString();
674 }
675
676 // Helper functions for CHECK_OP macro.
677 // The (int, int) specialization works around the issue that the compiler
678 // will not instantiate the template version of the function on values of
679 // unnamed enum type - see comment below.
680 #define DEFINE_CHECK_OP_IMPL(name, op) \
681   template <typename T1, typename T2> \
682   inline std::string* name##Impl(const T1& v1, const T2& v2,    \
683                             const char* exprtext) { \
684     if (GOOGLE_PREDICT_TRUE(v1 op v2)) return NULL; \
685     else return MakeCheckOpString(v1, v2, exprtext); \
686   } \
687   inline std::string* name##Impl(int v1, int v2, const char* exprtext) { \
688     return name##Impl<int, int>(v1, v2, exprtext); \
689   }
690
691 // We use the full name Check_EQ, Check_NE, etc. in case the file including
692 // base/logging.h provides its own #defines for the simpler names EQ, NE, etc.
693 // This happens if, for example, those are used as token names in a
694 // yacc grammar.
695 DEFINE_CHECK_OP_IMPL(Check_EQ, ==)  // Compilation error with CHECK_EQ(NULL, x)?
696 DEFINE_CHECK_OP_IMPL(Check_NE, !=)  // Use CHECK(x == NULL) instead.
697 DEFINE_CHECK_OP_IMPL(Check_LE, <=)
698 DEFINE_CHECK_OP_IMPL(Check_LT, < )
699 DEFINE_CHECK_OP_IMPL(Check_GE, >=)
700 DEFINE_CHECK_OP_IMPL(Check_GT, > )
701 #undef DEFINE_CHECK_OP_IMPL
702
703 // Helper macro for binary operators.
704 // Don't use this macro directly in your code, use CHECK_EQ et al below.
705
706 #if defined(STATIC_ANALYSIS)
707 // Only for static analysis tool to know that it is equivalent to assert
708 #define CHECK_OP_LOG(name, op, val1, val2, log) CHECK((val1) op (val2))
709 #elif !defined(NDEBUG)
710 // In debug mode, avoid constructing CheckOpStrings if possible,
711 // to reduce the overhead of CHECK statments by 2x.
712 // Real DCHECK-heavy tests have seen 1.5x speedups.
713
714 // The meaning of "string" might be different between now and 
715 // when this macro gets invoked (e.g., if someone is experimenting
716 // with other string implementations that get defined after this
717 // file is included).  Save the current meaning now and use it 
718 // in the macro.
719 typedef std::string _Check_string;
720 #define CHECK_OP_LOG(name, op, val1, val2, log)                         \
721   while (@ac_google_namespace@::_Check_string* _result =                \
722          @ac_google_namespace@::Check##name##Impl(                      \
723              @ac_google_namespace@::GetReferenceableValue(val1),        \
724              @ac_google_namespace@::GetReferenceableValue(val2),        \
725              #val1 " " #op " " #val2))                                  \
726     log(__FILE__, __LINE__,                                             \
727         @ac_google_namespace@::CheckOpString(_result)).stream()
728 #else
729 // In optimized mode, use CheckOpString to hint to compiler that
730 // the while condition is unlikely.
731 #define CHECK_OP_LOG(name, op, val1, val2, log)                         \
732   while (@ac_google_namespace@::CheckOpString _result =                 \
733          @ac_google_namespace@::Check##name##Impl(                      \
734              @ac_google_namespace@::GetReferenceableValue(val1),        \
735              @ac_google_namespace@::GetReferenceableValue(val2),        \
736              #val1 " " #op " " #val2))                                  \
737     log(__FILE__, __LINE__, _result).stream()
738 #endif  // STATIC_ANALYSIS, !NDEBUG
739
740 #if GOOGLE_STRIP_LOG <= 3
741 #define CHECK_OP(name, op, val1, val2) \
742   CHECK_OP_LOG(name, op, val1, val2, @ac_google_namespace@::LogMessageFatal)
743 #else
744 #define CHECK_OP(name, op, val1, val2) \
745   CHECK_OP_LOG(name, op, val1, val2, @ac_google_namespace@::NullStreamFatal)
746 #endif // STRIP_LOG <= 3
747
748 // Equality/Inequality checks - compare two values, and log a FATAL message
749 // including the two values when the result is not as expected.  The values
750 // must have operator<<(ostream, ...) defined.
751 //
752 // You may append to the error message like so:
753 //   CHECK_NE(1, 2) << ": The world must be ending!";
754 //
755 // We are very careful to ensure that each argument is evaluated exactly
756 // once, and that anything which is legal to pass as a function argument is
757 // legal here.  In particular, the arguments may be temporary expressions
758 // which will end up being destroyed at the end of the apparent statement,
759 // for example:
760 //   CHECK_EQ(string("abc")[1], 'b');
761 //
762 // WARNING: These don't compile correctly if one of the arguments is a pointer
763 // and the other is NULL. To work around this, simply static_cast NULL to the
764 // type of the desired pointer.
765
766 #define CHECK_EQ(val1, val2) CHECK_OP(_EQ, ==, val1, val2)
767 #define CHECK_NE(val1, val2) CHECK_OP(_NE, !=, val1, val2)
768 #define CHECK_LE(val1, val2) CHECK_OP(_LE, <=, val1, val2)
769 #define CHECK_LT(val1, val2) CHECK_OP(_LT, < , val1, val2)
770 #define CHECK_GE(val1, val2) CHECK_OP(_GE, >=, val1, val2)
771 #define CHECK_GT(val1, val2) CHECK_OP(_GT, > , val1, val2)
772
773 // Check that the input is non NULL.  This very useful in constructor
774 // initializer lists.
775
776 #define CHECK_NOTNULL(val) \
777   @ac_google_namespace@::CheckNotNull(__FILE__, __LINE__, "'" #val "' Must be non NULL", (val))
778
779 // Helper functions for string comparisons.
780 // To avoid bloat, the definitions are in logging.cc.
781 #define DECLARE_CHECK_STROP_IMPL(func, expected) \
782   GOOGLE_GLOG_DLL_DECL std::string* Check##func##expected##Impl( \
783       const char* s1, const char* s2, const char* names);
784 DECLARE_CHECK_STROP_IMPL(strcmp, true)
785 DECLARE_CHECK_STROP_IMPL(strcmp, false)
786 DECLARE_CHECK_STROP_IMPL(strcasecmp, true)
787 DECLARE_CHECK_STROP_IMPL(strcasecmp, false)
788 #undef DECLARE_CHECK_STROP_IMPL
789
790 // Helper macro for string comparisons.
791 // Don't use this macro directly in your code, use CHECK_STREQ et al below.
792 #define CHECK_STROP(func, op, expected, s1, s2) \
793   while (@ac_google_namespace@::CheckOpString _result = \
794          @ac_google_namespace@::Check##func##expected##Impl((s1), (s2), \
795                                      #s1 " " #op " " #s2)) \
796     LOG(FATAL) << *_result.str_
797
798
799 // String (char*) equality/inequality checks.
800 // CASE versions are case-insensitive.
801 //
802 // Note that "s1" and "s2" may be temporary strings which are destroyed
803 // by the compiler at the end of the current "full expression"
804 // (e.g. CHECK_STREQ(Foo().c_str(), Bar().c_str())).
805
806 #define CHECK_STREQ(s1, s2) CHECK_STROP(strcmp, ==, true, s1, s2)
807 #define CHECK_STRNE(s1, s2) CHECK_STROP(strcmp, !=, false, s1, s2)
808 #define CHECK_STRCASEEQ(s1, s2) CHECK_STROP(strcasecmp, ==, true, s1, s2)
809 #define CHECK_STRCASENE(s1, s2) CHECK_STROP(strcasecmp, !=, false, s1, s2)
810
811 #define CHECK_INDEX(I,A) CHECK(I < (sizeof(A)/sizeof(A[0])))
812 #define CHECK_BOUND(B,A) CHECK(B <= (sizeof(A)/sizeof(A[0])))
813
814 #define CHECK_DOUBLE_EQ(val1, val2)              \
815   do {                                           \
816     CHECK_LE((val1), (val2)+0.000000000000001L); \
817     CHECK_GE((val1), (val2)-0.000000000000001L); \
818   } while (0)
819
820 #define CHECK_NEAR(val1, val2, margin)           \
821   do {                                           \
822     CHECK_LE((val1), (val2)+(margin));           \
823     CHECK_GE((val1), (val2)-(margin));           \
824   } while (0)
825
826 // perror()..googly style!
827 //
828 // PLOG() and PLOG_IF() and PCHECK() behave exactly like their LOG* and
829 // CHECK equivalents with the addition that they postpend a description
830 // of the current state of errno to their output lines.
831
832 #define PLOG(severity) GOOGLE_PLOG(severity, 0).stream()
833
834 #define GOOGLE_PLOG(severity, counter)  \
835   @ac_google_namespace@::ErrnoLogMessage( \
836       __FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, counter, \
837       &@ac_google_namespace@::LogMessage::SendToLog)
838
839 #define PLOG_IF(severity, condition) \
840   !(condition) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & PLOG(severity)
841
842 // A CHECK() macro that postpends errno if the condition is false. E.g.
843 //
844 // if (poll(fds, nfds, timeout) == -1) { PCHECK(errno == EINTR); ... }
845 #define PCHECK(condition)  \
846       PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN(!(condition))) \
847               << "Check failed: " #condition " "
848
849 // A CHECK() macro that lets you assert the success of a function that
850 // returns -1 and sets errno in case of an error. E.g.
851 //
852 // CHECK_ERR(mkdir(path, 0700));
853 //
854 // or
855 //
856 // int fd = open(filename, flags); CHECK_ERR(fd) << ": open " << filename;
857 #define CHECK_ERR(invocation)                                          \
858 PLOG_IF(FATAL, GOOGLE_PREDICT_BRANCH_NOT_TAKEN((invocation) == -1))    \
859         << #invocation
860
861 // Use macro expansion to create, for each use of LOG_EVERY_N(), static
862 // variables with the __LINE__ expansion as part of the variable name.
863 #define LOG_EVERY_N_VARNAME(base, line) LOG_EVERY_N_VARNAME_CONCAT(base, line)
864 #define LOG_EVERY_N_VARNAME_CONCAT(base, line) base ## line
865
866 #define LOG_OCCURRENCES LOG_EVERY_N_VARNAME(occurrences_, __LINE__)
867 #define LOG_OCCURRENCES_MOD_N LOG_EVERY_N_VARNAME(occurrences_mod_n_, __LINE__)
868
869 #define SOME_KIND_OF_LOG_EVERY_N(severity, n, what_to_do) \
870   static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
871   ++LOG_OCCURRENCES; \
872   if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
873   if (LOG_OCCURRENCES_MOD_N == 1) \
874     @ac_google_namespace@::LogMessage( \
875         __FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
876         &what_to_do).stream()
877
878 #define SOME_KIND_OF_LOG_IF_EVERY_N(severity, condition, n, what_to_do) \
879   static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
880   ++LOG_OCCURRENCES; \
881   if (condition && \
882       ((LOG_OCCURRENCES_MOD_N=(LOG_OCCURRENCES_MOD_N + 1) % n) == (1 % n))) \
883     @ac_google_namespace@::LogMessage( \
884         __FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
885                  &what_to_do).stream()
886
887 #define SOME_KIND_OF_PLOG_EVERY_N(severity, n, what_to_do) \
888   static int LOG_OCCURRENCES = 0, LOG_OCCURRENCES_MOD_N = 0; \
889   ++LOG_OCCURRENCES; \
890   if (++LOG_OCCURRENCES_MOD_N > n) LOG_OCCURRENCES_MOD_N -= n; \
891   if (LOG_OCCURRENCES_MOD_N == 1) \
892     @ac_google_namespace@::ErrnoLogMessage( \
893         __FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
894         &what_to_do).stream()
895
896 #define SOME_KIND_OF_LOG_FIRST_N(severity, n, what_to_do) \
897   static int LOG_OCCURRENCES = 0; \
898   if (LOG_OCCURRENCES <= n) \
899     ++LOG_OCCURRENCES; \
900   if (LOG_OCCURRENCES <= n) \
901     @ac_google_namespace@::LogMessage( \
902         __FILE__, __LINE__, @ac_google_namespace@::GLOG_ ## severity, LOG_OCCURRENCES, \
903         &what_to_do).stream()
904
905 namespace glog_internal_namespace_ {
906 template <bool>
907 struct CompileAssert {
908 };
909 struct CrashReason;
910 }  // namespace glog_internal_namespace_
911
912 #define GOOGLE_GLOG_COMPILE_ASSERT(expr, msg) \
913   typedef @ac_google_namespace@::glog_internal_namespace_::CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
914
915 #define LOG_EVERY_N(severity, n)                                        \
916   GOOGLE_GLOG_COMPILE_ASSERT(@ac_google_namespace@::GLOG_ ## severity < \
917                              @ac_google_namespace@::NUM_SEVERITIES,     \
918                              INVALID_REQUESTED_LOG_SEVERITY);           \
919   SOME_KIND_OF_LOG_EVERY_N(severity, (n), @ac_google_namespace@::LogMessage::SendToLog)
920
921 #define SYSLOG_EVERY_N(severity, n) \
922   SOME_KIND_OF_LOG_EVERY_N(severity, (n), @ac_google_namespace@::LogMessage::SendToSyslogAndLog)
923
924 #define PLOG_EVERY_N(severity, n) \
925   SOME_KIND_OF_PLOG_EVERY_N(severity, (n), @ac_google_namespace@::LogMessage::SendToLog)
926
927 #define LOG_FIRST_N(severity, n) \
928   SOME_KIND_OF_LOG_FIRST_N(severity, (n), @ac_google_namespace@::LogMessage::SendToLog)
929
930 #define LOG_IF_EVERY_N(severity, condition, n) \
931   SOME_KIND_OF_LOG_IF_EVERY_N(severity, (condition), (n), @ac_google_namespace@::LogMessage::SendToLog)
932
933 // We want the special COUNTER value available for LOG_EVERY_X()'ed messages
934 enum PRIVATE_Counter {COUNTER};
935
936 #ifdef GLOG_NO_ABBREVIATED_SEVERITIES
937 // wingdi.h defines ERROR to be 0. When we call LOG(ERROR), it gets
938 // substituted with 0, and it expands to COMPACT_GOOGLE_LOG_0. To allow us
939 // to keep using this syntax, we define this macro to do the same thing
940 // as COMPACT_GOOGLE_LOG_ERROR.
941 #define COMPACT_GOOGLE_LOG_0 COMPACT_GOOGLE_LOG_ERROR
942 #define SYSLOG_0 SYSLOG_ERROR
943 #define LOG_TO_STRING_0 LOG_TO_STRING_ERROR
944 // Needed for LOG_IS_ON(ERROR).
945 const LogSeverity GLOG_0 = GLOG_ERROR;
946 #else
947 // Users may include windows.h after logging.h without
948 // GLOG_NO_ABBREVIATED_SEVERITIES nor WIN32_LEAN_AND_MEAN.
949 // For this case, we cannot detect if ERROR is defined before users
950 // actually use ERROR. Let's make an undefined symbol to warn users.
951 # define GLOG_ERROR_MSG ERROR_macro_is_defined_Define_GLOG_NO_ABBREVIATED_SEVERITIES_before_including_logging_h_See_the_document_for_detail
952 # define COMPACT_GOOGLE_LOG_0 GLOG_ERROR_MSG
953 # define SYSLOG_0 GLOG_ERROR_MSG
954 # define LOG_TO_STRING_0 GLOG_ERROR_MSG
955 # define GLOG_0 GLOG_ERROR_MSG
956 #endif
957
958 // Plus some debug-logging macros that get compiled to nothing for production
959
960 #ifndef NDEBUG
961
962 #define DLOG(severity) LOG(severity)
963 #define DVLOG(verboselevel) VLOG(verboselevel)
964 #define DLOG_IF(severity, condition) LOG_IF(severity, condition)
965 #define DLOG_EVERY_N(severity, n) LOG_EVERY_N(severity, n)
966 #define DLOG_IF_EVERY_N(severity, condition, n) \
967   LOG_IF_EVERY_N(severity, condition, n)
968 #define DLOG_ASSERT(condition) LOG_ASSERT(condition)
969
970 // debug-only checking.  not executed in NDEBUG mode.
971 #define DCHECK(condition) CHECK(condition)
972 #define DCHECK_EQ(val1, val2) CHECK_EQ(val1, val2)
973 #define DCHECK_NE(val1, val2) CHECK_NE(val1, val2)
974 #define DCHECK_LE(val1, val2) CHECK_LE(val1, val2)
975 #define DCHECK_LT(val1, val2) CHECK_LT(val1, val2)
976 #define DCHECK_GE(val1, val2) CHECK_GE(val1, val2)
977 #define DCHECK_GT(val1, val2) CHECK_GT(val1, val2)
978 #define DCHECK_NOTNULL(val) CHECK_NOTNULL(val)
979 #define DCHECK_STREQ(str1, str2) CHECK_STREQ(str1, str2)
980 #define DCHECK_STRCASEEQ(str1, str2) CHECK_STRCASEEQ(str1, str2)
981 #define DCHECK_STRNE(str1, str2) CHECK_STRNE(str1, str2)
982 #define DCHECK_STRCASENE(str1, str2) CHECK_STRCASENE(str1, str2)
983
984 #else  // NDEBUG
985
986 #define DLOG(severity) \
987   true ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
988
989 #define DVLOG(verboselevel) \
990   (true || !VLOG_IS_ON(verboselevel)) ?\
991     (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(INFO)
992
993 #define DLOG_IF(severity, condition) \
994   (true || !(condition)) ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
995
996 #define DLOG_EVERY_N(severity, n) \
997   true ? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
998
999 #define DLOG_IF_EVERY_N(severity, condition, n) \
1000   (true || !(condition))? (void) 0 : @ac_google_namespace@::LogMessageVoidify() & LOG(severity)
1001
1002 #define DLOG_ASSERT(condition) \
1003   true ? (void) 0 : LOG_ASSERT(condition)
1004
1005 // MSVC warning C4127: conditional expression is constant
1006 #define DCHECK(condition) \
1007   GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
1008   while (false) \
1009     GLOG_MSVC_POP_WARNING() CHECK(condition)
1010
1011 #define DCHECK_EQ(val1, val2) \
1012   GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
1013   while (false) \
1014     GLOG_MSVC_POP_WARNING() CHECK_EQ(val1, val2)
1015
1016 #define DCHECK_NE(val1, val2) \
1017   GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
1018   while (false) \
1019     GLOG_MSVC_POP_WARNING() CHECK_NE(val1, val2)
1020
1021 #define DCHECK_LE(val1, val2) \
1022   GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
1023   while (false) \
1024     GLOG_MSVC_POP_WARNING() CHECK_LE(val1, val2)
1025
1026 #define DCHECK_LT(val1, val2) \
1027   GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
1028   while (false) \
1029     GLOG_MSVC_POP_WARNING() CHECK_LT(val1, val2)
1030
1031 #define DCHECK_GE(val1, val2) \
1032   GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
1033   while (false) \
1034     GLOG_MSVC_POP_WARNING() CHECK_GE(val1, val2)
1035
1036 #define DCHECK_GT(val1, val2) \
1037   GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
1038   while (false) \
1039     GLOG_MSVC_POP_WARNING() CHECK_GT(val1, val2)
1040
1041 // You may see warnings in release mode if you don't use the return
1042 // value of DCHECK_NOTNULL. Please just use DCHECK for such cases.
1043 #define DCHECK_NOTNULL(val) (val)
1044
1045 #define DCHECK_STREQ(str1, str2) \
1046   GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
1047   while (false) \
1048     GLOG_MSVC_POP_WARNING() CHECK_STREQ(str1, str2)
1049
1050 #define DCHECK_STRCASEEQ(str1, str2) \
1051   GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
1052   while (false) \
1053     GLOG_MSVC_POP_WARNING() CHECK_STRCASEEQ(str1, str2)
1054
1055 #define DCHECK_STRNE(str1, str2) \
1056   GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
1057   while (false) \
1058     GLOG_MSVC_POP_WARNING() CHECK_STRNE(str1, str2)
1059
1060 #define DCHECK_STRCASENE(str1, str2) \
1061   GLOG_MSVC_PUSH_DISABLE_WARNING(4127) \
1062   while (false) \
1063     GLOG_MSVC_POP_WARNING() CHECK_STRCASENE(str1, str2)
1064
1065 #endif  // NDEBUG
1066
1067 // Log only in verbose mode.
1068
1069 #define VLOG(verboselevel) LOG_IF(INFO, VLOG_IS_ON(verboselevel))
1070
1071 #define VLOG_IF(verboselevel, condition) \
1072   LOG_IF(INFO, (condition) && VLOG_IS_ON(verboselevel))
1073
1074 #define VLOG_EVERY_N(verboselevel, n) \
1075   LOG_IF_EVERY_N(INFO, VLOG_IS_ON(verboselevel), n)
1076
1077 #define VLOG_IF_EVERY_N(verboselevel, condition, n) \
1078   LOG_IF_EVERY_N(INFO, (condition) && VLOG_IS_ON(verboselevel), n)
1079
1080 namespace base_logging {
1081
1082 // LogMessage::LogStream is a std::ostream backed by this streambuf.
1083 // This class ignores overflow and leaves two bytes at the end of the
1084 // buffer to allow for a '\n' and '\0'.
1085 class GOOGLE_GLOG_DLL_DECL LogStreamBuf : public std::streambuf {
1086  public:
1087   // REQUIREMENTS: "len" must be >= 2 to account for the '\n' and '\n'.
1088   LogStreamBuf(char *buf, int len) {
1089     setp(buf, buf + len - 2);
1090   }
1091   // This effectively ignores overflow.
1092   virtual int_type overflow(int_type ch) {
1093     return ch;
1094   }
1095
1096   // Legacy public ostrstream method.
1097   size_t pcount() const { return pptr() - pbase(); }
1098   char* pbase() const { return std::streambuf::pbase(); }
1099 };
1100
1101 }  // namespace base_logging
1102
1103 //
1104 // This class more or less represents a particular log message.  You
1105 // create an instance of LogMessage and then stream stuff to it.
1106 // When you finish streaming to it, ~LogMessage is called and the
1107 // full message gets streamed to the appropriate destination.
1108 //
1109 // You shouldn't actually use LogMessage's constructor to log things,
1110 // though.  You should use the LOG() macro (and variants thereof)
1111 // above.
1112 class GOOGLE_GLOG_DLL_DECL LogMessage {
1113 public:
1114   enum {
1115     // Passing kNoLogPrefix for the line number disables the
1116     // log-message prefix. Useful for using the LogMessage
1117     // infrastructure as a printing utility. See also the --log_prefix
1118     // flag for controlling the log-message prefix on an
1119     // application-wide basis.
1120     kNoLogPrefix = -1
1121   };
1122
1123   // LogStream inherit from non-DLL-exported class (std::ostrstream)
1124   // and VC++ produces a warning for this situation.
1125   // However, MSDN says "C4275 can be ignored in Microsoft Visual C++
1126   // 2005 if you are deriving from a type in the Standard C++ Library"
1127   // http://msdn.microsoft.com/en-us/library/3tdb471s(VS.80).aspx
1128   // Let's just ignore the warning.
1129 #ifdef _MSC_VER
1130 # pragma warning(disable: 4275)
1131 #endif
1132   class GOOGLE_GLOG_DLL_DECL LogStream : public std::ostream {
1133 #ifdef _MSC_VER
1134 # pragma warning(default: 4275)
1135 #endif
1136   public:
1137     LogStream(char *buf, int len, int ctr)
1138         : std::ostream(NULL),
1139           streambuf_(buf, len),
1140           ctr_(ctr),
1141           self_(this) {
1142       rdbuf(&streambuf_);
1143     }
1144
1145     int ctr() const { return ctr_; }
1146     void set_ctr(int ctr) { ctr_ = ctr; }
1147     LogStream* self() const { return self_; }
1148
1149     // Legacy std::streambuf methods.
1150     size_t pcount() const { return streambuf_.pcount(); }
1151     char* pbase() const { return streambuf_.pbase(); }
1152     char* str() const { return pbase(); }
1153
1154   private:
1155     base_logging::LogStreamBuf streambuf_;
1156     int ctr_;  // Counter hack (for the LOG_EVERY_X() macro)
1157     LogStream *self_;  // Consistency check hack
1158   };
1159
1160 public:
1161   // icc 8 requires this typedef to avoid an internal compiler error.
1162   typedef void (LogMessage::*SendMethod)();
1163
1164   LogMessage(const char* file, int line, LogSeverity severity, int ctr,
1165              SendMethod send_method);
1166
1167   // Two special constructors that generate reduced amounts of code at
1168   // LOG call sites for common cases.
1169
1170   // Used for LOG(INFO): Implied are:
1171   // severity = INFO, ctr = 0, send_method = &LogMessage::SendToLog.
1172   //
1173   // Using this constructor instead of the more complex constructor above
1174   // saves 19 bytes per call site.
1175   LogMessage(const char* file, int line);
1176
1177   // Used for LOG(severity) where severity != INFO.  Implied
1178   // are: ctr = 0, send_method = &LogMessage::SendToLog
1179   //
1180   // Using this constructor instead of the more complex constructor above
1181   // saves 17 bytes per call site.
1182   LogMessage(const char* file, int line, LogSeverity severity);
1183
1184   // Constructor to log this message to a specified sink (if not NULL).
1185   // Implied are: ctr = 0, send_method = &LogMessage::SendToSinkAndLog if
1186   // also_send_to_log is true, send_method = &LogMessage::SendToSink otherwise.
1187   LogMessage(const char* file, int line, LogSeverity severity, LogSink* sink,
1188              bool also_send_to_log);
1189
1190   // Constructor where we also give a vector<string> pointer
1191   // for storing the messages (if the pointer is not NULL).
1192   // Implied are: ctr = 0, send_method = &LogMessage::SaveOrSendToLog.
1193   LogMessage(const char* file, int line, LogSeverity severity,
1194              std::vector<std::string>* outvec);
1195
1196   // Constructor where we also give a string pointer for storing the
1197   // message (if the pointer is not NULL).  Implied are: ctr = 0,
1198   // send_method = &LogMessage::WriteToStringAndLog.
1199   LogMessage(const char* file, int line, LogSeverity severity,
1200              std::string* message);
1201
1202   // A special constructor used for check failures
1203   LogMessage(const char* file, int line, const CheckOpString& result);
1204
1205   ~LogMessage();
1206
1207   // Flush a buffered message to the sink set in the constructor.  Always
1208   // called by the destructor, it may also be called from elsewhere if
1209   // needed.  Only the first call is actioned; any later ones are ignored.
1210   void Flush();
1211
1212   // An arbitrary limit on the length of a single log message.  This
1213   // is so that streaming can be done more efficiently.
1214   static const size_t kMaxLogMessageLen;
1215
1216   // Theses should not be called directly outside of logging.*,
1217   // only passed as SendMethod arguments to other LogMessage methods:
1218   void SendToLog();  // Actually dispatch to the logs
1219   void SendToSyslogAndLog();  // Actually dispatch to syslog and the logs
1220
1221   // Call abort() or similar to perform LOG(FATAL) crash.
1222   static void @ac_cv___attribute___noreturn@ Fail();
1223
1224   std::ostream& stream();
1225
1226   int preserved_errno() const;
1227
1228   // Must be called without the log_mutex held.  (L < log_mutex)
1229   static int64 num_messages(int severity);
1230
1231   struct LogMessageData;
1232
1233 private:
1234   // Fully internal SendMethod cases:
1235   void SendToSinkAndLog();  // Send to sink if provided and dispatch to the logs
1236   void SendToSink();  // Send to sink if provided, do nothing otherwise.
1237
1238   // Write to string if provided and dispatch to the logs.
1239   void WriteToStringAndLog();
1240
1241   void SaveOrSendToLog();  // Save to stringvec if provided, else to logs
1242
1243   void Init(const char* file, int line, LogSeverity severity,
1244             void (LogMessage::*send_method)());
1245
1246   // Used to fill in crash information during LOG(FATAL) failures.
1247   void RecordCrashReason(glog_internal_namespace_::CrashReason* reason);
1248
1249   // Counts of messages sent at each priority:
1250   static int64 num_messages_[NUM_SEVERITIES];  // under log_mutex
1251
1252   // We keep the data in a separate struct so that each instance of
1253   // LogMessage uses less stack space.
1254   LogMessageData* allocated_;
1255   LogMessageData* data_;
1256
1257   friend class LogDestination;
1258
1259   LogMessage(const LogMessage&);
1260   void operator=(const LogMessage&);
1261 };
1262
1263 // This class happens to be thread-hostile because all instances share
1264 // a single data buffer, but since it can only be created just before
1265 // the process dies, we don't worry so much.
1266 class GOOGLE_GLOG_DLL_DECL LogMessageFatal : public LogMessage {
1267  public:
1268   LogMessageFatal(const char* file, int line);
1269   LogMessageFatal(const char* file, int line, const CheckOpString& result);
1270   @ac_cv___attribute___noreturn@ ~LogMessageFatal();
1271 };
1272
1273 // A non-macro interface to the log facility; (useful
1274 // when the logging level is not a compile-time constant).
1275 inline void LogAtLevel(int const severity, std::string const &msg) {
1276   LogMessage(__FILE__, __LINE__, severity).stream() << msg;
1277 }
1278
1279 // A macro alternative of LogAtLevel. New code may want to use this
1280 // version since there are two advantages: 1. this version outputs the
1281 // file name and the line number where this macro is put like other
1282 // LOG macros, 2. this macro can be used as C++ stream.
1283 #define LOG_AT_LEVEL(severity) @ac_google_namespace@::LogMessage(__FILE__, __LINE__, severity).stream()
1284
1285 // A small helper for CHECK_NOTNULL().
1286 template <typename T>
1287 T* CheckNotNull(const char *file, int line, const char *names, T* t) {
1288   if (t == NULL) {
1289     LogMessageFatal(file, line, new std::string(names));
1290   }
1291   return t;
1292 }
1293
1294 // Allow folks to put a counter in the LOG_EVERY_X()'ed messages. This
1295 // only works if ostream is a LogStream. If the ostream is not a
1296 // LogStream you'll get an assert saying as much at runtime.
1297 GOOGLE_GLOG_DLL_DECL std::ostream& operator<<(std::ostream &os,
1298                                               const PRIVATE_Counter&);
1299
1300
1301 // Derived class for PLOG*() above.
1302 class GOOGLE_GLOG_DLL_DECL ErrnoLogMessage : public LogMessage {
1303  public:
1304
1305   ErrnoLogMessage(const char* file, int line, LogSeverity severity, int ctr,
1306                   void (LogMessage::*send_method)());
1307
1308   // Postpends ": strerror(errno) [errno]".
1309   ~ErrnoLogMessage();
1310
1311  private:
1312   ErrnoLogMessage(const ErrnoLogMessage&);
1313   void operator=(const ErrnoLogMessage&);
1314 };
1315
1316
1317 // This class is used to explicitly ignore values in the conditional
1318 // logging macros.  This avoids compiler warnings like "value computed
1319 // is not used" and "statement has no effect".
1320
1321 class GOOGLE_GLOG_DLL_DECL LogMessageVoidify {
1322  public:
1323   LogMessageVoidify() { }
1324   // This has to be an operator with a precedence lower than << but
1325   // higher than ?:
1326   void operator&(std::ostream&) { }
1327 };
1328
1329
1330 // Flushes all log files that contains messages that are at least of
1331 // the specified severity level.  Thread-safe.
1332 GOOGLE_GLOG_DLL_DECL void FlushLogFiles(LogSeverity min_severity);
1333
1334 // Flushes all log files that contains messages that are at least of
1335 // the specified severity level. Thread-hostile because it ignores
1336 // locking -- used for catastrophic failures.
1337 GOOGLE_GLOG_DLL_DECL void FlushLogFilesUnsafe(LogSeverity min_severity);
1338
1339 //
1340 // Set the destination to which a particular severity level of log
1341 // messages is sent.  If base_filename is "", it means "don't log this
1342 // severity".  Thread-safe.
1343 //
1344 GOOGLE_GLOG_DLL_DECL void SetLogDestination(LogSeverity severity,
1345                                             const char* base_filename);
1346
1347 //
1348 // Set the basename of the symlink to the latest log file at a given
1349 // severity.  If symlink_basename is empty, do not make a symlink.  If
1350 // you don't call this function, the symlink basename is the
1351 // invocation name of the program.  Thread-safe.
1352 //
1353 GOOGLE_GLOG_DLL_DECL void SetLogSymlink(LogSeverity severity,
1354                                         const char* symlink_basename);
1355
1356 //
1357 // Used to send logs to some other kind of destination
1358 // Users should subclass LogSink and override send to do whatever they want.
1359 // Implementations must be thread-safe because a shared instance will
1360 // be called from whichever thread ran the LOG(XXX) line.
1361 class GOOGLE_GLOG_DLL_DECL LogSink {
1362  public:
1363   virtual ~LogSink();
1364
1365   // Sink's logging logic (message_len is such as to exclude '\n' at the end).
1366   // This method can't use LOG() or CHECK() as logging system mutex(s) are held
1367   // during this call.
1368   virtual void send(LogSeverity severity, const char* full_filename,
1369                     const char* base_filename, int line,
1370                     const struct ::tm* tm_time,
1371                     const char* message, size_t message_len) = 0;
1372
1373   // Redefine this to implement waiting for
1374   // the sink's logging logic to complete.
1375   // It will be called after each send() returns,
1376   // but before that LogMessage exits or crashes.
1377   // By default this function does nothing.
1378   // Using this function one can implement complex logic for send()
1379   // that itself involves logging; and do all this w/o causing deadlocks and
1380   // inconsistent rearrangement of log messages.
1381   // E.g. if a LogSink has thread-specific actions, the send() method
1382   // can simply add the message to a queue and wake up another thread that
1383   // handles real logging while itself making some LOG() calls;
1384   // WaitTillSent() can be implemented to wait for that logic to complete.
1385   // See our unittest for an example.
1386   virtual void WaitTillSent();
1387
1388   // Returns the normal text output of the log message.
1389   // Can be useful to implement send().
1390   static std::string ToString(LogSeverity severity, const char* file, int line,
1391                               const struct ::tm* tm_time,
1392                               const char* message, size_t message_len);
1393 };
1394
1395 // Add or remove a LogSink as a consumer of logging data.  Thread-safe.
1396 GOOGLE_GLOG_DLL_DECL void AddLogSink(LogSink *destination);
1397 GOOGLE_GLOG_DLL_DECL void RemoveLogSink(LogSink *destination);
1398
1399 //
1400 // Specify an "extension" added to the filename specified via
1401 // SetLogDestination.  This applies to all severity levels.  It's
1402 // often used to append the port we're listening on to the logfile
1403 // name.  Thread-safe.
1404 //
1405 GOOGLE_GLOG_DLL_DECL void SetLogFilenameExtension(
1406     const char* filename_extension);
1407
1408 //
1409 // Make it so that all log messages of at least a particular severity
1410 // are logged to stderr (in addition to logging to the usual log
1411 // file(s)).  Thread-safe.
1412 //
1413 GOOGLE_GLOG_DLL_DECL void SetStderrLogging(LogSeverity min_severity);
1414
1415 //
1416 // Make it so that all log messages go only to stderr.  Thread-safe.
1417 //
1418 GOOGLE_GLOG_DLL_DECL void LogToStderr();
1419
1420 //
1421 // Make it so that all log messages of at least a particular severity are
1422 // logged via email to a list of addresses (in addition to logging to the
1423 // usual log file(s)).  The list of addresses is just a string containing
1424 // the email addresses to send to (separated by spaces, say).  Thread-safe.
1425 //
1426 GOOGLE_GLOG_DLL_DECL void SetEmailLogging(LogSeverity min_severity,
1427                                           const char* addresses);
1428
1429 // A simple function that sends email. dest is a commma-separated
1430 // list of addressess.  Thread-safe.
1431 GOOGLE_GLOG_DLL_DECL bool SendEmail(const char *dest,
1432                                     const char *subject, const char *body);
1433
1434 GOOGLE_GLOG_DLL_DECL const std::vector<std::string>& GetLoggingDirectories();
1435
1436 // For tests only:  Clear the internal [cached] list of logging directories to
1437 // force a refresh the next time GetLoggingDirectories is called.
1438 // Thread-hostile.
1439 void TestOnly_ClearLoggingDirectoriesList();
1440
1441 // Returns a set of existing temporary directories, which will be a
1442 // subset of the directories returned by GetLogginDirectories().
1443 // Thread-safe.
1444 GOOGLE_GLOG_DLL_DECL void GetExistingTempDirectories(
1445     std::vector<std::string>* list);
1446
1447 // Print any fatal message again -- useful to call from signal handler
1448 // so that the last thing in the output is the fatal message.
1449 // Thread-hostile, but a race is unlikely.
1450 GOOGLE_GLOG_DLL_DECL void ReprintFatalMessage();
1451
1452 // Truncate a log file that may be the append-only output of multiple
1453 // processes and hence can't simply be renamed/reopened (typically a
1454 // stdout/stderr).  If the file "path" is > "limit" bytes, copy the
1455 // last "keep" bytes to offset 0 and truncate the rest. Since we could
1456 // be racing with other writers, this approach has the potential to
1457 // lose very small amounts of data. For security, only follow symlinks
1458 // if the path is /proc/self/fd/*
1459 GOOGLE_GLOG_DLL_DECL void TruncateLogFile(const char *path,
1460                                           int64 limit, int64 keep);
1461
1462 // Truncate stdout and stderr if they are over the value specified by
1463 // --max_log_size; keep the final 1MB.  This function has the same
1464 // race condition as TruncateLogFile.
1465 GOOGLE_GLOG_DLL_DECL void TruncateStdoutStderr();
1466
1467 // Return the string representation of the provided LogSeverity level.
1468 // Thread-safe.
1469 GOOGLE_GLOG_DLL_DECL const char* GetLogSeverityName(LogSeverity severity);
1470
1471 // ---------------------------------------------------------------------
1472 // Implementation details that are not useful to most clients
1473 // ---------------------------------------------------------------------
1474
1475 // A Logger is the interface used by logging modules to emit entries
1476 // to a log.  A typical implementation will dump formatted data to a
1477 // sequence of files.  We also provide interfaces that will forward
1478 // the data to another thread so that the invoker never blocks.
1479 // Implementations should be thread-safe since the logging system
1480 // will write to them from multiple threads.
1481
1482 namespace base {
1483
1484 class GOOGLE_GLOG_DLL_DECL Logger {
1485  public:
1486   virtual ~Logger();
1487
1488   // Writes "message[0,message_len-1]" corresponding to an event that
1489   // occurred at "timestamp".  If "force_flush" is true, the log file
1490   // is flushed immediately.
1491   //
1492   // The input message has already been formatted as deemed
1493   // appropriate by the higher level logging facility.  For example,
1494   // textual log messages already contain timestamps, and the
1495   // file:linenumber header.
1496   virtual void Write(bool force_flush,
1497                      time_t timestamp,
1498                      const char* message,
1499                      int message_len) = 0;
1500
1501   // Flush any buffered messages
1502   virtual void Flush() = 0;
1503
1504   // Get the current LOG file size.
1505   // The returned value is approximate since some
1506   // logged data may not have been flushed to disk yet.
1507   virtual uint32 LogSize() = 0;
1508 };
1509
1510 // Get the logger for the specified severity level.  The logger
1511 // remains the property of the logging module and should not be
1512 // deleted by the caller.  Thread-safe.
1513 extern GOOGLE_GLOG_DLL_DECL Logger* GetLogger(LogSeverity level);
1514
1515 // Set the logger for the specified severity level.  The logger
1516 // becomes the property of the logging module and should not
1517 // be deleted by the caller.  Thread-safe.
1518 extern GOOGLE_GLOG_DLL_DECL void SetLogger(LogSeverity level, Logger* logger);
1519
1520 }
1521
1522 // glibc has traditionally implemented two incompatible versions of
1523 // strerror_r(). There is a poorly defined convention for picking the
1524 // version that we want, but it is not clear whether it even works with
1525 // all versions of glibc.
1526 // So, instead, we provide this wrapper that automatically detects the
1527 // version that is in use, and then implements POSIX semantics.
1528 // N.B. In addition to what POSIX says, we also guarantee that "buf" will
1529 // be set to an empty string, if this function failed. This means, in most
1530 // cases, you do not need to check the error code and you can directly
1531 // use the value of "buf". It will never have an undefined value.
1532 // DEPRECATED: Use StrError(int) instead.
1533 GOOGLE_GLOG_DLL_DECL int posix_strerror_r(int err, char *buf, size_t len);
1534
1535 // A thread-safe replacement for strerror(). Returns a string describing the
1536 // given POSIX error code.
1537 GOOGLE_GLOG_DLL_DECL std::string StrError(int err);
1538
1539 // A class for which we define operator<<, which does nothing.
1540 class GOOGLE_GLOG_DLL_DECL NullStream : public LogMessage::LogStream {
1541  public:
1542   // Initialize the LogStream so the messages can be written somewhere
1543   // (they'll never be actually displayed). This will be needed if a
1544   // NullStream& is implicitly converted to LogStream&, in which case
1545   // the overloaded NullStream::operator<< will not be invoked.
1546   NullStream() : LogMessage::LogStream(message_buffer_, 1, 0) { }
1547   NullStream(const char* /*file*/, int /*line*/,
1548              const CheckOpString& /*result*/) :
1549       LogMessage::LogStream(message_buffer_, 1, 0) { }
1550   NullStream &stream() { return *this; }
1551  private:
1552   // A very short buffer for messages (which we discard anyway). This
1553   // will be needed if NullStream& converted to LogStream& (e.g. as a
1554   // result of a conditional expression).
1555   char message_buffer_[2];
1556 };
1557
1558 // Do nothing. This operator is inline, allowing the message to be
1559 // compiled away. The message will not be compiled away if we do
1560 // something like (flag ? LOG(INFO) : LOG(ERROR)) << message; when
1561 // SKIP_LOG=WARNING. In those cases, NullStream will be implicitly
1562 // converted to LogStream and the message will be computed and then
1563 // quietly discarded.
1564 template<class T>
1565 inline NullStream& operator<<(NullStream &str, const T &) { return str; }
1566
1567 // Similar to NullStream, but aborts the program (without stack
1568 // trace), like LogMessageFatal.
1569 class GOOGLE_GLOG_DLL_DECL NullStreamFatal : public NullStream {
1570  public:
1571   NullStreamFatal() { }
1572   NullStreamFatal(const char* file, int line, const CheckOpString& result) :
1573       NullStream(file, line, result) { }
1574   @ac_cv___attribute___noreturn@ ~NullStreamFatal() throw () { _exit(1); }
1575 };
1576
1577 // Install a signal handler that will dump signal information and a stack
1578 // trace when the program crashes on certain signals.  We'll install the
1579 // signal handler for the following signals.
1580 //
1581 // SIGSEGV, SIGILL, SIGFPE, SIGABRT, SIGBUS, and SIGTERM.
1582 //
1583 // By default, the signal handler will write the failure dump to the
1584 // standard error.  You can customize the destination by installing your
1585 // own writer function by InstallFailureWriter() below.
1586 //
1587 // Note on threading:
1588 //
1589 // The function should be called before threads are created, if you want
1590 // to use the failure signal handler for all threads.  The stack trace
1591 // will be shown only for the thread that receives the signal.  In other
1592 // words, stack traces of other threads won't be shown.
1593 GOOGLE_GLOG_DLL_DECL void InstallFailureSignalHandler();
1594
1595 // Installs a function that is used for writing the failure dump.  "data"
1596 // is the pointer to the beginning of a message to be written, and "size"
1597 // is the size of the message.  You should not expect the data is
1598 // terminated with '\0'.
1599 GOOGLE_GLOG_DLL_DECL void InstallFailureWriter(
1600     void (*writer)(const char* data, int size));
1601
1602 @ac_google_end_namespace@
1603
1604 #endif // _LOGGING_H_