From 1507a7b611992fcd2f323c1c944ff015516a2277 Mon Sep 17 00:00:00 2001 From: Jin-W-FS Date: Thu, 14 Feb 2019 20:57:01 +0800 Subject: [PATCH 1/6] 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_ */ From 79044bcc674fa7727be21ce5c80b543e45e30165 Mon Sep 17 00:00:00 2001 From: armink Date: Fri, 15 Feb 2019 17:18:07 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E3=80=90=E5=AE=8C=E5=96=84=E3=80=91?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=96=87=E4=BB=B6=E5=BE=AA=E7=8E=AF=E5=86=99?= =?UTF-8?q?=E5=85=A5=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 */ From 6734e930fd9e56827e5c0cd13e6252dee0963d12 Mon Sep 17 00:00:00 2001 From: armink Date: Sat, 30 Mar 2019 09:26:56 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E3=80=90=E4=BF=AE=E6=AD=A3=E3=80=91ELOG=5F?= =?UTF-8?q?OUTPUT=5FENABLE=20=E6=9C=AA=E5=AE=9A=E4=B9=89=E6=97=B6=E5=AD=98?= =?UTF-8?q?=E5=9C=A8=E7=9A=84=E7=BC=96=E8=AF=91=E9=94=99=E8=AF=AF=E3=80=82?= =?UTF-8?q?=E6=84=9F=E8=B0=A2=E7=BD=91=E5=8F=8B=20@mojinpan=20=E7=9A=84?= =?UTF-8?q?=E5=8F=8D=E9=A6=88=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- easylogger/inc/elog.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/easylogger/inc/elog.h b/easylogger/inc/elog.h index caa28f4..51dcffb 100644 --- a/easylogger/inc/elog.h +++ b/easylogger/inc/elog.h @@ -1,7 +1,7 @@ /* * This file is part of the EasyLogger Library. * - * Copyright (c) 2015-2018, Armink, + * Copyright (c) 2015-2019, Armink, * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -50,7 +50,7 @@ extern "C" { #define ELOG_LVL_TOTAL_NUM 6 /* EasyLogger software version number */ -#define ELOG_SW_VERSION "2.0.3" +#define ELOG_SW_VERSION "2.1.0" /* EasyLogger assert for developer. */ #ifdef ELOG_ASSERT_ENABLE @@ -69,12 +69,12 @@ extern "C" { #endif #ifndef ELOG_OUTPUT_ENABLE - #define elog_a(tag, ...) - #define elog_e(tag, ...) - #define elog_w(tag, ...) - #define elog_i(tag, ...) - #define elog_d(tag, ...) - #define elog_v(tag, ...) + #define elog_assert(tag, ...) + #define elog_error(tag, ...) + #define elog_warn(tag, ...) + #define elog_info(tag, ...) + #define elog_debug(tag, ...) + #define elog_verbose(tag, ...) #else /* ELOG_OUTPUT_ENABLE */ #if ELOG_OUTPUT_LVL >= ELOG_LVL_ASSERT #define elog_assert(tag, ...) \ From bd2281e935e29c1df08688c88ee158a178e9805c Mon Sep 17 00:00:00 2001 From: Zhao Chong Date: Fri, 26 Jul 2019 12:28:39 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BC=82=E6=AD=A5?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E4=B8=8B=E6=A6=82=E7=8E=87=E6=80=A7=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E8=BE=93=E5=87=BA=E6=97=A5=E5=BF=97=E4=B8=8D=E5=AE=8C?= =?UTF-8?q?=E6=95=B4=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: [背景]:异步输出模式开启、断言开启,elog_async.c在初始化时,elog_async_init创建子线程完成后, 子线程处理函数async_output在一开始会对全局变量init_ok进行断言,如果为false,那么就无法进入 后续sem_wait流程,进行结果输出了;而init_ok是在子线程创建完之后,elog_async_init最后才设置 为true的,这样程序的正确执行就取决于父子线程的调度顺序了 [解决办法]:子线程的处理程序中无需对init_ok进行判断,因为在while循环体中sem_wait会保证只有在 有需要输出的内容时,才会触发后续的内容输出过程 --- easylogger/src/elog_async.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/easylogger/src/elog_async.c b/easylogger/src/elog_async.c index 6df5f3a..cade4bf 100644 --- a/easylogger/src/elog_async.c +++ b/easylogger/src/elog_async.c @@ -283,8 +283,6 @@ static void *async_output(void *arg) { size_t get_log_size = 0; static char poll_get_buf[ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE]; - ELOG_ASSERT(init_ok); - while(true) { /* waiting log */ sem_wait(&output_notice); From fbdc6c23b01daa0ebcd349bd9e0c16a76f2ad8ae Mon Sep 17 00:00:00 2001 From: armink Date: Mon, 29 Jul 2019 09:00:56 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E3=80=90=E6=9B=B4=E6=96=B0=E3=80=91?= =?UTF-8?q?=E8=BD=AF=E4=BB=B6=E7=89=88=E6=9C=AC=E5=8F=B7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- LICENSE | 2 +- easylogger/inc/elog.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index b9947cc..26ffccb 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015-2018 Armink (armink.ztl@gmail.com) +Copyright (c) 2015-2019 Armink (armink.ztl@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/easylogger/inc/elog.h b/easylogger/inc/elog.h index 51dcffb..6d0f971 100644 --- a/easylogger/inc/elog.h +++ b/easylogger/inc/elog.h @@ -50,7 +50,7 @@ extern "C" { #define ELOG_LVL_TOTAL_NUM 6 /* EasyLogger software version number */ -#define ELOG_SW_VERSION "2.1.0" +#define ELOG_SW_VERSION "2.1.99" /* EasyLogger assert for developer. */ #ifdef ELOG_ASSERT_ENABLE From 1362ff7f4ebcc0dcb422e7c9d049112666cd9c3f Mon Sep 17 00:00:00 2001 From: jq <459635274@qq.com> Date: Sat, 3 Aug 2019 16:22:32 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E8=A7=A3=E5=86=B3elog=5Fhexdump=E4=B8=AD?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=81=8F=E7=A7=BB=E5=9C=B0=E5=9D=80=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easylogger/src/elog.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/easylogger/src/elog.c b/easylogger/src/elog.c index f5b1583..945072e 100644 --- a/easylogger/src/elog.c +++ b/easylogger/src/elog.c @@ -699,7 +699,7 @@ void elog_hexdump(const char *name, uint8_t width, uint8_t *buf, uint16_t size) for (i = 0; i < size; i += width) { /* package header */ - fmt_result = snprintf(log_buf, ELOG_LINE_BUF_SIZE, "D/HEX %s: %04X-%04X: ", name, i, i + width); + fmt_result = snprintf(log_buf, ELOG_LINE_BUF_SIZE, "D/HEX %s: %04X-%04X: ", name, i, i + width - 1); /* calculate log length */ if ((fmt_result > -1) && (fmt_result <= ELOG_LINE_BUF_SIZE)) { log_len = fmt_result;