MDGSF Software Engineer

[C/C++] glog 使用

2017-07-04

#define GOOGLE_GLOG_DLL_DECL
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include "glog/logging.h"
#pragma comment(lib, "libglog_static.lib")

#include <stdio.h>
#include <iostream>

using namespace std;
using namespace google;

int main()
{
    printf("glog demo\n");

    google::InitGoogleLogging("GlogDemo");
    google::SetLogDestination(GLOG_INFO, "C:\\/GlogDemoPrefix_"); //info以上的日志,输出到c盘,文件名前缀为GlogDemoPrefix_
    //google::SetLogDestination(GLOG_ERROR, "C:\\");
    google::SetStderrLogging(google::GLOG_INFO);  //输出到标准输出的时候大于 INFO 级别的都输出;等同于 FLAGS_stderrthreshold=google::INFO;
    //FLAGS_logbufsecs = 0;  //实时输出日志
    FLAGS_max_log_size = 100;  //最大日志大小(MB)

    LOG(INFO) << "Found" << google::COUNTER << endl;
    LOG(INFO) << "test";
    LOG(WARNING) << "warning test";
    LOG(ERROR) << "error test";


    google::ShutdownGoogleLogging();

    return 0;
}

GLOGWrapper.h

#ifndef LZMJ_GLOG_WRAPPER_HJ
#define LZMJ_GLOG_WRAPPER_HJ


#define GOOGLE_GLOG_DLL_DECL
#define GLOG_NO_ABBREVIATED_SEVERITIES
#include "glog/logging.h"
#pragma comment(lib, "libglog_static.lib")

#include <Windows.h>
#include <iostream>


void GLOGI(const char * pcFormat, ...);
void GLOGW(const char * pcFormat, ...);
const char * pcGLOGE(const char * pcFormat, ...);

#define GLOGE(format, ...) \
    do { \
        LOG(ERROR) << pcGLOGE(format, ##__VA_ARGS__); \
        google::FlushLogFiles(google::GLOG_INFO); \
    } while (0)

#define GLOGE_ID(format, ...) \
    do { \
        LOG(ERROR) << m_acIdentifier << pcGLOGE(format, ##__VA_ARGS__); \
        google::FlushLogFiles(google::GLOG_INFO); \
    } while (0)


#define GLOEG_IF(condition, format, ...) \
    do { \
        LOG_IF(ERROR, condition) << pcGLOGE(format, ##__VA_ARGS__); \
        google::FlushLogFiles(google::GLOG_INFO); \
    } while (0)


extern void vInitGlog();

#endif

GLOGWrapper.cpp

#include "stdafx.h"
#include "GLOGWrapper.h"
#include "conio.h"
#include "direct.h"
using namespace std;

#define LOG_BUFSIZ 1024

void vInitGlog()
{
    _mkdir("D:\\LZMJ_LOG");

    google::InitGoogleLogging("LZMJ");
    google::SetLogDestination(google::GLOG_INFO, "D:\\LZMJ_LOG/LZMJ_INFO_");
    google::FlushLogFilesUnsafe(google::GLOG_ERROR);
    FLAGS_logbufsecs = 0; //缓冲日志输出,默认为30秒,此处改为立即输出
    FLAGS_max_log_size = 100; //最大日志大小为 100MB
    FLAGS_colorlogtostderr = true; //设置输出到屏幕的日志显示相应颜色

    LOG(INFO) << "info log test";
    LOG(WARNING) << "warning log test";
    LOG(ERROR) << "error log test" << endl << endl;

    LOG(INFO) << __FUNCTION__;
    LOG(INFO) << "-----> " << __FUNCTION__;
    LOG(INFO) << "<----- " << __FUNCTION__;
    LOG(INFO) << "<----- " << __FUNCTION__ << endl << endl;
    google::FlushLogFiles(google::GLOG_INFO);
}

void GLOGI(const char * pcFormat, ...)
{
    va_list pcList;
    va_start(pcList, pcFormat);
    char acLog[LOG_BUFSIZ] = { 0 };
    vsnprintf(acLog, sizeof(acLog), pcFormat, pcList);

    LOG(INFO) << acLog;
    google::FlushLogFiles(google::GLOG_INFO);

    va_end(pcList);
}

void GLOGW(const char * pcFormat, ...)
{
    va_list pcList;
    va_start(pcList, pcFormat);
    char acLog[LOG_BUFSIZ] = { 0 };
    vsnprintf(acLog, sizeof(acLog), pcFormat, pcList);

    LOG(WARNING) << acLog;
    google::FlushLogFiles(google::GLOG_INFO);

    va_end(pcList);
}

const char * pcGLOGE(const char * pcFormat, ...)
{
    va_list pcList;
    va_start(pcList, pcFormat);
    static char acLog[LOG_BUFSIZ] = { 0 };
    vsnprintf(acLog, sizeof(acLog), pcFormat, pcList);
    va_end(pcList);
    return acLog;
}


weixingongzhonghao

Comments

Content