diff --git a/CMakeLists.txt b/CMakeLists.txt index c898e89b9..455a2695d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -551,6 +551,7 @@ CHECK_FUNCTION_EXISTS(atoll LWS_HAVE_ATOLL) CHECK_FUNCTION_EXISTS(_atoi64 LWS_HAVE__ATOI64) CHECK_FUNCTION_EXISTS(_stat32i64 LWS_HAVE__STAT32I64) CHECK_FUNCTION_EXISTS(clock_gettime LWS_HAVE_CLOCK_GETTIME) +CHECK_FUNCTION_EXISTS(localtime_r LWS_HAVE_LOCALTIME_R) CHECK_FUNCTION_EXISTS(getgrgid_r LWS_HAVE_GETGRGID_R) CHECK_FUNCTION_EXISTS(getgrnam_r LWS_HAVE_GETGRNAM_R) CHECK_FUNCTION_EXISTS(getpwuid_r LWS_HAVE_GETPWUID_R) diff --git a/cmake/lws_config.h.in b/cmake/lws_config.h.in index 78cc5a84f..04c8ceb00 100644 --- a/cmake/lws_config.h.in +++ b/cmake/lws_config.h.in @@ -50,6 +50,7 @@ #cmakedefine LWS_HAVE_EVP_aes_128_xts #cmakedefine LWS_HAVE_EVP_PKEY_new_raw_private_key #cmakedefine LWS_HAVE_EXECVPE +#cmakedefine LWS_HAVE_LOCALTIME_R #cmakedefine LWS_HAVE_GETGRGID_R #cmakedefine LWS_HAVE_GETGRNAM_R #cmakedefine LWS_HAVE_GETPWUID_R diff --git a/lib/core/logs.c b/lib/core/logs.c index d5d6a19cb..9d8a73387 100644 --- a/lib/core/logs.c +++ b/lib/core/logs.c @@ -200,7 +200,7 @@ lwsl_timestamp(int level, char *p, size_t len) unsigned long long now; struct timeval tv; struct tm *ptm = NULL; -#ifndef WIN32 +#if defined(LWS_HAVE_LOCALTIME_R) struct tm tm; #endif int n; @@ -209,13 +209,10 @@ lwsl_timestamp(int level, char *p, size_t len) o_now = tv.tv_sec; now = ((unsigned long long)tv.tv_sec * 10000) + (unsigned int)(tv.tv_usec / 100); -#ifndef _WIN32_WCE -#ifdef WIN32 - ptm = localtime(&o_now); +#if defined(LWS_HAVE_LOCALTIME_R) + ptm = localtime_r(&o_now, &tm); #else - if (localtime_r(&o_now, &tm)) - ptm = &tm; -#endif + ptm = localtime(&o_now); #endif p[0] = '\0'; for (n = 0; n < LLL_COUNT; n++) { diff --git a/lib/roles/http/server/access-log.c b/lib/roles/http/server/access-log.c index 9623d6f2b..98b13284c 100644 --- a/lib/roles/http/server/access-log.c +++ b/lib/roles/http/server/access-log.c @@ -48,7 +48,10 @@ lws_prepare_access_log_info(struct lws *wsi, char *uri_ptr, int uri_len, int met struct lws *nwsi; const char *me; int l = 256, m; - struct tm *tmp; + struct tm *ptm = NULL; +#if defined(LWS_HAVE_LOCALTIME_R) + struct tm tm; +#endif if (!wsi->a.vhost) return; @@ -64,9 +67,13 @@ lws_prepare_access_log_info(struct lws *wsi, char *uri_ptr, int uri_len, int met if (!wsi->http.access_log.header_log) return; - tmp = localtime(&t); - if (tmp) - strftime(da, sizeof(da), "%d/%b/%Y:%H:%M:%S %z", tmp); +#if defined(LWS_HAVE_LOCALTIME_R) + ptm = localtime_r(&t, &tm); +#else + ptm = localtime(&t); +#endif + if (ptm) + strftime(da, sizeof(da), "%d/%b/%Y:%H:%M:%S %z", ptm); else strcpy(da, "01/Jan/1970:00:00:00 +0000");