修复仅支持东八区时间的问题,使用localtime_r进行转换

This commit is contained in:
yangwei
2018-12-05 21:04:42 +06:00
committed by dump2file
parent f47f6d8bbf
commit 6c8af75e99
6 changed files with 20 additions and 15 deletions

View File

@@ -5,7 +5,7 @@
all:
cd src && $(MAKE)
#cd demo && $(MAKE)
cd demo && $(MAKE)
clean:
#cd demo && $(MAKE) clean
cd src && $(MAKE) clean

View File

@@ -6,8 +6,9 @@ CC=gcc
CFLAGS= -g3 -Wall -fPIC -O -Werror
CFLAGS+=-I../inc/
LIB=-L../lib/
LIB+=-lMESA_handle_logger -lpthread
#LIB=-L../lib/
LIB=../lib/libMESA_handle_logger.a
LIB+=-lpthread
LIB_FILE=$(wildcard ../lib/*.a)
@@ -17,7 +18,7 @@ TARGET=test_handle_logger
all:$(TARGET)
$(TARGET):$(SRC) $(LIB_FILE)
$(CC) $(CFLAGS) $(INC) $(LIBPATH) $< -static $(LIB) -o $@
$(CC) $(CFLAGS) $(INC) $(LIBPATH) $< $(LIB) -o $@
clean :
rm -f $(TARGET)

View File

@@ -51,7 +51,7 @@ int main()
pthread_t t[THREAD_NUM];
int i = 0;
sample_handle = MESA_create_runtime_log_handle("./log/", RLOG_LV_DEBUG);
sample_handle = MESA_create_runtime_log_handle("./log/test_log", RLOG_LV_DEBUG);
if(sample_handle == NULL)
{
printf("get log sample_handle error\n");

Binary file not shown.

Binary file not shown.

View File

@@ -19,17 +19,19 @@ typedef struct log_handle_t
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};
static unsigned char weekday_str[7][4] =
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
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;
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"};
static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
sec = * tp;
min = sec / 60; sec = sec % 60;
hour = min / 60; min = min % 60; hour += 8;
@@ -164,8 +166,11 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
if(level < p_handle->runtime_log_level) return;
time(&t);
THREAD_CTIME(&t, buf, LOGMSG_MAX_LEN);
len = strlen(buf);
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);
switch(level)
{
@@ -204,7 +209,6 @@ void MESA_handle_runtime_log(void *handle, int level, const char *module, const
len += snprintf(buf + len, LOGMSG_MAX_LEN - len, "\n");
if(NULL == (localtime_r(&t, &local_time))) return;
sprintf(tmp_log_file_name, "%s.%04d-%02d-%02d", p_handle->runtime_log_file,
local_time.tm_year + 1900, local_time.tm_mon + 1,