From 95a985311dd1a1019762375e9013d018eb6e18bb Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Wed, 16 Feb 2011 21:16:40 +0000 Subject: log_printf is thread safe now, however log_init and log_add_target isn't diff --git a/src/log.c b/src/log.c index e167c5c..353fa06 100644 --- a/src/log.c +++ b/src/log.c @@ -32,6 +32,7 @@ #include #include #include +#include #define SYSLOG_NAMES #include @@ -151,6 +152,7 @@ void log_targets_log(log_targets_t* targets, log_prio_t prio, const char* msg) if(!targets) return; + pthread_mutex_lock(&(stdlog.log_mutex_)); log_target_t* tmp = targets->first_; while(tmp) { if(tmp->log != NULL && tmp->enabled_ && tmp->max_prio_ >= prio) @@ -158,6 +160,7 @@ void log_targets_log(log_targets_t* targets, log_prio_t prio, const char* msg) tmp = tmp->next_; } + pthread_mutex_unlock(&(stdlog.log_mutex_)); } void log_targets_clear(log_targets_t* targets) @@ -181,10 +184,12 @@ void log_init() { stdlog.max_prio_ = 0; stdlog.targets_.first_ = NULL; + pthread_mutex_init(&(stdlog.log_mutex_), NULL); } void log_close() { + pthread_mutex_destroy(&(stdlog.log_mutex_)); log_targets_clear(&stdlog.targets_); } @@ -214,7 +219,7 @@ void log_printf(log_prio_t prio, const char* fmt, ...) if(stdlog.max_prio_ < prio) return; - static char msg[MSG_LENGTH_MAX]; + char msg[MSG_LENGTH_MAX]; va_list args; va_start(args, fmt); @@ -229,7 +234,7 @@ void log_print_hex_dump(log_prio_t prio, const uint8_t* buf, uint32_t len) if(stdlog.max_prio_ < prio) return; - static char msg[MSG_LENGTH_MAX]; + char msg[MSG_LENGTH_MAX]; if(!buf) { snprintf(msg, MSG_LENGTH_MAX, "(NULL)"); diff --git a/src/log.h b/src/log.h index 30ae7df..aaf7a1a 100644 --- a/src/log.h +++ b/src/log.h @@ -28,6 +28,8 @@ #ifndef RHARCHIVE_log_h_INCLUDED #define RHARCHIVE_log_h_INCLUDED +#include + #define MSG_LENGTH_MAX 1024 enum log_prio_enum { ERROR = 1, WARNING = 2, NOTICE = 3, @@ -69,6 +71,7 @@ void log_targets_clear(log_targets_t* targets); struct log_struct { log_prio_t max_prio_; log_targets_t targets_; + pthread_mutex_t log_mutex_; }; typedef struct log_struct log_t; diff --git a/src/rharchive.c b/src/rharchive.c index a44a5d2..6ab1538 100644 --- a/src/rharchive.c +++ b/src/rharchive.c @@ -47,7 +47,7 @@ static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data) switch (GST_MESSAGE_TYPE(msg)) { case GST_MESSAGE_EOS: { - log_printf(NOTICE, "End of stream"); // thread safety... + log_printf(NOTICE, "End of stream"); g_main_loop_quit (loop); break; } @@ -57,7 +57,7 @@ static gboolean bus_call(GstBus *bus, GstMessage *msg, gpointer data) gst_message_parse_error(msg, &error, &debug); g_free(debug); - log_printf(ERROR, "%s", error->message); // thread safety... + log_printf(ERROR, "%s", error->message); g_error_free(error); g_main_loop_quit(loop); break; -- cgit v0.10.2