From 524bca27149d970d4953450a3e0a770606d58a4e Mon Sep 17 00:00:00 2001 From: yangwei Date: Mon, 7 Sep 2020 13:45:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=86=99=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=89=88=E6=9C=AC=EF=BC=8C=E6=AF=8F=E6=AC=A1?= =?UTF-8?q?=E5=86=99=E5=85=A5=E9=83=BDfopen=E5=92=8Cfclose=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MESA_handle_logger.c | 133 ++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 79 deletions(-) diff --git a/src/MESA_handle_logger.c b/src/MESA_handle_logger.c index b1c7acd..d9531cb 100644 --- a/src/MESA_handle_logger.c +++ b/src/MESA_handle_logger.c @@ -8,23 +8,17 @@ #include #include -int LOGMSG_MAX_LEN = 4096; -int FLUSH_LOG_NUM = 0; 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; +#define THREAD_CTIME(t, buf, len) thread_safe_ctime(t, buf, len) +#define LOGMSG_MAX_LEN 4096 - -//const int HANDLE_LOGGER_VERSION_20190218 = 1; - -#define GIT_VERSION_CATTER(v) __attribute__((__used__)) const char * GIT_VERSION_##v = NULL +//const int HANDLE_LOGGER_VERSION_20170816 = 1; +#define GIT_VERSION_CATTER(v) __attribute__((__used__)) const char *GIT_VERSION_##v = NULL #define GIT_VERSION_EXPEND(v) GIT_VERSION_CATTER(v) #ifdef __cplusplus @@ -34,9 +28,9 @@ extern "C" /* VERSION TAG */ #ifdef GIT_VERSION -GIT_VERSION_EXPEND(GIT_VERSION); + GIT_VERSION_EXPEND(GIT_VERSION); #else -static __attribute__((__used__)) const char * GIT_VERSION_UNKNOWN = NULL; +static __attribute__((__used__)) const char *GIT_VERSION_UNKNOWN = NULL; #endif #undef GIT_VERSION_CATTER #undef GIT_VERSION_EXPEND @@ -45,6 +39,8 @@ static __attribute__((__used__)) const char * GIT_VERSION_UNKNOWN = NULL; } #endif +static unsigned int month_days[12] = +{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; static unsigned char weekday_str[7][4] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; @@ -52,6 +48,44 @@ 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) @@ -98,12 +132,7 @@ void *MESA_create_runtime_log_handle(const char *file_path, int level) { if(file_path == NULL) return NULL; - char *max_len = getenv("HLOG_MSG_SIZE"); - char *flush_num = getenv("HLOG_FLUSH_NUM"); - if(max_len != NULL)LOGMSG_MAX_LEN = atoi(max_len); - if(flush_num != NULL)FLUSH_LOG_NUM = atoi(flush_num); - FILE *fp = NULL; log_handle_t *p_handle = NULL; @@ -117,7 +146,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); @@ -127,8 +156,6 @@ 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; } @@ -136,45 +163,13 @@ void MESA_destroy_runtime_log_handle(void *handle) { if(handle != NULL) { - log_handle_t *p_handle = (log_handle_t *)handle; - if (p_handle->fp != NULL) - { - fclose(p_handle->fp); - p_handle->fp = NULL; - } - free(handle); + free(handle); handle = NULL; } 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]; @@ -183,7 +178,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[1400]; + char tmp_log_file_name[1201]; log_handle_t *p_handle = (log_handle_t *)handle; if(p_handle == NULL || p_handle->runtime_log_file == NULL)return; @@ -192,6 +187,7 @@ 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); @@ -238,30 +234,9 @@ 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(p_handle->fp == NULL) - { - if(0 != MESA_handle_open_file(tmp_log_file_name,p_handle))return; - } - else - { - 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)) - { - 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; - } - } - return; + if(NULL == (fp = fopen(tmp_log_file_name, "a"))) return; + + fprintf(fp, "%s", buf); + fclose(fp); }