glog compile and use

glog google is a lightweight logging library provided with chromium experience in the development of people will find it in the library and the base library much like a log, in fact, base library logging library is more lightweight than it is. glog used in everyday development is very extensive. Here are its basic use to compile and under.

1. Download

Take the latest version of the code from github pull through git:

git clone https://github.com/google/glog.git

2. Compile

2.1 2.1Ubuntu

Runs directly under the project directory

./autogen.sh && ./configure && make

After compilation will generate libglog.so libglog.a dynamic library and dynamic library in the libs directory, .h header files with the src / glog directory can use.

2.1 Windows

cmake need to compile on windows. First, configure cmake environment, not repeat them here.
In order not to pollute the code, we first create a folder cmake_build perform inside:

cmake ..

After you run cmake command generates glog.sln file, double click to open, it is VS solution. Then direct the compiler is run, it will directly generate static library lib.
To generate dll dynamic library, we need to use cmake-gui, check the "BUILD_SHARED_LIBS" option, so the default is generated glog engineering dll library.
image.png
After compilation will generate static library lib or dll dynamic library Debug or Release directory.
Header files are automatically placed in glog directory (missing log_severity.h):
image.png
Here is a compiled dll libraries: glog.zip

3. glog use

3.1 log level

glog and chromium in the same log level is divided into:

  • INFO(=0)
  • WARNING(=1)
  • ERROR(=2)
  • FATAL(=3)

Direct use:

LOG(INFO) << "-------------123";

Also you can use DLOG in DBUG mode to set the debug log:

DLOG(INFO) << "-------------123";

3.2 Initialization log configuration

  1. initialization

To log output to a file, you need to initialize the log repository:

google::InitGoogleLogging(argv[0]);  //括号内是程序名

The default logging output to: under "C \ Users \ user \ AppData \ Local \ Temp" directory.

  1. Close logging library

With the initialization, that relatively before exiting the program, but also the need for close operation log library:

google::ShutdownGoogleLogging();
  1. Set the log save directory

Note that this directory must already exist, or can not generate a log file:

FLAGS_log_dir = "D:\\Logs";
  1. Several parameters
FLAGS_logtostderr = true;  //设置日志消息是否转到标准输出而不是日志文件
FLAGS_alsologtostderr = true;  //设置日志消息除了日志文件之外是否输出到标准输出
FLAGS_colorlogtostderr = true;  //设置记录到标准输出的颜色消息(如果终端支持)
FLAGS_log_prefix = true;  //设置日志前缀是否应该添加到每行输出
FLAGS_logbufsecs = 0;  //设置可以缓冲日志的最大秒数,0指实时输出
FLAGS_max_log_size = 10;  //设置最大日志文件大小(以MB为单位),超过会对文件进行分割
FLAGS_stop_logging_if_full_disk = true;  //设置是否在磁盘已满时避免日志记录到磁盘
FLAGS_minloglevel = google::GLOG_WARNING;   //设置最小处理日志的级别
  1. Several functions
google::SetLogDestination(google::GLOG_INFO, "log/prefix_");  //设置特定严重级别的日志的输出目录和前缀。第一个参数为日志级别,第二个参数表示输出目录及日志文件名前缀
google::SetLogFilenameExtension("logExtension");  //在日志文件名中级别后添加一个扩展名。适用于所有严重级别
google::SetStderrLogging(google::GLOG_INFO);  //大于指定级别的日志都输出到标准输出

4. Modify glog

4.1 log output format

Accustomed to chromium-style log, not used to log glog, therefore, we can modify the output format glog logs.
First, logging.cc the LogFileObject :: Write function, the log format specifier when creating a log file will be written to change it:

//Write a header message into the log file
    ostringstream file_header_stream;
    file_header_stream.fill('0');
    file_header_stream << "Log file created at: "
                      << 1900+tm_time.tm_year << '/'
                      << setw(2) << 1+tm_time.tm_mon << '/'
                      << setw(2) << tm_time.tm_mday
                      << ' '
                      << setw(2) << tm_time.tm_hour << ':'
                      << setw(2) << tm_time.tm_min << ':'
                      << setw(2) << tm_time.tm_sec << '\n'
                      << "Running on machine: "
                      << LogDestination::hostname() << '\n'
                      << "Log line format: [pid:tid:mmdd/hh:mm:ss.uuuuuu:severity:file(line)] msg" << '\n';

