Fix plugin file the structure and format

Signed-off-by: qintl <qintl@yytek.com>
pull/37/head
qintl 7 years ago
parent a9a7b9123e
commit b3c38630e9

@ -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))

@ -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 */

@ -1,34 +0,0 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdbool.h>
#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();
}

@ -1,30 +0,0 @@
#ifndef __ELOG_FILE__H__
#define __ELOG_FILE__H__
#include <elog.h>
/* 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

@ -1,3 +1,31 @@
/*
* This file is part of the EasyLogger Library.
*
* Copyright (c) 2015-2019, Qintl, <qintl_linux@163.com>
*
* 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_ */

@ -1,3 +1,31 @@
/*
* This file is part of the EasyLogger Library.
*
* Copyright (c) 2015-2019, Qintl, <qintl_linux@163.com>
*
* 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 <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
@ -7,9 +35,10 @@
#include <unistd.h>
#include "elog_file.h"
#include "elog_file_cfg.h"
#include <file/elog_file.h>
#include <file/elog_file_cfg.h>
#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;
}

@ -32,7 +32,7 @@
#include <unistd.h>
#include <time.h>
#include <elog_file.h>
#include <file/elog_file.h>
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);

@ -1,38 +1,99 @@
/*
* This file is part of the EasyLogger Library.
*
* Copyright (c) 2015-2019, Qintl, <qintl_linux@163.com>
*
* 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 <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdbool.h>
#include "elog_file.h"
#include <file/elog_file.h>
#include <file/elog_file_cfg.h>
/* 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);
}

@ -1,25 +1,66 @@
/*
* This file is part of the EasyLogger Library.
*
* Copyright (c) 2015-2019, Qintl, <qintl_linux@163.com>
*
* 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 <stdio.h>
#include <elog.h>
/* 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
}

@ -1,3 +1,31 @@
/*
* This file is part of the EasyLogger Library.
*
* Copyright (c) 2015-2019, Qintl, <qintl_linux@163.com>
*
* 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_ */

@ -1,3 +1,31 @@
/*
* This file is part of the EasyLogger Library.
*
* Copyright (c) 2015-2019, Qintl, <qintl_linux@163.com>
*
* 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 */
}

Loading…
Cancel
Save