diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2013-01-04 10:19:22 +0100 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2013-01-04 10:19:22 +0100 |
commit | 26bffbb6a4036a1a1bc3bca99ad9dad66f0618d7 (patch) | |
tree | df3597685dcb26345a6eb756fbbaa4b757134ee3 /debug.c | |
parent | c0a0deec86c79d276bc4443fd0f6aef7b6b12f9f (diff) |
Added debug_util.h with threadsafe localtime (localtime_r does not exist on windows.)
Diffstat (limited to 'debug.c')
-rw-r--r-- | debug.c | 30 |
1 files changed, 29 insertions, 1 deletions
@@ -81,12 +81,38 @@ struct dbg_config_t { */ }; +#ifdef WITH_DBG_MUTEX + pthread_mutex_t localtime_mutex; +#endif + +struct tm *dbg_localtime(const time_t *timep, struct tm *result) +{ + struct tm *res = NULL; +#ifdef WITH_DBG_MUTEX + pthread_mutex_lock(&localtime_mutex); +#endif + + if(timep && result) { + memcpy(result,localtime(timep),sizeof(*result)); + res = result; + } + +#ifdef WITH_DBG_MUTEX + pthread_mutex_unlock(&localtime_mutex); +#endif + + return res; +} + + static void dbg_mutex_init() { #ifdef WITH_DBG_MUTEX if(dbg_config.flags & DBG_FLAG_USE_MUTEX) { pthread_mutex_init(&dbg_config.mutex, NULL); } + + pthread_mutex_init(&localtime_mutex, NULL); #endif } @@ -117,6 +143,8 @@ static void dbg_mutex_close() dbg_mutex_unlock(); pthread_mutex_destroy(&dbg_config.mutex); } + + pthread_mutex_destroy(&localtime_mutex); #endif } @@ -206,7 +234,7 @@ static int dbg_create_header(char *hdr, size_t size) { time_t rawtime = time(NULL); struct tm t; - localtime_r(&rawtime, &t); + dbg_localtime(&rawtime, &t); return snprintf(hdr, size, "%d-%02d-%02d %02d:%02d:%02d", |