summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2013-01-04 10:19:22 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2013-01-04 10:19:22 +0100
commit26bffbb6a4036a1a1bc3bca99ad9dad66f0618d7 (patch)
treedf3597685dcb26345a6eb756fbbaa4b757134ee3
parentc0a0deec86c79d276bc4443fd0f6aef7b6b12f9f (diff)
Added debug_util.h with threadsafe localtime (localtime_r does not exist on windows.)
-rw-r--r--debug.c30
-rw-r--r--debug_syslog.c4
-rw-r--r--debug_util.h39
3 files changed, 71 insertions, 2 deletions
diff --git a/debug.c b/debug.c
index 5c04b61..70a4df1 100644
--- a/debug.c
+++ b/debug.c
@@ -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",
diff --git a/debug_syslog.c b/debug_syslog.c
index 5ed82a9..2644246 100644
--- a/debug_syslog.c
+++ b/debug_syslog.c
@@ -37,6 +37,8 @@
#include <netinet/in.h>
#include <errno.h>
+#include "debug_util.h"
+
#ifndef WITH_DBG_SYSLOG
#warning debug_syslog.c compiled but WITH_DBG_SYSLOG not defined
#endif
@@ -184,7 +186,7 @@ void dbg_syslog_output(char* msg)
const time_t rawtime = time(NULL);
struct tm time;
- localtime_r(&rawtime, &time);
+ dbg_localtime(&rawtime, &time);
char buftime[SYSLOG_TIMELEN];
strftime(buftime, SYSLOG_TIMELEN, "%b %e %H:%M:%S ", &time);
diff --git a/debug_util.h b/debug_util.h
new file mode 100644
index 0000000..589f223
--- /dev/null
+++ b/debug_util.h
@@ -0,0 +1,39 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * debug_util.h
+ *
+ * Fri Jan 4 10:10:03 CET 2013
+ * Copyright 2013 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Debug Module.
+ *
+ * Debug Module is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Debug Module is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Debug Module; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#ifndef __DEBUG_MODULE_DEBUG_UTIL_H__
+#define __DEBUG_MODULE_DEBUG_UTIL_H__
+
+#include <time.h>
+
+/**
+ * Threadsafe version of localtime (if compiled with mutex support).
+ * See man page for the posix localtime_r function.
+ */
+struct tm *dbg_localtime(const time_t *timep, struct tm *result);
+
+#endif/*__DEBUG_MODULE_DEBUG_UTIL_H__*/