no message

master
zcy 2021-12-15 17:34:02 +08:00
parent 4374d4f2aa
commit 4d706d8ede
3 changed files with 84 additions and 26 deletions

View File

@ -15,27 +15,30 @@ using namespace std;
typedef enum Mode{
Mode_Daily, // 每天保存一次日志
MODE_Monthly, // 每个月保存一次日志
MODE_Weekly // 每周保存一次日志
MODE_Weekly, // 每周保存一次日志
MODE_Size // 根据已存储的容量来保存日志
}ESaveMode;
class Loger {
private:
string mCurrentPath;
FILE *mFile; // 日志文件
ESaveMode mMode; // 工作模式
string mCurrentDate; // 当天
int error;
class Loger {
public:
Loger(FILE *p);
Loger(string path);
Loger(string path,string prefix);
#define DEBUG_FILE_POSITION __FILE__,__LINE__
int Debug(string,string,int);
int Log();
int LogFile();
void operator+(const string&);
void operator<<(const string&);
}Loger;
private:
string mPath;
string mPrefix;
string mCurrentPath;
FILE *mFile; // 日志文件
ESaveMode mMode; // 工作模式
string mCurrentDate; // 当天
bool mValid;
};
#endif //CPP11FEATURETEST_LOGER_H

View File

@ -2,7 +2,6 @@
// Created by bt110 on 2019/8/19.
//
#include "loger.h"
using namespace Loger;
string getTimeDate()
{
@ -23,6 +22,16 @@ string getTime()
}
int Loger::Debug(string dat,string function,int line){
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 = getTime();
@ -38,7 +47,7 @@ int Loger::Debug(string dat,string function,int line){
string path = getTimeDate() + ".log";
this->mFile = fopen(path.c_str(),"w+");
if (this->mFile == nullptr){
this->error = true;
mValid = false;
}
string tmp = getTime();
tmp += ": ";
@ -52,7 +61,17 @@ int Loger::Debug(string dat,string function,int line){
return 0;
}
void Loger::operator<<(const string& wb){
void Loger::operator+(const string& wb){
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 = getTime();
@ -66,7 +85,7 @@ void Loger::operator<<(const string& wb){
string path = getTimeDate() + ".log";
this->mFile = fopen(path.c_str(),"w+");
if (this->mFile == nullptr){
this->error = true;
mValid = true;
}
string tmp = getTime();
tmp += ":";
@ -77,7 +96,43 @@ void Loger::operator<<(const string& wb){
}
}
bool file_existed(string path) {
void Loger::operator<<(const string& wb){
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 = getTime();
tmp += ":";
tmp += wb;
tmp += " \n";
fwrite(tmp.c_str(),tmp.size(),1,this->mFile);
fflush(this->mFile);
}else{ // 已经过天了
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;
}
string tmp = getTime();
tmp += ":";
tmp += wb;
tmp += " \n";
fwrite(tmp.c_str(),tmp.size(),1,this->mFile);
fflush(this->mFile);
}
}
bool file_existed(string path) {
fstream _file;
_file.open(path.c_str(),ios::in);
if(!_file)
@ -91,19 +146,18 @@ bool file_existed(string path) {
}
}
Loger::Loger(FILE *p){
this->mFile = p;
this->mCurrentDate = getTime();
}
Loger::Loger(string path) {
Loger::Loger(string path,string prefix) {
this->mPath = path;
this->mPrefix = prefix;
this->mCurrentDate = getTimeDate();
this->mCurrentPath = path + this->mCurrentDate;
this->mCurrentPath = path + "/" + prefix + "/" + this->mCurrentDate + ".log";
mValid = false;
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;
}
}
}

View File

@ -89,6 +89,7 @@ namespace general{
}
mStarted = false;
}
void CThreadPool::StopAll() {
{
while(this->mTasks.size() > 0)
@ -96,13 +97,13 @@ namespace general{
mStarted = false;
mQueueAvaliableCondition.notify_all();
}
for (auto &th : mThreads)
{
th->join();
}
mStarted = false;
}
bool CThreadPool::Started(){
return this->mStarted;
}