Then, under the prefix modify LogMessage :: Init in each log:

    //stream() << LogSeverityNames[severity][0]
    //         << setw(2) << 1+data_->tm_time_.tm_mon
    //         << setw(2) << data_->tm_time_.tm_mday
    //         << ' '
    //         << setw(2) << data_->tm_time_.tm_hour  << ':'
    //         << setw(2) << data_->tm_time_.tm_min   << ':'
    //         << setw(2) << data_->tm_time_.tm_sec   << "."
    //         << setw(6) << data_->usecs_
    //         << ' '
    //         << setfill(' ') << setw(5)
    //         << static_cast<unsigned int>(GetTID()) << setfill('0')
    //         << ' '
    //         << data_->basename_ << ':' << data_->line_ << "] ";

    stream() << '[' << getpid() << ":" << static_cast<unsigned int>(GetTID()) << ":"
             << std::setfill('0')
             << std::setw(2) << 1 + data_->tm_time_.tm_mon
             << std::setw(2) << data_->tm_time_.tm_mday
             << '/'
             << std::setw(2) << data_->tm_time_.tm_hour
             << std::setw(2) << data_->tm_time_.tm_min
             << std::setw(2) << data_->tm_time_.tm_sec
             << '.'
             << std::setw(6) << data_->usecs_
             << ':'
             << LogSeverityNames[severity] << ":" << data_->basename_ << "(" << data_->line_ << ")] ";

Also modify the format under LogSink :: ToString where:

  //stream << LogSeverityNames[severity][0]
  //       << setw(2) << 1+tm_time->tm_mon
  //       << setw(2) << tm_time->tm_mday
  //       << ' '
  //       << setw(2) << tm_time->tm_hour << ':'
  //       << setw(2) << tm_time->tm_min << ':'
  //       << setw(2) << tm_time->tm_sec << '.'
  //       << setw(6) << usecs
  //       << ' '
  //       << setfill(' ') << setw(5) << GetTID() << setfill('0')
  //       << ' '
  //       << file << ':' << line << "] ";
  stream << '[' << getpid() << ":" << GetTID() << ":"
      << std::setfill('0')
      << std::setw(2) << 1 + tm_time->tm_mon
      << std::setw(2) << tm_time->tm_mday
      << '/'
      << std::setw(2) << tm_time->tm_hour << ':'
      << std::setw(2) << tm_time->tm_min << ':'
      << std::setw(2) << tm_time->tm_sec << '.'
      << '.'
      << std::setw(6) << usecs
      << ':'
      << LogSeverityNames[severity] << ":" << file << "(" << line << ")] ";

Log output modified as follows:
image.png
and chromium consistent style.

4.2 log file name

Glog will default to each level of the log in the log directory will create a log file, and each time you start the application will modify the log file name (because the process name change), even though it can play a role in filtering logs, but it will also resulting in excessive log file. I hope that the various levels are stored in a log file on the line.
Modify this part more, due to the logic of the original glog different levels of logs are processed, and my goal is to unify them into a single file, contrary to its design principle, and therefore the modified many functions will be affected by possible influences. So I encapsulation layer interfaces:

LOG_DLL_DECL int InitLogger(
    const std::string& logName = "",
    const std::string& logPath = "",
    const google::LogSeverity minSeverity = google::GLOG_INFO,
    const unsigned int fileMaxSize = 10);

LOG_DLL_DECL void ReleaseLogger();

Use these two settings interface is no problem.

Code:  https://github.com/243286065/glog.git  (branch: chromium_style)
here is already compiled windows win32 libraries: glog.zip

Example:

#include <iostream>

#include "glog/logger.h"
#define APP_NAME "GlogTest"

int main(int argc, char* argv[])
{
    InitLogger(APP_NAME, "D:\\logs\\", google::GLOG_INFO);

    LOG(INFO) << "-------------123";
    LOG(WARNING) << "-------------123";
    LOG(ERROR) << "-------------123";
    std::cout << "Hello World!\n"; 


    ReleaseLogger();
}

Finally, I feel like this whole, not as directly to the chromium in the base library logging library out with ~ ~ ~

Guess you like

Origin www.cnblogs.com/xl2432/p/11825966.html