Fix plugin file the structure and format
Signed-off-by: qintl <qintl@yytek.com>pull/37/head
parent
a9a7b9123e
commit
b3c38630e9
@ -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,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/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "elog_file.h"
|
#include <file/elog_file.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 void elog_file_config_init(Elog_File *file);
|
||||||
|
static void elog_file_config_deinit(void);
|
||||||
|
|
||||||
ElogErrCode elog_file_init(void)
|
ElogErrCode elog_file_init(void)
|
||||||
{
|
{
|
||||||
ElogErrCode result = ELOG_NO_ERR;
|
ElogErrCode result = ELOG_NO_ERR;
|
||||||
if (init_ok)
|
if (init_ok)
|
||||||
goto __exit;
|
goto __exit;
|
||||||
|
|
||||||
elog_file_port_init();
|
elog_file_config_init(&file);
|
||||||
|
elog_file_port_init();
|
||||||
|
|
||||||
init_ok = true;
|
init_ok = true;
|
||||||
__exit:
|
__exit:
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void elog_file_write(const char *log, size_t size)
|
void elog_file_write(const char *log, size_t size)
|
||||||
{
|
{
|
||||||
ELOG_ASSERT(init_ok);
|
ELOG_ASSERT(init_ok);
|
||||||
ELOG_ASSERT(log);
|
ELOG_ASSERT(log);
|
||||||
|
|
||||||
|
if (unlikely(elog_file_port_get_size(&file) > file.max_size))
|
||||||
|
return ;
|
||||||
|
|
||||||
elog_file_port_lock();
|
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();
|
elog_file_port_unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void elog_file_deinit(void)
|
void elog_file_deinit(void)
|
||||||
{
|
{
|
||||||
ELOG_ASSERT(init_ok);
|
ELOG_ASSERT(init_ok);
|
||||||
elog_file_port_deinit();
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue