Comment on page

GLog

Installation in Ubuntu

sudo apt install libgoogle-glog-dev

Usage in CMake

Google GLog team does provide GlogConfig.cmake file, but it is not adopted by the default apt package available in Ubuntu. You can
  • compile glog from source and enable CMake support, so that config file will be installed; or
  • use default Ubuntu package by sudo apt install libgoogle-glog-dev, and then find a FindGlog.cmake file and add the path to this file to CMAKE_MODULE_PATH variable, such that CMake will use this file to load glog.
After the package is found, use the following variables for header files and libraries.
find_package(Glog REQUIRED) # GlogConfig.cmake NOT available; use FindGlog.cmake
include_directories(${GLOG_INCLUDE_DIRS})
​
add_executable (main src/main.cpp)
target_link_libraries (main ${GLOG_LIBRARIES})

References

Tutorial / User Guide

Google glog defines a series of macros that simplify many common logging tasks. It provides logging APIs based on C++-style streams and various helper macros. You can log messages by severity level, control logging behavior from the command line, log based on conditionals, abort the program when expected conditions are not met, introduce your own verbose logging levels, and more.
You can log a message by simply streaming things to LOG(<severity level>), e.g.,
#include <glog/logging.h>
​
int main(int argc, char* argv[]) {
// Initialize Google's logging library.
google::InitGoogleLogging(argv[0]);
​
// ...
LOG(INFO) << "Found " << num_cookies << " cookies";
}
Unless otherwise specified, glog writes to the filename
  • /tmp/<program_name>.<hostname>.<username>.log.<severity_level>.<date>.<time>.<pid>, or
  • /tmp/test.hteng-nuc.hteng.log.INFO.20210806-211849 for example.
Note that if InitGoogleLogging is not called for initialization, glog will turn into logtostderr mode and this log file will not be generated under /tmp. (corresponding source code)

Severity Level

You can specify one of the following severity levels (in increasing order of severity): INFO, WARNING, ERROR, and FATAL.
  • Logging a FATAL message terminates the program (after the message is logged).
  • Messages of a given severity are logged not only in the logfile for that severity, but also in all logfiles of lower severity. For example, 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.
  • By default, glog copies the log messages of severity level ERROR or FATAL to standard error (stderr) in addition to log files.
See the official User Guide for more tutorials.