1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| #include "log.h"
void Log::init(int level, const char *path, const char *suffix, int maxQueueCapacity) { isOpen_ = true; level_ = level; if (maxQueueCapacity > 0) { isAsync_ = true; if (!deque_) { std::unique_ptr<BlockDeque<std::string>> newDequeue( new BlockDeque<std::string>); deque_ = move(newDequeue); std::unique_ptr<std::thread> newThread(new std::thread(FlushLogThread)); writeThread_ = move(newThread); } } else { isAsync_ = false; }
lineCount_ = 0; time_t timer = std::time(nullptr); struct tm *sysTime = localtime(&timer); struct tm t = *sysTime; path_ = path; suffix_ = suffix;
char fileName[LOG_NAME_LEN] = {0}; snprintf(fileName, LOG_NAME_LEN, "%s/%04d_%02d_%02d%s", path_, t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, suffix_); toDay_ = t.tm_mday;
{ std::lock_guard<std::mutex> locker(mtx_); buff_.RetrieveAll(); if (fp_) { flush(); fclose(fp_); }
fp_ = fopen(fileName, "a"); if (fp_ == nullptr) { mkdir(path_, 0777); fp_ = fopen(fileName, "a"); }
assert(fp_ != nullptr); } }
Log *Log::Instance() { static Log inst; return &inst; }
void Log::FlushLogThread() { Log::Instance()->AsyncWrite_(); }
void Log::write(int level, const char *format, ...) { 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))) { std::unique_lock<std::mutex> locker(mtx_); locker.unlock(); char newfileName[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) { snprintf(newfileName, LOG_NAME_LEN - 72, "%s/%s%s", path_, tail, suffix_); toDay_ = t.tm_mday; lineCount_ = 0; } else { snprintf(newfileName, LOG_NAME_LEN - 72, "%s/%s-%d%s", path_, tail, (lineCount_ / MAX_LINES), suffix_); }
locker.lock(); flush(); fclose(fp_); fp_ = fopen(newfileName, "a"); assert(fp_ != nullptr); }
{ std::unique_lock<std::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); buff_.HasWritten(n); 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 { fputs(buff_.Peek(), fp_); } buff_.RetrieveAll(); }
} void Log::flush() { if (isAsync_) { deque_->flush(); } fflush(fp_); }
int Log::GetLevel() { std::unique_lock<std::mutex> locker(mtx_); return level_; } void Log::SetLevel(int level) { std::unique_lock<std::mutex> locker(mtx_); level_ = level; }
Log::Log() { lineCount_ = 0; toDay_ = 0; isAsync_ = false; fp_ = nullptr; deque_ = nullptr; writeThread_ = nullptr; }
void Log::AppendLogLevelTitle_(int level) {
switch (level) { case 0: buff_.Append("[debug]: ", 9); break; case 1: buff_.Append("[info] : ", 9); break; case 2: buff_.Append("[warn] : ", 9); break; case 3: buff_.Append("[error]: ", 9); break; default: buff_.Append("[info] : ", 9); break; } }
Log::~Log() { if (writeThread_ && writeThread_->joinable()) { std::lock_guard<std::mutex> locker(mtx_); while (!deque_->empty()) { deque_->flush(); } deque_->Close(); writeThread_->join(); } if (fp_) { std::lock_guard<std::mutex> locker(mtx_); flush(); fclose(fp_); } } void Log::AsyncWrite_() { std::string str = ""; while (deque_->pop(str)) { std::lock_guard<std::mutex> locker(mtx_); fputs(str.c_str(), fp_); } }
|