安装git
yum install git
安装glog
1 git clone https://github.com/google/glog.git
2 cd glog
3 ./autogen.sh
4 ./configure //可以不配置文件夹--prefix=path(install)
5 make
6 make install //安装到/usr/local/lib路径下
在执行第三步骤的时候可能会遇到一些问题,:
安装以下工具解决
yum install autoconf
yum install automake
yum install libtool
使用glog
#include <string>
#include <iostream>
#include "glog/logging.h" // glog 头文件
#include "glog/raw_logging.h"
int main(int argc, char** argv){
// FLAGS_log_dir="."; //设置log目录 没有指定则输出到控制台
FLAGS_logtostderr = 1; //输出到控制台
google::InitGoogleLogging(argv[0]); // 初始化
std::string test = "this is test";
int i = 2, number = 8;
LOG(INFO) << "it is info"; // 打印log:“hello glog. 类似于C++ stream。
LOG_IF(INFO, number > 10) << "number > 10";
LOG_IF(INFO, number < 10) << "number < 10";
for(i=0; i<20 ;i++){
LOG_EVERY_N(INFO, 5) << "log i = " << i;
}
LOG(WARNING) << "It is error info";
LOG(ERROR) << "It is error info";
DLOG(INFO) << "it is debug mode";
DLOG_IF(INFO, number > 10) << "debug number > 10";
// DLOG_EVERY_N(INFO, 10) << "log i = " << i;
RAW_LOG(INFO, "it is pthread log");
return 0;
}
编译(需要把、usr/local/lib的库拷贝到test_main.cpp同级文件夹)
g++ test_main.cpp ./lib/libglog.a -std=c++11 -DDEBUG -lpthread -o sample
//g++ test_main.cpp ./lib/libglog.a -I./include -std=c++11 -DDEBUG -lpthread -o sample