Merge pull request #46 from tezc/localtime-warning

use localtime_r in sc_logger
This commit is contained in:
Tezc 2021-02-16 06:59:28 +03:00 committed by GitHub
commit e7dea7d4e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 12 deletions

View File

@ -36,7 +36,7 @@ if(SC_BUILD_TEST)
target_compile_options(${PROJECT_NAME}_test PRIVATE -DSC_HAVE_WRAP)
target_compile_options(${PROJECT_NAME}_test PRIVATE -fno-builtin)
target_link_options(${PROJECT_NAME}_test PRIVATE
-Wl,--wrap=fprintf,--wrap=vfprintf,--wrap=fopen,--wrap=localtime
-Wl,--wrap=fprintf,--wrap=vfprintf,--wrap=fopen,--wrap=localtime_r
-Wl,--wrap=pthread_mutexattr_init,--wrap=pthread_mutex_init
-Wl,--wrap=fclose)
endif ()

View File

@ -170,12 +170,12 @@ int __wrap_fclose (FILE *__stream)
return -1;
}
bool mock_localtime = false;
extern struct tm *__real_localtime(const time_t *timer);
struct tm *__wrap_localtime(const time_t *timer)
bool mock_localtime_r = false;
extern struct tm *__real_localtime_r(const time_t *timer, struct tm* res);
struct tm *__wrap_localtime_r(const time_t *timer, struct tm* res)
{
if (!mock_localtime) {
return __real_localtime(timer);
if (!mock_localtime_r) {
return __real_localtime_r(timer, res);
}
return NULL;
@ -275,9 +275,9 @@ void fail_test(void)
assert(sc_log_set_file("prev.txt", "current.txt") == -1);
mock_fopen = false;
assert(sc_log_set_file("prev.txt", "current.txt") == 0);
mock_localtime = true;
mock_localtime_r= true;
assert(sc_log_error("test") == -1);
mock_localtime = false;
mock_localtime_r = false;
mock_vfprintf = false;
mock_fprintf = false;
@ -299,7 +299,7 @@ void fail_test(void)
mock_fclose = false;
mock_fprintf = false;
mock_vfprintf = false;
mock_localtime = false;
mock_localtime_r = false;
mock_fopen = false;
}
#else

View File

@ -66,7 +66,7 @@ thread_local char sc_name[32] = "Thread";
#pragma warning(disable : 4996)
#define strcasecmp _stricmp
#define localtime_r(a, b) (localtime_s(b, a) == 0 ? b : NULL)
#include <windows.h>
struct sc_log_mutex
@ -297,8 +297,11 @@ void sc_log_set_callback(void *arg, int (*cb)(void *, enum sc_log_level,
static int sc_log_print_header(FILE *fp, enum sc_log_level level)
{
int rc;
time_t t = time(NULL);
struct tm *tm = localtime(&t);
time_t t;
struct tm result, *tm;
t = time(NULL);
tm = localtime_r(&t, &result);
if (tm == NULL) {
return -1;