Skip to content

Commit 4e696f0

Browse files
committed
improve daily rotaion & compress for co/log
1 parent f0e53ea commit 4e696f0

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

src/log/log.cc

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ class LevelLogger {
122122
void write(fastream* fs);
123123
void thread_fun();
124124
void compress_rotated_log(const fastring& path);
125+
uint32 get_day_from_path(const fastring& path);
125126

126127
private:
127128
Mutex _log_mutex;
@@ -193,14 +194,6 @@ LevelLogger::~LevelLogger() {
193194
#endif
194195
}
195196

196-
void LevelLogger::compress_rotated_log(const fastring& path) {
197-
Thread([path]() {
198-
fastring cmd(path.size() + 8);
199-
cmd.append("xz ").append(path);
200-
os::system(cmd);
201-
}).detach();
202-
}
203-
204197
void LevelLogger::init() {
205198
fastring s = FLG_log_file_name;
206199
s.remove_tail(".log");
@@ -336,22 +329,22 @@ void LevelLogger::thread_fun() {
336329
}
337330

338331
void LevelLogger::write(fastream* fs) {
332+
fs::file& f = _file;
333+
if (FLG_log_daily) {
334+
uint32 day = *(uint32*)_t;
335+
if (_day != day) {
336+
_day = day;
337+
f.close();
338+
}
339+
}
340+
339341
if (!_write_cb || FLG_also_log_to_local) {
340-
fs::file& f = _file;
341342
if (f || this->open_log_file()) {
342343
f.write(fs->data(), fs->size());
343344
}
344345

345-
if (f) {
346-
if (!f.exists() || f.size() >= FLG_max_log_file_size) {
347-
f.close();
348-
} else if (FLG_log_daily) {
349-
uint32 day = *(uint32*)_t;
350-
if (_day != day) {
351-
_day = day;
352-
f.close();
353-
}
354-
}
346+
if (f && (!f.exists() || f.size() >= FLG_max_log_file_size)) {
347+
f.close();
355348
}
356349
}
357350

@@ -409,6 +402,25 @@ inline void write_to_stderr(const char* s, size_t n) {
409402
#endif
410403
}
411404

405+
// get day from xx_0808_15_30_08.123.log
406+
inline uint32 LevelLogger::get_day_from_path(const fastring& path) {
407+
if (path.size() > 21) {
408+
uint32 x;
409+
memcpy(&x, path.data() + path.size() - 21, 4);
410+
return x;
411+
}
412+
return 0;
413+
}
414+
415+
// compress log file in another thread
416+
inline void LevelLogger::compress_rotated_log(const fastring& path) {
417+
Thread([path]() {
418+
fastring cmd(path.size() + 8);
419+
cmd.append("xz ").append(path);
420+
os::system(cmd);
421+
}).detach();
422+
}
423+
412424
bool LevelLogger::open_log_file(int level) {
413425
const fastring& path_base = _config->log_path_base;
414426
if (!fs::exists(_config->log_dir)) fs::mkdir(_config->log_dir, true);
@@ -418,13 +430,22 @@ bool LevelLogger::open_log_file(int level) {
418430

419431
if (level < xx::fatal) {
420432
_path.append(path_base).append(".log");
421-
if (fs::exists(_path) && !_old_paths.empty()) {
422-
auto& path = _old_paths.back();
423-
fs::rename(_path, path); // rename xx.log to xx_0808_15_30_08.123.log
424-
if (FLG_log_compress) this->compress_rotated_log(path);
433+
434+
bool need_create_new_file = !fs::exists(_path);
435+
if (!need_create_new_file) {
436+
if (!_old_paths.empty()) {
437+
auto& path = _old_paths.back();
438+
if (fs::fsize(_path) >= FLG_max_log_file_size || this->get_day_from_path(path) != _day) {
439+
fs::rename(_path, path); // rename xx.log to xx_0808_15_30_08.123.log
440+
if (FLG_log_compress) this->compress_rotated_log(path);
441+
need_create_new_file = true;
442+
}
443+
} else {
444+
need_create_new_file = true;
445+
}
425446
}
426447

427-
if (_file.open(_path.c_str(), 'a')) {
448+
if (_file.open(_path.c_str(), 'a') && need_create_new_file) {
428449
char t[24] = { 0 }; // 0723 17:00:00.123
429450
memcpy(t, _log_time.get(), _log_time.size());
430451
for (int i = 0; i < _log_time.size(); ++i) {

0 commit comments

Comments
 (0)