1、下载、安装:

方式一:从github上下载源码、编译、安装:https://github.com/google/glog

$ cd /usr/local
$ git clone https://github.com/google/glog.git
$ cd glog
$ ./autogen.sh && ./configure && make && make install

执行过程:
---------------------------------------------------------------------
 /usr/bin/mkdir -p '/usr/local/share/doc/glog-0.4.0'
 /usr/bin/install -c -m 644 AUTHORS COPYING ChangeLog INSTALL README.md README.windows doc/designstyle.css doc/glog.html '/usr/local/share/doc/glog-0.4.0'
 /usr/bin/mkdir -p '/usr/local/include/glog'
 /usr/bin/install -c -m 644 src/glog/log_severity.h '/usr/local/include/glog'
 /usr/bin/mkdir -p '/usr/local/include/glog'
 /usr/bin/install -c -m 644 src/glog/logging.h src/glog/raw_logging.h src/glog/vlog_is_on.h src/glog/stl_logging.h '/usr/local/include/glog'
 /usr/bin/mkdir -p '/usr/local/lib/pkgconfig'
 /usr/bin/install -c -m 644 libglog.pc '/usr/local/lib/pkgconfig'
make[1]: Leaving directory `/usr/local/glog'

可以看到对应的头文件、库文件已经放到了/usr/local/include/和/usr/local/lib下面了。

方式二:下载tar包,地址:https://download.csdn.net/download/liuxiao723846/11609337

然后执行install.sh:

#!/bin/bash

cd $(dirname $0)

# should run as a root
user=`whoami`
if [ "$user" != "root" ]; then
    echo "Please run as root"
    exit 1
fi

export PATH="/usr/local/bin/:$PATH"
# Disable debug mode
export CPPFLAGS="-DNDEBUG"

all_parameters="$@"

function check_install_status() {
    if [ $? -ne 0 ]; then
        echo -e "\033[31mERROR: install failed.\033[0m"
        exit 1
    fi
}

#install glog
if [ ! -d glog-0.3.5 ]; then
    tar -zxf glog-0.3.5.tar.gz
    cd glog-0.3.5/
    ./configure
        check_install_status
    make -j8
        check_install_status
    make install
        check_install_status
    ldconfig
    cd ../
fi

注:这时 glog 库会默认安装在 /usr/local/lib/ 下,头文件放在 /usr/local/include/glog/ 中。

2、菜鸟级:

1)代码:test1.cpp

#include <glog/logging.h>

int main(int argc,char* argv[]){
    google::InitGoogleLogging(argv[0]); //初始化 glog
    LOG(INFO) << "Hello,GOOGLE!";
}

2)编译、连接:

$ g++ test1.cpp -lglog
$ ./a.out

注:首次编译时可能会报错,需要执行一下:ldconfig

3)说明:

3.1)运行a.out,默认glog的日志会生成在/tmp下,可以看到:a.out.Info 文件是一个软连接,指向了另一个真实的日志文件

# ll /tmp/
total 3844
lrwxrwxrwx. 1 root root     61 Aug 24 17:28 a.out.INFO -> a.out.localhost.localdomain.roo.log.INFO.20190824-172834.9600
-rw-r--r--. 1 root root    206 Aug 24 17:28 a.out.localhost.localdomain.roo.log.INFO.20190824-172834.9600

# cat /tmp/a.out.INFO 
Log file created at: 2019/08/24 17:28:34
Running on machine: localhost.localdomain
Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg
I0824 17:28:34.898052  9600 test1.cpp:5] hello,glog...

3.2)日志文件名的格式为: <program name>.<hostname>.<user name>.log.<severity level>.<date>.<time>.<pid>

3.3)日志格式:I+日期 时:分:秒.微秒 线程号 源文件名:行数] 信息

3.4)glog 支持四种日志级别,INFO、WARNING、ERROR和FATAL。不同级别的日志信息会输出到不同文件,同时高级别的日志也会写入到低级别中。默认情况下,在打印完FATAL日志之后,程序将会终止。ERROR和FATAL的日志除了会写到日志中,还会输出到 stderr。

3、新手级:

3.1)指定日志位置:

日志文件的位置可以通过 gflags 命令行参数 log_dir 来指定。有两种方式:

  • 在 main 函数开始时初始化 gflags: google::ParseCommandLineFlags(&argc, &argv, true);,然后命令行指定log_dir(推荐)
  • 直接在代码里 指定 FLAGS_log_dir 的值。

看一个例子:

[root@localhost test_glog]# cat test1.cpp 
#include <gflags/gflags.h>
#include <glog/logging.h>

int main(int argc,char* argv[]){
  gflags::ParseCommandLineFlags(&argc, &argv, true);
  google::InitGoogleLogging(argv[0]);
  LOG(INFO)<<"hello,glog...";
  //LOG(ERROR)<<"ERROR......................";
  return 0;
}

编译:

g++ test1.cpp -lglog -lgflags

注:这里需要引入gflags库。

运行:

./a.out --log_dir=./
ERROR: unknown command line flag 'log_dir'

注意:glog可以单独安装、使用,如上面的例子。但是如果使用高级特性,需要依赖gflags。这里报错的因为是glog源代码编译安装glog时,会默认依赖gflags。如果此时gflags没有安装,编译安装的glog将无法使用gflags相关的命令,因此也无法在可执行程序后添加“ –log_dir”。于是,先安装gflags(https://blog.csdn.net/liuxiao723846/article/details/100057363)然后重新编译安装glog。但编译glog的时候报错:

/usr/bin/ld: //usr/local/lib/libgflags.a(gflags_completions.cc.o): relocation R_X86_64_32S against symbol `_ZNSs4_Rep20_S_empty_rep_storageE@@GLIBCXX_3.4' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status

