]> git.imager.perl.org - imager.git/commitdiff
handle the race between writing the head and tail of an entry between threads
authorTony Cook <tony@develop-help.com>
Sat, 27 Oct 2012 02:18:10 +0000 (13:18 +1100)
committerTony Cook <tony@develop-help.com>
Fri, 14 Dec 2012 09:27:39 +0000 (20:27 +1100)
in some cases the head and tails from different threads would interleave

log.c

diff --git a/log.c b/log.c
index ebcb9a0d78458c8d7887863202101c3408af697a..62b8eab8c79132d5594b174916f306d5902155a2 100644 (file)
--- a/log.c
+++ b/log.c
@@ -13,6 +13,8 @@
 
 #define LOG_DATE_FORMAT "%Y/%m/%d %H:%M:%S"
 
+static i_mutex_t log_mutex;
+
 static void
 im_vloog(pIMCTX, int level, const char *fmt, va_list ap);
 
@@ -24,6 +26,10 @@ int
 im_init_log(pIMCTX, const char* name,int level) {
   i_clear_error();
 
+  if (!log_mutex) {
+    log_mutex = i_mutex_new();
+  }
+
   if (aIMCTX->lg_file) {
     if (aIMCTX->own_log)
       fclose(aIMCTX->lg_file);
@@ -95,6 +101,8 @@ im_vloog(pIMCTX, int level, const char *fmt, va_list ap) {
 
   if (!aIMCTX->lg_file || level > aIMCTX->log_level)
     return;
+
+  i_mutex_lock(log_mutex);
   
   timi = time(NULL);
   str_tm = localtime(&timi);
@@ -103,6 +111,8 @@ im_vloog(pIMCTX, int level, const char *fmt, va_list ap) {
          aIMCTX->filename, aIMCTX->line, level);
   vfprintf(aIMCTX->lg_file, fmt, ap);
   fflush(aIMCTX->lg_file);
+
+  i_mutex_unlock(log_mutex);
 }
 
 void