From: Neale Ferguson Date: Thu, 2 Jun 2016 19:33:12 +0000 (-0400) Subject: Correct use of localtime() in Win32 X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;h=583d1a2580a5fc61d0002920c17c23fa71375c3d;p=mono.git Correct use of localtime() in Win32 --- diff --git a/mono/sgen/sgen-gc.h b/mono/sgen/sgen-gc.h index 655efdeb30e..fb264a4b070 100644 --- a/mono/sgen/sgen-gc.h +++ b/mono/sgen/sgen-gc.h @@ -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) diff --git a/mono/utils/mono-log-common.c b/mono/utils/mono-log-common.c index 23b22f34005..bb22e6a3147 100644 --- a/mono/utils/mono-log-common.c +++ b/mono/utils/mono-log-common.c @@ -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); } diff --git a/mono/utils/mono-log-windows.c b/mono/utils/mono-log-windows.c index 2bcc145ad23..2609c1ea9da 100644 --- a/mono/utils/mono-log-windows.c +++ b/mono/utils/mono-log-windows.c @@ -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;