[c/c++] glog demo

log.hpp

#ifndef LOGER_H
#define LOGER_H

#ifndef GLOG_NO_ABBREVIATED_SEVERITIES
#define GLOG_NO_ABBREVIATED_SEVERITIES
#endif

#include "glog/logging.h"
#include "glog/raw_logging.h"
#include "glog/log_severity.h"
#include <iostream>
#include <fstream>
#ifdef WIN32
#include <direct.h>
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif

#ifdef WIN32
#define GLOG_FOLDER ".//LOG"
#define GLOG_INFO_PATH ".//LOG//info" //directory of INFO level log
#define GLOG_WARN_PATH ".//LOG//warn" //directory of WARNING level log
#define GLOG_ERROR_PATH ".//LOG//error"  //directory of ERROR level log
#define GLOG_FATAL_PATH ".//LOG//fatal" //directory of FATAL level log
#else
#define GLOG_FOLDER "./LOG"
#define GLOG_INFO_PATH "./LOG/info" //directory of INFO level log
#define GLOG_WARN_PATH "./LOG/warn" //directory of WARNING level log
#define GLOG_ERROR_PATH "./LOG/error"  //directory of ERROR level log
#define GLOG_FATAL_PATH "./LOG/fatal" //directory of FATAL level log
#endif


class Gloger
{
public:
    Gloger();
    Gloger(const char* program){
        google::InitGoogleLogging(program);
        createDirect(GLOG_FOLDER);

        google::SetLogDestination(google::GLOG_INFO,    GLOG_INFO_PATH);
        google::SetLogDestination(google::GLOG_WARNING, GLOG_WARN_PATH);
        google::SetLogDestination(google::GLOG_ERROR,   GLOG_ERROR_PATH);
        google::SetLogDestination(google::GLOG_FATAL,   GLOG_FATAL_PATH);

        FLAGS_logbufsecs = 0;
        FLAGS_max_log_size = 100;
        FLAGS_stop_logging_if_full_disk = true;
    }
    ~Gloger(void){
        google::ShutdownGoogleLogging();
    }


    int createDirect(const char * dirName)
    {
        std::fstream _file;
        _file.open(dirName,std::ios::in);

        if(!_file){
    #ifdef WIN32
            return _mkdir(dirName);
    #else
            return mkdir(dirName,S_IRWXU);
    #endif
        }
        else{
            return 0;
        }
    }
};

//usage
//Gloger* logger = new Gloger("appname");

#endif // LOGER_H

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值