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
|