Skip to content

Commit

Permalink
🐛 fix log async bug
Browse files Browse the repository at this point in the history
  • Loading branch information
markparticle committed Jun 29, 2020
1 parent a19db3a commit 225dd3c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 56 deletions.
110 changes: 58 additions & 52 deletions code/log/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Log::Log() {
isAsync_ = false;
writeThread_ = nullptr;
deque_ = nullptr;

toDay_ = 0;
fp_ = nullptr;
}
Expand All @@ -31,7 +30,8 @@ Log::~Log() {
}
}

int Log::GetLevel() const {
int Log::GetLevel() {
lock_guard<mutex> locker(mtx_);
return level_;
}

Expand All @@ -56,7 +56,7 @@ void Log::init(int level = 1, const char* path, const char* suffix,
} else {
isAsync_ = false;
}
buff_.RetrieveAll();

lineCount_ = 0;

time_t timer = time(nullptr);
Expand All @@ -68,83 +68,89 @@ void Log::init(int level = 1, const char* path, const char* suffix,
snprintf(fileName, LOG_NAME_LEN - 1, "%s/%04d_%02d_%02d%s",
path_, t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, suffix_);
toDay_ = t.tm_mday;

if(fp_) { fclose(fp_); }
fp_ = fopen(fileName, "a");
if(fp_ == nullptr) {
mkdir(path_, 0777);

{
lock_guard<mutex> locker(mtx_);
buff_.RetrieveAll();
if(fp_) {
fflush(fp_);
fclose(fp_);
}

fp_ = fopen(fileName, "a");
}
assert(fp_ != nullptr);
if(fp_ == nullptr) {
mkdir(path_, 0777);
fp_ = fopen(fileName, "a");
}
assert(fp_ != nullptr);
}

}

void Log::write(int level, const char *format, ...) {
if(isAsync_) { deque_->flush(); }

struct timeval now = {0, 0};
gettimeofday(&now, nullptr);
time_t tSec = now.tv_sec;
struct tm *sysTime = localtime(&tSec);
struct tm t = *sysTime;

va_list vaList;

/* 日志日期 日志行数 */
if (toDay_ != t.tm_mday || (lineCount_ && (lineCount_ % MAX_LINES == 0)))
{
unique_lock<mutex> locker(mtx_);
locker.unlock();
/* 日志日期 日志行数 */
if (toDay_ != t.tm_mday || (lineCount_ && (lineCount_ % MAX_LINES == 0)))
char newFile[LOG_NAME_LEN];
char tail[36] = {0};
snprintf(tail, 36, "%04d_%02d_%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);

if (toDay_ != t.tm_mday)
{
char newFile[LOG_NAME_LEN];
char tail[16] = {0};
snprintf(tail, 20, "%04d_%02d_%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);

if (toDay_ != t.tm_mday)
{
snprintf(newFile, LOG_NAME_LEN - 1, "%s/%s%s", path_, tail, suffix_);
toDay_ = t.tm_mday;
lineCount_ = 0;
}
else {
snprintf(newFile, LOG_NAME_LEN - 1, "%s/%s-%d%s",
path_, tail, (lineCount_ / MAX_LINES), suffix_);
}

locker.lock();
fflush(fp_);
fclose(fp_);
fp_ = fopen(newFile, "a");
assert(fp_ != nullptr);
snprintf(newFile, LOG_NAME_LEN - 72, "%s/%s%s", path_, tail, suffix_);
toDay_ = t.tm_mday;
lineCount_ = 0;
}
lineCount_++;
else {
snprintf(newFile, LOG_NAME_LEN - 72, "%s/%s-%d%s", path_, tail, (lineCount_ / MAX_LINES), suffix_);
}

locker.lock();
fflush(fp_);
fclose(fp_);
fp_ = fopen(newFile, "a");
assert(fp_ != nullptr);
}

va_list vaList;
va_start(vaList, format);
{
lock_guard<mutex> locker(mtx_);
unique_lock<mutex> locker(mtx_);
lineCount_++;
int n = snprintf(buff_.BeginWrite(), 128, "%d-%02d-%02d %02d:%02d:%02d.%06ld ",
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec, now.tv_usec);
t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
t.tm_hour, t.tm_min, t.tm_sec, now.tv_usec);

buff_.HasWritten(n);
AppendLogLevel_();

AppendLogLevelTitle_(level);

va_start(vaList, format);
int m = vsnprintf(buff_.BeginWrite(), buff_.WritableBytes(), format, vaList);
va_end(vaList);

buff_.HasWritten(m);
buff_.Append("\n\0", 2);
}

if(isAsync_ || (deque_ && deque_->full())) {
deque_->push_back(buff_.RetrieveAllToStr());
}
else {
lock_guard<mutex> locker(mtx_);
fputs(buff_.Peek(), fp_);
if(isAsync_ && deque_ && !deque_->full()) {
deque_->push_back(buff_.RetrieveAllToStr());
} else {
fputs(buff_.Peek(), fp_);
}
buff_.RetrieveAll();

}
va_end(vaList);
}

void Log::AppendLogLevel_() {
switch(level_) {
void Log::AppendLogLevelTitle_(int level) {
switch(level) {
case 0:
buff_.Append("[debug]: ", 9);
break;
Expand Down
8 changes: 4 additions & 4 deletions code/log/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ class Log
public:
void init(int level, const char* path = "./log",
const char* suffix =".log",
int maxQueueCapacity = 800);
int maxQueueCapacity = 1024);

static Log* Instance();
static void FlushLogThread();

void write(int level, const char *format,...);
void flush();

int GetLevel() const;
int GetLevel();
void SetLevel(int level);
bool IsOpen() { return isOpen_; }

private:
Log();
void AppendLogLevel_();
void AppendLogLevelTitle_(int level);
virtual ~Log();
void AsyncWrite_();

private:
static const int LOG_PATH_LEN = 128;
static const int LOG_PATH_LEN = 256;
static const int LOG_NAME_LEN = 256;
static const int MAX_LINES = 50000;

Expand Down

0 comments on commit 225dd3c

Please sign in to comment.