diff options
| author | 杨威 <[email protected]> | 2019-02-18 17:51:13 +0800 |
|---|---|---|
| committer | 杨威 <[email protected]> | 2019-02-18 17:51:13 +0800 |
| commit | 5001fdee287b2e559131aae5ed9cf045fd5d6c61 (patch) | |
| tree | b59c14755134e40b06a275477352ef8e5ae349bf /src | |
| parent | 6c8af75e99ed577adfeecb2cad69771ce5b8f474 (diff) | |
| parent | e63b7ee3855b1b0f2fb08196b0dd4cb80d14a8b9 (diff) | |
Merge branch 'Feature-fp-in-handle' into 'master'
文件操作逻辑升级
See merge request MESA_framework/MESA_handle_logger!2
Diffstat (limited to 'src')
| -rw-r--r-- | src/MESA_handle_logger.c | 73 | ||||
| -rw-r--r-- | src/Makefile | 7 |
2 files changed, 29 insertions, 51 deletions
diff --git a/src/MESA_handle_logger.c b/src/MESA_handle_logger.c index 49c393c..21e18e2 100644 --- a/src/MESA_handle_logger.c +++ b/src/MESA_handle_logger.c @@ -11,16 +11,13 @@ typedef struct log_handle_t { int runtime_log_level; + FILE *fp; char runtime_log_file[1200]; } log_handle_t; -#define THREAD_CTIME(t, buf, len) thread_safe_ctime(t, buf, len) #define LOGMSG_MAX_LEN 4096 -const int HANDLE_LOGGER_VERSION_20170816 = 1; - -static unsigned int month_days[12] = -{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; +const int HANDLE_LOGGER_VERSION_20190218 = 1; static unsigned char weekday_str[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; @@ -28,44 +25,6 @@ static unsigned char weekday_str[7][4] = static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; -char *thread_safe_ctime(const time_t *tp, char *buf, int len) -{ - unsigned int year, month, day, weekday, hour, min, sec; - unsigned int year_days = 365; - sec = * tp; - min = sec / 60; sec = sec % 60; - hour = min / 60; min = min % 60; hour += 8; - day = hour / 24; hour = hour % 24; - weekday = day % 7; weekday = (weekday + 4) % 7; - - for(year = 1970; day >= year_days;) - { - day -= year_days; - year ++; - - if(0 == year % 4 && (0 != year % 100 || 0 == year % 400)) - year_days = 366; - else year_days = 365; - } - - if(366 == year_days) month_days[1] = 29; - - //bug fix by yw 20120808 - for(month = 0; day >= month_days[month];) - { - day -= month_days[month]; - month ++; - } - - /* - snprintf(buf, len, "%02d:%02d:%02d, %04d/%02d/%02d, %s", - hour, min, sec, year, month, day, weekday_str[week_day]); - */ - snprintf(buf, len, "%s %s %d %02d:%02d:%02d %d", weekday_str[weekday], - month_str[month], day + 1, hour, min, sec, year); - return buf; -} - static int create_dir(const char *dir_path, int path_len) { if(dir_path == NULL) @@ -126,7 +85,7 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level) if(NULL == (fp = fopen(file_path, "w"))) return NULL; - fclose(fp); + //fclose(fp); //remove(file_path); p_handle = (log_handle_t *)calloc(sizeof(log_handle_t), 1); @@ -136,6 +95,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; + //p_handle->fp = fp; return (void *)p_handle; } @@ -143,7 +103,13 @@ void MESA_destroy_runtime_log_handle(void *handle) { if(handle != NULL) { - free(handle); + log_handle_t *p_handle = (log_handle_t *)handle; + if (p_handle->fp != NULL) + { + fclose(p_handle->fp); + p_handle->fp = NULL; + } + free(handle); handle = NULL; } @@ -158,7 +124,7 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const va_list ap; FILE *fp; struct tm local_time; - char tmp_log_file_name[1201]; + char tmp_log_file_name[1400]; log_handle_t *p_handle = (log_handle_t *)handle; if(p_handle == NULL || p_handle->runtime_log_file == NULL)return; @@ -167,7 +133,6 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const time(&t); if(NULL == (localtime_r(&t, &local_time))) return; - //THREAD_CTIME(&t, buf, LOGMSG_MAX_LEN); len = snprintf(buf, sizeof(buf), "%s %s %d %02d:%02d:%02d %d", weekday_str[local_time.tm_wday], month_str[local_time.tm_mon], local_time.tm_mday, local_time.tm_hour, local_time.tm_min, local_time.tm_sec, local_time.tm_year+1900); //len = strlen(buf); @@ -214,9 +179,17 @@ 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); - if(NULL == (fp = fopen(tmp_log_file_name, "a"))) return; + if(p_handle->fp == NULL) + { + if(NULL == (fp = fopen(tmp_log_file_name, "a"))) return; - fprintf(fp, "%s", buf); - fclose(fp); + p_handle->fp = fp; + } + if (0 > fprintf(p_handle->fp, "%s", buf)) + { + fclose(p_handle->fp); + p_handle->fp = NULL; + } + return; } diff --git a/src/Makefile b/src/Makefile index a7a913c..16fbe8c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -3,6 +3,11 @@ VPATH=../inc CFLAGS= -g3 -Wall -fPIC -O -Werror CFLAGS+=-I../inc/ +ifdef ASAN +CFLAGS_+= -fsanitize=address -fno-omit-frame-pointer +LIB+=-lasan +endif + SRC=MESA_handle_logger.c OBJS=$(SRC:.c=.o) @@ -15,7 +20,7 @@ libMESA_handle_logger.a:$(OBJS) ar cqs $@ $< libMESA_handle_logger.so:$(OBJS) - $(CC) $(CFLAGS) -shared $< -o $@ + $(CC) $(CFLAGS) $(LIB) -shared $< -o $@ .c.o: #$(OBJS):$(SRC) |
