Correct use of localtime() in Win32
authorNeale Ferguson <neale@sinenomine.net>
Thu, 2 Jun 2016 19:33:12 +0000 (15:33 -0400)
committerNeale Ferguson <neale@sinenomine.net>
Thu, 2 Jun 2016 19:33:12 +0000 (15:33 -0400)
mono/sgen/sgen-gc.h
mono/utils/mono-log-common.c
mono/utils/mono-log-windows.c

index 655efdeb30e10c340aad669a21b56019dfdffb6b..fb264a4b070b8ec638928729400e554c5a9257ff 100644 (file)
@@ -127,33 +127,38 @@ extern guint64 stat_objects_copied_major;
                g_error (__VA_ARGS__);  \
 } } while (0)
 
-
-#ifdef HOST_WIN32
-# define LOG_LOCALTIME localtime
+#ifndef HOST_WIN32
+# define LOG_TIMESTAMP  \
+       do {    \
+               time_t t;                                                                       \
+               struct tm tod;                                                                  \
+               time(&t);                                                                       \
+               localtime_r(&t, &tod);                                                          \
+               strftime(logTime, sizeof(logTime), "%F %T", &tod);                              \
+       } while (0)
 #else
-# define LOG_LOCALTIME localtime_r
+# define LOG_TIMESTAMP  \
+       do {    \
+               time_t t;                                                                       \
+               struct tm *tod;                                                                 \
+               time(&t);                                                                       \
+               tod = localtime(&t);                                                            \
+               strftime(logTime, sizeof(logTime), "%F %T", tod);                               \
+       } while (0)
 #endif
 
 #define SGEN_LOG(level, format, ...) do {      \
        if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) {        \
-               time_t t;                                                                       \
-               struct tm tod;                                                                  \
                char logTime[80];                                                               \
-               time(&t);                                                                       \
-               LOG_LOCALTIME(&t, &tod);                                                        \
-               strftime(logTime, sizeof(logTime), "%F %T", &tod);                              \
+               LOG_TIMESTAMP;                                                                  \
                mono_gc_printf (gc_debug_file, "%s " format "\n", logTime, ##__VA_ARGS__);      \
 } } while (0)
 
 #define SGEN_COND_LOG(level, cond, format, ...) do {   \
        if (G_UNLIKELY ((level) <= SGEN_MAX_DEBUG_LEVEL && (level) <= gc_debug_level)) {                \
                if (cond) {                                                                             \
-                       time_t t;                                                                       \
-                       struct tm tod;                                                                  \
                        char logTime[80];                                                               \
-                       time(&t);                                                                       \
-                       LOG_LOCALTIME(&t,&tod);                                                         \
-                       strftime(logTime, sizeof(logTime), "%F %T", &tod);                              \
+                       LOG_TIMESTAMP;                                                                  \
                        mono_gc_printf (gc_debug_file, "%s " format "\n", logTime, ##__VA_ARGS__);      \
                }                                                                                       \
 } } while (0)
index 23b22f34005908b056825fa44d20a0df74ba71d5..bb22e6a31479b6f7a2fe0485b8f2c64e47d63bf1 100644 (file)
@@ -90,7 +90,6 @@ void
 mono_log_write_logfile(const char *domain, GLogLevelFlags level, mono_bool hdr, const char *format, va_list args)
 {
        time_t t;
-       struct tm tod;
        char logTime[80],       
             logMessage[512];
        pid_t pid;
@@ -101,15 +100,19 @@ mono_log_write_logfile(const char *domain, GLogLevelFlags level, mono_bool hdr,
                logFile = stdout;
 
        if (hdr) {
-               time(&t);
 #ifndef HOST_WIN32
+               struct tm tod;
+               time(&t);
                localtime_r(&t, &tod);
                pid = getpid();
+               strftime(logTime, sizeof(logTime), "%F %T", &tod);
 #else
-               localtime(&t, &tod);
+               struct tm *tod;
+               time(&t);
+               tod = localtime(&t);
                pid = _getpid();
+               strftime(logTime, sizeof(logTime), "%F %T", tod);
 #endif
-               strftime(logTime, sizeof(logTime), "%F %T", &tod);
                iLog = snprintf(logMessage, sizeof(logMessage), "%s level[%c] mono[%d]: ",
                                logTime,mapLogFileLevel(level),pid);
        }
index 2bcc145ad23a1353514432ca4e0e5fd47d0118f1..2609c1ea9da00231b451172720fb0d14ef3ba614 100644 (file)
@@ -86,7 +86,7 @@ void
 mono_log_write_syslog(const char *domain, GLogLevelFlags level, mono_bool hdr, const char *format, va_list args)
 {
        time_t t;
-       struct tm tod;
+       struct tm *tod;
        char logTime[80],
              logMessage[512];
        pid_t pid;
@@ -97,9 +97,9 @@ mono_log_write_syslog(const char *domain, GLogLevelFlags level, mono_bool hdr, c
                mono_log_open_logfile(NULL, NULL);
 
        time(&t);
-       localtime(&t, &tod);
+       tod = localtime(&t);
        pid = _getpid();
-       strftime(logTime, sizeof(logTime), "%F %T", &tod);
+       strftime(logTime, sizeof(logTime), "%F %T", tod);
        iLog = snprintf(logMessage, sizeof(logMessage), "%s level[%c] mono[%d]: ",
                        logTime,mapLogFileLevel(level),pid);
        nLog = sizeof(logMessage) - iLog - 2;