1.下载安装glog

git clone https://github.com/boboxxd/glog.git
./autogen.sh && ./configure && make && sudo make install

2.下载安装 gflags

git clone https://github.com/gflags/gflags
sudo apt-get install autoconf automake libtool 
#进入源码目录(即gflags文件夹) 
cmake . 
make -j 24 
sudo make install

3.示例使用 glog

//main.cpp
#include <iostream>
using namespace std;

#include <glog/logging.h>

int main(int argc, char **argv)
{
    google::InitGoogleLogging("KLH");
    google::SetLogDestination(google::INFO, "./");

    char str[20] = "hello log!";
    LOG(INFO) << "Found " << google::COUNTER <<endl;
    LOG(INFO) << str ;                // << " cookies";
    LOG(WARNING) << "warning test";    // 会输出一个Warning日志
    LOG(ERROR) << "error test";        // 会输出一个Error日志

    return 0;
}

刚安装完,运行 vim ~/.bashrc,添加如下内容,以配置库的路径

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/

之后运行 sudo ldconfig

编译

g++ main.cpp -L/usr/local/lib/ -lglog -lgflags  -o test

运行 ./test,j结果如下:

E0309 09:36:58.242391 19444 main.cpp:15] error test
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