From 75cd8806b821828d87f28ce7a5af843f8258573c Mon Sep 17 00:00:00 2001 From: qintl Date: Sun, 6 Jan 2019 10:40:26 +0800 Subject: [PATCH 1/6] add plugin file Signed-off-by: qintl --- easylogger/plugins/file/elog_file.c | 38 +++++++++++++++++ easylogger/plugins/file/elog_file.h | 28 +++++++++++++ easylogger/plugins/file/elog_file_cfg.h | 13 ++++++ easylogger/plugins/file/elog_file_port.c | 52 ++++++++++++++++++++++++ 4 files changed, 131 insertions(+) create mode 100644 easylogger/plugins/file/elog_file.c create mode 100644 easylogger/plugins/file/elog_file.h create mode 100644 easylogger/plugins/file/elog_file_cfg.h create mode 100644 easylogger/plugins/file/elog_file_port.c diff --git a/easylogger/plugins/file/elog_file.c b/easylogger/plugins/file/elog_file.c new file mode 100644 index 0000000..4fd06ba --- /dev/null +++ b/easylogger/plugins/file/elog_file.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +#include "elog_file.h" + +/* initialize OK flag */ +static bool init_ok = false; + +ElogErrCode elog_file_init(void) +{ + ElogErrCode result = ELOG_NO_ERR; + if (init_ok) + goto __exit; + + elog_file_port_init(); + + init_ok = true; +__exit: + return result; +} + +void elog_file_write(const char *log, size_t size) +{ + ELOG_ASSERT(init_ok); + ELOG_ASSERT(log); + + elog_file_port_lock(); + elog_file_port_write(log, size); + elog_file_port_unlock(); +} +void elog_file_deinit(void) +{ + ELOG_ASSERT(init_ok); + elog_file_port_deinit(); +} + diff --git a/easylogger/plugins/file/elog_file.h b/easylogger/plugins/file/elog_file.h new file mode 100644 index 0000000..6ff5806 --- /dev/null +++ b/easylogger/plugins/file/elog_file.h @@ -0,0 +1,28 @@ +#ifndef __ELOG_FILE__H__ +#define __ELOG_FILE__H__ + +#include + +/* EasyLogger file log plugin's software version number */ +#define ELOG_FILE_SW_VERSION "V1.0.0" + +#ifdef __cplusplus +extern "C" { +#endif + /* elog_file.c */ + ElogErrCode elog_file_init(void); + void elog_file_write(const char *log, size_t size); + void elog_file_deinit(void); + + /* elog_file_port.c */ + ElogErrCode elog_flash_port_init(void); + void elog_file_port_write(const char *log, size_t size); + void elog_file_port_lock(void); + void elog_file_port_unlock(void); + void elog_file_port_deinit(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/easylogger/plugins/file/elog_file_cfg.h b/easylogger/plugins/file/elog_file_cfg.h new file mode 100644 index 0000000..a8afe49 --- /dev/null +++ b/easylogger/plugins/file/elog_file_cfg.h @@ -0,0 +1,13 @@ +#ifndef _ELOG_FILE_CFG_H_ +#define _ELOG_FILE_CFG_H_ + +/* EasyLogger file log plugin's using file name */ +#define ELOG_FILE_NAME + +/* EasyLogger file log plugin's using file max size */ +#define ELOG_FILE_MAX_SIZE + +/* EasyLogger file log plugin's using semaphore key */ +#define ELOG_FILE_SEM_KEY + +#endif /* _ELOG_FILE_CFG_H_ */ diff --git a/easylogger/plugins/file/elog_file_port.c b/easylogger/plugins/file/elog_file_port.c new file mode 100644 index 0000000..2878664 --- /dev/null +++ b/easylogger/plugins/file/elog_file_port.c @@ -0,0 +1,52 @@ +#include "elog_file.h" + +/** + * EasyLogger flile log pulgin port initialize + * + * @return result + */ +ElogErrCode elog_file_port_init(void) { + ElogErrCode result = ELOG_NO_ERR; + + /* add your code here */ + + return result; +} + +/** + * output file saved log port interface + * + * @param log file saved log + * @param size log size + */ +void elog_file_port_output(const char *log, size_t size) { + + /* add your code here */ + +} + +/** + * file log lock + */ +void elog_file_port_lock(void) { + + /* add your code here */ + +} + +/** + * file log unlock + */ +void elog_file_port_unlock(void) { + + /* add your code here */ + +} +/** + * file log deinit + */ +void elog_file_port_deinit(void) { + + /* add your code here */ + +} From 0d647b692ae193eaf68625d015ba8eb62c41e624 Mon Sep 17 00:00:00 2001 From: qintl Date: Sun, 6 Jan 2019 10:43:00 +0800 Subject: [PATCH 2/6] add Makefile, delete make.sh Signed-off-by: qintl --- demo/os/linux/Makefile | 20 ++++++++++++++++++++ demo/os/linux/make.sh | 8 -------- 2 files changed, 20 insertions(+), 8 deletions(-) create mode 100755 demo/os/linux/Makefile delete mode 100755 demo/os/linux/make.sh diff --git a/demo/os/linux/Makefile b/demo/os/linux/Makefile new file mode 100755 index 0000000..e84e61c --- /dev/null +++ b/demo/os/linux/Makefile @@ -0,0 +1,20 @@ +CC = cc +INCLUDE = -I./easylogger/plugins/file -I./easylogger/inc -I../../../easylogger/inc +LIB=-lpthread + +OBJ += $(patsubst %.c, %.o, $(wildcard *.c)) +OBJ += $(patsubst %.c, %.o, $(wildcard ../../../easylogger/src/*.c)) +OBJ += $(patsubst %.c, %.o, $(wildcard easylogger/port/*.c)) +OBJ += $(patsubst %.c, %.o, $(wildcard easylogger/plugins/file/*.c)) + +CFLAGS = -O0 -g3 -Wall +target = EasyLoggerLinuxDemo + +all:$(OBJ) + $(CC) out/*.o -o $(target) $(LIB) + mv $(target) out +%.o:%.c + $(CC) $(CFLAGS) -c $< -o $@ $(INCLUDE) + mv $@ out +clean: + rm -rf out/* diff --git a/demo/os/linux/make.sh b/demo/os/linux/make.sh deleted file mode 100755 index 19c6a30..0000000 --- a/demo/os/linux/make.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash -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/src/elog_async.c" -o "out/elog_async.o" -gcc -I "easylogger/inc" -I "../../../easylogger/inc" -O0 -g3 -Wall -c "../../../easylogger/src/elog_buf.c" -o "out/elog_buf.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" -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 "main.c" -o "out/main.o" -gcc -o out/EasyLoggerLinuxDemo "out/elog.o" "out/elog_async.o" "out/elog_buf.o" "out/elog_port.o" "out/elog_utils.o" "out/main.o" -lpthread From a9a7b9123ed06f5176aa981f333b740bf882d950 Mon Sep 17 00:00:00 2001 From: qintl Date: Sun, 6 Jan 2019 10:50:09 +0800 Subject: [PATCH 3/6] add linux-os plugin file support Signed-off-by: qintl --- demo/os/linux/easylogger/inc/elog_cfg.h | 2 + .../linux/easylogger/plugins/file/elog_file.c | 34 ++++ .../linux/easylogger/plugins/file/elog_file.h | 30 ++++ .../easylogger/plugins/file/elog_file_cfg.h | 13 ++ .../easylogger/plugins/file/elog_file_port.c | 167 ++++++++++++++++++ demo/os/linux/easylogger/port/elog_port.c | 9 + 6 files changed, 255 insertions(+) create mode 100644 demo/os/linux/easylogger/plugins/file/elog_file.c create mode 100644 demo/os/linux/easylogger/plugins/file/elog_file.h create mode 100644 demo/os/linux/easylogger/plugins/file/elog_file_cfg.h create mode 100644 demo/os/linux/easylogger/plugins/file/elog_file_port.c diff --git a/demo/os/linux/easylogger/inc/elog_cfg.h b/demo/os/linux/easylogger/inc/elog_cfg.h index 52c8081..778972b 100644 --- a/demo/os/linux/easylogger/inc/elog_cfg.h +++ b/demo/os/linux/easylogger/inc/elog_cfg.h @@ -31,6 +31,8 @@ /* enable log output. default open this macro */ #define ELOG_OUTPUT_ENABLE +/* enable log write file. default open this macro */ +#define ELOG_FILE_ENABLE /* setting static output log level */ #define ELOG_OUTPUT_LVL ELOG_LVL_VERBOSE /* enable assert check */ diff --git a/demo/os/linux/easylogger/plugins/file/elog_file.c b/demo/os/linux/easylogger/plugins/file/elog_file.c new file mode 100644 index 0000000..f7f2a98 --- /dev/null +++ b/demo/os/linux/easylogger/plugins/file/elog_file.c @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +#include "elog_file.h" + +/* initialize OK flag */ +static bool init_ok = false; + +ElogErrCode elog_file_init(void) +{ + ElogErrCode result = ELOG_NO_ERR; + + elog_file_port_init(); + + init_ok = true; + return result; +} + +void elog_file_write(const char *log, size_t size) +{ + ELOG_ASSERT(init_ok); + ELOG_ASSERT(log); + + elog_file_port_lock(); + elog_file_port_write(log, size); + elog_file_port_unlock(); +} + +void elog_file_deinit(void) +{ + elog_file_port_deinit(); +} diff --git a/demo/os/linux/easylogger/plugins/file/elog_file.h b/demo/os/linux/easylogger/plugins/file/elog_file.h new file mode 100644 index 0000000..6cc8bf6 --- /dev/null +++ b/demo/os/linux/easylogger/plugins/file/elog_file.h @@ -0,0 +1,30 @@ +#ifndef __ELOG_FILE__H__ +#define __ELOG_FILE__H__ + +#include + +/* EasyLogger file log plugin's software version number */ +#define ELOG_FILE_SW_VERSION "V1.0.0" + +#ifdef __cplusplus +extern "C" { +#endif + /* elog_file.c */ + ElogErrCode elog_file_init(void); + void elog_file_set_name(const char *name); + void elog_file_get_name(const char *name); + void elog_file_write(const char *log, size_t size); + void elog_file_deinit(void); + + /* elog_file_port.c */ + ElogErrCode elog_file_port_init(void); + void elog_file_port_write(const char *log, size_t size); + void elog_file_port_lock(void); + void elog_file_port_unlock(void); + void elog_file_port_deinit(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/demo/os/linux/easylogger/plugins/file/elog_file_cfg.h b/demo/os/linux/easylogger/plugins/file/elog_file_cfg.h new file mode 100644 index 0000000..42a2b36 --- /dev/null +++ b/demo/os/linux/easylogger/plugins/file/elog_file_cfg.h @@ -0,0 +1,13 @@ +#ifndef _ELOG_FILE_CFG_H_ +#define _ELOG_FILE_CFG_H_ + +/* EasyLogger file log plugin's using file name */ +#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) + +/* EasyLogger file log plugin's using semaphore key */ +#define ELOG_FILE_SEM_KEY ((key_t)0x19910612) + +#endif /* _ELOG_FILE_CFG_H_ */ diff --git a/demo/os/linux/easylogger/plugins/file/elog_file_port.c b/demo/os/linux/easylogger/plugins/file/elog_file_port.c new file mode 100644 index 0000000..9e3a2ae --- /dev/null +++ b/demo/os/linux/easylogger/plugins/file/elog_file_port.c @@ -0,0 +1,167 @@ +#include +#include +#include +#include +#include +#include + +#include + +#include "elog_file.h" +#include "elog_file_cfg.h" + +#ifdef _SEM_SEMUN_UNDEFINED +union semun { + int val; /* Value for SETVAL */ + struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ + unsigned short *array; /* Array for GETALL, SETALL */ + struct seminfo *__buf; /* Buffer for IPC_INFO + (Linux-specific) */ +}; +#endif + +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) + +static FILE *fp; +static int fd; +static int semid = -1; +static struct sembuf const up = {0, 1, SEM_UNDO}; +static struct sembuf const down = {0, -1, SEM_UNDO}; + +static void lock_init(void); +static int lock_open(void); +static inline int file_size(void); +/** + * EasyLogger flile log pulgin port initialize + * + * @return result + */ +ElogErrCode elog_file_port_init(void) { + ElogErrCode result = ELOG_NO_ERR; + + fp = fopen(ELOG_FILE_NAME, "a+"); + if (unlikely(!fp)) + goto __exit; + + lock_init(); + + fd = fileno(fp); +__exit: + return result; +} + +/** + * output file saved log port interface + * + * @param log file saved log + * @param size log size + */ +void elog_file_port_write(const char *log, size_t size) { + if(unlikely(file_size() >= ELOG_FILE_MAX_SIZE)) + return ; + + fwrite(log, size, 1, fp); + fdatasync(fd); +} + +/** + * file log lock + */ +void elog_file_port_lock(void) { + semid == -1 ? -1 : semop(semid, (struct sembuf *)&down, 1); +} + +/** + * file log unlock + */ +void elog_file_port_unlock(void) { + semid == -1 ? -1 : semop(semid, (struct sembuf *)&up, 1); +} +/** + * file log deinit + */ +void elog_file_port_deinit(void) { + fclose(fp); +} + +/** + * initialize the lock + */ +static void lock_init(void) +{ + int id, rc; + union semun arg; + struct sembuf sembuf; + + id = semget(ELOG_FILE_SEM_KEY, 1, IPC_CREAT | IPC_EXCL | 0666); + if(likely(id == -1)) { + id = lock_open(); + if (id == -1) + goto __exit; + } else { + arg.val = 0; + rc = semctl(id, 0, SETVAL, arg); + if (rc == -1) + goto __exit; + + sembuf.sem_num = 0; + sembuf.sem_op = 1; + sembuf.sem_flg = 0; + + rc = semop(semid, &sembuf, 1); + if (rc == -1) + goto __exit; + } + + semid = id; +__exit: + return ; +} + +/** + * gets the lock + */ +static int lock_open(void) +{ + int id, rc, i; + union semun arg; + struct semid_ds ds; + + id = semget(ELOG_FILE_SEM_KEY, 1, 0666); + if(unlikely(id == -1)) + goto err; + + arg.buf = &ds; + + for (i = 0; i < 10; i++) { + rc = semctl(id, 0, IPC_STAT, arg); + if (unlikely(rc == -1)) + goto err; + + if(ds.sem_otime != 0) + break; + + usleep(10 * 1000); + } + + if (unlikely(ds.sem_otime == 0)) + goto err; + + return id; +err: + return -1; +} + +/** + * gets the file size + */ +static inline int file_size() +{ + struct stat statbuf; + + statbuf.st_size = 0; + stat(ELOG_FILE_NAME, &statbuf); + + return statbuf.st_size; +} diff --git a/demo/os/linux/easylogger/port/elog_port.c b/demo/os/linux/easylogger/port/elog_port.c index 4c8c183..456888c 100644 --- a/demo/os/linux/easylogger/port/elog_port.c +++ b/demo/os/linux/easylogger/port/elog_port.c @@ -32,6 +32,7 @@ #include #include +#include static pthread_mutex_t output_lock; /** @@ -44,6 +45,10 @@ ElogErrCode elog_port_init(void) { pthread_mutex_init(&output_lock, NULL); +#ifdef ELOG_FILE_ENABLE + elog_file_init(); +#endif + return result; } @@ -56,6 +61,10 @@ 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 } /** From b3c38630e9b6d81426c90df48ea7b4fa0ed5a196 Mon Sep 17 00:00:00 2001 From: qintl Date: Thu, 10 Jan 2019 15:01:36 +0800 Subject: [PATCH 4/6] Fix plugin file the structure and format Signed-off-by: qintl --- demo/os/linux/Makefile | 7 +- demo/os/linux/easylogger/inc/elog_cfg.h | 2 + .../linux/easylogger/plugins/file/elog_file.c | 34 ------- .../linux/easylogger/plugins/file/elog_file.h | 30 ------ .../easylogger/plugins/file/elog_file_cfg.h | 31 +++++- .../easylogger/plugins/file/elog_file_port.c | 95 +++++++++++-------- demo/os/linux/easylogger/port/elog_port.c | 4 +- easylogger/plugins/file/elog_file.c | 87 ++++++++++++++--- easylogger/plugins/file/elog_file.h | 65 ++++++++++--- easylogger/plugins/file/elog_file_cfg.h | 31 +++++- easylogger/plugins/file/elog_file_port.c | 67 ++++++++++--- 11 files changed, 299 insertions(+), 154 deletions(-) delete mode 100644 demo/os/linux/easylogger/plugins/file/elog_file.c delete mode 100644 demo/os/linux/easylogger/plugins/file/elog_file.h diff --git a/demo/os/linux/Makefile b/demo/os/linux/Makefile index e84e61c..25cf136 100755 --- a/demo/os/linux/Makefile +++ b/demo/os/linux/Makefile @@ -1,9 +1,12 @@ CC = cc -INCLUDE = -I./easylogger/plugins/file -I./easylogger/inc -I../../../easylogger/inc + +ROOTPATH=../../.. +INCLUDE = -I./easylogger/inc -I./easylogger/plugins/ -I$(ROOTPATH)/easylogger/plugins/ -I$(ROOTPATH)/easylogger/inc LIB=-lpthread OBJ += $(patsubst %.c, %.o, $(wildcard *.c)) -OBJ += $(patsubst %.c, %.o, $(wildcard ../../../easylogger/src/*.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)) diff --git a/demo/os/linux/easylogger/inc/elog_cfg.h b/demo/os/linux/easylogger/inc/elog_cfg.h index 778972b..8f3fa20 100644 --- a/demo/os/linux/easylogger/inc/elog_cfg.h +++ b/demo/os/linux/easylogger/inc/elog_cfg.h @@ -33,6 +33,8 @@ #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/linux/easylogger/plugins/file/elog_file.c b/demo/os/linux/easylogger/plugins/file/elog_file.c deleted file mode 100644 index f7f2a98..0000000 --- a/demo/os/linux/easylogger/plugins/file/elog_file.c +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include -#include -#include - -#include "elog_file.h" - -/* initialize OK flag */ -static bool init_ok = false; - -ElogErrCode elog_file_init(void) -{ - ElogErrCode result = ELOG_NO_ERR; - - elog_file_port_init(); - - init_ok = true; - return result; -} - -void elog_file_write(const char *log, size_t size) -{ - ELOG_ASSERT(init_ok); - ELOG_ASSERT(log); - - elog_file_port_lock(); - elog_file_port_write(log, size); - elog_file_port_unlock(); -} - -void elog_file_deinit(void) -{ - elog_file_port_deinit(); -} diff --git a/demo/os/linux/easylogger/plugins/file/elog_file.h b/demo/os/linux/easylogger/plugins/file/elog_file.h deleted file mode 100644 index 6cc8bf6..0000000 --- a/demo/os/linux/easylogger/plugins/file/elog_file.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef __ELOG_FILE__H__ -#define __ELOG_FILE__H__ - -#include - -/* EasyLogger file log plugin's software version number */ -#define ELOG_FILE_SW_VERSION "V1.0.0" - -#ifdef __cplusplus -extern "C" { -#endif - /* elog_file.c */ - ElogErrCode elog_file_init(void); - void elog_file_set_name(const char *name); - void elog_file_get_name(const char *name); - void elog_file_write(const char *log, size_t size); - void elog_file_deinit(void); - - /* elog_file_port.c */ - ElogErrCode elog_file_port_init(void); - void elog_file_port_write(const char *log, size_t size); - void elog_file_port_lock(void); - void elog_file_port_unlock(void); - void elog_file_port_deinit(void); - -#ifdef __cplusplus -} -#endif - -#endif 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 42a2b36..278b48e 100644 --- a/demo/os/linux/easylogger/plugins/file/elog_file_cfg.h +++ b/demo/os/linux/easylogger/plugins/file/elog_file_cfg.h @@ -1,3 +1,31 @@ +/* + * 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_ @@ -7,7 +35,4 @@ /* EasyLogger file log plugin's using file max size */ #define ELOG_FILE_MAX_SIZE (10 * 1024 * 1024) -/* EasyLogger file log plugin's using semaphore key */ -#define ELOG_FILE_SEM_KEY ((key_t)0x19910612) - #endif /* _ELOG_FILE_CFG_H_ */ diff --git a/demo/os/linux/easylogger/plugins/file/elog_file_port.c b/demo/os/linux/easylogger/plugins/file/elog_file_port.c index 9e3a2ae..cb8fc21 100644 --- a/demo/os/linux/easylogger/plugins/file/elog_file_port.c +++ b/demo/os/linux/easylogger/plugins/file/elog_file_port.c @@ -1,3 +1,31 @@ +/* + * 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 #include #include @@ -7,9 +35,10 @@ #include -#include "elog_file.h" -#include "elog_file_cfg.h" +#include +#include +#define ELOG_FILE_SEM_KEY ((key_t)0x19910612) #ifdef _SEM_SEMUN_UNDEFINED union semun { int val; /* Value for SETVAL */ @@ -20,18 +49,12 @@ union semun { }; #endif -#define likely(x) __builtin_expect(!!(x), 1) -#define unlikely(x) __builtin_expect(!!(x), 0) - -static FILE *fp; -static int fd; static int semid = -1; static struct sembuf const up = {0, 1, SEM_UNDO}; static struct sembuf const down = {0, -1, SEM_UNDO}; static void lock_init(void); static int lock_open(void); -static inline int file_size(void); /** * EasyLogger flile log pulgin port initialize * @@ -40,49 +63,54 @@ static inline int file_size(void); ElogErrCode elog_file_port_init(void) { ElogErrCode result = ELOG_NO_ERR; - fp = fopen(ELOG_FILE_NAME, "a+"); - if (unlikely(!fp)) - goto __exit; - lock_init(); - fd = fileno(fp); -__exit: return result; } /** - * output file saved log port interface - * - * @param log file saved log - * @param size log size + * 46 * flush file cache + * 47 */ +void inline elog_file_port_flush_cache(Elog_File *file) +{ + fflush(file->fp); + fsync(file->fd); +} + +/** + * get file size */ -void elog_file_port_write(const char *log, size_t size) { - if(unlikely(file_size() >= ELOG_FILE_MAX_SIZE)) - return ; +size_t inline elog_file_port_get_size(Elog_File *file) +{ + struct stat statbuf; + + statbuf.st_size = 0; + stat(file->name, &statbuf); - fwrite(log, size, 1, fp); - fdatasync(fd); + return statbuf.st_size; } /** * file log lock */ -void elog_file_port_lock(void) { +void inline elog_file_port_lock(void) +{ semid == -1 ? -1 : semop(semid, (struct sembuf *)&down, 1); } /** * file log unlock */ -void elog_file_port_unlock(void) { +void inline elog_file_port_unlock(void) +{ semid == -1 ? -1 : semop(semid, (struct sembuf *)&up, 1); } /** * file log deinit */ -void elog_file_port_deinit(void) { - fclose(fp); +void elog_file_port_deinit(void) +{ + } /** @@ -152,16 +180,3 @@ static int lock_open(void) err: return -1; } - -/** - * gets the file size - */ -static inline int file_size() -{ - struct stat statbuf; - - statbuf.st_size = 0; - stat(ELOG_FILE_NAME, &statbuf); - - return statbuf.st_size; -} diff --git a/demo/os/linux/easylogger/port/elog_port.c b/demo/os/linux/easylogger/port/elog_port.c index 456888c..1ad3d8b 100644 --- a/demo/os/linux/easylogger/port/elog_port.c +++ b/demo/os/linux/easylogger/port/elog_port.c @@ -32,7 +32,7 @@ #include #include -#include +#include static pthread_mutex_t output_lock; /** @@ -60,7 +60,7 @@ ElogErrCode elog_port_init(void) { */ void elog_port_output(const char *log, size_t size) { /* output to terminal */ - printf("%.*s", size, log); + printf("%.*s", (int)size, log); #ifdef ELOG_FILE_ENABLE /* write the file */ elog_file_write(log, size); diff --git a/easylogger/plugins/file/elog_file.c b/easylogger/plugins/file/elog_file.c index 4fd06ba..d9ca874 100644 --- a/easylogger/plugins/file/elog_file.c +++ b/easylogger/plugins/file/elog_file.c @@ -1,38 +1,99 @@ +/* + * 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: Save log to file. + * Created on: 2019-01-05 + */ + #include #include #include #include -#include "elog_file.h" - +#include +#include /* initialize OK flag */ static bool init_ok = false; +static Elog_File file; + +static void elog_file_config_init(Elog_File *file); +static void elog_file_config_deinit(void); ElogErrCode elog_file_init(void) { - ElogErrCode result = ELOG_NO_ERR; - if (init_ok) - goto __exit; + ElogErrCode result = ELOG_NO_ERR; + if (init_ok) + goto __exit; - elog_file_port_init(); + elog_file_config_init(&file); + elog_file_port_init(); - init_ok = true; + init_ok = true; __exit: - return result; + return result; } void elog_file_write(const char *log, size_t size) { - ELOG_ASSERT(init_ok); - ELOG_ASSERT(log); + ELOG_ASSERT(init_ok); + ELOG_ASSERT(log); + + if (unlikely(elog_file_port_get_size(&file) > file.max_size)) + return ; elog_file_port_lock(); - elog_file_port_write(log, size); + + fwrite(log, size, 1, file.fp); +#ifdef ELOG_FILE_FLUSH_CAHCE_ENABLE + elog_file_port_flush_cache(&file); +#endif + elog_file_port_unlock(); } + void elog_file_deinit(void) { - ELOG_ASSERT(init_ok); - elog_file_port_deinit(); + ELOG_ASSERT(init_ok); + + elog_file_config_deinit(); + elog_file_port_deinit(); +} + +static void elog_file_config_init(Elog_File *file) +{ + file->name = ELOG_FILE_NAME; + file->max_size = ELOG_FILE_MAX_SIZE; + file->fp = fopen(file->name, "a+"); +#ifdef linux + if (file->fp) + file->fd = fileno(file->fp); + else + file->fd = -1; +#endif } +static void elog_file_config_deinit(void) +{ + fclose(file.fp); +} diff --git a/easylogger/plugins/file/elog_file.h b/easylogger/plugins/file/elog_file.h index 6ff5806..1c5e58c 100644 --- a/easylogger/plugins/file/elog_file.h +++ b/easylogger/plugins/file/elog_file.h @@ -1,25 +1,66 @@ +/* + * 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 an head file for file log plugin. You can see all be called functions. + * Created on: 2019-01-05 + */ + #ifndef __ELOG_FILE__H__ #define __ELOG_FILE__H__ - +#include #include /* EasyLogger file log plugin's software version number */ #define ELOG_FILE_SW_VERSION "V1.0.0" +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) + +typedef struct { + FILE *fp; +#ifdef linux + int fd; +#endif + char *name; + size_t max_size; +} Elog_File; #ifdef __cplusplus extern "C" { #endif - /* elog_file.c */ - ElogErrCode elog_file_init(void); - void elog_file_write(const char *log, size_t size); - void elog_file_deinit(void); - - /* elog_file_port.c */ - ElogErrCode elog_flash_port_init(void); - void elog_file_port_write(const char *log, size_t size); - void elog_file_port_lock(void); - void elog_file_port_unlock(void); - void elog_file_port_deinit(void); + +/* elog_file.c */ +ElogErrCode elog_file_init(void); +void elog_file_write(const char *log, size_t size); +void elog_file_deinit(void); + +/* elog_file_port.c */ +ElogErrCode elog_file_port_init(void); +size_t elog_file_port_get_size(Elog_File *file); +void elog_file_port_flush_cache(Elog_File *file); +void elog_file_port_lock(void); +void elog_file_port_unlock(void); +void elog_file_port_deinit(void); #ifdef __cplusplus } diff --git a/easylogger/plugins/file/elog_file_cfg.h b/easylogger/plugins/file/elog_file_cfg.h index a8afe49..0dd75c9 100644 --- a/easylogger/plugins/file/elog_file_cfg.h +++ b/easylogger/plugins/file/elog_file_cfg.h @@ -1,3 +1,31 @@ +/* + * 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_ @@ -7,7 +35,4 @@ /* EasyLogger file log plugin's using file max size */ #define ELOG_FILE_MAX_SIZE -/* EasyLogger file log plugin's using semaphore key */ -#define ELOG_FILE_SEM_KEY - #endif /* _ELOG_FILE_CFG_H_ */ diff --git a/easylogger/plugins/file/elog_file_port.c b/easylogger/plugins/file/elog_file_port.c index 2878664..2342df4 100644 --- a/easylogger/plugins/file/elog_file_port.c +++ b/easylogger/plugins/file/elog_file_port.c @@ -1,3 +1,31 @@ +/* + * 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 "elog_file.h" /** @@ -5,48 +33,57 @@ * * @return result */ -ElogErrCode elog_file_port_init(void) { +ElogErrCode elog_file_port_init(void) +{ ElogErrCode result = ELOG_NO_ERR; + + /* add your code here */ + return result; +} + +/** + * flush file cache + */ +void elog_file_port_flush_cache(Elog_File *file) { + /* add your code here */ - return result; } /** - * output file saved log port interface - * - * @param log file saved log - * @param size log size + * get file current size */ -void elog_file_port_output(const char *log, size_t size) { - +size_t elog_file_port_get_size(Elog_File *file) +{ + /* add your code here */ - + + return 0; } /** * file log lock */ void elog_file_port_lock(void) { - + /* add your code here */ - + } /** * file log unlock */ void elog_file_port_unlock(void) { - + /* add your code here */ - + } /** * file log deinit */ void elog_file_port_deinit(void) { - + /* add your code here */ - + } From 27c26903fec6524da15ad9c771bbc0c9b63c4248 Mon Sep 17 00:00:00 2001 From: tianlongqin Date: Thu, 10 Jan 2019 15:36:56 +0800 Subject: [PATCH 5/6] Update README.md --- demo/os/linux/README.md | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/demo/os/linux/README.md b/demo/os/linux/README.md index c640d0a..452f983 100644 --- a/demo/os/linux/README.md +++ b/demo/os/linux/README.md @@ -2,23 +2,24 @@ --- -## 1 +## 1、简介 -ʹGCC롣ͨ `main.c` `test_elog()` ־ `easylogger\inc\elog_cfg.h` 첽ģʽ +使用GCC编译。通过 `main.c` 的 `test_elog()` 方法来测试日志的输出。已在 `easylogger\inc\elog_cfg.h` 开启异步输出模式。 -### 1.1ʹ÷ +### 1.1、使用方法 -ʹǰǰúñ뻷óɹִ `make.sh` űȴɺ `out\EasyLoggerLinuxDemo` ɿн +使用前需提前配置好编译环境,配置成功后,执行 make,等待编译完成后,运行 `out\EasyLoggerLinuxDemo` 即可看到运行结果。 -## 2ļУ˵ +## 2、文件(夹)说明 -`easylogger\port\elog_port.c` ֲοļ +- `easylogger\port\elog_port.c` 移植参考文件 +- easylogger\plugins\file\elog_file_port.c File Log功能移植参考文件 -## 3 +## 3、其他功能 -Դ `main.c` еIJעͣ¹ܡ +可以打开 `main.c` 中的部分注释,来测试以下功能。 -- `elog_set_output_enabled(false);` ̬ʹܻʧ־ -- `elog_set_filter_lvl(ELOG_LVL_WARN);` ̬ùȼ -- `elog_set_filter_tag("main");` ̬ù˱ǩ -- `elog_set_filter_kw("Hello");` ̬ù˹ؼ +- `elog_set_output_enabled(false);` :动态使能或失能日志输出 +- `elog_set_filter_lvl(ELOG_LVL_WARN);` :动态设置过滤优先级 +- `elog_set_filter_tag("main");` :动态设置过滤标签 +- `elog_set_filter_kw("Hello");` :动态设置过滤关键词 From cee83af21cbc2d89ef2e00265e2159b033f87e59 Mon Sep 17 00:00:00 2001 From: qintl Date: Thu, 10 Jan 2019 16:42:46 +0800 Subject: [PATCH 6/6] File plugin details processing Signed-off-by: qintl --- .../easylogger/plugins/file/elog_file_port.c | 22 ---------- demo/os/linux/easylogger/port/elog_port.c | 2 + easylogger/plugins/file/elog_file.c | 43 ++++++++++--------- easylogger/plugins/file/elog_file.h | 25 ++++++----- 4 files changed, 37 insertions(+), 55 deletions(-) diff --git a/demo/os/linux/easylogger/plugins/file/elog_file_port.c b/demo/os/linux/easylogger/plugins/file/elog_file_port.c index cb8fc21..74b92e0 100644 --- a/demo/os/linux/easylogger/plugins/file/elog_file_port.c +++ b/demo/os/linux/easylogger/plugins/file/elog_file_port.c @@ -68,28 +68,6 @@ ElogErrCode elog_file_port_init(void) { return result; } -/** - * 46 * flush file cache - * 47 */ -void inline elog_file_port_flush_cache(Elog_File *file) -{ - fflush(file->fp); - fsync(file->fd); -} - -/** - * get file size - */ -size_t inline elog_file_port_get_size(Elog_File *file) -{ - struct stat statbuf; - - statbuf.st_size = 0; - stat(file->name, &statbuf); - - return statbuf.st_size; -} - /** * file log lock */ diff --git a/demo/os/linux/easylogger/port/elog_port.c b/demo/os/linux/easylogger/port/elog_port.c index 1ad3d8b..0721b05 100644 --- a/demo/os/linux/easylogger/port/elog_port.c +++ b/demo/os/linux/easylogger/port/elog_port.c @@ -32,7 +32,9 @@ #include #include +#ifdef ELOG_FILE_ENABLE #include +#endif static pthread_mutex_t output_lock; /** diff --git a/easylogger/plugins/file/elog_file.c b/easylogger/plugins/file/elog_file.c index d9ca874..e028e50 100644 --- a/easylogger/plugins/file/elog_file.c +++ b/easylogger/plugins/file/elog_file.c @@ -26,6 +26,7 @@ * Created on: 2019-01-05 */ +#include #include #include #include @@ -35,10 +36,11 @@ #include /* initialize OK flag */ static bool init_ok = false; -static Elog_File file; +static FILE *fp; +static int fd; +static Elog_File_Cfg file; -static void elog_file_config_init(Elog_File *file); -static void elog_file_config_deinit(void); +static void elog_file_config_init(Elog_File_Cfg *file); ElogErrCode elog_file_init(void) { @@ -47,6 +49,13 @@ ElogErrCode elog_file_init(void) goto __exit; elog_file_config_init(&file); + + fp = fopen(file.name, "a+"); + if (fp) + fd = fileno(fp); + else + fd = -1; + elog_file_port_init(); init_ok = true; @@ -58,15 +67,21 @@ void elog_file_write(const char *log, size_t size) { ELOG_ASSERT(init_ok); ELOG_ASSERT(log); + struct stat statbuf; + + statbuf.st_size = 0; + fstat(fd, &statbuf); + - if (unlikely(elog_file_port_get_size(&file) > file.max_size)) + if (unlikely(statbuf.st_size > file.max_size)) return ; elog_file_port_lock(); - fwrite(log, size, 1, file.fp); + fwrite(log, size, 1, fp); #ifdef ELOG_FILE_FLUSH_CAHCE_ENABLE - elog_file_port_flush_cache(&file); + fflush(fp); + fsync(fd); #endif elog_file_port_unlock(); @@ -76,24 +91,12 @@ void elog_file_deinit(void) { ELOG_ASSERT(init_ok); - elog_file_config_deinit(); elog_file_port_deinit(); + fclose(fp); } -static void elog_file_config_init(Elog_File *file) +static void elog_file_config_init(Elog_File_Cfg *file) { file->name = ELOG_FILE_NAME; file->max_size = ELOG_FILE_MAX_SIZE; - file->fp = fopen(file->name, "a+"); -#ifdef linux - if (file->fp) - file->fd = fileno(file->fp); - else - file->fd = -1; -#endif -} - -static void elog_file_config_deinit(void) -{ - fclose(file.fp); } diff --git a/easylogger/plugins/file/elog_file.h b/easylogger/plugins/file/elog_file.h index 1c5e58c..4ed1b32 100644 --- a/easylogger/plugins/file/elog_file.h +++ b/easylogger/plugins/file/elog_file.h @@ -31,23 +31,24 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + /* EasyLogger file log plugin's software version number */ #define ELOG_FILE_SW_VERSION "V1.0.0" +#ifdef linux #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) - -typedef struct { - FILE *fp; -#ifdef linux - int fd; +#else +#define likely(x) (x) +#define unlikely(x) (x) #endif - char *name; - size_t max_size; -} Elog_File; -#ifdef __cplusplus -extern "C" { -#endif +typedef struct { + char *name;/* file name */ + size_t max_size;/* file max size */ +} Elog_File_Cfg; /* elog_file.c */ ElogErrCode elog_file_init(void); @@ -56,8 +57,6 @@ void elog_file_deinit(void); /* elog_file_port.c */ ElogErrCode elog_file_port_init(void); -size_t elog_file_port_get_size(Elog_File *file); -void elog_file_port_flush_cache(Elog_File *file); void elog_file_port_lock(void); void elog_file_port_unlock(void); void elog_file_port_deinit(void);