From 1507a7b611992fcd2f323c1c944ff015516a2277 Mon Sep 17 00:00:00 2001 From: Jin-W-FS Date: Thu, 14 Feb 2019 20:57:01 +0800 Subject: [PATCH] Support log-rotate. rotate each xxx.log.n-1 to xxx.log.n(n < local_cfg.max_rotate), and xxx.log to xxx.log.0 when xxx.log achieves local_cfg.max_size and local_cfg.max_rotate > 0. --- .../easylogger/plugins/file/elog_file_cfg.h | 3 ++ easylogger/plugins/file/elog_file.c | 48 ++++++++++++++++++- easylogger/plugins/file/elog_file.h | 2 + easylogger/plugins/file/elog_file_cfg.h | 3 ++ 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/demo/os/linux/easylogger/plugins/file/elog_file_cfg.h b/demo/os/linux/easylogger/plugins/file/elog_file_cfg.h index 278b48e..f43f98a 100644 --- a/demo/os/linux/easylogger/plugins/file/elog_file_cfg.h +++ b/demo/os/linux/easylogger/plugins/file/elog_file_cfg.h @@ -35,4 +35,7 @@ /* EasyLogger file log plugin's using file max size */ #define ELOG_FILE_MAX_SIZE (10 * 1024 * 1024) +/* EasyLogger file log plugin's using max rotate file count */ +#define ELOG_FILE_MAX_ROTATE 10 + #endif /* _ELOG_FILE_CFG_H_ */ diff --git a/easylogger/plugins/file/elog_file.c b/easylogger/plugins/file/elog_file.c index 9db0867..30e1799 100644 --- a/easylogger/plugins/file/elog_file.c +++ b/easylogger/plugins/file/elog_file.c @@ -31,6 +31,8 @@ #include #include #include +#include +#include #include #include @@ -53,6 +55,7 @@ ElogErrCode elog_file_init(void) cfg.name = ELOG_FILE_NAME; cfg.max_size = ELOG_FILE_MAX_SIZE; + cfg.max_rotate = ELOG_FILE_MAX_ROTATE; elog_file_config(&cfg); @@ -70,8 +73,13 @@ void elog_file_write(const char *log, size_t size) statbuf.st_size = 0; fstat(fd, &statbuf); - if (unlikely(statbuf.st_size > local_cfg.max_size)) - return; + if (unlikely(statbuf.st_size > local_cfg.max_size)) { + if (local_cfg.max_rotate > 0) { + elog_file_rotate(); + } else { + return; + } + } elog_file_port_lock(); @@ -103,6 +111,42 @@ void elog_file_config(ElogFileCfg *cfg) local_cfg.name = cfg->name; local_cfg.max_size = cfg->max_size; + local_cfg.max_rotate = cfg->max_rotate; + + fp = fopen(local_cfg.name, "a+"); + if (fp) + fd = fileno(fp); + else + fd = -1; + + 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) diff --git a/easylogger/plugins/file/elog_file.h b/easylogger/plugins/file/elog_file.h index 03c9fdc..440c5ea 100644 --- a/easylogger/plugins/file/elog_file.h +++ b/easylogger/plugins/file/elog_file.h @@ -49,12 +49,14 @@ extern "C" { typedef struct { char *name; /* file name */ size_t max_size; /* file max size */ + int max_rotate; /* max rotate file count */ } ElogFileCfg; /* elog_file.c */ 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 */ diff --git a/easylogger/plugins/file/elog_file_cfg.h b/easylogger/plugins/file/elog_file_cfg.h index c1d6973..2454ef8 100644 --- a/easylogger/plugins/file/elog_file_cfg.h +++ b/easylogger/plugins/file/elog_file_cfg.h @@ -35,4 +35,7 @@ /* EasyLogger file log plugin's using file max size */ #define ELOG_FILE_MAX_SIZE /* @note you must define it for a value */ +/* EasyLogger file log plugin's using max rotate file count */ +#define ELOG_FILE_MAX_ROTATE /* @note you must define it for a value */ + #endif /* _ELOG_FILE_CFG_H_ */