Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
michael@0 | 2 | |
michael@0 | 3 | <html> |
michael@0 | 4 | <head> |
michael@0 | 5 | <title>How To Use Google Logging Library (glog)</title> |
michael@0 | 6 | |
michael@0 | 7 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
michael@0 | 8 | <link href="http://www.google.com/favicon.ico" type="image/x-icon" |
michael@0 | 9 | rel="shortcut icon"> |
michael@0 | 10 | <link href="designstyle.css" type="text/css" rel="stylesheet"> |
michael@0 | 11 | <style type="text/css"> |
michael@0 | 12 | <!-- |
michael@0 | 13 | ol.bluelist li { |
michael@0 | 14 | color: #3366ff; |
michael@0 | 15 | font-family: sans-serif; |
michael@0 | 16 | } |
michael@0 | 17 | ol.bluelist li p { |
michael@0 | 18 | color: #000; |
michael@0 | 19 | font-family: "Times Roman", times, serif; |
michael@0 | 20 | } |
michael@0 | 21 | ul.blacklist li { |
michael@0 | 22 | color: #000; |
michael@0 | 23 | font-family: "Times Roman", times, serif; |
michael@0 | 24 | } |
michael@0 | 25 | //--> |
michael@0 | 26 | </style> |
michael@0 | 27 | </head> |
michael@0 | 28 | |
michael@0 | 29 | <body> |
michael@0 | 30 | |
michael@0 | 31 | <h1>How To Use Google Logging Library (glog)</h1> |
michael@0 | 32 | <small>(as of |
michael@0 | 33 | <script type=text/javascript> |
michael@0 | 34 | var lm = new Date(document.lastModified); |
michael@0 | 35 | document.write(lm.toDateString()); |
michael@0 | 36 | </script>) |
michael@0 | 37 | </small> |
michael@0 | 38 | <br> |
michael@0 | 39 | |
michael@0 | 40 | <h2> <A NAME=intro>Introduction</A> </h2> |
michael@0 | 41 | |
michael@0 | 42 | <p><b>Google glog</b> is a library that implements application-level |
michael@0 | 43 | logging. This library provides logging APIs based on C++-style |
michael@0 | 44 | streams and various helper macros. |
michael@0 | 45 | You can log a message by simply streaming things to LOG(<a |
michael@0 | 46 | particular <a href="#severity">severity level</a>>), e.g. |
michael@0 | 47 | |
michael@0 | 48 | <pre> |
michael@0 | 49 | #include <glog/logging.h> |
michael@0 | 50 | |
michael@0 | 51 | int main(int argc, char* argv[]) { |
michael@0 | 52 | // Initialize Google's logging library. |
michael@0 | 53 | google::InitGoogleLogging(argv[0]); |
michael@0 | 54 | |
michael@0 | 55 | // ... |
michael@0 | 56 | LOG(INFO) << "Found " << num_cookies << " cookies"; |
michael@0 | 57 | } |
michael@0 | 58 | </pre> |
michael@0 | 59 | |
michael@0 | 60 | <p>Google glog defines a series of macros that simplify many common logging |
michael@0 | 61 | tasks. You can log messages by severity level, control logging |
michael@0 | 62 | behavior from the command line, log based on conditionals, abort the |
michael@0 | 63 | program when expected conditions are not met, introduce your own |
michael@0 | 64 | verbose logging levels, and more. This document describes the |
michael@0 | 65 | functionality supported by glog. Please note that this document |
michael@0 | 66 | doesn't describe all features in this library, but the most useful |
michael@0 | 67 | ones. If you want to find less common features, please check |
michael@0 | 68 | header files under <code>src/glog</code> directory. |
michael@0 | 69 | |
michael@0 | 70 | <h2> <A NAME=severity>Severity Level</A> </h2> |
michael@0 | 71 | |
michael@0 | 72 | <p> |
michael@0 | 73 | You can specify one of the following severity levels (in |
michael@0 | 74 | increasing order of severity): <code>INFO</code>, <code>WARNING</code>, |
michael@0 | 75 | <code>ERROR</code>, and <code>FATAL</code>. |
michael@0 | 76 | Logging a <code>FATAL</code> message terminates the program (after the |
michael@0 | 77 | message is logged). |
michael@0 | 78 | Note that messages of a given severity are logged not only in the |
michael@0 | 79 | logfile for that severity, but also in all logfiles of lower severity. |
michael@0 | 80 | E.g., a message of severity <code>FATAL</code> will be logged to the |
michael@0 | 81 | logfiles of severity <code>FATAL</code>, <code>ERROR</code>, |
michael@0 | 82 | <code>WARNING</code>, and <code>INFO</code>. |
michael@0 | 83 | |
michael@0 | 84 | <p> |
michael@0 | 85 | The <code>DFATAL</code> severity logs a <code>FATAL</code> error in |
michael@0 | 86 | debug mode (i.e., there is no <code>NDEBUG</code> macro defined), but |
michael@0 | 87 | avoids halting the program in production by automatically reducing the |
michael@0 | 88 | severity to <code>ERROR</code>. |
michael@0 | 89 | |
michael@0 | 90 | <p>Unless otherwise specified, glog writes to the filename |
michael@0 | 91 | "/tmp/<program name>.<hostname>.<user name>.log.<severity level>.<date>.<time>.<pid>" |
michael@0 | 92 | (e.g., "/tmp/hello_world.example.com.hamaji.log.INFO.20080709-222411.10474"). |
michael@0 | 93 | By default, glog copies the log messages of severity level |
michael@0 | 94 | <code>ERROR</code> or <code>FATAL</code> to standard error (stderr) |
michael@0 | 95 | in addition to log files. |
michael@0 | 96 | |
michael@0 | 97 | <h2><A NAME=flags>Setting Flags</A></h2> |
michael@0 | 98 | |
michael@0 | 99 | <p>Several flags influence glog's output behavior. |
michael@0 | 100 | If the <a href="http://code.google.com/p/google-gflags/">Google |
michael@0 | 101 | gflags library</a> is installed on your machine, the |
michael@0 | 102 | <code>configure</code> script (see the INSTALL file in the package for |
michael@0 | 103 | detail of this script) will automatically detect and use it, |
michael@0 | 104 | allowing you to pass flags on the command line. For example, if you |
michael@0 | 105 | want to turn the flag <code>--logtostderr</code> on, you can start |
michael@0 | 106 | your application with the following command line: |
michael@0 | 107 | |
michael@0 | 108 | <pre> |
michael@0 | 109 | ./your_application --logtostderr=1 |
michael@0 | 110 | </pre> |
michael@0 | 111 | |
michael@0 | 112 | If the Google gflags library isn't installed, you set flags via |
michael@0 | 113 | environment variables, prefixing the flag name with "GLOG_", e.g. |
michael@0 | 114 | |
michael@0 | 115 | <pre> |
michael@0 | 116 | GLOG_logtostderr=1 ./your_application |
michael@0 | 117 | </pre> |
michael@0 | 118 | |
michael@0 | 119 | <!-- TODO(hamaji): Fill the version number |
michael@0 | 120 | <p>By glog version 0.x.x, you can use GLOG_* environment variables |
michael@0 | 121 | even if you have gflags. If both an environment variable and a flag |
michael@0 | 122 | are specified, the value specified by a flag wins. E.g., if GLOG_v=0 |
michael@0 | 123 | and --v=1, the verbosity will be 1, not 0. |
michael@0 | 124 | --> |
michael@0 | 125 | |
michael@0 | 126 | <p>The following flags are most commonly used: |
michael@0 | 127 | |
michael@0 | 128 | <dl> |
michael@0 | 129 | <dt><code>logtostderr</code> (<code>bool</code>, default=<code>false</code>) |
michael@0 | 130 | <dd>Log messages to stderr instead of logfiles.<br> |
michael@0 | 131 | Note: you can set binary flags to <code>true</code> by specifying |
michael@0 | 132 | <code>1</code>, <code>true</code>, or <code>yes</code> (case |
michael@0 | 133 | insensitive). |
michael@0 | 134 | Also, you can set binary flags to <code>false</code> by specifying |
michael@0 | 135 | <code>0</code>, <code>false</code>, or <code>no</code> (again, case |
michael@0 | 136 | insensitive). |
michael@0 | 137 | <dt><code>stderrthreshold</code> (<code>int</code>, default=2, which |
michael@0 | 138 | is <code>ERROR</code>) |
michael@0 | 139 | <dd>Copy log messages at or above this level to stderr in |
michael@0 | 140 | addition to logfiles. The numbers of severity levels |
michael@0 | 141 | <code>INFO</code>, <code>WARNING</code>, <code>ERROR</code>, and |
michael@0 | 142 | <code>FATAL</code> are 0, 1, 2, and 3, respectively. |
michael@0 | 143 | <dt><code>minloglevel</code> (<code>int</code>, default=0, which |
michael@0 | 144 | is <code>INFO</code>) |
michael@0 | 145 | <dd>Log messages at or above this level. Again, the numbers of |
michael@0 | 146 | severity levels <code>INFO</code>, <code>WARNING</code>, |
michael@0 | 147 | <code>ERROR</code>, and <code>FATAL</code> are 0, 1, 2, and 3, |
michael@0 | 148 | respectively. |
michael@0 | 149 | <dt><code>log_dir</code> (<code>string</code>, default="") |
michael@0 | 150 | <dd>If specified, logfiles are written into this directory instead |
michael@0 | 151 | of the default logging directory. |
michael@0 | 152 | <dt><code>v</code> (<code>int</code>, default=0) |
michael@0 | 153 | <dd>Show all <code>VLOG(m)</code> messages for <code>m</code> less or |
michael@0 | 154 | equal the value of this flag. Overridable by --vmodule. |
michael@0 | 155 | See <a href="#verbose">the section about verbose logging</a> for more |
michael@0 | 156 | detail. |
michael@0 | 157 | <dt><code>vmodule</code> (<code>string</code>, default="") |
michael@0 | 158 | <dd>Per-module verbose level. The argument has to contain a |
michael@0 | 159 | comma-separated list of <module name>=<log level>. |
michael@0 | 160 | <module name> |
michael@0 | 161 | is a glob pattern (e.g., <code>gfs*</code> for all modules whose name |
michael@0 | 162 | starts with "gfs"), matched against the filename base |
michael@0 | 163 | (that is, name ignoring .cc/.h./-inl.h). |
michael@0 | 164 | <log level> overrides any value given by --v. |
michael@0 | 165 | See also <a href="#verbose">the section about verbose logging</a>. |
michael@0 | 166 | </dl> |
michael@0 | 167 | |
michael@0 | 168 | <p>There are some other flags defined in logging.cc. Please grep the |
michael@0 | 169 | source code for "DEFINE_" to see a complete list of all flags. |
michael@0 | 170 | |
michael@0 | 171 | <h2><A NAME=conditional>Conditional / Occasional Logging</A></h2> |
michael@0 | 172 | |
michael@0 | 173 | <p>Sometimes, you may only want to log a message under certain |
michael@0 | 174 | conditions. You can use the following macros to perform conditional |
michael@0 | 175 | logging: |
michael@0 | 176 | |
michael@0 | 177 | <pre> |
michael@0 | 178 | LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
michael@0 | 179 | </pre> |
michael@0 | 180 | |
michael@0 | 181 | The "Got lots of cookies" message is logged only when the variable |
michael@0 | 182 | <code>num_cookies</code> exceeds 10. |
michael@0 | 183 | |
michael@0 | 184 | If a line of code is executed many times, it may be useful to only log |
michael@0 | 185 | a message at certain intervals. This kind of logging is most useful |
michael@0 | 186 | for informational messages. |
michael@0 | 187 | |
michael@0 | 188 | <pre> |
michael@0 | 189 | LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie"; |
michael@0 | 190 | </pre> |
michael@0 | 191 | |
michael@0 | 192 | <p>The above line outputs a log messages on the 1st, 11th, |
michael@0 | 193 | 21st, ... times it is executed. Note that the special |
michael@0 | 194 | <code>google::COUNTER</code> value is used to identify which repetition is |
michael@0 | 195 | happening. |
michael@0 | 196 | |
michael@0 | 197 | <p>You can combine conditional and occasional logging with the |
michael@0 | 198 | following macro. |
michael@0 | 199 | |
michael@0 | 200 | <pre> |
michael@0 | 201 | LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER |
michael@0 | 202 | << "th big cookie"; |
michael@0 | 203 | </pre> |
michael@0 | 204 | |
michael@0 | 205 | <p>Instead of outputting a message every nth time, you can also limit |
michael@0 | 206 | the output to the first n occurrences: |
michael@0 | 207 | |
michael@0 | 208 | <pre> |
michael@0 | 209 | LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie"; |
michael@0 | 210 | </pre> |
michael@0 | 211 | |
michael@0 | 212 | <p>Outputs log messages for the first 20 times it is executed. Again, |
michael@0 | 213 | the <code>google::COUNTER</code> identifier indicates which repetition is |
michael@0 | 214 | happening. |
michael@0 | 215 | |
michael@0 | 216 | <h2><A NAME=debug>Debug Mode Support</A></h2> |
michael@0 | 217 | |
michael@0 | 218 | <p>Special "debug mode" logging macros only have an effect in debug |
michael@0 | 219 | mode and are compiled away to nothing for non-debug mode |
michael@0 | 220 | compiles. Use these macros to avoid slowing down your production |
michael@0 | 221 | application due to excessive logging. |
michael@0 | 222 | |
michael@0 | 223 | <pre> |
michael@0 | 224 | DLOG(INFO) << "Found cookies"; |
michael@0 | 225 | |
michael@0 | 226 | DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies"; |
michael@0 | 227 | |
michael@0 | 228 | DLOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie"; |
michael@0 | 229 | </pre> |
michael@0 | 230 | |
michael@0 | 231 | <h2><A NAME=check>CHECK Macros</A></h2> |
michael@0 | 232 | |
michael@0 | 233 | <p>It is a good practice to check expected conditions in your program |
michael@0 | 234 | frequently to detect errors as early as possible. The |
michael@0 | 235 | <code>CHECK</code> macro provides the ability to abort the application |
michael@0 | 236 | when a condition is not met, similar to the <code>assert</code> macro |
michael@0 | 237 | defined in the standard C library. |
michael@0 | 238 | |
michael@0 | 239 | <p><code>CHECK</code> aborts the application if a condition is not |
michael@0 | 240 | true. Unlike <code>assert</code>, it is *not* controlled by |
michael@0 | 241 | <code>NDEBUG</code>, so the check will be executed regardless of |
michael@0 | 242 | compilation mode. Therefore, <code>fp->Write(x)</code> in the |
michael@0 | 243 | following example is always executed: |
michael@0 | 244 | |
michael@0 | 245 | <pre> |
michael@0 | 246 | CHECK(fp->Write(x) == 4) << "Write failed!"; |
michael@0 | 247 | </pre> |
michael@0 | 248 | |
michael@0 | 249 | <p>There are various helper macros for |
michael@0 | 250 | equality/inequality checks - <code>CHECK_EQ</code>, |
michael@0 | 251 | <code>CHECK_NE</code>, <code>CHECK_LE</code>, <code>CHECK_LT</code>, |
michael@0 | 252 | <code>CHECK_GE</code>, and <code>CHECK_GT</code>. |
michael@0 | 253 | They compare two values, and log a |
michael@0 | 254 | <code>FATAL</code> message including the two values when the result is |
michael@0 | 255 | not as expected. The values must have <code>operator<<(ostream, |
michael@0 | 256 | ...)</code> defined. |
michael@0 | 257 | |
michael@0 | 258 | <p>You may append to the error message like so: |
michael@0 | 259 | |
michael@0 | 260 | <pre> |
michael@0 | 261 | CHECK_NE(1, 2) << ": The world must be ending!"; |
michael@0 | 262 | </pre> |
michael@0 | 263 | |
michael@0 | 264 | <p>We are very careful to ensure that each argument is evaluated exactly |
michael@0 | 265 | once, and that anything which is legal to pass as a function argument is |
michael@0 | 266 | legal here. In particular, the arguments may be temporary expressions |
michael@0 | 267 | which will end up being destroyed at the end of the apparent statement, |
michael@0 | 268 | for example: |
michael@0 | 269 | |
michael@0 | 270 | <pre> |
michael@0 | 271 | CHECK_EQ(string("abc")[1], 'b'); |
michael@0 | 272 | </pre> |
michael@0 | 273 | |
michael@0 | 274 | <p>The compiler reports an error if one of the arguments is a |
michael@0 | 275 | pointer and the other is NULL. To work around this, simply static_cast |
michael@0 | 276 | NULL to the type of the desired pointer. |
michael@0 | 277 | |
michael@0 | 278 | <pre> |
michael@0 | 279 | CHECK_EQ(some_ptr, static_cast<SomeType*>(NULL)); |
michael@0 | 280 | </pre> |
michael@0 | 281 | |
michael@0 | 282 | <p>Better yet, use the CHECK_NOTNULL macro: |
michael@0 | 283 | |
michael@0 | 284 | <pre> |
michael@0 | 285 | CHECK_NOTNULL(some_ptr); |
michael@0 | 286 | some_ptr->DoSomething(); |
michael@0 | 287 | </pre> |
michael@0 | 288 | |
michael@0 | 289 | <p>Since this macro returns the given pointer, this is very useful in |
michael@0 | 290 | constructor initializer lists. |
michael@0 | 291 | |
michael@0 | 292 | <pre> |
michael@0 | 293 | struct S { |
michael@0 | 294 | S(Something* ptr) : ptr_(CHECK_NOTNULL(ptr)) {} |
michael@0 | 295 | Something* ptr_; |
michael@0 | 296 | }; |
michael@0 | 297 | </pre> |
michael@0 | 298 | |
michael@0 | 299 | <p>Note that you cannot use this macro as a C++ stream due to this |
michael@0 | 300 | feature. Please use <code>CHECK_EQ</code> described above to log a |
michael@0 | 301 | custom message before aborting the application. |
michael@0 | 302 | |
michael@0 | 303 | <p>If you are comparing C strings (char *), a handy set of macros |
michael@0 | 304 | performs case sensitive as well as case insensitive comparisons - |
michael@0 | 305 | <code>CHECK_STREQ</code>, <code>CHECK_STRNE</code>, |
michael@0 | 306 | <code>CHECK_STRCASEEQ</code>, and <code>CHECK_STRCASENE</code>. The |
michael@0 | 307 | CASE versions are case-insensitive. You can safely pass <code>NULL</code> |
michael@0 | 308 | pointers for this macro. They treat <code>NULL</code> and any |
michael@0 | 309 | non-<code>NULL</code> string as not equal. Two <code>NULL</code>s are |
michael@0 | 310 | equal. |
michael@0 | 311 | |
michael@0 | 312 | <p>Note that both arguments may be temporary strings which are |
michael@0 | 313 | destructed at the end of the current "full expression" |
michael@0 | 314 | (e.g., <code>CHECK_STREQ(Foo().c_str(), Bar().c_str())</code> where |
michael@0 | 315 | <code>Foo</code> and <code>Bar</code> return C++'s |
michael@0 | 316 | <code>std::string</code>). |
michael@0 | 317 | |
michael@0 | 318 | <p>The <code>CHECK_DOUBLE_EQ</code> macro checks the equality of two |
michael@0 | 319 | floating point values, accepting a small error margin. |
michael@0 | 320 | <code>CHECK_NEAR</code> accepts a third floating point argument, which |
michael@0 | 321 | specifies the acceptable error margin. |
michael@0 | 322 | |
michael@0 | 323 | <h2><A NAME=verbose>Verbose Logging</A></h2> |
michael@0 | 324 | |
michael@0 | 325 | <p>When you are chasing difficult bugs, thorough log messages are very |
michael@0 | 326 | useful. However, you may want to ignore too verbose messages in usual |
michael@0 | 327 | development. For such verbose logging, glog provides the |
michael@0 | 328 | <code>VLOG</code> macro, which allows you to define your own numeric |
michael@0 | 329 | logging levels. The <code>--v</code> command line option controls |
michael@0 | 330 | which verbose messages are logged: |
michael@0 | 331 | |
michael@0 | 332 | <pre> |
michael@0 | 333 | VLOG(1) << "I'm printed when you run the program with --v=1 or higher"; |
michael@0 | 334 | VLOG(2) << "I'm printed when you run the program with --v=2 or higher"; |
michael@0 | 335 | </pre> |
michael@0 | 336 | |
michael@0 | 337 | <p>With <code>VLOG</code>, the lower the verbose level, the more |
michael@0 | 338 | likely messages are to be logged. For example, if |
michael@0 | 339 | <code>--v==1</code>, <code>VLOG(1)</code> will log, but |
michael@0 | 340 | <code>VLOG(2)</code> will not log. This is opposite of the severity |
michael@0 | 341 | level, where <code>INFO</code> is 0, and <code>ERROR</code> is 2. |
michael@0 | 342 | <code>--minloglevel</code> of 1 will log <code>WARNING</code> and |
michael@0 | 343 | above. Though you can specify any integers for both <code>VLOG</code> |
michael@0 | 344 | macro and <code>--v</code> flag, the common values for them are small |
michael@0 | 345 | positive integers. For example, if you write <code>VLOG(0)</code>, |
michael@0 | 346 | you should specify <code>--v=-1</code> or lower to silence it. This |
michael@0 | 347 | is less useful since we may not want verbose logs by default in most |
michael@0 | 348 | cases. The <code>VLOG</code> macros always log at the |
michael@0 | 349 | <code>INFO</code> log level (when they log at all). |
michael@0 | 350 | |
michael@0 | 351 | <p>Verbose logging can be controlled from the command line on a |
michael@0 | 352 | per-module basis: |
michael@0 | 353 | |
michael@0 | 354 | <pre> |
michael@0 | 355 | --vmodule=mapreduce=2,file=1,gfs*=3 --v=0 |
michael@0 | 356 | </pre> |
michael@0 | 357 | |
michael@0 | 358 | <p>will: |
michael@0 | 359 | |
michael@0 | 360 | <ul> |
michael@0 | 361 | <li>a. Print VLOG(2) and lower messages from mapreduce.{h,cc} |
michael@0 | 362 | <li>b. Print VLOG(1) and lower messages from file.{h,cc} |
michael@0 | 363 | <li>c. Print VLOG(3) and lower messages from files prefixed with "gfs" |
michael@0 | 364 | <li>d. Print VLOG(0) and lower messages from elsewhere |
michael@0 | 365 | </ul> |
michael@0 | 366 | |
michael@0 | 367 | <p>The wildcarding functionality shown by (c) supports both '*' |
michael@0 | 368 | (matches 0 or more characters) and '?' (matches any single character) |
michael@0 | 369 | wildcards. Please also check the section about <a |
michael@0 | 370 | href="#flags">command line flags</a>. |
michael@0 | 371 | |
michael@0 | 372 | <p>There's also <code>VLOG_IS_ON(n)</code> "verbose level" condition |
michael@0 | 373 | macro. This macro returns true when the <code>--v</code> is equal or |
michael@0 | 374 | greater than <code>n</code>. To be used as |
michael@0 | 375 | |
michael@0 | 376 | <pre> |
michael@0 | 377 | if (VLOG_IS_ON(2)) { |
michael@0 | 378 | // do some logging preparation and logging |
michael@0 | 379 | // that can't be accomplished with just VLOG(2) << ...; |
michael@0 | 380 | } |
michael@0 | 381 | </pre> |
michael@0 | 382 | |
michael@0 | 383 | <p>Verbose level condition macros <code>VLOG_IF</code>, |
michael@0 | 384 | <code>VLOG_EVERY_N</code> and <code>VLOG_IF_EVERY_N</code> behave |
michael@0 | 385 | analogous to <code>LOG_IF</code>, <code>LOG_EVERY_N</code>, |
michael@0 | 386 | <code>LOF_IF_EVERY</code>, but accept a numeric verbosity level as |
michael@0 | 387 | opposed to a severity level. |
michael@0 | 388 | |
michael@0 | 389 | <pre> |
michael@0 | 390 | VLOG_IF(1, (size > 1024)) |
michael@0 | 391 | << "I'm printed when size is more than 1024 and when you run the " |
michael@0 | 392 | "program with --v=1 or more"; |
michael@0 | 393 | VLOG_EVERY_N(1, 10) |
michael@0 | 394 | << "I'm printed every 10th occurrence, and when you run the program " |
michael@0 | 395 | "with --v=1 or more. Present occurence is " << google::COUNTER; |
michael@0 | 396 | VLOG_IF_EVERY_N(1, (size > 1024), 10) |
michael@0 | 397 | << "I'm printed on every 10th occurence of case when size is more " |
michael@0 | 398 | " than 1024, when you run the program with --v=1 or more. "; |
michael@0 | 399 | "Present occurence is " << google::COUNTER; |
michael@0 | 400 | </pre> |
michael@0 | 401 | |
michael@0 | 402 | <h2> <A name="signal">Failure Signal Handler</A> </h2> |
michael@0 | 403 | |
michael@0 | 404 | <p> |
michael@0 | 405 | The library provides a convenient signal handler that will dump useful |
michael@0 | 406 | information when the program crashes on certain signals such as SIGSEGV. |
michael@0 | 407 | The signal handler can be installed by |
michael@0 | 408 | google::InstallFailureSignalHandler(). The following is an example of output |
michael@0 | 409 | from the signal handler. |
michael@0 | 410 | |
michael@0 | 411 | <pre> |
michael@0 | 412 | *** Aborted at 1225095260 (unix time) try "date -d @1225095260" if you are using GNU date *** |
michael@0 | 413 | *** SIGSEGV (@0x0) received by PID 17711 (TID 0x7f893090a6f0) from PID 0; stack trace: *** |
michael@0 | 414 | PC: @ 0x412eb1 TestWaitingLogSink::send() |
michael@0 | 415 | @ 0x7f892fb417d0 (unknown) |
michael@0 | 416 | @ 0x412eb1 TestWaitingLogSink::send() |
michael@0 | 417 | @ 0x7f89304f7f06 google::LogMessage::SendToLog() |
michael@0 | 418 | @ 0x7f89304f35af google::LogMessage::Flush() |
michael@0 | 419 | @ 0x7f89304f3739 google::LogMessage::~LogMessage() |
michael@0 | 420 | @ 0x408cf4 TestLogSinkWaitTillSent() |
michael@0 | 421 | @ 0x4115de main |
michael@0 | 422 | @ 0x7f892f7ef1c4 (unknown) |
michael@0 | 423 | @ 0x4046f9 (unknown) |
michael@0 | 424 | </pre> |
michael@0 | 425 | |
michael@0 | 426 | <p> |
michael@0 | 427 | By default, the signal handler writes the failure dump to the standard |
michael@0 | 428 | error. You can customize the destination by InstallFailureWriter(). |
michael@0 | 429 | |
michael@0 | 430 | <h2> <A name="misc">Miscellaneous Notes</A> </h2> |
michael@0 | 431 | |
michael@0 | 432 | <h3><A NAME=message>Performance of Messages</A></h3> |
michael@0 | 433 | |
michael@0 | 434 | <p>The conditional logging macros provided by glog (e.g., |
michael@0 | 435 | <code>CHECK</code>, <code>LOG_IF</code>, <code>VLOG</code>, ...) are |
michael@0 | 436 | carefully implemented and don't execute the right hand side |
michael@0 | 437 | expressions when the conditions are false. So, the following check |
michael@0 | 438 | may not sacrifice the performance of your application. |
michael@0 | 439 | |
michael@0 | 440 | <pre> |
michael@0 | 441 | CHECK(obj.ok) << obj.CreatePrettyFormattedStringButVerySlow(); |
michael@0 | 442 | </pre> |
michael@0 | 443 | |
michael@0 | 444 | <h3><A NAME=failure>User-defined Failure Function</A></h3> |
michael@0 | 445 | |
michael@0 | 446 | <p><code>FATAL</code> severity level messages or unsatisfied |
michael@0 | 447 | <code>CHECK</code> condition terminate your program. You can change |
michael@0 | 448 | the behavior of the termination by |
michael@0 | 449 | <code>InstallFailureFunction</code>. |
michael@0 | 450 | |
michael@0 | 451 | <pre> |
michael@0 | 452 | void YourFailureFunction() { |
michael@0 | 453 | // Reports something... |
michael@0 | 454 | exit(1); |
michael@0 | 455 | } |
michael@0 | 456 | |
michael@0 | 457 | int main(int argc, char* argv[]) { |
michael@0 | 458 | google::InstallFailureFunction(&YourFailureFunction); |
michael@0 | 459 | } |
michael@0 | 460 | </pre> |
michael@0 | 461 | |
michael@0 | 462 | <p>By default, glog tries to dump stacktrace and makes the program |
michael@0 | 463 | exit with status 1. The stacktrace is produced only when you run the |
michael@0 | 464 | program on an architecture for which glog supports stack tracing (as |
michael@0 | 465 | of September 2008, glog supports stack tracing for x86 and x86_64). |
michael@0 | 466 | |
michael@0 | 467 | <h3><A NAME=raw>Raw Logging</A></h3> |
michael@0 | 468 | |
michael@0 | 469 | <p>The header file <code><glog/raw_logging.h></code> can be |
michael@0 | 470 | used for thread-safe logging, which does not allocate any memory or |
michael@0 | 471 | acquire any locks. Therefore, the macros defined in this |
michael@0 | 472 | header file can be used by low-level memory allocation and |
michael@0 | 473 | synchronization code. |
michael@0 | 474 | Please check <code>src/glog/raw_logging.h.in</code> for detail. |
michael@0 | 475 | </p> |
michael@0 | 476 | |
michael@0 | 477 | <h3><A NAME=plog>Google Style perror()</A></h3> |
michael@0 | 478 | |
michael@0 | 479 | <p><code>PLOG()</code> and <code>PLOG_IF()</code> and |
michael@0 | 480 | <code>PCHECK()</code> behave exactly like their <code>LOG*</code> and |
michael@0 | 481 | <code>CHECK</code> equivalents with the addition that they append a |
michael@0 | 482 | description of the current state of errno to their output lines. |
michael@0 | 483 | E.g. |
michael@0 | 484 | |
michael@0 | 485 | <pre> |
michael@0 | 486 | PCHECK(write(1, NULL, 2) >= 0) << "Write NULL failed"; |
michael@0 | 487 | </pre> |
michael@0 | 488 | |
michael@0 | 489 | <p>This check fails with the following error message. |
michael@0 | 490 | |
michael@0 | 491 | <pre> |
michael@0 | 492 | F0825 185142 test.cc:22] Check failed: write(1, NULL, 2) >= 0 Write NULL failed: Bad address [14] |
michael@0 | 493 | </pre> |
michael@0 | 494 | |
michael@0 | 495 | <h3><A NAME=syslog>Syslog</A></h3> |
michael@0 | 496 | |
michael@0 | 497 | <p><code>SYSLOG</code>, <code>SYSLOG_IF</code>, and |
michael@0 | 498 | <code>SYSLOG_EVERY_N</code> macros are available. |
michael@0 | 499 | These log to syslog in addition to the normal logs. Be aware that |
michael@0 | 500 | logging to syslog can drastically impact performance, especially if |
michael@0 | 501 | syslog is configured for remote logging! Make sure you understand the |
michael@0 | 502 | implications of outputting to syslog before you use these macros. In |
michael@0 | 503 | general, it's wise to use these macros sparingly. |
michael@0 | 504 | |
michael@0 | 505 | <h3><A NAME=strip>Strip Logging Messages</A></h3> |
michael@0 | 506 | |
michael@0 | 507 | <p>Strings used in log messages can increase the size of your binary |
michael@0 | 508 | and present a privacy concern. You can therefore instruct glog to |
michael@0 | 509 | remove all strings which fall below a certain severity level by using |
michael@0 | 510 | the GOOGLE_STRIP_LOG macro: |
michael@0 | 511 | |
michael@0 | 512 | <p>If your application has code like this: |
michael@0 | 513 | |
michael@0 | 514 | <pre> |
michael@0 | 515 | #define GOOGLE_STRIP_LOG 1 // this must go before the #include! |
michael@0 | 516 | #include <glog/logging.h> |
michael@0 | 517 | </pre> |
michael@0 | 518 | |
michael@0 | 519 | <p>The compiler will remove the log messages whose severities are less |
michael@0 | 520 | than the specified integer value. Since |
michael@0 | 521 | <code>VLOG</code> logs at the severity level <code>INFO</code> |
michael@0 | 522 | (numeric value <code>0</code>), |
michael@0 | 523 | setting <code>GOOGLE_STRIP_LOG</code> to 1 or greater removes |
michael@0 | 524 | all log messages associated with <code>VLOG</code>s as well as |
michael@0 | 525 | <code>INFO</code> log statements. |
michael@0 | 526 | |
michael@0 | 527 | <h3><A NAME=windows>Notes for Windows users</A></h3> |
michael@0 | 528 | |
michael@0 | 529 | <p>Google glog defines a severity level <code>ERROR</code>, which is |
michael@0 | 530 | also defined in <code>windows.h</code> |
michael@0 | 531 | There are two known workarounds to avoid this conflict: |
michael@0 | 532 | |
michael@0 | 533 | <ul> |
michael@0 | 534 | <li>#define <code>WIN32_LEAN_AND_MEAN</code> or <code>NOGDI</code> |
michael@0 | 535 | <strong>before</strong> you #include <code>windows.h</code> . |
michael@0 | 536 | <li>#undef <code>ERROR</code> <strong>after</strong> you #include |
michael@0 | 537 | <code>windows.h</code> . |
michael@0 | 538 | </ul> |
michael@0 | 539 | |
michael@0 | 540 | <p>See <a href="http://code.google.com/p/google-glog/issues/detail?id=33"> |
michael@0 | 541 | this issue</a> for more detail. |
michael@0 | 542 | |
michael@0 | 543 | <hr> |
michael@0 | 544 | <address> |
michael@0 | 545 | Shinichiro Hamaji<br> |
michael@0 | 546 | Gregor Hohpe<br> |
michael@0 | 547 | <script type=text/javascript> |
michael@0 | 548 | var lm = new Date(document.lastModified); |
michael@0 | 549 | document.write(lm.toDateString()); |
michael@0 | 550 | </script> |
michael@0 | 551 | </address> |
michael@0 | 552 | |
michael@0 | 553 | </body> |
michael@0 | 554 | </html> |