1 安装
Ubuntu 下安装方法为:sudo apt-get install libgoogle-glog-dev
2 使用方法
2.1 常用函数说明:
-
初始化日志模块
google::InitGoogleLogging("");
- 与设置日志相关的函数
FLAGS_log_dir = "./log"; //设置log路径
- 关闭日志模块
google::ShutdownGoogleLogging();
3 使用方法
简单示例:
#include <iostream>
#include <glog/logging.h>
#include <thread>
#include <sys/syscall.h>
#include <sys/types.h>
int main(int argc, char* argv[])
{
// Initialize Google's logging library.
google::InitGoogleLogging(argv[0]);
//需要先在本目录下先建立有个名为“log”的文件夹,否则会报错
FLAGS_log_dir = "./log";
//or google::SetLogDestination(google::GLOG_INFO, "./log_");
FLAGS_alsologtostderr = true; //设置同时打印到屏幕
std::ostringstream oss;
oss << std::this_thread::get_id();
std::string stid = oss.str();
unsigned long long tid = std::stoull(stid);
LOG(INFO) << "Hello world! main_thread: " << std::this_thread::get_id() <<" " <<
(long int)syscall(SYS_gettid);
return 0;
}
g++ demo1.cpp -o demo1 -lpthread -lglog
将会在log目录下面生成,log文件。
4 参考文献