google::glog tutorial
foreword
glog
The function is very powerful but also very simple. It is powerful because it can log
make a process colorful, can output exceptions, and can terminate the program; single means that its function is to output logs for testers and users to see. No matter how good it is, it only realizes the function of logging. If you want to standardize your log records, you can continue to look down. If you think the format of the log records doesn’t matter, then I suggest you close the web page and don’t waste time.
1. Installation
download
Open source project, not confidential, download is still very convenient, you can download it from the link provided below, the latest version has 0.4
not been updated for a while, but it does not matter, after all, it is quite perfect:
download link: google::glog
compile
Nextwin
, just use CMake-gui
it, because it is a relatively small library with few dependencies, so it is also very simple, as shown in the figure below, just need to gflag, gtest
remove it. Click Configure
first , if it does not appear , click Generate again , if there is no problem, just open VS to compile, after the compilation is completed, the main files are as follows (testing below , you need to adjust the path of some files according to the following location ):error
win
-glog:
-include:
-logging.h
-glog:
-export.h
-log_severity.h
-raw_logging.h
-stl_logging.h
-vlog_is_on.h
-glog.dll
-glog.lib
Two, use
configuration
Since it is a general open source library, it can be include
directly added to VS
the default path:
···
D:\Program Files (x86)\Microsoft Visual Studio 19\VC\Tools\MSVC\14.28.29333\include
···
minimal routine
Description: Can achieve output to a specific folder
#include <glog/include/logging.h>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
google::InitGoogleLogging(argv[0]);
google::SetLogDestination(google::GLOG_INFO, "./log/"); // 设置日志路径
FLAGS_stop_logging_if_full_disk = true;
FLAGS_colorlogtostderr = true;
FLAGS_max_log_size = 8;
int num = 10;
LOG(INFO) << "FOUND = " << num;
LOG(WARNING) << "WARNIGN";
google::ShutdownGoogleLogging();
return 0;
}
Concern 1 : Init*
It Shutdown
must be paired, otherwise it may cause memory leaks;
Concern 2 : Severity level, which can be divided into:INFO, WARNING, ERROR, FATAL