From 8f8812e90d6c606629b11b2ebc89165b3c501d5c Mon Sep 17 00:00:00 2001 From: "konishi.zhang" Date: Thu, 21 May 2020 14:24:32 +0000 Subject: [PATCH 1/6] add notice log level Signed-off-by: konishi Changes to be committed: modified: demo/os/linux/main.c modified: easylogger/inc/elog.h modified: easylogger/inc/elog_cfg.h modified: easylogger/src/elog.c --- demo/os/linux/main.c | 2 ++ easylogger/inc/elog.h | 20 +++++++++++++++++--- easylogger/inc/elog_cfg.h | 1 + easylogger/src/elog.c | 5 +++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/demo/os/linux/main.c b/demo/os/linux/main.c index 76b40f5..c3f96f6 100644 --- a/demo/os/linux/main.c +++ b/demo/os/linux/main.c @@ -44,6 +44,7 @@ int main(void) { elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL); elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); + elog_set_fmt(ELOG_LVL_NOTICE, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME); elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC); elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC); @@ -86,6 +87,7 @@ void test_elog(void) { log_a("Hello EasyLogger!"); log_e("Hello EasyLogger!"); log_w("Hello EasyLogger!"); + log_n("Hello EasyLogger!"); log_i("Hello EasyLogger!"); log_d("Hello EasyLogger!"); log_v("Hello EasyLogger!"); diff --git a/easylogger/inc/elog.h b/easylogger/inc/elog.h index 692285c..792bc53 100644 --- a/easylogger/inc/elog.h +++ b/easylogger/inc/elog.h @@ -42,9 +42,10 @@ extern "C" { #define ELOG_LVL_ASSERT 0 #define ELOG_LVL_ERROR 1 #define ELOG_LVL_WARN 2 -#define ELOG_LVL_INFO 3 -#define ELOG_LVL_DEBUG 4 -#define ELOG_LVL_VERBOSE 5 +#define ELOG_LVL_NOTICE 3 +#define ELOG_LVL_INFO 4 +#define ELOG_LVL_DEBUG 5 +#define ELOG_LVL_VERBOSE 6 /* the output silent level and all level for filter setting */ #define ELOG_FILTER_LVL_SILENT ELOG_LVL_ASSERT @@ -101,6 +102,13 @@ extern "C" { #define elog_warn(tag, ...) #endif /* ELOG_OUTPUT_LVL >= ELOG_LVL_WARN */ + #if ELOG_OUTPUT_LVL >= ELOG_LVL_NOTICE + #define elog_notice(tag, ...) \ + elog_output(ELOG_LVL_NOTICE, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) + #else + #define elog_notice(tag, ...) + #endif /* ELOG_OUTPUT_LVL >= ELOG_LVL_NOTICE */ + #if ELOG_OUTPUT_LVL >= ELOG_LVL_INFO #define elog_info(tag, ...) \ elog_output(ELOG_LVL_INFO, tag, __FILE__, __FUNCTION__, __LINE__, __VA_ARGS__) @@ -202,6 +210,7 @@ void elog_hexdump(const char *name, uint8_t width, uint8_t *buf, uint16_t size); #define elog_a(tag, ...) elog_assert(tag, __VA_ARGS__) #define elog_e(tag, ...) elog_error(tag, __VA_ARGS__) #define elog_w(tag, ...) elog_warn(tag, __VA_ARGS__) +#define elog_n(tag, ...) elog_notice(tag, __VA_ARGS__) #define elog_i(tag, ...) elog_info(tag, __VA_ARGS__) #define elog_d(tag, ...) elog_debug(tag, __VA_ARGS__) #define elog_v(tag, ...) elog_verbose(tag, __VA_ARGS__) @@ -231,6 +240,11 @@ void elog_hexdump(const char *name, uint8_t width, uint8_t *buf, uint16_t size); #else #define log_w(...) ((void)0); #endif +#if LOG_LVL >= ELOG_LVL_NOTICE + #define log_n(...) elog_n(LOG_TAG, __VA_ARGS__) +#else + #define log_n(...) ((void)0); +#endif #if LOG_LVL >= ELOG_LVL_INFO #define log_i(...) elog_i(LOG_TAG, __VA_ARGS__) #else diff --git a/easylogger/inc/elog_cfg.h b/easylogger/inc/elog_cfg.h index 91faf67..46f94e5 100644 --- a/easylogger/inc/elog_cfg.h +++ b/easylogger/inc/elog_cfg.h @@ -54,6 +54,7 @@ #define ELOG_COLOR_ASSERT (F_MAGENTA B_NULL S_NORMAL) #define ELOG_COLOR_ERROR (F_RED B_NULL S_NORMAL) #define ELOG_COLOR_WARN (F_YELLOW B_NULL S_NORMAL) +#define ELOG_COLOR_NOTICE (F_CYAN B_NULL S_NORMAL) #define ELOG_COLOR_INFO (F_CYAN B_NULL S_NORMAL) #define ELOG_COLOR_DEBUG (F_GREEN B_NULL S_NORMAL) #define ELOG_COLOR_VERBOSE (F_BLUE B_NULL S_NORMAL) diff --git a/easylogger/src/elog.c b/easylogger/src/elog.c index 828c42e..a2c10b7 100644 --- a/easylogger/src/elog.c +++ b/easylogger/src/elog.c @@ -103,6 +103,9 @@ #ifndef ELOG_COLOR_WARN #define ELOG_COLOR_WARN (F_YELLOW B_NULL S_NORMAL) #endif +#ifndef ELOG_COLOR_NOTICE +#define ELOG_COLOR_NOTICE (F_CYAN B_NULL S_NORMAL) +#endif #ifndef ELOG_COLOR_INFO #define ELOG_COLOR_INFO (F_CYAN B_NULL S_NORMAL) #endif @@ -123,6 +126,7 @@ static const char *level_output_info[] = { [ELOG_LVL_ASSERT] = "A/", [ELOG_LVL_ERROR] = "E/", [ELOG_LVL_WARN] = "W/", + [ELOG_LVL_NOTICE] = "N/", [ELOG_LVL_INFO] = "I/", [ELOG_LVL_DEBUG] = "D/", [ELOG_LVL_VERBOSE] = "V/", @@ -134,6 +138,7 @@ static const char *color_output_info[] = { [ELOG_LVL_ASSERT] = ELOG_COLOR_ASSERT, [ELOG_LVL_ERROR] = ELOG_COLOR_ERROR, [ELOG_LVL_WARN] = ELOG_COLOR_WARN, + [ELOG_LVL_NOTICE] = ELOG_COLOR_NOTICE, [ELOG_LVL_INFO] = ELOG_COLOR_INFO, [ELOG_LVL_DEBUG] = ELOG_COLOR_DEBUG, [ELOG_LVL_VERBOSE] = ELOG_COLOR_VERBOSE, -- Gitee From 1791f30eff683eea6301d7d974045ac12eea439c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E7=AE=80=E7=BE=8E?= Date: Fri, 5 Jun 2020 11:05:08 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=8D=A2=E8=A1=8C=E7=9A=84=E6=93=8D=E4=BD=9C=EF=BC=8C=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E7=9A=84=E8=BE=93=E5=87=BA=EF=BC=8C=E6=8D=A2=E8=A1=8C?= =?UTF-8?q?=E6=9C=80=E5=A5=BD=E6=98=AF=E8=B0=83=E7=94=A8=E8=80=85=E6=9D=A5?= =?UTF-8?q?=E5=86=B3=E5=AE=9A=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easylogger/src/elog.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/easylogger/src/elog.c b/easylogger/src/elog.c index a2c10b7..824ffb2 100644 --- a/easylogger/src/elog.c +++ b/easylogger/src/elog.c @@ -219,7 +219,7 @@ void elog_start(void) { #endif /* show version */ - log_i("EasyLogger V%s is initialize success.", ELOG_SW_VERSION); + log_i("EasyLogger V%s is initialize success.\n", ELOG_SW_VERSION); } /** @@ -657,7 +657,11 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f #endif /* package newline sign */ - log_len += elog_strcpy(log_len, log_buf + log_len, ELOG_NEWLINE_SIGN); + if(log_len == EELOG_LINE_BUF_SIZE - 1) + { + log_len--; + log_len += elog_strcpy(log_len, log_buf + log_len, ELOG_NEWLINE_SIGN); + } /* output log */ #if defined(ELOG_ASYNC_OUTPUT_ENABLE) extern void elog_async_output(uint8_t level, const char *log, size_t size); -- Gitee From b32c02f829f937622fa92917db2da339211bf0f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E7=AE=80=E7=BE=8E?= Date: Fri, 5 Jun 2020 11:14:46 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E6=A0=BC=E5=BC=8F=EF=BC=8C=E5=85=88=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- easylogger/src/elog.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/easylogger/src/elog.c b/easylogger/src/elog.c index 824ffb2..cff6f3e 100644 --- a/easylogger/src/elog.c +++ b/easylogger/src/elog.c @@ -548,6 +548,11 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f } #endif + /* package time info */ + if (get_fmt_enabled(level, ELOG_FMT_TIME)) { + log_len += elog_strcpy(log_len, log_buf + log_len, elog_port_get_time()); + log_len += elog_strcpy(log_len, log_buf + log_len, "ms "); + } /* package level info */ if (get_fmt_enabled(level, ELOG_FMT_LVL)) { log_len += elog_strcpy(log_len, log_buf + log_len, level_output_info[level]); @@ -563,15 +568,8 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f log_len += elog_strcpy(log_len, log_buf + log_len, " "); } /* package time, process and thread info */ - if (get_fmt_enabled(level, ELOG_FMT_TIME | ELOG_FMT_P_INFO | ELOG_FMT_T_INFO)) { + if (get_fmt_enabled(level, ELOG_FMT_P_INFO | ELOG_FMT_T_INFO)) { log_len += elog_strcpy(log_len, log_buf + log_len, "["); - /* package time info */ - if (get_fmt_enabled(level, ELOG_FMT_TIME)) { - log_len += elog_strcpy(log_len, log_buf + log_len, elog_port_get_time()); - if (get_fmt_enabled(level, ELOG_FMT_P_INFO | ELOG_FMT_T_INFO)) { - log_len += elog_strcpy(log_len, log_buf + log_len, " "); - } - } /* package process info */ if (get_fmt_enabled(level, ELOG_FMT_P_INFO)) { log_len += elog_strcpy(log_len, log_buf + log_len, elog_port_get_p_info()); @@ -611,6 +609,7 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f } log_len += elog_strcpy(log_len, log_buf + log_len, ")"); } + log_len += elog_strcpy(log_len, log_buf + log_len, " - "); /* package other log data to buffer. '\0' must be added in the end by vsnprintf. */ fmt_result = vsnprintf(log_buf + log_len, ELOG_LINE_BUF_SIZE - log_len, format, args); @@ -657,7 +656,7 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f #endif /* package newline sign */ - if(log_len == EELOG_LINE_BUF_SIZE - 1) + if(log_len == ELOG_LINE_BUF_SIZE - 1) { log_len--; log_len += elog_strcpy(log_len, log_buf + log_len, ELOG_NEWLINE_SIGN); -- Gitee From fddc769637dc55f24a12bc4b4604053fbacadb0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=81=E7=AE=80=E7=BE=8E?= Date: Fri, 5 Jun 2020 11:18:20 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=B5=8C=E5=85=A5=E5=BC=8F=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=BA=93=EF=BC=8C=E5=8E=BB=E6=8E=89=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=B7=AF=E5=BE=84=E6=89=93=E5=8D=B0?= 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 cff6f3e..06b7336 100644 --- a/easylogger/src/elog.c +++ b/easylogger/src/elog.c @@ -588,7 +588,7 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f log_len += elog_strcpy(log_len, log_buf + log_len, "("); /* package time info */ if (get_fmt_enabled(level, ELOG_FMT_DIR)) { - log_len += elog_strcpy(log_len, log_buf + log_len, file); + log_len += elog_strcpy(log_len, log_buf + log_len, strrchr(file,'/')?strrchr(file,'/')+1:file); if (get_fmt_enabled(level, ELOG_FMT_FUNC)) { log_len += elog_strcpy(log_len, log_buf + log_len, " "); } else if (get_fmt_enabled(level, ELOG_FMT_LINE)) { -- Gitee From aa2dc990644d791115507add48609a53247009c5 Mon Sep 17 00:00:00 2001 From: "konishi.zhang" Date: Tue, 30 Jun 2020 05:45:27 +0000 Subject: [PATCH 5/6] fix level bug, and compiler warning Signed-off-by: konishi Changes to be committed: modified: inc/elog.h modified: src/elog.c --- easylogger/inc/elog.h | 2 +- easylogger/src/elog.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/easylogger/inc/elog.h b/easylogger/inc/elog.h index 792bc53..79aa081 100644 --- a/easylogger/inc/elog.h +++ b/easylogger/inc/elog.h @@ -52,7 +52,7 @@ extern "C" { #define ELOG_FILTER_LVL_ALL ELOG_LVL_VERBOSE /* output log's level total number */ -#define ELOG_LVL_TOTAL_NUM 6 +#define ELOG_LVL_TOTAL_NUM 7 /* EasyLogger software version number */ #define ELOG_SW_VERSION "2.2.99" diff --git a/easylogger/src/elog.c b/easylogger/src/elog.c index 06b7336..6c765f2 100644 --- a/easylogger/src/elog.c +++ b/easylogger/src/elog.c @@ -146,7 +146,7 @@ static const char *color_output_info[] = { #endif /* ELOG_COLOR_ENABLE */ static bool get_fmt_enabled(uint8_t level, size_t set); -static void elog_set_filter_tag_lvl_default(); +static void elog_set_filter_tag_lvl_default(void); /* EasyLogger assert hook */ void (*elog_assert_hook)(const char* expr, const char* func, size_t line); @@ -345,7 +345,7 @@ void elog_output_unlock(void) { /** * set log filter's tag level val to default */ -static void elog_set_filter_tag_lvl_default() +static void elog_set_filter_tag_lvl_default(void) { uint8_t i = 0; @@ -581,7 +581,7 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f if (get_fmt_enabled(level, ELOG_FMT_T_INFO)) { log_len += elog_strcpy(log_len, log_buf + log_len, elog_port_get_t_info()); } - log_len += elog_strcpy(log_len, log_buf + log_len, "] "); + log_len += elog_strcpy(log_len, log_buf + log_len, "]"); } /* package file directory and name, function name and line number info */ if (get_fmt_enabled(level, ELOG_FMT_DIR | ELOG_FMT_FUNC | ELOG_FMT_LINE)) { -- Gitee From b563592e02ca0648e830049057d26e9b4704f268 Mon Sep 17 00:00:00 2001 From: "konishi.zhang" Date: Tue, 30 Jun 2020 06:07:56 +0000 Subject: [PATCH 6/6] keep synchronization with https://gitee.com/Armink/EasyLogger Signed-off-by: konishi.zhang Changes to be committed: modified: README.md modified: demo/os/linux/Makefile renamed: demo/os/linux/easylogger/plugins/file/elog_file_cfg.h -> demo/os/linux/easylogger/inc/elog_file_cfg.h renamed: demo/os/linux/easylogger/plugins/file/elog_file_port.c -> demo/os/linux/easylogger/port/elog_file_port.c modified: demo/os/linux/easylogger/port/elog_port.c new file: demo/os/windows/.gitignore modified: demo/os/windows/README.md modified: demo/os/windows/easylogger/inc/elog_cfg.h new file: demo/os/windows/easylogger/inc/elog_file_cfg.h new file: demo/os/windows/easylogger/port/elog_file_port.c modified: demo/os/windows/easylogger/port/elog_port.c modified: demo/os/windows/main.c modified: demo/os/windows/make.bat deleted: demo/os/windows/out/.gitignore modified: easylogger/plugins/file/elog_file.c modified: easylogger/plugins/file/elog_file.h --- README.md | 2 +- demo/os/linux/Makefile | 3 +- .../{plugins/file => inc}/elog_file_cfg.h | 4 +- .../{plugins/file => port}/elog_file_port.c | 4 +- demo/os/linux/easylogger/port/elog_port.c | 2 +- demo/os/windows/.gitignore | 2 + demo/os/windows/README.md | 2 + demo/os/windows/easylogger/inc/elog_cfg.h | 4 + .../os/windows/easylogger/inc/elog_file_cfg.h | 41 +++++++ .../windows/easylogger/port/elog_file_port.c | 65 +++++++++++ demo/os/windows/easylogger/port/elog_port.c | 21 +++- demo/os/windows/main.c | 2 +- demo/os/windows/make.bat | 6 +- demo/os/windows/out/.gitignore | 2 - easylogger/plugins/file/elog_file.c | 102 +++++++----------- easylogger/plugins/file/elog_file.h | 1 + 16 files changed, 182 insertions(+), 81 deletions(-) rename demo/os/linux/easylogger/{plugins/file => inc}/elog_file_cfg.h (95%) rename demo/os/linux/easylogger/{plugins/file => port}/elog_file_port.c (98%) create mode 100644 demo/os/windows/.gitignore create mode 100644 demo/os/windows/easylogger/inc/elog_file_cfg.h create mode 100644 demo/os/windows/easylogger/port/elog_file_port.c delete mode 100644 demo/os/windows/out/.gitignore diff --git a/README.md b/README.md index d99cdda..73499fb 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ --- -# 1. 介绍([English](https://github.com/armink/EasyFlash#1-introduction)) +# 1. 介绍 [EasyLogger](https://github.com/armink/EasyLogger) 是一款超轻量级(ROM<1.6K, RAM<0.3K)、高性能的 C/C++ 日志库,非常适合对资源敏感的软件项目,例如: IoT 产品、可穿戴设备、智能家居等等。相比 log4c、zlog 这些知名的 C/C++ 日志库, EasyLogger 的功能更加简单,提供给用户的接口更少,但上手会很快,更多实用功能支持以插件形式进行动态扩展。 diff --git a/demo/os/linux/Makefile b/demo/os/linux/Makefile index 25cf136..f49cd49 100755 --- a/demo/os/linux/Makefile +++ b/demo/os/linux/Makefile @@ -1,14 +1,13 @@ CC = cc ROOTPATH=../../.. -INCLUDE = -I./easylogger/inc -I./easylogger/plugins/ -I$(ROOTPATH)/easylogger/plugins/ -I$(ROOTPATH)/easylogger/inc +INCLUDE = -I./easylogger/inc -I$(ROOTPATH)/easylogger/inc -I$(ROOTPATH)/easylogger/plugins/file LIB=-lpthread OBJ += $(patsubst %.c, %.o, $(wildcard *.c)) OBJ += $(patsubst %.c, %.o, $(wildcard $(ROOTPATH)/easylogger/src/*.c)) OBJ += $(patsubst %.c, %.o, $(wildcard $(ROOTPATH)/easylogger/plugins/file/elog_file.c)) OBJ += $(patsubst %.c, %.o, $(wildcard easylogger/port/*.c)) -OBJ += $(patsubst %.c, %.o, $(wildcard easylogger/plugins/file/*.c)) CFLAGS = -O0 -g3 -Wall target = EasyLoggerLinuxDemo diff --git a/demo/os/linux/easylogger/plugins/file/elog_file_cfg.h b/demo/os/linux/easylogger/inc/elog_file_cfg.h similarity index 95% rename from demo/os/linux/easylogger/plugins/file/elog_file_cfg.h rename to demo/os/linux/easylogger/inc/elog_file_cfg.h index f43f98a..b3b8f22 100644 --- a/demo/os/linux/easylogger/plugins/file/elog_file_cfg.h +++ b/demo/os/linux/easylogger/inc/elog_file_cfg.h @@ -33,9 +33,9 @@ #define ELOG_FILE_NAME "/tmp/elog_file.log" /* EasyLogger file log plugin's using file max size */ -#define ELOG_FILE_MAX_SIZE (10 * 1024 * 1024) +#define ELOG_FILE_MAX_SIZE (1 * 1024 * 1024) /* EasyLogger file log plugin's using max rotate file count */ -#define ELOG_FILE_MAX_ROTATE 10 +#define ELOG_FILE_MAX_ROTATE 5 #endif /* _ELOG_FILE_CFG_H_ */ diff --git a/demo/os/linux/easylogger/plugins/file/elog_file_port.c b/demo/os/linux/easylogger/port/elog_file_port.c similarity index 98% rename from demo/os/linux/easylogger/plugins/file/elog_file_port.c rename to demo/os/linux/easylogger/port/elog_file_port.c index b3fbec0..a08347e 100644 --- a/demo/os/linux/easylogger/plugins/file/elog_file_port.c +++ b/demo/os/linux/easylogger/port/elog_file_port.c @@ -35,8 +35,8 @@ #include -#include -#include +#include +#include #define ELOG_FILE_SEM_KEY ((key_t)0x19910612) #ifdef _SEM_SEMUN_UNDEFINED diff --git a/demo/os/linux/easylogger/port/elog_port.c b/demo/os/linux/easylogger/port/elog_port.c index 0721b05..cde29bc 100644 --- a/demo/os/linux/easylogger/port/elog_port.c +++ b/demo/os/linux/easylogger/port/elog_port.c @@ -33,7 +33,7 @@ #include #ifdef ELOG_FILE_ENABLE -#include +#include #endif static pthread_mutex_t output_lock; diff --git a/demo/os/windows/.gitignore b/demo/os/windows/.gitignore new file mode 100644 index 0000000..40f07dc --- /dev/null +++ b/demo/os/windows/.gitignore @@ -0,0 +1,2 @@ +out +elog_file.* diff --git a/demo/os/windows/README.md b/demo/os/windows/README.md index b557405..d1a2f10 100644 --- a/demo/os/windows/README.md +++ b/demo/os/windows/README.md @@ -6,6 +6,8 @@ ʹGCC롣ͨ `main.c` `test_elog()` ־ +ĿǰԶ file Զ־洢ļ + ### 1.1ʹ÷ ʹǰǰúñ뻷óɹ󣬵 `make.bat` űȴɺ󣬴 `out\EasyLoggerWinDemo.exe` ɿн diff --git a/demo/os/windows/easylogger/inc/elog_cfg.h b/demo/os/windows/easylogger/inc/elog_cfg.h index 54301bd..de2b31a 100644 --- a/demo/os/windows/easylogger/inc/elog_cfg.h +++ b/demo/os/windows/easylogger/inc/elog_cfg.h @@ -31,6 +31,10 @@ /* enable log output. default open this macro */ #define ELOG_OUTPUT_ENABLE +/* enable log write file. default open this macro */ +#define ELOG_FILE_ENABLE +/* enable flush file cache. default open this macro */ +#define ELOG_FILE_FLUSH_CAHCE_ENABLE /* setting static output log level */ #define ELOG_OUTPUT_LVL ELOG_LVL_VERBOSE /* enable assert check */ diff --git a/demo/os/windows/easylogger/inc/elog_file_cfg.h b/demo/os/windows/easylogger/inc/elog_file_cfg.h new file mode 100644 index 0000000..0d34e15 --- /dev/null +++ b/demo/os/windows/easylogger/inc/elog_file_cfg.h @@ -0,0 +1,41 @@ +/* + * This file is part of the EasyLogger Library. + * + * Copyright (c) 2015-2019, Qintl, + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * 'Software'), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * Function: It is the configure head file for this flash log plugin. + * Created on: 2019-01-05 + */ + +#ifndef _ELOG_FILE_CFG_H_ +#define _ELOG_FILE_CFG_H_ + +/* EasyLogger file log plugin's using file name */ +#define ELOG_FILE_NAME "elog_file.log" + +/* EasyLogger file log plugin's using file max size */ +#define ELOG_FILE_MAX_SIZE (1 * 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/demo/os/windows/easylogger/port/elog_file_port.c b/demo/os/windows/easylogger/port/elog_file_port.c new file mode 100644 index 0000000..2e44bc6 --- /dev/null +++ b/demo/os/windows/easylogger/port/elog_file_port.c @@ -0,0 +1,65 @@ +/* + * This file is part of the EasyLogger Library. + * + * Copyright (c) 2015-2019, Qintl, + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * 'Software'), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * Function: Portable interface for EasyLogger's file log pulgin. + * Created on: 2019-01-05 + */ + +#include + +/** + * EasyLogger flile log pulgin port initialize + * + * @return result + */ +ElogErrCode elog_file_port_init(void) { + ElogErrCode result = ELOG_NO_ERR; + + /* do noting, using elog_port.c's locker only */ + + return result; +} + +/** + * file log lock + */ +void elog_file_port_lock(void) +{ + /* do noting, using elog_port.c's locker only */ +} + +/** + * file log unlock + */ +void elog_file_port_unlock(void) +{ + /* do noting, using elog_port.c's locker only */ +} +/** + * file log deinit + */ +void elog_file_port_deinit(void) +{ + /* do noting, using elog_port.c's locker only */ +} diff --git a/demo/os/windows/easylogger/port/elog_port.c b/demo/os/windows/easylogger/port/elog_port.c index 26969b5..68b43ec 100644 --- a/demo/os/windows/easylogger/port/elog_port.c +++ b/demo/os/windows/easylogger/port/elog_port.c @@ -28,10 +28,13 @@ #include #include -#include #include -static pthread_mutex_t output_lock; +#ifdef ELOG_FILE_ENABLE +#include +#endif + +static HANDLE output_lock = NULL; /** * EasyLogger port initialize @@ -41,8 +44,12 @@ static pthread_mutex_t output_lock; ElogErrCode elog_port_init(void) { ElogErrCode result = ELOG_NO_ERR; - pthread_mutex_init(&output_lock, NULL); + output_lock = CreateMutex(NULL, FALSE, NULL); +#ifdef ELOG_FILE_ENABLE + elog_file_init(); +#endif + return result; } @@ -55,20 +62,24 @@ ElogErrCode elog_port_init(void) { void elog_port_output(const char *log, size_t size) { /* output to terminal */ printf("%.*s", size, log); +#ifdef ELOG_FILE_ENABLE + /* write the file */ + elog_file_write(log, size); +#endif } /** * output lock */ void elog_port_output_lock(void) { - pthread_mutex_lock(&output_lock); + WaitForSingleObject(output_lock, INFINITE); } /** * output unlock */ void elog_port_output_unlock(void) { - pthread_mutex_unlock(&output_lock); + ReleaseMutex( output_lock ); } diff --git a/demo/os/windows/main.c b/demo/os/windows/main.c index 3a860bf..80afb4b 100644 --- a/demo/os/windows/main.c +++ b/demo/os/windows/main.c @@ -80,6 +80,6 @@ void test_elog(void) { log_d("Hello EasyLogger!"); log_v("Hello EasyLogger!"); // elog_raw("Hello EasyLogger!"); - Sleep(5000); + Sleep(1000); } } diff --git a/demo/os/windows/make.bat b/demo/os/windows/make.bat index 4b0cabd..828b5a5 100644 --- a/demo/os/windows/make.bat +++ b/demo/os/windows/make.bat @@ -1,5 +1,7 @@ gcc -I "easylogger\inc" -I "..\..\..\easylogger\inc" -O0 -g3 -Wall -c "..\..\..\easylogger\src\elog.c" -o "out\elog.o" -gcc -I "easylogger\inc" -I "..\..\..\easylogger\inc" -O0 -g3 -Wall -c "easylogger\port\elog_port.c" -o "out\elog_port.o" +gcc -I "easylogger\inc" -I "..\..\..\easylogger\inc" -I "..\..\..\easylogger\plugins\file" -O0 -g3 -Wall -c "easylogger\port\elog_port.c" -o "out\elog_port.o" gcc -I "easylogger\inc" -I "..\..\..\easylogger\inc" -O0 -g3 -Wall -c "..\..\..\easylogger\src\elog_utils.c" -o "out\elog_utils.o" +gcc -I "easylogger\inc" -I "..\..\..\easylogger\inc" -O0 -g3 -Wall -c "..\..\..\easylogger\plugins\file\elog_file.c" -o "out\elog_file.o" +gcc -I "easylogger\inc" -I "..\..\..\easylogger\inc" -I "..\..\..\easylogger\plugins\file" -O0 -g3 -Wall -c "..\..\..\easylogger\plugins\file\elog_file_port.c" -o "out\elog_file_port.o" gcc -I "easylogger\inc" -I "..\..\..\easylogger\inc" -O0 -g3 -Wall -c "main.c" -o "out\main.o" -gcc -o out\EasyLoggerWinDemo.exe "out\main.o" "out\elog_utils.o" "out\elog.o" "out\elog_port.o" -lpthread +gcc -o out\EasyLoggerWinDemo.exe "out\main.o" "out\elog_utils.o" "out\elog.o" "out\elog_port.o" "out\elog_file.o" "out\elog_file_port.o" diff --git a/demo/os/windows/out/.gitignore b/demo/os/windows/out/.gitignore deleted file mode 100644 index 25a7384..0000000 --- a/demo/os/windows/out/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -*.o -*.exe diff --git a/easylogger/plugins/file/elog_file.c b/easylogger/plugins/file/elog_file.c index 0f93108..e990db8 100644 --- a/easylogger/plugins/file/elog_file.c +++ b/easylogger/plugins/file/elog_file.c @@ -26,21 +26,18 @@ * Created on: 2019-01-05 */ -#include -#include -#include -#include + #define LOG_TAG "elog.file" + +#include #include #include #include -#include -#include +#include "elog_file.h" /* initialize OK flag */ static bool init_ok = false; static FILE *fp = NULL; -static int fd = -1; static ElogFileCfg local_cfg; ElogErrCode elog_file_init(void) @@ -64,88 +61,71 @@ __exit: return result; } -/* - * Reopen file - */ -static bool elog_file_reopen(void) -{ - FILE *tmp_fp; - - tmp_fp = fopen(local_cfg.name, "a+"); - if (tmp_fp) { - if (fp) - fclose(fp); - - fp = tmp_fp; - fd = fileno(fp); - return true; - } - - return false; -} - /* * rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */ -static void elog_file_rotate(void) +static bool elog_file_rotate(void) { #define SUFFIX_LEN 10 /* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */ - int n; + int n, err = 0; char oldpath[256], newpath[256]; size_t base = strlen(local_cfg.name); + bool result = true; + FILE *tmp_fp; memcpy(oldpath, local_cfg.name, base); memcpy(newpath, local_cfg.name, base); + fclose(fp); + 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); - } -} + /* remove the old file */ + if ((tmp_fp = fopen(newpath , "r")) != NULL) { + fclose(tmp_fp); + remove(newpath); + } + /* change the new log file to old file name */ + if ((tmp_fp = fopen(oldpath , "r")) != NULL) { + fclose(tmp_fp); + err = rename(oldpath, newpath); + } -/* - * Check if it needed retate - */ -static bool elog_file_retate_check(void) -{ - struct stat statbuf; - statbuf.st_size = 0; - if (stat(local_cfg.name, &statbuf) < 0) - return false; + if (err < 0) { + result = false; + goto __exit; + } + } - if (statbuf.st_size > local_cfg.max_size) - return true; +__exit: + /* reopen the file */ + fp = fopen(local_cfg.name, "a+"); - return false; + return result; } + void elog_file_write(const char *log, size_t size) { + size_t file_size = 0; + ELOG_ASSERT(init_ok); ELOG_ASSERT(log); - struct stat statbuf; - - statbuf.st_size = 0; elog_file_port_lock(); - fstat(fd, &statbuf); + fseek(fp, 0L, SEEK_END); + file_size = ftell(fp); - if (unlikely(statbuf.st_size > local_cfg.max_size)) { + if (unlikely(file_size > local_cfg.max_size)) { #if ELOG_FILE_MAX_ROTATE > 0 - if (elog_file_retate_check()) { - /* rotate the log file */ - elog_file_rotate(); - } - - if (!elog_file_reopen()) { - elog_file_port_unlock(); - return; + if (!elog_file_rotate()) { + goto __exit; } #else - return ; + goto __exit; #endif } @@ -153,9 +133,9 @@ void elog_file_write(const char *log, size_t size) #ifdef ELOG_FILE_FLUSH_CAHCE_ENABLE fflush(fp); - fsync(fd); #endif +__exit: elog_file_port_unlock(); } @@ -180,10 +160,6 @@ void elog_file_config(ElogFileCfg *cfg) 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(); } diff --git a/easylogger/plugins/file/elog_file.h b/easylogger/plugins/file/elog_file.h index 11df802..8b03f28 100644 --- a/easylogger/plugins/file/elog_file.h +++ b/easylogger/plugins/file/elog_file.h @@ -31,6 +31,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { -- Gitee