google::Glog

windows下使用google的Glog库
下载glog-0.3.3.tar.gz,解压。
vs2013打开工程, 有四个项目
libglog
libglog_static
logging_unittest
logging_unittest_static
在liblog项目中logging.cc 文件中添加 #include <algorithm>  (不然无法编译过)
然后逐个编译
libglog  --> libglog.dll (动态工程库)
libglog_static --> libglog_static.lib (静态工程库)
logging_unittest --> logging_unittest.exe  (动态工程库测试工程)
logging_unittest_static --> logging_unittest_static.exe (静态工程库测试工程)
1、使用静态库
将glog-0.3.3glogsrcwindowslog 目录拷贝到项目工程中。
在stdifx.h中添加
#define GOOGLE_GLOG_DLL_DECL
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include "glog/logging.h"
#ifdef DEBUG
    #pragma comment(lib, "libglog_static_d.lib")
#else
    #pragma comment(lib, "libglog_static.lib")
#endif // DEBUG


2、在程序初始化时加 google::InitGoogleLogging("logInit"); google::SetLogDestination(google::GLOG_INFO, "F:\logs\");//设置路径 google::SetStderrLogging(google::GLOG_INFO);//设置错误路径 google::SetLogFilenameExtension("log_");//设置文件头 FLAGS_colorlogtostderr = true; // Set log color FLAGS_logbufsecs = 0; // Set log output speed(s) FLAGS_max_log_size = 1024; // Set max log file size FLAGS_stop_logging_if_full_disk = true; // If disk is full

3、程序输出 LOG(INFO) << str <<std::endl; 4、在程序退出地方加 google::ShutdownGoogleLogging();
glog使用与功能修改

1.增加文件按天区分

glog是根据进程ID来区分文件的,如果你重新启动了程序,则log文件的名字就会变,
这样似乎不太满足我的需求,我还要求它可以每天生成文件,方便我整理和分析log,
其实也有办法,自己增加一个按天滚文件的函数就可以了。
在utilities.cc中增加函数如下,(因为PidHasChanged()在这个文件里)
static int32 g_main_day = 0;
bool DayHasChanged()
{
    time_t raw_time;
    struct tm* tm_info;

    time(&raw_time);
    tm_info = localtime(&raw_time);

    if (tm_info->tm_mday != g_main_day)
    {
        g_main_day = tm_info->tm_mday;
        return true;
    }

    return false;
}

在logging.cc的LogFileObject::Write函数中将
if (static_cast<int>(file_length_ >> 20) >= MaxLogSize() ||
    PidHasChanged()) {
改成
if (static_cast<int>(file_length_ >> 20) >= MaxLogSize() ||
 PidHasChanged() || DayHasChanged()) {

这样就会按照天来输出log了。

  

原文地址:https://www.cnblogs.com/osbreak/p/9482389.html