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

  • 相关阅读:
    视图中访问 路由参数
    视图中的Layout使用(转)
    js判断浏览器类型以及版本
    再关于IE11
    关于IE11
    js获取当前页面的网址域名地址
    contenteditable 属性
    HTMl5的sessionStorage和localStorage(转)
    c 数组做为形參时 该參数退化为指针
    汉诺塔-递归实现
  • 原文地址:https://www.cnblogs.com/chaofn/p/10803328.html
Copyright © 2011-2022 走看看