安装
sudo apt-get update
sudo apt-get install libgoogle-glog-dev
配置
- 采用
cmakelist
的话,直接在target_link_libraries
中加入glog
即可,无需多做处理。
使用
#include "glog/logging.h"void initGlog(const char *arg)
{auto ErrorCallback = [](const char *data, int size){ std::string ss(data, size - 1);LOG(ERROR) << ss; };google::InitGoogleLogging(arg); google::InstallFailureSignalHandler(); google::InstallFailureWriter(ErrorCallback); FLAGS_alsologtostderr = true; FLAGS_colorlogtostderr = true; FLAGS_logbuflevel = google::INFO; mkdir("./logs", S_IRWXU | S_IRWXG | S_IRWXO); FLAGS_log_dir = "./logs";
}int main(int argc, char* argv[])
{initGlog(argv[0]);LOG(INFO) << "This is an info message";LOG(WARNING) << "This is a warning message";LOG(ERROR) << "This is an error message";google::ShutdownGoogleLogging();return 0;
}