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
2 在CMakeLists.txt中添加glog
find_package(Glog REQUIRED) include_directories( ${GLOG_INCLUDE_DIRS} ) ## 把glog链接到库${PROJECT_LIB_NAME}中 target_link_libraries(${PROJECT_LIB_NAME} glog)
3. 在C++ main函数中初始化配置glog
// 扑捉到程序崩溃或者中断时,把相应的信息打印到log文件和输出到屏幕。 void SignalHandler(const char *data, int size) { std::string glog_file = "./log/error.log"; std::ofstream fs(glog_file, std::ios::app); std::string str = std::string(data, size); fs << str; fs.close(); LOG(INFO) << str; } int main(int argc, char **argv) { FLAGS_colorlogtostderr = true; // log信息区分颜色 FLAGS_logtostderr = 1; // 允许log信息打印到屏幕 google::SetStderrLogging(google::GLOG_INFO); // 输出log的最低等级是 INFO (可以设置为WARNING或者更高) google::InstallFailureSignalHandler(); // 配置安装程序崩溃失败信号处理器 google::InstallFailureWriter(&SignalHandler); // 安装配置程序失败信号的信息打印过程,设置回调函数 google::InitGoogleLogging((const char *)argv[0]); // 用当前可执行程序初始化glog // 下面测试glog LOG(INFO) << "this is info."; LOG(WARNING) << "this is warning."; LOG(ERROR) <<"this is error."; google::SetStderrLogging(google::GLOG_WARNING); // 提高log输出等级为WARNING LOG(INFO) << "this is info."; LOG(WARNING) << "this is warning."; LOG(ERROR) <<"this is error."; // test int i = 0; int k = 10/i; }
e