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