glog 是一个 C++ 日志库,它提供 C++ 流式风格的 API。在安装 glog 之前需要先安装 gflags,这样 glog 就可以使用 gflags 去解析命令行参数(可以参见gflags 安装教程)。下面是 glog 的安装步骤:

安装方式一,下载原始代码编译:

$ git clone https://github.com/google/glog.git
$ cd glog
$ mkdir build
$ cmake ..
$ make
$ sudo make install

安装之后要怎么使用 glog 呢?如果程序是使用 CMake 构建的,那么只要在 CMakeListsx.txt 里面加上下面几行配置就可以了:

find_package (glog 0.3. REQUIRED)
add_executable (main main.cpp)
target_link_libraries (main glog::glog)

安装方式二,直接安装:

sudo apt-get install libgoogle-glog-dev

若是使用第二种方式安装的glog,如果程序是使用 CMake 构建的, CMakeListsx.txt 的配置会不同:

首先需要有CMake能找的到的FindGlog.cmake文件,这个文件在Google上可以找的到,见 Glog使用文档:

Example:

int main(int argc, char* argv[])
{
string home = "./log/"; //要先创建此目录,否则运行报错. google::InitGoogleLogging(argv[0]); string info_log = home + "master_info_";
google::SetLogDestination(google::INFO, info_log.c_str()); string warning_log = home + "master_warning_";
google::SetLogDestination(google::WARNING, warning_log.c_str()); string error_log = home + "master_error_";
google::SetLogDestination(google::ERROR, error_log.c_str()); string fatal_log = home + "master_fatal_";
google::SetLogDestination(google::FATAL, fatal_log.c_str()); // You can specify one of the following severity levels (in increasing order of severity)
LOG(INFO) << "info";
LOG(WARNING) << "warning";
LOG(ERROR) << "error";
LOG(FATAL) << "fatal"; // Logging a FATAL message terminates the program (after the message is logged)! return 0;
}

分别会在./log目录下生成4个log文件。只需要在main函数中初始化一次,便可以在该工程中的其他文件中使用!哪个文件需要写日志,只需要引用头文件#include "glog/logging.h"即可

最新文章

  1. properties属性文件工具类
  2. C# 处理应用程序减少内存占用
  3. WebAPI文件上传与下载
  4. javascript组件化
  5. VI使用技巧
  6. linux学习笔记---一些有趣的命令
  7. libvirt API管理hypervisors
  8. C# 自己定义 implicit和explicit转换
  9. Matlab单元(Cell)数据的应用
  10. skin++ 终极破解之法
  11. GUI动态创建button
  12. 超级坑人的Couchbase数据库问题!!!
  13. ASP.NET MVC3开发 - CodeFisrt数据库篇之M层验证之调用远程方法(Remote)验证
  14. window下nginx注册成服务
  15. 洛谷 p5020 货币系统 题解
  16. 二叉搜索树(BST)学习笔记
  17. Eclipse实现数据库反向生成实体类(pojo)-------(插件安装和实现步骤的说明)
  18. windows10 搭建Dubbo
  19. Python Mock的入门学习
  20. Java Web HelloWorld!

热门文章

  1. jmeter-分布式压测部署之负载机的设置
  2. python3之本地文件模拟登录
  3. PHP 数组函数大全
  4. MTSC2019大会日程重磅发布,腾讯WeTest独家Topic大揭秘!
  5. vuex防止数据刷新数据刷掉
  6. SQL注入:宽字节注入
  7. Unity整合TortoiseSVN
  8. 构建n位元的格雷码
  9. http异步通信
  10. TCP/IP协议族(五)