Skip to content

Commit a0b7541

Browse files
modify the log rotation method when program start (alibaba#932)
Co-authored-by: yyaoling <lihj00@163.com>
1 parent f048dd4 commit a0b7541

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

common/alog.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,21 @@ class AsyncLogOutput final : public BaseLogOutput {
508508
};
509509

510510
ILogOutput* new_log_output_file(const char* fn, uint64_t rotate_limit,
511-
int max_log_files, uint64_t throttle) {
511+
int max_log_files, uint64_t throttle, bool rotate_on_start) {
512512
auto ret = new LogOutputFile();
513513
if (ret->log_output_file_setting(fn, rotate_limit, max_log_files) < 0) {
514514
delete ret;
515515
LOG_ERROR_RETURN(0, nullptr, "Failed to open log file ", fn);
516516
return nullptr;
517517
}
518518
ret->set_throttle(throttle);
519+
520+
// when init the new log output file, rotate the log files that last program created
521+
if (rotate_on_start && ret->log_file_size != 0) {
522+
ret->log_file_rotate();
523+
ret->reopen_log_output_file();
524+
}
525+
519526
return ret;
520527
}
521528

common/alog.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ extern ILogOutput * const log_output_null;
7373
extern ILogOutput * const log_output_stderr;
7474
extern ILogOutput * const log_output_stdout;
7575

76-
ILogOutput* new_log_output_file(const char* fn, uint64_t rotate_limit = UINT64_MAX, int max_log_files = 10, uint64_t throttle = -1UL);
76+
ILogOutput* new_log_output_file(const char* fn, uint64_t rotate_limit = UINT64_MAX, int max_log_files = 10,
77+
uint64_t throttle = -1UL, bool rotate_on_start = false);
7778
ILogOutput* new_log_output_file(int fd, uint64_t throttle = -1UL);
7879
ILogOutput* new_async_log_output(ILogOutput* output, int queue_num = 1);
7980

0 commit comments

Comments
 (0)