All notes
Glog
* [googleDoc](http://google-glog.googlecode.com/svn/trunk/doc/glog.html "googleDoc") * [yeolar](http://www.yeolar.com/note/2014/12/20/glog/ "yeolar") ### wcf * Use VLOG(1) - VLOG(3) for tracing. * Run app with `APP -v` ``` #include int main(int argc, char* argv[]) { // Logging before InitGoogleLogging() is written to STDERR LOG(ERROR)<<"This is written to STDERR"; // Initialize Google's logging library. google::InitGoogleLogging(argv[0]); LOG(INFO) << "Found " << num_cookies << " cookies"; // Conditional logging. LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; // Occasional logging. // Log at 1st, 11th, 21st, ... The special google::COUNTER value is used to identify which repetition is happening. LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie"; LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER<< "the big cookie"; // limit the output to the first n occurrences: LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie"; // Log only in Debug. // Use these macros to avoid slowing down your production application due to excessive logging. DLOG(INFO) << "Found cookies"; DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; DLOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie"; // When chasing difficult bugs, thorough log messages are very useful. However, you may want to ignore too verbose messages in usual development. // For such verbose logging, glog provides the VLOG macro, which allows you to define your own numeric logging levels. // The --v command line option controls which verbose messages are logged: VLOG(1) << "I'm printed when you run the program with --v=1 or higher"; VLOG(2) << "I'm printed when you run the program with --v=2 or higher"; // The lower the verbose level, the more likely messages are to be logged. For example, if --v==1, VLOG(1) will log, but VLOG(2) will not log. This is opposite of the severity level. // if you write VLOG(0), you should specify --v=-1 or lower to silence it. // The VLOG macros always log at the INFO log level (when they log at all). if (VLOG_IS_ON(2)) { // do some logging preparation and logging // that can't be accomplished with just VLOG(2) << ...; } VLOG_IF(1, (size > 1024))<< "I'm printed when size is more than 1024 and when you run the " "program with --v=1 or more"; VLOG_EVERY_N(1, 10)<< "I'm printed every 10th occurrence, and when you run the program " "with --v=1 or more. Present occurence is " << google::COUNTER; VLOG_IF_EVERY_N(1, (size > 1024), 10)<< "I'm printed on every 10th occurence of case when size is more " " than 1024, when you run the program with --v=1 or more. "; "Present occurence is " << google::COUNTER; // Google Style perror() // PLOG() and PLOG_IF() and PCHECK() behave exactly like their LOG* and CHECK equivalents with the addition that they append a description of the current state of errno to their output lines. E.g. PCHECK(write(1, NULL, 2) >= 0) << "Write NULL failed"; // This check fails with the following error message. // F0825 185142 test.cc:22] Check failed: write(1, NULL, 2) >= 0 Write NULL failed: Bad address [14] // SYSLOG, SYSLOG_IF, and SYSLOG_EVERY_N macros are available. These log to syslog in addition to the normal logs. Be aware that logging to syslog can drastically impact performance, especially if syslog is configured for remote logging! // FATAL severity level messages or unsatisfied CHECK condition terminate your program. You can change the behavior of the termination by InstallFailureFunction. void YourFailureFunction() { // Reports something... exit(1); } google::InstallFailureFunction(&YourFailureFunction); return 0; } ``` ## Intro * If you want to find less common features, please check header files under src/glog directory. * The header file can be used for thread-safe logging, which does not allocate any memory or acquire any locks. ### Dump * The library provides a convenient signal handler that will dump useful information when the program crashes on certain signals such as SIGSEGV. The signal handler can be installed by google::InstallFailureSignalHandler(). * By default, the signal handler writes the failure dump to the standard error. You can customize the destination by InstallFailureWriter(). * By default, glog tries to dump stacktrace and makes the program exit with status 1. ### Severity * In increasing order of severity: INFO, WARNING, ERROR, and FATAL. * Logging a FATAL message terminates the program (after the message is logged). * Note that messages of a given severity are logged not only in the logfile for that severity, but also in all logfiles of lower severity. E.g., a message of severity FATAL will be logged to the logfiles of severity FATAL, ERROR, WARNING, and INFO. * The __DFATAL__ severity logs a FATAL error in debug mode (i.e., there is no NDEBUG macro defined), but avoids halting the program in production by automatically reducing the severity to ERROR. * Unless otherwise specified, glog writes to the filename "/tmp/...log...