经过排查发现,这是因为glog需要动态编译安装的gflags。如果使用静态安装的gflags就会报错。重新编译gflag,cmake的时候加上参数(cmake -DBUILD_SHARED_LIBS=1 -DBUILD_STATIC_LIBS=1)即可,然后再编译glog就可以了。

3.2)自定义日志级别:

使用VLOG自定义日志类型 。用法: VLOG(50) << "MY VLOG INFO";。这里的 50 是自己定义的一个级别,事实上可以定义任何一个数字,但越小的数字代表这条输出日志越重要。通过命令行参数 -v 50 这样子来指定 VLOG 输出的级别,-v 50 意味着只有小于 50 以下的 VLOG 才会被输出(这里不会影响 LOG(INFO)这些)。一般项目里越底层的库使用越大数字的 VLOG 来打印调试信息,这样可以使得日志不会被一堆底层库的运行信息淹没。

看一个例子:

[root@localhost test_glog]# cat test2.cpp 
#include <gflags/gflags.h>
#include <glog/logging.h>

int main(int argc,char* argv[]){
  gflags::ParseCommandLineFlags(&argc, &argv, true);
  google::InitGoogleLogging(argv[0]);
  LOG(INFO)<<"hello,glog...";
  VLOG(100)<<"vlog info 100";
  VLOG(50)<<"vlog info 50";
  VLOG(10)<<"vlog info 10";
  return 0;
}

编译:

g++ test2.cpp -lglog -lgflags

运行:

# ./a.out --log_dir=./ --v=50

查看日志:
# cat a.out.INFO 
Log file created at: 2019/08/26 12:45:11
Running on machine: localhost.localdomain
Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg
I0826 12:45:11.296849 24901 test2.cpp:7] hello,glog...
I0826 12:45:11.298123 24901 test2.cpp:9] vlog info 50
I0826 12:45:11.298192 24901 test2.cpp:10] vlog info 10

4、进阶版:

4.1)glog支持常用的flag:

  • logtostderr:将日志输出到 stderr,不输出到日志文件,默认值是 false。
  • minloglevel:如果程序输出的日志的级别大于或等于这个值,那么日志就会被记录下来,它的默认值是 0,代表INFO (1 代表WARNING,2 代表ERROR,而 3 代表FATAL)

4.2)debug模式:

一种常见的调试手段就是使用 debug 日志,程序没有开启NDEBUG宏,可以使用DLOG()来输出 debug 日志,例如:

[root@localhost test_glog]# cat test3.cpp 
#define NDEBUG
#include <gflags/gflags.h>
#include <glog/logging.h>

int main(int argc,char* argv[]){
  gflags::ParseCommandLineFlags(&argc, &argv, true);
  google::InitGoogleLogging(argv[0]);
  
  LOG(INFO)<<"hello,glog...";
  DLOG(INFO)<<"debug model...";
  return 0;
}

编译、运行:

# g++ test3.cpp -lglog -lgflags
# ./a.out --log_dir=./
# cat a.out.INFO 
Log file created at: 2019/08/26 13:04:10
Running on machine: localhost.localdomain
Log line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msg
I0826 13:04:10.424523 26447 test3.cpp:9] hello,glog...

注:如果将test3.cpp最上面的#define NDEBUG去掉,就可以在INFO日志中看到debug信息了。

4.3)检查程序错误

glog 提供了CHECK()宏帮助我们检查程序的错误,当CHECK()的条件不满足时,它会记录FATAL日志并终止程序:

CHECK(fun() == 0) << "Call fun() failed!";

与assert()不同的是,无论程序是否开启NODEBUG宏,CHECK()都会执行。除了CHECK(),glog 还提供其它的宏,包括CHECK_EQ()、CHECK_NE()、CHECK_LE()、CHECK_LT()、CHECK_GE()和CHECK_GT()。

 

参考:

https://blog.csdn.net/u012348774/article/details/80558533

https://blog.csdn.net/jcjc918/article/details/51682853

https://www.cnblogs.com/xueqiuqiu/articles/10176161.html

https://github.com/google/glog/issues/174

 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