summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryangwei <[email protected]>2020-05-23 20:47:06 +0800
committer杨威 <[email protected]>2020-08-17 13:52:43 +0800
commit7e10ca27d5d25a32678543caab1edc70e22515bb (patch)
treef6592ac632323fafcd1015835dcbd8a62fb4d263
parentbd5f0acc18999ffe23b0a6c0ea0d113da7328c68 (diff)
增加mutex,解决跨天切换文件时,重复fclose导致double free的bug
-rw-r--r--src/MESA_handle_logger.c61
1 files changed, 41 insertions, 20 deletions
diff --git a/src/MESA_handle_logger.c b/src/MESA_handle_logger.c
index 84b0612..b1c7acd 100644
--- a/src/MESA_handle_logger.c
+++ b/src/MESA_handle_logger.c
@@ -15,6 +15,7 @@ typedef struct log_handle_t
int runtime_log_level;
int flush_log_count;
FILE *fp;
+ pthread_mutex_t mutex;
char runtime_log_file[1200];
char cur_log_file[4096];
} log_handle_t;
@@ -126,6 +127,7 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level)
strncpy(p_handle->runtime_log_file, file_path, 1024);
p_handle->runtime_log_file[1024] = '\0';
p_handle->runtime_log_level = level;
+ pthread_mutex_init(&p_handle->mutex,NULL);
//p_handle->fp = fp;
return (void *)p_handle;
}
@@ -147,6 +149,32 @@ void MESA_destroy_runtime_log_handle(void *handle)
return;
}
+static void MESA_handle_close_file(log_handle_t *p_handle)
+{
+ pthread_mutex_lock(&p_handle->mutex);
+ if(p_handle->fp != NULL)
+ {
+ fclose(p_handle->fp);
+ p_handle->fp = NULL;
+ }
+ pthread_mutex_unlock(&p_handle->mutex);
+ return;
+}
+
+static int MESA_handle_open_file(char *file_name, log_handle_t *p_handle)
+{
+ FILE *fp = NULL;
+ MESA_handle_close_file(p_handle);
+ if(NULL == (fp = fopen(file_name, "a")))
+ {
+ return -1;
+ }
+ p_handle->fp = fp;
+ p_handle->flush_log_count = 0;
+ memcpy(p_handle->cur_log_file, file_name, strlen(file_name));
+ return 0;
+}
+
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...)
{
char buf[LOGMSG_MAX_LEN + 1];
@@ -210,37 +238,30 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
local_time.tm_year + 1900, local_time.tm_mon + 1,
local_time.tm_mday);
-OPEN_LOG_FILE:
if(p_handle->fp == NULL)
{
- if(NULL == (fp = fopen(tmp_log_file_name, "a"))) return;
- p_handle->fp = fp;
- p_handle->flush_log_count = 0;
- memcpy(p_handle->cur_log_file, tmp_log_file_name, strlen(tmp_log_file_name));
+ if(0 != MESA_handle_open_file(tmp_log_file_name,p_handle))return;
}
-
- if (0 != memcmp(tmp_log_file_name, p_handle->cur_log_file, strlen(tmp_log_file_name)))
+ else
{
- fclose(p_handle->fp);
- p_handle->fp = NULL;
- goto OPEN_LOG_FILE;
+ if (0 != memcmp(tmp_log_file_name, p_handle->cur_log_file, strlen(tmp_log_file_name)))
+ {
+ if(0 != MESA_handle_open_file(tmp_log_file_name,p_handle))return;
+ }
}
-
if (0 > fprintf(p_handle->fp, "%s", buf))
{
- fclose(p_handle->fp);
- p_handle->fp = NULL;
+ MESA_handle_close_file(p_handle);
}
else
{
- p_handle->flush_log_count+=1;
- if (p_handle->flush_log_count >= FLUSH_LOG_NUM)
- {
- fflush(p_handle->fp);
- p_handle->flush_log_count = 0;
- }
+ p_handle->flush_log_count+=1;
+ if (p_handle->flush_log_count >= FLUSH_LOG_NUM)
+ {
+ fflush(p_handle->fp);
+ p_handle->flush_log_count = 0;
+ }
}
-
return;
}