zoukankan      html  css  js  c++  java
  • ubutun16.04 安装编译glog日志库

    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.5 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"即可

  • 相关阅读:
    centos7.6 使用yum安装mysql5.7
    解决hadoop本地库问题
    docker-compose 启动警告
    docker 安装zabbix5.0 界面乱码问题解决
    docker 部署zabbix问题
    zookeeper 超时问题
    hbase regionserver异常宕机
    (转载)hadoop 滚动升级
    hadoop Requested data length 86483783 is longer than maximum configured RPC length
    zkfc 异常退出问题,报错Received stat error from Zookeeper. code:CONNECTIONLOSS
  • 原文地址:https://www.cnblogs.com/chaofn/p/10803328.html
Copyright © 2011-2022 走看看