From 79044bcc674fa7727be21ce5c80b543e45e30165 Mon Sep 17 00:00:00 2001 From: armink Date: Fri, 15 Feb 2019 17:18:07 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=AE=8C=E5=96=84=E3=80=91=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=96=87=E4=BB=B6=E5=BE=AA=E7=8E=AF=E5=86=99=E5=85=A5?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- easylogger/plugins/file/elog_file.c | 93 ++++++++++++++++------------- easylogger/plugins/file/elog_file.h | 1 - 2 files changed, 53 insertions(+), 41 deletions(-) diff --git a/easylogger/plugins/file/elog_file.c b/easylogger/plugins/file/elog_file.c index 30e1799..99e1cab 100644 --- a/easylogger/plugins/file/elog_file.c +++ b/easylogger/plugins/file/elog_file.c @@ -64,6 +64,52 @@ __exit: return result; } +/* + * rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 + * + * it will return true when rotate successfully + */ +static bool elog_file_rotate(void) +{ +#define SUFFIX_LEN 10 + /* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */ + size_t base = strlen(local_cfg.name); + char *oldpath = NULL, *newpath = NULL; + int n; + FILE *fp_bak = fp; + + oldpath = (char *) malloc(base + SUFFIX_LEN); + newpath = (char *) malloc(base + SUFFIX_LEN); + + if (oldpath == NULL || newpath == NULL) { + return false; + } + + memcpy(oldpath, local_cfg.name, base); + memcpy(newpath, local_cfg.name, base); + + for (n = local_cfg.max_rotate - 1; n >= 0; --n) { + snprintf(oldpath + base, SUFFIX_LEN, n ? ".%d" : "", n - 1); + snprintf(newpath + base, SUFFIX_LEN, ".%d", n); + rename(oldpath, newpath); + } + free(oldpath); + free(newpath); + + fp = fopen(local_cfg.name, "a+"); + if (fp) { + if (fp_bak) { + fclose(fp_bak); + } + fd = fileno(fp); + return true; + } else { + fp = fp_bak; + fd = -1; + return false; + } +} + void elog_file_write(const char *log, size_t size) { ELOG_ASSERT(init_ok); @@ -71,18 +117,20 @@ void elog_file_write(const char *log, size_t size) struct stat statbuf; statbuf.st_size = 0; + + elog_file_port_lock(); + fstat(fd, &statbuf); if (unlikely(statbuf.st_size > local_cfg.max_size)) { - if (local_cfg.max_rotate > 0) { - elog_file_rotate(); - } else { + /* rotate the log file */ + if (local_cfg.max_rotate <= 0 || !elog_file_rotate()) { + /* not enabled rotate or rotate failed */ + elog_file_port_unlock(); return; } } - elog_file_port_lock(); - fwrite(log, size, 1, fp); #ifdef ELOG_FILE_FLUSH_CAHCE_ENABLE @@ -121,38 +169,3 @@ void elog_file_config(ElogFileCfg *cfg) elog_file_port_unlock(); } - -void elog_file_rotate(void) -{ - if (local_cfg.max_rotate <= 0) return; - - elog_file_port_lock(); - - if (fp) { - fclose(fp); - fp = NULL; - } - - /* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */ - size_t base = strlen(local_cfg.name); - char* oldpath = (char*)malloc(base + 10); // use c99 variable length array or alloca(3) instead? - char* newpath = (char*)malloc(base + 10); - memcpy(oldpath, local_cfg.name, base); - memcpy(newpath, local_cfg.name, base); - int n; - for (n = local_cfg.max_rotate - 1; n >= 0; --n) { - sprintf(oldpath + base, n ? ".%d" : "", n-1); - sprintf(newpath + base, ".%d", n); - rename(oldpath, newpath); - } - free(oldpath); - free(newpath); - - fp = fopen(local_cfg.name, "a+"); - if (fp) - fd = fileno(fp); - else - fd = -1; - - elog_file_port_unlock(); -} diff --git a/easylogger/plugins/file/elog_file.h b/easylogger/plugins/file/elog_file.h index 440c5ea..11df802 100644 --- a/easylogger/plugins/file/elog_file.h +++ b/easylogger/plugins/file/elog_file.h @@ -56,7 +56,6 @@ typedef struct { ElogErrCode elog_file_init(void); void elog_file_write(const char *log, size_t size); void elog_file_config(ElogFileCfg *cfg); -void elog_file_rotate(void); void elog_file_deinit(void); /* elog_file_port.c */