Glog Installation (reprint)

Source: Internet
Author: User

Foreveryl

Glog Introduction

Glog easy to get started, glog although the configuration parameters in the trouble, but in the small-scale program, because of its simple and flexible, may have advantages.

0, Glog is Google's open-source log system, compared to the LOG4 series of log system, it is more lightweight and flexible, and the function is more perfect. In conjunction with some of the previously seen information, here is a brief introduction to it.

1, Installation:

Latest Version: 0.3.1 http://code.google.com/p/google-glog/

Installation only:./configure; Make Make install

2, Simple Example

Main.cpp:

#include <iostream>

#include "glog/logging.h"//Glog header file

using namespace Std;

int main (int argc, char** argv) {

Google::initgooglelogging (Argv[0]); Initialization

Flags_log_dir= ".";

LOG (INFO) << "Hello Glog";  Print log: "Hello Glog." Similar to C + + stream.

}

Makefile:

lib=$ (HOME)/install/glog/lib #glog installation path

include=$ (HOME)/install/glog/include

test_glog:main.o

g++-o [email protected] $^-l$ (LIB)-lglog–lpthread #-lpthread because glog requires some locking mechanisms in multiple threads.

Main.o:main.cpp

g++-c-o [email protected] $^-i$ (INCLUDE)

Description

Glog default to log is divided into 4 levels: INFO, WARNING, ERROR, FATAL. Printing a log statement is similar to a stream in C + +, in fact the log (INFO) macro returns an object that inherits from the Std::ostrstream class.

Compile and run the demo above, Glog will generate the log log file in the/tmp/directory by default: test_glog.search-x2.username.log.info.20111003-161341.2083

The file name fields correspond to the following meanings:

<program Name>.

which

1), <program name> In fact corresponds to Google::initgooglelogging (Argv[0]), in the argv[0], that is, by changing the GOOGLE::I The nitgooglelogging parameter can modify the name of the log file.

2), each level of the log is output to a different file. And the high-level log files are also entered into the lower levels of the log file. That is, the fatal information is recorded in the Info,warning,error,fatal log file at the same time. By default, Glog will also send fatal logs to stderr.

Now the problem is: Log can not be printed to the/tmp/directory. The following summary to solve:

3, parameter settings:

Unlike the LOG4 series of log systems, the configuration file glog the use of commands to configure parameters. In Glog's official documentation, there are two ways to configure parameters (for example, modify the log directory):

1), GFlags:

./your_application--log_dir=.

(GFlags I haven't used yet)

2), export Modify the environment variable as follows: Modify Glog_log_dir as the upper level directory

3) The above two methods need to use the command line, in addition, can also be specified directly in the program (not mentioned in the official document, Glog source code is not encouraged to do so, but it is feasible):

In the glog/logging.h header file 287---350 lines, there are variables such as "GLOG_LOG_DIR" macro definition, then its glog_log_dir is actually flags_log_dir, so only need to set the Flags_log_ in the program The value of Dir can be. Other variables are similar. Cancels the comment line "//flags_log_dir=" in main.cpp. ";" Try it.

4, the glog support Feature List is as follows :

1, parameter setting, set flag parameter in the way of command line parameter to control logging behavior;

2, severity rating, according to log severity rating logging;

3, log information can be recorded conditionally;

4, conditional abort procedure. Rich conditions to determine the macro, can be preset program termination conditions;

5, abnormal signal processing. Program abnormal situation, can customize the exception processing process;

6, support Debug function. Can only be used in debug mode;

7, custom log information;

8, the thread security log record way;

9, System level log record;

Google perror style log information;

11, thin log string information.

See demo instance for function points. Other features are described in the following links are simple and clear, here also do not elaborate:

1) Official document (English): Http://google-glog.googlecode.com/svn/trunk/doc/glog.html#verbose

2) A Chinese translator with a netizen: http://mengjh.blog.51cto.com/2860827/546766

5, Summary : Glog simple and easy to use, the disadvantage of feeling or configuration parameters than the LOG4 series using configuration files to facilitate. In a small-scale program (unit test?) function point self-test), it should be a good choice.

Glog Installation (reprint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.