diff --git a/README.md b/README.md index d99cddae0ab69495175f45a5be553c56d7ab1f0e..73499fbbaea05426dc9d74d33d74151845b043a4 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 25cf1368c279cedcc873a1e60cd420bd002dc293..f49cd49f4ea761cdcba7b753ce83b93d0689d32f 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 f43f98a44e47290de340f50863f9d62032852598..b3b8f229391211de517e5d7fa64df3148296d6ca 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 b3fbec04a45b38b9d070f4a082d7eaaf16079c81..a08347ecd0356197f3e7048832b5777d2bae4ebe 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 0721b05b8ebb136b18f30d2feb798c489f7974de..cde29bcf8890b6a9d620e84cbfcaff3ecf7117c5 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/linux/main.c b/demo/os/linux/main.c index 76b40f57776b8fad81f18211e0eb5a639b18bbbb..c3f96f6b29c29f97f9782aec635fc33dd556dfbc 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/demo/os/windows/.gitignore b/demo/os/windows/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..40f07dc3fff39b4234d1c6c7f6b5b73eead17a35 --- /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 b5574057cc6f4d0486ada7d546de0a5be858ab18..d1a2f10d5ce548cbf2bc64cd781d66882917c662 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 54301bd3a0f6a3c615e8e069dbaa5021e3623369..de2b31a6fbd67aa955fab285e8d2e248d5efdbe6 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 0000000000000000000000000000000000000000..0d34e15d8bd54f738ee1441db44ac4c1bfda76aa --- /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 0000000000000000000000000000000000000000..2e44bc658dcc65385720939a2c4bfcf1d4d72ce9 --- /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 26969b5bd9ae631f3b0e723a308cd4ff65e3ee85..68b43ec0e8d05115c497f1a3a2442428415d2d3b 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 3a860bf2e8bfd865806ad17367a8144994fc6a35..80afb4b9704213f4a2559b9b5fa1f159a6370e40 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 4b0cabdaf1736925ecb21ef2a88c7205912590ad..828b5a5980e3d752b5c303204c6501a92d42b515 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 25a7384d6c6ffe73c8cd21527b89d688f5970815..0000000000000000000000000000000000000000 --- a/demo/os/windows/out/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -*.o -*.exe diff --git a/easylogger/inc/elog.h b/easylogger/inc/elog.h index 692285c30450c1e1ae400d4dc8a9fec3773756c4..79aa081df410d657cafeb1fe98dd705ea3f2f47e 100644 --- a/easylogger/inc/elog.h +++ b/easylogger/inc/elog.h @@ -42,16 +42,17 @@ 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 #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" @@ -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 91faf67b52406b1ca8e69e905cb568dba1175d5e..46f94e546f4bad829155ab89815464832cc890d1 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/plugins/file/elog_file.c b/easylogger/plugins/file/elog_file.c index 0f93108de1b5132a1d5862b143f74e40b8a63414..e990db8cf2068c37a2b7ea0e1564ec2b4f460aca 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 11df80255e00563e4b2b688dd612e7b42583f8f6..8b03f28cda51a7835b94204fc07ebebf4802b352 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" { diff --git a/easylogger/src/elog.c b/easylogger/src/elog.c index 828c42e46d5b4b4fd530bbe581512f240bbb8af1..6c765f227074ceb78bdb23a04e4b4aa4d4536d1f 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, @@ -141,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); @@ -214,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); } /** @@ -340,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; @@ -543,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]); @@ -558,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()); @@ -578,14 +581,14 @@ 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)) { 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)) { @@ -606,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); @@ -652,7 +656,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 == ELOG_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);