log add level
parent
4d706d8ede
commit
bf3911d4df
|
@ -1,7 +1,3 @@
|
|||
//
|
||||
// Created by bt110 on 2019/8/19.
|
||||
//
|
||||
|
||||
#ifndef CPP11FEATURETEST_LOGER_H
|
||||
#define CPP11FEATURETEST_LOGER_H
|
||||
|
||||
|
@ -10,35 +6,48 @@
|
|||
#include <time.h>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
|
||||
using namespace std;
|
||||
typedef enum Mode{
|
||||
typedef enum {
|
||||
Mode_Daily, // 每天保存一次日志
|
||||
MODE_Monthly, // 每个月保存一次日志
|
||||
MODE_Weekly, // 每周保存一次日志
|
||||
MODE_Size // 根据已存储的容量来保存日志
|
||||
}ESaveMode;
|
||||
}Mode;
|
||||
|
||||
typedef enum {
|
||||
LEVEL_DEBUG = 0, // 调试模式所有日志都会显示
|
||||
LEVEL_INFO, // INFO和WARNING和ERROR
|
||||
LEVEL_WARNING, // WARNING和ERROR
|
||||
LEVEL_ERROR // 仅显示ERROR
|
||||
}Level;
|
||||
|
||||
class Loger {
|
||||
public:
|
||||
Loger(string path,string prefix);
|
||||
Loger(string path,string prefix,bool old);
|
||||
~Loger();
|
||||
#define DEBUG_FILE_POSITION __FILE__,__LINE__
|
||||
int Debug(string,string,int);
|
||||
int Log();
|
||||
int LogFile();
|
||||
void SetMinimalLevel(Level);
|
||||
void SetWorkMode(Mode);
|
||||
int Debug(string,string file = __FILE__,int line = __LINE__);
|
||||
int Info(string,string file = __FILE__,int line = __LINE__);
|
||||
int Warning(string,string file = __FILE__,int line = __LINE__);
|
||||
int Error(string,string file = __FILE__,int line = __LINE__);
|
||||
|
||||
void operator+(const string&);
|
||||
void operator<<(const string&);
|
||||
|
||||
private:
|
||||
string mPath;
|
||||
string mPrefix;
|
||||
string mCurrentPath;
|
||||
FILE *mFile; // 日志文件
|
||||
ESaveMode mMode; // 工作模式
|
||||
Mode mMode; // 工作模式
|
||||
Level mLevel; // 最低调试输出
|
||||
string mCurrentDate; // 当天
|
||||
bool mValid;
|
||||
std::mutex mMux;
|
||||
};
|
||||
|
||||
|
||||
#endif //CPP11FEATURETEST_LOGER_H
|
||||
#endif
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
//
|
||||
// Created by bt110 on 2019/8/19.
|
||||
//
|
||||
#include "loger.h"
|
||||
|
||||
string getTimeDate()
|
||||
|
@ -21,7 +18,15 @@ string getTime()
|
|||
return string(tmp);
|
||||
}
|
||||
|
||||
int Loger::Debug(string dat,string function,int line){
|
||||
void Loger::SetMinimalLevel(Level level){
|
||||
mLevel = level;
|
||||
}
|
||||
|
||||
int Loger::Debug(string dat,string function,int line) {
|
||||
if(mLevel > LEVEL_DEBUG) {
|
||||
return 0;
|
||||
}
|
||||
std::lock_guard<std::mutex> guard(mMux);
|
||||
if (!mValid){
|
||||
this->mCurrentDate = getTimeDate();
|
||||
this->mCurrentPath = this->mPath + "/" + this->mPrefix + "/" + this->mCurrentDate + ".log";
|
||||
|
@ -34,10 +39,11 @@ int Loger::Debug(string dat,string function,int line){
|
|||
}
|
||||
// 还没有过天
|
||||
if(getTimeDate() == this->mCurrentDate){
|
||||
string tmp = getTime();
|
||||
string tmp = "[" + this->mPrefix + "] ";
|
||||
tmp += getTime();
|
||||
tmp += ": ";
|
||||
tmp += dat;
|
||||
tmp += "at [" + function + " line " + std::to_string(line) + "]";
|
||||
tmp += " at [" + function + " line " + std::to_string(line) + "]";
|
||||
tmp += "\n";
|
||||
int ret =fwrite(tmp.c_str(),tmp.size(),1,this->mFile);
|
||||
fflush(this->mFile);
|
||||
|
@ -48,8 +54,11 @@ int Loger::Debug(string dat,string function,int line){
|
|||
this->mFile = fopen(path.c_str(),"w+");
|
||||
if (this->mFile == nullptr){
|
||||
mValid = false;
|
||||
std::cout<<"error Loger open file\r\n"<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
string tmp = getTime();
|
||||
string tmp = "[" + this->mPrefix + "] ";
|
||||
tmp += getTime();
|
||||
tmp += ": ";
|
||||
tmp += dat;
|
||||
tmp += "at [" + function + " line " + std::to_string(line) + "]";
|
||||
|
@ -61,7 +70,9 @@ int Loger::Debug(string dat,string function,int line){
|
|||
return 0;
|
||||
}
|
||||
|
||||
void Loger::operator+(const string& wb){
|
||||
|
||||
int Loger::Error(string dat,string function,int line) {
|
||||
std::lock_guard<std::mutex> guard(mMux);
|
||||
if (!mValid){
|
||||
this->mCurrentDate = getTimeDate();
|
||||
this->mCurrentPath = this->mPath + "/" + this->mPrefix + "/" + this->mCurrentDate + ".log";
|
||||
|
@ -69,25 +80,172 @@ void Loger::operator+(const string& wb){
|
|||
if (this->mFile == nullptr){
|
||||
mValid = false;
|
||||
std::cout<<"error Loger open file\r\n"<<std::endl;
|
||||
return;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// 还没有过天
|
||||
// 还没有过天
|
||||
if(getTimeDate() == this->mCurrentDate){
|
||||
string tmp = getTime();
|
||||
tmp += ":";
|
||||
tmp += wb;
|
||||
tmp += " \n";
|
||||
fwrite(tmp.c_str(),tmp.size(),1,this->mFile);
|
||||
string tmp = "[ERROR] ";
|
||||
tmp += getTime();
|
||||
tmp += ": ";
|
||||
tmp += dat;
|
||||
tmp += " at [" + function + " line " + std::to_string(line) + "]";
|
||||
tmp += "\n";
|
||||
int ret =fwrite(tmp.c_str(),tmp.size(),1,this->mFile);
|
||||
fflush(this->mFile);
|
||||
return ret;
|
||||
}else{ // 已经过天了
|
||||
this->mCurrentDate = getTimeDate();
|
||||
string path = getTimeDate() + ".log";
|
||||
this->mFile = fopen(path.c_str(),"w+");
|
||||
if (this->mFile == nullptr){
|
||||
mValid = true;
|
||||
mValid = false;
|
||||
std::cout<<"error Loger open file\r\n"<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
string tmp = getTime();
|
||||
string tmp = "[ERROR] ";
|
||||
tmp += getTime();
|
||||
tmp += ": ";
|
||||
tmp += dat;
|
||||
tmp += "at [" + function + " line " + std::to_string(line) + "]";
|
||||
tmp += "\n";
|
||||
int ret = fwrite(tmp.c_str(),tmp.size(),1,this->mFile);
|
||||
fflush(this->mFile);
|
||||
return ret;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Loger::Warning(string dat,string function,int line) {
|
||||
if(mLevel > LEVEL_WARNING) {
|
||||
return 0;
|
||||
}
|
||||
std::lock_guard<std::mutex> guard(mMux);
|
||||
if (!mValid){
|
||||
this->mCurrentDate = getTimeDate();
|
||||
this->mCurrentPath = this->mPath + "/" + this->mPrefix + "/" + this->mCurrentDate + ".log";
|
||||
this->mFile = fopen(mCurrentPath.c_str(),"w+");
|
||||
if (this->mFile == nullptr){
|
||||
mValid = false;
|
||||
std::cout<<"error Loger open file\r\n"<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// 还没有过天
|
||||
if(getTimeDate() == this->mCurrentDate){
|
||||
string tmp = "[" + this->mPrefix + "] ";
|
||||
tmp += getTime();
|
||||
tmp += ": ";
|
||||
tmp += dat;
|
||||
tmp += " at [" + function + " line " + std::to_string(line) + "]";
|
||||
tmp += "\n";
|
||||
int ret =fwrite(tmp.c_str(),tmp.size(),1,this->mFile);
|
||||
fflush(this->mFile);
|
||||
return ret;
|
||||
}else{ // 已经过天了
|
||||
this->mCurrentDate = getTimeDate();
|
||||
string path = getTimeDate() + ".log";
|
||||
this->mFile = fopen(path.c_str(),"w+");
|
||||
if (this->mFile == nullptr){
|
||||
mValid = false;
|
||||
std::cout<<"error Loger open file\r\n"<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
string tmp = "[" + this->mPrefix + "] ";
|
||||
tmp += getTime();
|
||||
tmp += ": ";
|
||||
tmp += dat;
|
||||
tmp += "at [" + function + " line " + std::to_string(line) + "]";
|
||||
tmp += "\n";
|
||||
int ret = fwrite(tmp.c_str(),tmp.size(),1,this->mFile);
|
||||
fflush(this->mFile);
|
||||
return ret;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int Loger::Info(string dat,string function,int line) {
|
||||
if(mLevel > LEVEL_WARNING){
|
||||
return 0;
|
||||
}
|
||||
std::lock_guard<std::mutex> guard(mMux);
|
||||
if (!mValid){
|
||||
this->mCurrentDate = getTimeDate();
|
||||
this->mCurrentPath = this->mPath + "/" + this->mPrefix + "/" + this->mCurrentDate + ".log";
|
||||
this->mFile = fopen(mCurrentPath.c_str(),"w+");
|
||||
if (this->mFile == nullptr){
|
||||
mValid = false;
|
||||
std::cout<<"error Loger open file\r\n"<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
// 还没有过天
|
||||
if(getTimeDate() == this->mCurrentDate){
|
||||
string tmp = "[" + this->mPrefix + "] ";
|
||||
tmp += getTime();
|
||||
tmp += ": ";
|
||||
tmp += dat;
|
||||
tmp += " at [" + function + " line " + std::to_string(line) + "]";
|
||||
tmp += "\n";
|
||||
int ret =fwrite(tmp.c_str(),tmp.size(),1,this->mFile);
|
||||
fflush(this->mFile);
|
||||
return ret;
|
||||
}else{ // 已经过天了
|
||||
this->mCurrentDate = getTimeDate();
|
||||
string path = getTimeDate() + ".log";
|
||||
this->mFile = fopen(path.c_str(),"w+");
|
||||
if (this->mFile == nullptr){
|
||||
mValid = false;
|
||||
std::cout<<"error Loger open file\r\n"<<std::endl;
|
||||
return -1;
|
||||
}
|
||||
string tmp = "[" + this->mPrefix + "] ";
|
||||
tmp += getTime();
|
||||
tmp += ": ";
|
||||
tmp += dat;
|
||||
tmp += "at [" + function + " line " + std::to_string(line) + "]";
|
||||
tmp += "\n";
|
||||
int ret = fwrite(tmp.c_str(),tmp.size(),1,this->mFile);
|
||||
fflush(this->mFile);
|
||||
return ret;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
void Loger::operator+(const string& wb){
|
||||
std::lock_guard<std::mutex> guard(mMux);
|
||||
if (!mValid){
|
||||
this->mCurrentDate = getTimeDate();
|
||||
this->mCurrentPath = this->mPath + "/" + this->mPrefix + "/" + this->mCurrentDate + ".log";
|
||||
this->mFile = fopen(mCurrentPath.c_str(),"w+");
|
||||
if (this->mFile == nullptr){
|
||||
mValid = false;
|
||||
std::cout<<"error Loger open file\r\n"<<std::endl;
|
||||
return ;
|
||||
}
|
||||
}
|
||||
// 还没有过天
|
||||
if(getTimeDate() == this->mCurrentDate) {
|
||||
string tmp = "[" + this->mPrefix + "] ";
|
||||
tmp += getTime();
|
||||
tmp += ":";
|
||||
tmp += wb;
|
||||
tmp += " \n";
|
||||
fwrite(tmp.c_str(),tmp.size(),1,this->mFile);
|
||||
fflush(this->mFile);
|
||||
} else { // 已经过天了
|
||||
this->mCurrentDate = getTimeDate();
|
||||
string path = getTimeDate() + ".log";
|
||||
this->mFile = fopen(path.c_str(),"w+");
|
||||
if (this->mFile == nullptr){
|
||||
mValid = false;
|
||||
std::cout<<"error Loger open file\r\n"<<std::endl;
|
||||
return ;
|
||||
}
|
||||
string tmp = "[" + this->mPrefix + "] ";
|
||||
tmp += getTime();
|
||||
tmp += ":";
|
||||
tmp += wb;
|
||||
tmp += " \n";
|
||||
|
@ -97,6 +255,7 @@ void Loger::operator+(const string& wb){
|
|||
}
|
||||
|
||||
void Loger::operator<<(const string& wb){
|
||||
std::lock_guard<std::mutex> guard(mMux);
|
||||
if (!mValid){
|
||||
this->mCurrentDate = getTimeDate();
|
||||
this->mCurrentPath = this->mPath + "/" + this->mPrefix + "/" + this->mCurrentDate + ".log";
|
||||
|
@ -104,13 +263,12 @@ void Loger::operator<<(const string& wb){
|
|||
if (this->mFile == nullptr){
|
||||
mValid = false;
|
||||
std::cout<<"error Loger open file\r\n"<<std::endl;
|
||||
return;
|
||||
return ;
|
||||
}
|
||||
}
|
||||
|
||||
// 还没有过天
|
||||
if(getTimeDate() == this->mCurrentDate){
|
||||
string tmp = getTime();
|
||||
string tmp = "[" + this->mPrefix + "] ";
|
||||
tmp += getTime();
|
||||
tmp += ":";
|
||||
tmp += wb;
|
||||
tmp += " \n";
|
||||
|
@ -122,8 +280,11 @@ void Loger::operator<<(const string& wb){
|
|||
this->mFile = fopen(mCurrentPath.c_str(),"w+");
|
||||
if (this->mFile == nullptr){
|
||||
mValid = false;
|
||||
std::cout<<"error Loger open file\r\n"<<std::endl;
|
||||
return ;
|
||||
}
|
||||
string tmp = getTime();
|
||||
string tmp = "[" + this->mPrefix + "] ";
|
||||
tmp += getTime();
|
||||
tmp += ":";
|
||||
tmp += wb;
|
||||
tmp += " \n";
|
||||
|
@ -146,11 +307,19 @@ bool file_existed(string path) {
|
|||
}
|
||||
}
|
||||
|
||||
Loger::~Loger(){
|
||||
if(mValid){
|
||||
fflush(mFile);
|
||||
fclose(mFile);
|
||||
}
|
||||
}
|
||||
|
||||
Loger::Loger(string path,string prefix) {
|
||||
mLevel = LEVEL_INFO;
|
||||
this->mPath = path;
|
||||
this->mPrefix = prefix;
|
||||
this->mCurrentDate = getTimeDate();
|
||||
this->mCurrentPath = path + "/" + prefix + "/" + this->mCurrentDate + ".log";
|
||||
this->mCurrentPath = path + "/" + prefix + "-" + this->mCurrentDate + ".log";
|
||||
mValid = false;
|
||||
|
||||
this->mFile = fopen(this->mCurrentPath.c_str(),"a+");
|
||||
|
@ -161,3 +330,24 @@ Loger::Loger(string path,string prefix) {
|
|||
mValid = true;
|
||||
}
|
||||
}
|
||||
|
||||
Loger::Loger(string path,string prefix,bool old) {
|
||||
mLevel = LEVEL_INFO;
|
||||
this->mPath = path;
|
||||
this->mPrefix = prefix;
|
||||
this->mCurrentDate = getTimeDate();
|
||||
this->mCurrentPath = path + "/" + prefix + "-" + this->mCurrentDate + ".log";
|
||||
mValid = false;
|
||||
|
||||
if(old){
|
||||
this->mFile = fopen(this->mCurrentPath.c_str(),"w+");
|
||||
}else{
|
||||
this->mFile = fopen(this->mCurrentPath.c_str(),"a+");
|
||||
}
|
||||
if(! this->mFile){
|
||||
fprintf(stderr,"error open log files %s code %d,please check file path",this->mCurrentPath.c_str(),errno);
|
||||
exit(0);
|
||||
}else{
|
||||
mValid = true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ project(cpp11)
|
|||
|
||||
|
||||
set(CMAKE_BUILD_TYPE DEBUG)
|
||||
set(CMAKE_CXX_FLAGS " /MTd /std:c++11 /EHsc")
|
||||
set(CMAKE_CXX_FLAGS " /MDd /std:c++11 /EHsc")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "/DEBUG /std:c++11 /EHsc")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "")
|
||||
|
||||
|
@ -15,15 +15,13 @@ link_directories("../../../obj/")
|
|||
link_directories("./third/gtest/lib")
|
||||
|
||||
link_libraries(generallib)
|
||||
link_libraries(gtestd)
|
||||
link_libraries(jsoncpp)
|
||||
|
||||
add_executable(cpp11 cpp11_test.cpp )
|
||||
add_executable(gtest gtest.cpp )
|
||||
add_executable(thread_test thread_usage.cpp threadpool.cpp)
|
||||
|
||||
include_directories("./third/jsoncpp/include/json")
|
||||
include_directories("../../../obj/inc/")
|
||||
include_directories("./third/gtest/include")
|
||||
|
||||
|
||||
add_executable(cpp11 cpp11_test.cpp )
|
||||
add_executable(gtest gtest.cpp )
|
||||
add_executable(thread_test thread_usage.cpp threadpool.cpp)
|
||||
add_executable(log_test.cpp log_test.cpp)
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
#include "loger.h"
|
||||
|
||||
int main(){
|
||||
Loger loger1("d://","test",true);
|
||||
loger1.Debug("hello",DEBUG_FILE_POSITION);
|
||||
loger1<< "12131";
|
||||
loger1 + "12121";
|
||||
loger1.Debug("hello11212",DEBUG_FILE_POSITION);
|
||||
|
||||
}
|
Loading…
Reference in New Issue