1
0
mirror of https://github.com/armink/EasyLogger.git synced 2025-01-31 21:42:53 +08:00

File plugin details processing

Signed-off-by: qintl <qintl@yytek.com>
This commit is contained in:
qintl 2019-01-10 16:42:46 +08:00
parent b3c38630e9
commit cee83af21c
4 changed files with 40 additions and 58 deletions

View File

@ -68,28 +68,6 @@ ElogErrCode elog_file_port_init(void) {
return result; 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 * file log lock
*/ */

View File

@ -32,7 +32,9 @@
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#ifdef ELOG_FILE_ENABLE
#include <file/elog_file.h> #include <file/elog_file.h>
#endif
static pthread_mutex_t output_lock; static pthread_mutex_t output_lock;
/** /**

View File

@ -26,6 +26,7 @@
* Created on: 2019-01-05 * Created on: 2019-01-05
*/ */
#include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
@ -35,10 +36,11 @@
#include <file/elog_file_cfg.h> #include <file/elog_file_cfg.h>
/* initialize OK flag */ /* initialize OK flag */
static bool init_ok = false; 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_init(Elog_File_Cfg *file);
static void elog_file_config_deinit(void);
ElogErrCode elog_file_init(void) ElogErrCode elog_file_init(void)
{ {
@ -47,6 +49,13 @@ ElogErrCode elog_file_init(void)
goto __exit; goto __exit;
elog_file_config_init(&file); elog_file_config_init(&file);
fp = fopen(file.name, "a+");
if (fp)
fd = fileno(fp);
else
fd = -1;
elog_file_port_init(); elog_file_port_init();
init_ok = true; init_ok = true;
@ -58,15 +67,21 @@ void elog_file_write(const char *log, size_t size)
{ {
ELOG_ASSERT(init_ok); ELOG_ASSERT(init_ok);
ELOG_ASSERT(log); ELOG_ASSERT(log);
struct stat statbuf;
if (unlikely(elog_file_port_get_size(&file) > file.max_size)) statbuf.st_size = 0;
fstat(fd, &statbuf);
if (unlikely(statbuf.st_size > file.max_size))
return ; return ;
elog_file_port_lock(); elog_file_port_lock();
fwrite(log, size, 1, file.fp); fwrite(log, size, 1, fp);
#ifdef ELOG_FILE_FLUSH_CAHCE_ENABLE #ifdef ELOG_FILE_FLUSH_CAHCE_ENABLE
elog_file_port_flush_cache(&file); fflush(fp);
fsync(fd);
#endif #endif
elog_file_port_unlock(); elog_file_port_unlock();
@ -76,24 +91,12 @@ void elog_file_deinit(void)
{ {
ELOG_ASSERT(init_ok); ELOG_ASSERT(init_ok);
elog_file_config_deinit();
elog_file_port_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->name = ELOG_FILE_NAME;
file->max_size = ELOG_FILE_MAX_SIZE; 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);
} }

View File

@ -31,24 +31,25 @@
#include <stdio.h> #include <stdio.h>
#include <elog.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 #ifdef __cplusplus
extern "C" { extern "C" {
#endif #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)
#else
#define likely(x) (x)
#define unlikely(x) (x)
#endif
typedef struct {
char *name;/* file name */
size_t max_size;/* file max size */
} Elog_File_Cfg;
/* elog_file.c */ /* elog_file.c */
ElogErrCode elog_file_init(void); ElogErrCode elog_file_init(void);
void elog_file_write(const char *log, size_t size); void elog_file_write(const char *log, size_t size);
@ -56,8 +57,6 @@ void elog_file_deinit(void);
/* elog_file_port.c */ /* elog_file_port.c */
ElogErrCode elog_file_port_init(void); 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_lock(void);
void elog_file_port_unlock(void); void elog_file_port_unlock(void);
void elog_file_port_deinit(void); void elog_file_port_deinit(void);