|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
<html>
|
|
Packit Service |
9def5d |
<head>
|
|
Packit Service |
9def5d |
<title>How To Use Google Logging Library (glog)</title>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
rel="shortcut icon">
|
|
Packit Service |
9def5d |
<link href="designstyle.css" type="text/css" rel="stylesheet">
|
|
Packit Service |
9def5d |
<style type="text/css">
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
ol.bluelist li {
|
|
Packit Service |
9def5d |
color: #3366ff;
|
|
Packit Service |
9def5d |
font-family: sans-serif;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
ol.bluelist li p {
|
|
Packit Service |
9def5d |
color: #000;
|
|
Packit Service |
9def5d |
font-family: "Times Roman", times, serif;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
ul.blacklist li {
|
|
Packit Service |
9def5d |
color: #000;
|
|
Packit Service |
9def5d |
font-family: "Times Roman", times, serif;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
//-->
|
|
Packit Service |
9def5d |
</style>
|
|
Packit Service |
9def5d |
</head>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
<body>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
How To Use Google Logging Library (glog)
|
|
Packit Service |
9def5d |
<small>(as of
|
|
Packit Service |
9def5d |
<script type=text/javascript>
|
|
Packit Service |
9def5d |
var lm = new Date(document.lastModified);
|
|
Packit Service |
9def5d |
document.write(lm.toDateString());
|
|
Packit Service |
9def5d |
</script>)
|
|
Packit Service |
9def5d |
</small>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Google glog is a library that implements application-level
|
|
Packit Service |
9def5d |
logging. This library provides logging APIs based on C++-style
|
|
Packit Service |
9def5d |
streams and various helper macros.
|
|
Packit Service |
9def5d |
You can log a message by simply streaming things to LOG(<a
|
|
Packit Service |
9def5d |
particular severity level>), e.g.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#include <glog/logging.h>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
int main(int argc, char* argv[]) {
|
|
Packit Service |
9def5d |
// Initialize Google's logging library.
|
|
Packit Service |
9def5d |
google::InitGoogleLogging(argv[0]);
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// ...
|
|
Packit Service |
9def5d |
LOG(INFO) << "Found " << num_cookies << " cookies";
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Google glog defines a series of macros that simplify many common logging
|
|
Packit Service |
9def5d |
tasks. You can log messages by severity level, control logging
|
|
Packit Service |
9def5d |
behavior from the command line, log based on conditionals, abort the
|
|
Packit Service |
9def5d |
program when expected conditions are not met, introduce your own
|
|
Packit Service |
9def5d |
verbose logging levels, and more. This document describes the
|
|
Packit Service |
9def5d |
functionality supported by glog. Please note that this document
|
|
Packit Service |
9def5d |
doesn't describe all features in this library, but the most useful
|
|
Packit Service |
9def5d |
ones. If you want to find less common features, please check
|
|
Packit Service |
9def5d |
header files under src/glog directory.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
You can specify one of the following severity levels (in
|
|
Packit Service |
9def5d |
increasing order of severity): INFO , WARNING ,
|
|
Packit Service |
9def5d |
ERROR , and FATAL .
|
|
Packit Service |
9def5d |
Logging a FATAL message terminates the program (after the
|
|
Packit Service |
9def5d |
message is logged).
|
|
Packit Service |
9def5d |
Note that messages of a given severity are logged not only in the
|
|
Packit Service |
9def5d |
logfile for that severity, but also in all logfiles of lower severity.
|
|
Packit Service |
9def5d |
E.g., a message of severity FATAL will be logged to the
|
|
Packit Service |
9def5d |
logfiles of severity FATAL , ERROR ,
|
|
Packit Service |
9def5d |
WARNING , and INFO .
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
The DFATAL severity logs a FATAL error in
|
|
Packit Service |
9def5d |
debug mode (i.e., there is no NDEBUG macro defined), but
|
|
Packit Service |
9def5d |
avoids halting the program in production by automatically reducing the
|
|
Packit Service |
9def5d |
severity to ERROR .
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Unless otherwise specified, glog writes to the filename
|
|
Packit Service |
9def5d |
"/tmp/<program name>.<hostname>.<user name>.log.<severity level>.<date>.<time>.<pid>"
|
|
Packit Service |
9def5d |
(e.g., "/tmp/hello_world.example.com.hamaji.log.INFO.20080709-222411.10474").
|
|
Packit Service |
9def5d |
By default, glog copies the log messages of severity level
|
|
Packit Service |
9def5d |
ERROR or FATAL to standard error (stderr)
|
|
Packit Service |
9def5d |
in addition to log files.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Several flags influence glog's output behavior.
|
|
Packit Service |
9def5d |
If the Google
|
|
Packit Service |
9def5d |
gflags library is installed on your machine, the
|
|
Packit Service |
9def5d |
configure script (see the INSTALL file in the package for
|
|
Packit Service |
9def5d |
detail of this script) will automatically detect and use it,
|
|
Packit Service |
9def5d |
allowing you to pass flags on the command line. For example, if you
|
|
Packit Service |
9def5d |
want to turn the flag --logtostderr on, you can start
|
|
Packit Service |
9def5d |
your application with the following command line:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
./your_application --logtostderr=1
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
If the Google gflags library isn't installed, you set flags via
|
|
Packit Service |
9def5d |
environment variables, prefixing the flag name with "GLOG_", e.g.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
GLOG_logtostderr=1 ./your_application
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
By glog version 0.x.x, you can use GLOG_* environment variables
|
|
Packit Service |
9def5d |
even if you have gflags. If both an environment variable and a flag
|
|
Packit Service |
9def5d |
are specified, the value specified by a flag wins. E.g., if GLOG_v=0
|
|
Packit Service |
9def5d |
and --v=1, the verbosity will be 1, not 0.
|
|
Packit Service |
9def5d |
-->
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
The following flags are most commonly used:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
logtostderr (bool , default=false )
|
|
Packit Service |
9def5d |
Log messages to stderr instead of logfiles.
|
|
Packit Service |
9def5d |
Note: you can set binary flags to true by specifying
|
|
Packit Service |
9def5d |
1 , true , or yes (case
|
|
Packit Service |
9def5d |
insensitive).
|
|
Packit Service |
9def5d |
Also, you can set binary flags to false by specifying
|
|
Packit Service |
9def5d |
0 , false , or no (again, case
|
|
Packit Service |
9def5d |
insensitive).
|
|
Packit Service |
9def5d |
stderrthreshold (int , default=2, which
|
|
Packit Service |
9def5d |
is ERROR )
|
|
Packit Service |
9def5d |
Copy log messages at or above this level to stderr in
|
|
Packit Service |
9def5d |
addition to logfiles. The numbers of severity levels
|
|
Packit Service |
9def5d |
INFO , WARNING , ERROR , and
|
|
Packit Service |
9def5d |
FATAL are 0, 1, 2, and 3, respectively.
|
|
Packit Service |
9def5d |
minloglevel (int , default=0, which
|
|
Packit Service |
9def5d |
is INFO )
|
|
Packit Service |
9def5d |
Log messages at or above this level. Again, the numbers of
|
|
Packit Service |
9def5d |
severity levels INFO , WARNING ,
|
|
Packit Service |
9def5d |
ERROR , and FATAL are 0, 1, 2, and 3,
|
|
Packit Service |
9def5d |
respectively.
|
|
Packit Service |
9def5d |
log_dir (string , default="")
|
|
Packit Service |
9def5d |
If specified, logfiles are written into this directory instead
|
|
Packit Service |
9def5d |
of the default logging directory.
|
|
Packit Service |
9def5d |
v (int , default=0)
|
|
Packit Service |
9def5d |
Show all VLOG(m) messages for m less or
|
|
Packit Service |
9def5d |
equal the value of this flag. Overridable by --vmodule.
|
|
Packit Service |
9def5d |
See the section about verbose logging for more
|
|
Packit Service |
9def5d |
detail.
|
|
Packit Service |
9def5d |
vmodule (string , default="")
|
|
Packit Service |
9def5d |
Per-module verbose level. The argument has to contain a
|
|
Packit Service |
9def5d |
comma-separated list of <module name>=<log level>.
|
|
Packit Service |
9def5d |
<module name>
|
|
Packit Service |
9def5d |
is a glob pattern (e.g., gfs* for all modules whose name
|
|
Packit Service |
9def5d |
starts with "gfs"), matched against the filename base
|
|
Packit Service |
9def5d |
(that is, name ignoring .cc/.h./-inl.h).
|
|
Packit Service |
9def5d |
<log level> overrides any value given by --v.
|
|
Packit Service |
9def5d |
See also the section about verbose logging.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
There are some other flags defined in logging.cc. Please grep the
|
|
Packit Service |
9def5d |
source code for "DEFINE_" to see a complete list of all flags.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
You can also modify flag values in your program by modifying global
|
|
Packit Service |
9def5d |
variables FLAGS_* . Most settings start working
|
|
Packit Service |
9def5d |
immediately after you update FLAGS_* . The exceptions are
|
|
Packit Service |
9def5d |
the flags related to destination files. For example, you might want to
|
|
Packit Service |
9def5d |
set FLAGS_log_dir before
|
|
Packit Service |
9def5d |
calling google::InitGoogleLogging . Here is an example:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
LOG(INFO) << "file";
|
|
Packit Service |
9def5d |
// Most flags work immediately after updating values.
|
|
Packit Service |
9def5d |
FLAGS_logtostderr = 1;
|
|
Packit Service |
9def5d |
LOG(INFO) << "stderr";
|
|
Packit Service |
9def5d |
FLAGS_logtostderr = 0;
|
|
Packit Service |
9def5d |
// This won't change the log destination. If you want to set this
|
|
Packit Service |
9def5d |
// value, you should do this before google::InitGoogleLogging .
|
|
Packit Service |
9def5d |
FLAGS_log_dir = "/some/log/directory";
|
|
Packit Service |
9def5d |
LOG(INFO) << "the same file";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Sometimes, you may only want to log a message under certain
|
|
Packit Service |
9def5d |
conditions. You can use the following macros to perform conditional
|
|
Packit Service |
9def5d |
logging:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
LOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
The "Got lots of cookies" message is logged only when the variable
|
|
Packit Service |
9def5d |
num_cookies exceeds 10.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
If a line of code is executed many times, it may be useful to only log
|
|
Packit Service |
9def5d |
a message at certain intervals. This kind of logging is most useful
|
|
Packit Service |
9def5d |
for informational messages.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
LOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
The above line outputs a log messages on the 1st, 11th,
|
|
Packit Service |
9def5d |
21st, ... times it is executed. Note that the special
|
|
Packit Service |
9def5d |
google::COUNTER value is used to identify which repetition is
|
|
Packit Service |
9def5d |
happening.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
You can combine conditional and occasional logging with the
|
|
Packit Service |
9def5d |
following macro.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
LOG_IF_EVERY_N(INFO, (size > 1024), 10) << "Got the " << google::COUNTER
|
|
Packit Service |
9def5d |
<< "th big cookie";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Instead of outputting a message every nth time, you can also limit
|
|
Packit Service |
9def5d |
the output to the first n occurrences:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
LOG_FIRST_N(INFO, 20) << "Got the " << google::COUNTER << "th cookie";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Outputs log messages for the first 20 times it is executed. Again,
|
|
Packit Service |
9def5d |
the google::COUNTER identifier indicates which repetition is
|
|
Packit Service |
9def5d |
happening.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Special "debug mode" logging macros only have an effect in debug
|
|
Packit Service |
9def5d |
mode and are compiled away to nothing for non-debug mode
|
|
Packit Service |
9def5d |
compiles. Use these macros to avoid slowing down your production
|
|
Packit Service |
9def5d |
application due to excessive logging.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
DLOG(INFO) << "Found cookies";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
DLOG_IF(INFO, num_cookies > 10) << "Got lots of cookies";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
DLOG_EVERY_N(INFO, 10) << "Got the " << google::COUNTER << "th cookie";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
It is a good practice to check expected conditions in your program
|
|
Packit Service |
9def5d |
frequently to detect errors as early as possible. The
|
|
Packit Service |
9def5d |
CHECK macro provides the ability to abort the application
|
|
Packit Service |
9def5d |
when a condition is not met, similar to the assert macro
|
|
Packit Service |
9def5d |
defined in the standard C library.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
CHECK aborts the application if a condition is not
|
|
Packit Service |
9def5d |
true. Unlike assert , it is *not* controlled by
|
|
Packit Service |
9def5d |
NDEBUG , so the check will be executed regardless of
|
|
Packit Service |
9def5d |
compilation mode. Therefore, fp->Write(x) in the
|
|
Packit Service |
9def5d |
following example is always executed:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
CHECK(fp->Write(x) == 4) << "Write failed!";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
There are various helper macros for
|
|
Packit Service |
9def5d |
equality/inequality checks - CHECK_EQ ,
|
|
Packit Service |
9def5d |
CHECK_NE , CHECK_LE , CHECK_LT ,
|
|
Packit Service |
9def5d |
CHECK_GE , and CHECK_GT .
|
|
Packit Service |
9def5d |
They compare two values, and log a
|
|
Packit Service |
9def5d |
FATAL message including the two values when the result is
|
|
Packit Service |
9def5d |
not as expected. The values must have operator<<(ostream,
|
|
Packit Service |
9def5d |
...) defined.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
You may append to the error message like so:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
CHECK_NE(1, 2) << ": The world must be ending!";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
We are very careful to ensure that each argument is evaluated exactly
|
|
Packit Service |
9def5d |
once, and that anything which is legal to pass as a function argument is
|
|
Packit Service |
9def5d |
legal here. In particular, the arguments may be temporary expressions
|
|
Packit Service |
9def5d |
which will end up being destroyed at the end of the apparent statement,
|
|
Packit Service |
9def5d |
for example:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
CHECK_EQ(string("abc")[1], 'b');
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
The compiler reports an error if one of the arguments is a
|
|
Packit Service |
9def5d |
pointer and the other is NULL. To work around this, simply static_cast
|
|
Packit Service |
9def5d |
NULL to the type of the desired pointer.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
CHECK_EQ(some_ptr, static_cast<SomeType*>(NULL));
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Better yet, use the CHECK_NOTNULL macro:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
CHECK_NOTNULL(some_ptr);
|
|
Packit Service |
9def5d |
some_ptr->DoSomething();
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Since this macro returns the given pointer, this is very useful in
|
|
Packit Service |
9def5d |
constructor initializer lists.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
struct S {
|
|
Packit Service |
9def5d |
S(Something* ptr) : ptr_(CHECK_NOTNULL(ptr)) {}
|
|
Packit Service |
9def5d |
Something* ptr_;
|
|
Packit Service |
9def5d |
};
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Note that you cannot use this macro as a C++ stream due to this
|
|
Packit Service |
9def5d |
feature. Please use CHECK_EQ described above to log a
|
|
Packit Service |
9def5d |
custom message before aborting the application.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
If you are comparing C strings (char *), a handy set of macros
|
|
Packit Service |
9def5d |
performs case sensitive as well as case insensitive comparisons -
|
|
Packit Service |
9def5d |
CHECK_STREQ , CHECK_STRNE ,
|
|
Packit Service |
9def5d |
CHECK_STRCASEEQ , and CHECK_STRCASENE . The
|
|
Packit Service |
9def5d |
CASE versions are case-insensitive. You can safely pass NULL
|
|
Packit Service |
9def5d |
pointers for this macro. They treat NULL and any
|
|
Packit Service |
9def5d |
non-NULL string as not equal. Two NULL s are
|
|
Packit Service |
9def5d |
equal.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Note that both arguments may be temporary strings which are
|
|
Packit Service |
9def5d |
destructed at the end of the current "full expression"
|
|
Packit Service |
9def5d |
(e.g., CHECK_STREQ(Foo().c_str(), Bar().c_str()) where
|
|
Packit Service |
9def5d |
Foo and Bar return C++'s
|
|
Packit Service |
9def5d |
std::string ).
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
The CHECK_DOUBLE_EQ macro checks the equality of two
|
|
Packit Service |
9def5d |
floating point values, accepting a small error margin.
|
|
Packit Service |
9def5d |
CHECK_NEAR accepts a third floating point argument, which
|
|
Packit Service |
9def5d |
specifies the acceptable error margin.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
When you are chasing difficult bugs, thorough log messages are very
|
|
Packit Service |
9def5d |
useful. However, you may want to ignore too verbose messages in usual
|
|
Packit Service |
9def5d |
development. For such verbose logging, glog provides the
|
|
Packit Service |
9def5d |
VLOG macro, which allows you to define your own numeric
|
|
Packit Service |
9def5d |
logging levels. The --v command line option controls
|
|
Packit Service |
9def5d |
which verbose messages are logged:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
VLOG(1) << "I'm printed when you run the program with --v=1 or higher";
|
|
Packit Service |
9def5d |
VLOG(2) << "I'm printed when you run the program with --v=2 or higher";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
With VLOG , the lower the verbose level, the more
|
|
Packit Service |
9def5d |
likely messages are to be logged. For example, if
|
|
Packit Service |
9def5d |
--v==1 , VLOG(1) will log, but
|
|
Packit Service |
9def5d |
VLOG(2) will not log. This is opposite of the severity
|
|
Packit Service |
9def5d |
level, where INFO is 0, and ERROR is 2.
|
|
Packit Service |
9def5d |
--minloglevel of 1 will log WARNING and
|
|
Packit Service |
9def5d |
above. Though you can specify any integers for both VLOG
|
|
Packit Service |
9def5d |
macro and --v flag, the common values for them are small
|
|
Packit Service |
9def5d |
positive integers. For example, if you write VLOG(0) ,
|
|
Packit Service |
9def5d |
you should specify --v=-1 or lower to silence it. This
|
|
Packit Service |
9def5d |
is less useful since we may not want verbose logs by default in most
|
|
Packit Service |
9def5d |
cases. The VLOG macros always log at the
|
|
Packit Service |
9def5d |
INFO log level (when they log at all).
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Verbose logging can be controlled from the command line on a
|
|
Packit Service |
9def5d |
per-module basis:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
--vmodule=mapreduce=2,file=1,gfs*=3 --v=0
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
will:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
a. Print VLOG(2) and lower messages from mapreduce.{h,cc}
|
|
Packit Service |
9def5d |
b. Print VLOG(1) and lower messages from file.{h,cc}
|
|
Packit Service |
9def5d |
c. Print VLOG(3) and lower messages from files prefixed with "gfs"
|
|
Packit Service |
9def5d |
d. Print VLOG(0) and lower messages from elsewhere
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
The wildcarding functionality shown by (c) supports both '*'
|
|
Packit Service |
9def5d |
(matches 0 or more characters) and '?' (matches any single character)
|
|
Packit Service |
9def5d |
wildcards. Please also check the section about
|
|
Packit Service |
9def5d |
href="#flags">command line flags.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
There's also VLOG_IS_ON(n) "verbose level" condition
|
|
Packit Service |
9def5d |
macro. This macro returns true when the --v is equal or
|
|
Packit Service |
9def5d |
greater than n . To be used as
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
if (VLOG_IS_ON(2)) {
|
|
Packit Service |
9def5d |
// do some logging preparation and logging
|
|
Packit Service |
9def5d |
// that can't be accomplished with just VLOG(2) << ...;
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Verbose level condition macros VLOG_IF ,
|
|
Packit Service |
9def5d |
VLOG_EVERY_N and VLOG_IF_EVERY_N behave
|
|
Packit Service |
9def5d |
analogous to LOG_IF , LOG_EVERY_N ,
|
|
Packit Service |
9def5d |
LOF_IF_EVERY , but accept a numeric verbosity level as
|
|
Packit Service |
9def5d |
opposed to a severity level.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
VLOG_IF(1, (size > 1024))
|
|
Packit Service |
9def5d |
<< "I'm printed when size is more than 1024 and when you run the "
|
|
Packit Service |
9def5d |
"program with --v=1 or more";
|
|
Packit Service |
9def5d |
VLOG_EVERY_N(1, 10)
|
|
Packit Service |
9def5d |
<< "I'm printed every 10th occurrence, and when you run the program "
|
|
Packit Service |
9def5d |
"with --v=1 or more. Present occurence is " << google::COUNTER;
|
|
Packit Service |
9def5d |
VLOG_IF_EVERY_N(1, (size > 1024), 10)
|
|
Packit Service |
9def5d |
<< "I'm printed on every 10th occurence of case when size is more "
|
|
Packit Service |
9def5d |
" than 1024, when you run the program with --v=1 or more. ";
|
|
Packit Service |
9def5d |
"Present occurence is " << google::COUNTER;
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
The library provides a convenient signal handler that will dump useful
|
|
Packit Service |
9def5d |
information when the program crashes on certain signals such as SIGSEGV.
|
|
Packit Service |
9def5d |
The signal handler can be installed by
|
|
Packit Service |
9def5d |
google::InstallFailureSignalHandler(). The following is an example of output
|
|
Packit Service |
9def5d |
from the signal handler.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
*** Aborted at 1225095260 (unix time) try "date -d @1225095260" if you are using GNU date ***
|
|
Packit Service |
9def5d |
*** SIGSEGV (@0x0) received by PID 17711 (TID 0x7f893090a6f0) from PID 0; stack trace: ***
|
|
Packit Service |
9def5d |
PC: @ 0x412eb1 TestWaitingLogSink::send()
|
|
Packit Service |
9def5d |
@ 0x7f892fb417d0 (unknown)
|
|
Packit Service |
9def5d |
@ 0x412eb1 TestWaitingLogSink::send()
|
|
Packit Service |
9def5d |
@ 0x7f89304f7f06 google::LogMessage::SendToLog()
|
|
Packit Service |
9def5d |
@ 0x7f89304f35af google::LogMessage::Flush()
|
|
Packit Service |
9def5d |
@ 0x7f89304f3739 google::LogMessage::~LogMessage()
|
|
Packit Service |
9def5d |
@ 0x408cf4 TestLogSinkWaitTillSent()
|
|
Packit Service |
9def5d |
@ 0x4115de main
|
|
Packit Service |
9def5d |
@ 0x7f892f7ef1c4 (unknown)
|
|
Packit Service |
9def5d |
@ 0x4046f9 (unknown)
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
By default, the signal handler writes the failure dump to the standard
|
|
Packit Service |
9def5d |
error. You can customize the destination by InstallFailureWriter().
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
The conditional logging macros provided by glog (e.g.,
|
|
Packit Service |
9def5d |
CHECK , LOG_IF , VLOG , ...) are
|
|
Packit Service |
9def5d |
carefully implemented and don't execute the right hand side
|
|
Packit Service |
9def5d |
expressions when the conditions are false. So, the following check
|
|
Packit Service |
9def5d |
may not sacrifice the performance of your application.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
CHECK(obj.ok) << obj.CreatePrettyFormattedStringButVerySlow();
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
FATAL severity level messages or unsatisfied
|
|
Packit Service |
9def5d |
CHECK condition terminate your program. You can change
|
|
Packit Service |
9def5d |
the behavior of the termination by
|
|
Packit Service |
9def5d |
InstallFailureFunction .
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
void YourFailureFunction() {
|
|
Packit Service |
9def5d |
// Reports something...
|
|
Packit Service |
9def5d |
exit(1);
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
int main(int argc, char* argv[]) {
|
|
Packit Service |
9def5d |
google::InstallFailureFunction(&YourFailureFunction);
|
|
Packit Service |
9def5d |
}
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
By default, glog tries to dump stacktrace and makes the program
|
|
Packit Service |
9def5d |
exit with status 1. The stacktrace is produced only when you run the
|
|
Packit Service |
9def5d |
program on an architecture for which glog supports stack tracing (as
|
|
Packit Service |
9def5d |
of September 2008, glog supports stack tracing for x86 and x86_64).
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
The header file <glog/raw_logging.h> can be
|
|
Packit Service |
9def5d |
used for thread-safe logging, which does not allocate any memory or
|
|
Packit Service |
9def5d |
acquire any locks. Therefore, the macros defined in this
|
|
Packit Service |
9def5d |
header file can be used by low-level memory allocation and
|
|
Packit Service |
9def5d |
synchronization code.
|
|
Packit Service |
9def5d |
Please check src/glog/raw_logging.h.in for detail.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
PLOG() and PLOG_IF() and
|
|
Packit Service |
9def5d |
PCHECK() behave exactly like their LOG* and
|
|
Packit Service |
9def5d |
CHECK equivalents with the addition that they append a
|
|
Packit Service |
9def5d |
description of the current state of errno to their output lines.
|
|
Packit Service |
9def5d |
E.g.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
PCHECK(write(1, NULL, 2) >= 0) << "Write NULL failed";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
This check fails with the following error message.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
F0825 185142 test.cc:22] Check failed: write(1, NULL, 2) >= 0 Write NULL failed: Bad address [14]
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
SYSLOG , SYSLOG_IF , and
|
|
Packit Service |
9def5d |
SYSLOG_EVERY_N macros are available.
|
|
Packit Service |
9def5d |
These log to syslog in addition to the normal logs. Be aware that
|
|
Packit Service |
9def5d |
logging to syslog can drastically impact performance, especially if
|
|
Packit Service |
9def5d |
syslog is configured for remote logging! Make sure you understand the
|
|
Packit Service |
9def5d |
implications of outputting to syslog before you use these macros. In
|
|
Packit Service |
9def5d |
general, it's wise to use these macros sparingly.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Strings used in log messages can increase the size of your binary
|
|
Packit Service |
9def5d |
and present a privacy concern. You can therefore instruct glog to
|
|
Packit Service |
9def5d |
remove all strings which fall below a certain severity level by using
|
|
Packit Service |
9def5d |
the GOOGLE_STRIP_LOG macro:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
If your application has code like this:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#define GOOGLE_STRIP_LOG 1 // this must go before the #include!
|
|
Packit Service |
9def5d |
#include <glog/logging.h>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
The compiler will remove the log messages whose severities are less
|
|
Packit Service |
9def5d |
than the specified integer value. Since
|
|
Packit Service |
9def5d |
VLOG logs at the severity level INFO
|
|
Packit Service |
9def5d |
(numeric value 0 ),
|
|
Packit Service |
9def5d |
setting GOOGLE_STRIP_LOG to 1 or greater removes
|
|
Packit Service |
9def5d |
all log messages associated with VLOG s as well as
|
|
Packit Service |
9def5d |
INFO log statements.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
Google glog defines a severity level ERROR , which is
|
|
Packit Service |
9def5d |
also defined in windows.h . You can make glog not define
|
|
Packit Service |
9def5d |
INFO , WARNING , ERROR ,
|
|
Packit Service |
9def5d |
and FATAL by defining
|
|
Packit Service |
9def5d |
GLOG_NO_ABBREVIATED_SEVERITIES before
|
|
Packit Service |
9def5d |
including glog/logging.h . Even with this macro, you can
|
|
Packit Service |
9def5d |
still use the iostream like logging facilities:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#define GLOG_NO_ABBREVIATED_SEVERITIES
|
|
Packit Service |
9def5d |
#include <windows.h>
|
|
Packit Service |
9def5d |
#include <glog/logging.h>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// ...
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
LOG(ERROR) << "This should work";
|
|
Packit Service |
9def5d |
LOG_IF(ERROR, x > y) << "This should be also OK";
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
However, you cannot
|
|
Packit Service |
9def5d |
use INFO , WARNING , ERROR ,
|
|
Packit Service |
9def5d |
and FATAL anymore for functions defined
|
|
Packit Service |
9def5d |
in glog/logging.h .
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#define GLOG_NO_ABBREVIATED_SEVERITIES
|
|
Packit Service |
9def5d |
#include <windows.h>
|
|
Packit Service |
9def5d |
#include <glog/logging.h>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// ...
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// This won't work.
|
|
Packit Service |
9def5d |
// google::FlushLogFiles(google::ERROR);
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
// Use this instead.
|
|
Packit Service |
9def5d |
google::FlushLogFiles(google::GLOG_ERROR);
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
If you don't need ERROR defined
|
|
Packit Service |
9def5d |
by windows.h , there are a couple of more workarounds
|
|
Packit Service |
9def5d |
which sometimes don't work:
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
#define WIN32_LEAN_AND_MEAN or NOGDI
|
|
Packit Service |
9def5d |
before you #include windows.h .
|
|
Packit Service |
9def5d |
#undef ERROR after you #include
|
|
Packit Service |
9def5d |
windows.h .
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
See
|
|
Packit Service |
9def5d |
this issue for more detail.
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
<address>
|
|
Packit Service |
9def5d |
Shinichiro Hamaji
|
|
Packit Service |
9def5d |
Gregor Hohpe
|
|
Packit Service |
9def5d |
<script type=text/javascript>
|
|
Packit Service |
9def5d |
var lm = new Date(document.lastModified);
|
|
Packit Service |
9def5d |
document.write(lm.toDateString());
|
|
Packit Service |
9def5d |
</script>
|
|
Packit Service |
9def5d |
</address>
|
|
Packit Service |
9def5d |
|
|
Packit Service |
9def5d |
</body>
|
|
Packit Service |
9def5d |
</html>
|