ubutun16.04 安装编译glog日志库
2024-10-20 01:32:04
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"即可
最新文章
- properties属性文件工具类
- C# 处理应用程序减少内存占用
- WebAPI文件上传与下载
- javascript组件化
- VI使用技巧
- linux学习笔记---一些有趣的命令
- libvirt API管理hypervisors
- C# 自己定义 implicit和explicit转换
- Matlab单元(Cell)数据的应用
- skin++ 终极破解之法
- GUI动态创建button
- 超级坑人的Couchbase数据库问题!!!
- ASP.NET MVC3开发 - CodeFisrt数据库篇之M层验证之调用远程方法(Remote)验证
- window下nginx注册成服务
- 洛谷 p5020 货币系统 题解
- 二叉搜索树(BST)学习笔记
- Eclipse实现数据库反向生成实体类(pojo)-------(插件安装和实现步骤的说明)
- windows10 搭建Dubbo
- Python Mock的入门学习
- Java Web HelloWorld!