1
0
mirror of https://github.com/armink/EasyLogger.git synced 2025-01-19 07:42:52 +08:00

Merge pull request #88 from 5ooo/master

add deinit for linux
This commit is contained in:
朱天龙 (Armink) 2021-04-01 18:30:03 +08:00 committed by GitHub
commit c5bb36dc1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 134 additions and 12 deletions

13
demo/os/linux/easylogger/port/elog_file_port.c Normal file → Executable file
View File

@ -55,6 +55,8 @@ static struct sembuf const down = {0, -1, SEM_UNDO};
static void lock_init(void);
static int lock_open(void);
static void lock_deinit(void);
/**
* EasyLogger flile log pulgin port initialize
*
@ -83,12 +85,13 @@ 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)
{
lock_deinit();
}
/**
@ -158,3 +161,11 @@ static int lock_open(void)
err:
return -1;
}
/**
* deinitialize the lock
*/
static void lock_deinit(void)
{
semid = -1;
}

13
demo/os/linux/easylogger/port/elog_port.c Normal file → Executable file
View File

@ -54,6 +54,19 @@ ElogErrCode elog_port_init(void) {
return result;
}
/**
* EasyLogger port deinitialize
*
*/
void elog_port_deinit(void) {
#ifdef ELOG_FILE_ENABLE
elog_file_deinit();
#endif
pthread_mutex_destroy(&output_lock);
}
/**
* output log port interface
*

2
easylogger/inc/elog.h Normal file → Executable file
View File

@ -177,7 +177,9 @@ typedef enum {
/* elog.c */
ElogErrCode elog_init(void);
void elog_deinit(void);
void elog_start(void);
void elog_stop(void);
void elog_set_output_enabled(bool enabled);
bool elog_get_output_enabled(void);
void elog_set_text_color_enabled(bool enabled);

27
easylogger/plugins/file/elog_file.c Normal file → Executable file
View File

@ -143,23 +143,32 @@ void elog_file_deinit(void)
{
ELOG_ASSERT(init_ok);
ElogFileCfg cfg = {NULL, 0, 0};
elog_file_config(&cfg);
elog_file_port_deinit();
fclose(fp);
init_ok = false;
}
void elog_file_config(ElogFileCfg *cfg)
{
if (fp) {
fclose(fp);
}
elog_file_port_lock();
local_cfg.name = cfg->name;
local_cfg.max_size = cfg->max_size;
local_cfg.max_rotate = cfg->max_rotate;
if (fp) {
fclose(fp);
fp = NULL;
}
fp = fopen(local_cfg.name, "a+");
if (cfg != NULL) {
local_cfg.name = cfg->name;
local_cfg.max_size = cfg->max_size;
local_cfg.max_rotate = cfg->max_rotate;
if (local_cfg.name != NULL && strlen(local_cfg.name) > 0)
fp = fopen(local_cfg.name, "a+");
}
elog_file_port_unlock();
}

10
easylogger/port/elog_port.c Normal file → Executable file
View File

@ -41,6 +41,16 @@ ElogErrCode elog_port_init(void) {
return result;
}
/**
* EasyLogger port deinitialize
*
*/
void elog_port_deinit(void) {
/* add your code here */
}
/**
* output log port interface
*

49
easylogger/src/elog.c Normal file → Executable file
View File

@ -200,10 +200,37 @@ ElogErrCode elog_init(void) {
return result;
}
/**
* EasyLogger deinitialize.
*
*/
void elog_deinit(void) {
extern ElogErrCode elog_port_deinit(void);
extern ElogErrCode elog_async_deinit(void);
if (!elog.init_ok) {
return ;
}
#ifdef ELOG_ASYNC_OUTPUT_ENABLE
elog_async_deinit();
#endif
/* port deinitialize */
elog_port_deinit();
elog.init_ok = false;
}
/**
* EasyLogger start after initialize.
*/
void elog_start(void) {
if (!elog.init_ok) {
return ;
}
/* enable output */
elog_set_output_enabled(true);
@ -217,6 +244,28 @@ void elog_start(void) {
log_i("EasyLogger V%s is initialize success.", ELOG_SW_VERSION);
}
/**
* EasyLogger stop after initialize.
*/
void elog_stop(void) {
if (!elog.init_ok) {
return ;
}
/* disable output */
elog_set_output_enabled(false);
#if defined(ELOG_ASYNC_OUTPUT_ENABLE)
elog_async_enabled(false);
#elif defined(ELOG_BUF_OUTPUT_ENABLE)
elog_buf_enabled(false);
#endif
/* show version */
log_i("EasyLogger V%s is deinitialize success.", ELOG_SW_VERSION);
}
/**
* set output enable or disable
*

32
easylogger/src/elog_async.c Normal file → Executable file
View File

@ -74,6 +74,8 @@ static pthread_t async_output_thread;
/* Initialize OK flag */
static bool init_ok = false;
/* thread running flag */
static bool thread_running = false;
/* asynchronous output mode enabled flag */
static bool is_enabled = false;
/* asynchronous output mode's ring buffer */
@ -283,7 +285,7 @@ static void *async_output(void *arg) {
size_t get_log_size = 0;
static char poll_get_buf[ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE];
while(true) {
while(thread_running) {
/* waiting log */
sem_wait(&output_notice);
/* polling gets and outputs the log */
@ -334,8 +336,10 @@ ElogErrCode elog_async_init(void) {
sem_init(&output_notice, 0, 0);
thread_running = true;
pthread_attr_init(&thread_attr);
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
//pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
pthread_attr_setstacksize(&thread_attr, ELOG_ASYNC_OUTPUT_PTHREAD_STACK_SIZE);
pthread_attr_setschedpolicy(&thread_attr, SCHED_RR);
thread_sched_param.sched_priority = ELOG_ASYNC_OUTPUT_PTHREAD_PRIORITY;
@ -349,4 +353,28 @@ ElogErrCode elog_async_init(void) {
return result;
}
/**
* asynchronous output mode deinitialize
*
*/
void elog_async_deinit(void) {
if (!init_ok) {
return ;
}
#ifdef ELOG_ASYNC_OUTPUT_USING_PTHREAD
thread_running = false;
elog_async_output_notice();
pthread_join(async_output_thread, NULL);
sem_destroy(&output_notice);
#endif
init_ok = false;
}
#endif /* ELOG_ASYNC_OUTPUT_ENABLE */