|
29 | 29 | //#include "htime.h" |
30 | 30 | #define SECONDS_PER_HOUR 3600 |
31 | 31 | #define SECONDS_PER_DAY 86400 // 24*3600 |
32 | | -#define SECONDS_PER_WEEK 604800 // 7*24*3600; |
| 32 | +#define SECONDS_PER_WEEK 604800 // 7*24*3600 |
| 33 | + |
| 34 | +static inline struct tm* hv_localtime_r(time_t ts, struct tm* tm) { |
| 35 | +#ifdef _WIN32 |
| 36 | + localtime_s(tm, &ts); |
| 37 | +#else |
| 38 | + tm = localtime_r(&ts, tm); |
| 39 | +#endif |
| 40 | + return tm; |
| 41 | +} |
| 42 | + |
| 43 | +static inline struct tm* hv_gmtime_r(time_t ts, struct tm* tm) { |
| 44 | +#ifdef _WIN32 |
| 45 | + gmtime_s(tm, &ts); |
| 46 | +#else |
| 47 | + tm = gmtime_r(&ts, tm); |
| 48 | +#endif |
| 49 | + return tm; |
| 50 | +} |
33 | 51 |
|
34 | 52 | static int s_gmtoff = 28800; // 8*3600 |
| 53 | +static void init_gmtoff() { |
| 54 | + time_t ts = time(NULL); |
| 55 | + struct tm local_tm, gmt_tm; |
| 56 | + memset(&local_tm, 0, sizeof(local_tm)); |
| 57 | + memset(&gmt_tm, 0, sizeof(gmt_tm)); |
| 58 | + hv_localtime_r(ts, &local_tm); |
| 59 | + hv_gmtime_r(ts, &gmt_tm); |
| 60 | + s_gmtoff = (local_tm.tm_hour - gmt_tm.tm_hour) * 3600 + |
| 61 | + (local_tm.tm_min - gmt_tm.tm_min) * 60 + |
| 62 | + (local_tm.tm_sec - gmt_tm.tm_sec); |
| 63 | + |
| 64 | + if (local_tm.tm_yday > gmt_tm.tm_yday) { |
| 65 | + s_gmtoff += SECONDS_PER_DAY; |
| 66 | + } else if (local_tm.tm_yday < gmt_tm.tm_yday) { |
| 67 | + s_gmtoff -= SECONDS_PER_DAY; |
| 68 | + } |
| 69 | +} |
35 | 70 |
|
36 | 71 | struct logger_s { |
37 | 72 | logger_handler handler; |
@@ -79,13 +114,7 @@ static void logger_init(logger_t* logger) { |
79 | 114 | } |
80 | 115 |
|
81 | 116 | logger_t* logger_create() { |
82 | | - // init gmtoff here |
83 | | - time_t ts = time(NULL); |
84 | | - struct tm* local_tm = localtime(&ts); |
85 | | - int local_hour = local_tm->tm_hour; |
86 | | - struct tm* gmt_tm = gmtime(&ts); |
87 | | - int gmt_hour = gmt_tm->tm_hour; |
88 | | - s_gmtoff = (local_hour - gmt_hour) * SECONDS_PER_HOUR; |
| 117 | + init_gmtoff(); |
89 | 118 |
|
90 | 119 | logger_t* logger = (logger_t*)malloc(sizeof(logger_t)); |
91 | 120 | logger_init(logger); |
@@ -214,12 +243,14 @@ const char* logger_get_cur_file(logger_t* logger) { |
214 | 243 | } |
215 | 244 |
|
216 | 245 | static void logfile_name(const char* filepath, time_t ts, char* buf, int len) { |
217 | | - struct tm* tm = localtime(&ts); |
| 246 | + struct tm tm; |
| 247 | + memset(&tm, 0, sizeof(tm)); |
| 248 | + hv_localtime_r(ts, &tm); |
218 | 249 | snprintf(buf, len, "%s.%04d%02d%02d.log", |
219 | 250 | filepath, |
220 | | - tm->tm_year+1900, |
221 | | - tm->tm_mon+1, |
222 | | - tm->tm_mday); |
| 251 | + tm.tm_year+1900, |
| 252 | + tm.tm_mon+1, |
| 253 | + tm.tm_mday); |
223 | 254 | } |
224 | 255 |
|
225 | 256 | static void logfile_truncate(logger_t* logger) { |
@@ -375,17 +406,17 @@ int logger_print(logger_t* logger, int level, const char* fmt, ...) { |
375 | 406 | us = tm.wMilliseconds * 1000; |
376 | 407 | #else |
377 | 408 | struct timeval tv; |
378 | | - struct tm* tm = NULL; |
379 | 409 | gettimeofday(&tv, NULL); |
380 | | - time_t tt = tv.tv_sec; |
381 | | - struct tm tm_buf; |
382 | | - tm = localtime_r(&tt, &tm_buf); |
383 | | - year = tm->tm_year + 1900; |
384 | | - month = tm->tm_mon + 1; |
385 | | - day = tm->tm_mday; |
386 | | - hour = tm->tm_hour; |
387 | | - min = tm->tm_min; |
388 | | - sec = tm->tm_sec; |
| 410 | + time_t ts = tv.tv_sec; |
| 411 | + struct tm tm; |
| 412 | + memset(&tm, 0, sizeof(tm)); |
| 413 | + localtime_r(&ts, &tm); |
| 414 | + year = tm.tm_year + 1900; |
| 415 | + month = tm.tm_mon + 1; |
| 416 | + day = tm.tm_mday; |
| 417 | + hour = tm.tm_hour; |
| 418 | + min = tm.tm_min; |
| 419 | + sec = tm.tm_sec; |
389 | 420 | us = tv.tv_usec; |
390 | 421 | #endif |
391 | 422 |
|
|
0 commit comments