Ver código fonte

BLog: implement thread-safety

ambrop7 13 anos atrás
pai
commit
46212f3fb9
2 arquivos alterados com 53 adições e 11 exclusões
  1. 49 10
      base/BLog.h
  2. 4 1
      ncd/modules/socket.c

+ 49 - 10
base/BLog.h

@@ -38,6 +38,7 @@
 #include <string.h>
 
 #include <misc/debug.h>
+#include <base/BMutex.h>
 
 // auto-generated channel numbers and number of channels
 #include <generated/blog_channels_defines.h>
@@ -66,6 +67,10 @@ struct _BLog_global {
     struct _BLog_channel channels[BLOG_NUM_CHANNELS];
     _BLog_log_func log_func;
     _BLog_free_func free_func;
+    BMutex mutex;
+#ifndef NDEBUG
+    int logging;
+#endif
     char logbuf[2048];
     int logbuf_pos;
 };
@@ -81,6 +86,7 @@ static void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func);
 static void BLog_Free (void);
 static void BLog_SetChannelLoglevel (int channel, int loglevel);
 static int BLog_WouldLog (int channel, int level);
+static void BLog_Begin (void);
 static void BLog_AppendVarArg (const char *fmt, va_list vl);
 static void BLog_Append (const char *fmt, ...);
 static void BLog_AppendBytes (const char *data, size_t len);
@@ -118,13 +124,23 @@ void BLog_Init (_BLog_log_func log_func, _BLog_free_func free_func)
     
     blog_global.log_func = log_func;
     blog_global.free_func = free_func;
+#ifndef NDEBUG
+    blog_global.logging = 0;
+#endif
     blog_global.logbuf_pos = 0;
     blog_global.logbuf[0] = '\0';
+    
+    ASSERT_FORCE(BMutex_Init(&blog_global.mutex))
 }
 
 void BLog_Free (void)
 {
     ASSERT(blog_global.initialized)
+#ifndef NDEBUG
+    ASSERT(!blog_global.logging)
+#endif
+    
+    BMutex_Free(&blog_global.mutex);
     
     #ifndef NDEBUG
     blog_global.initialized = 0;
@@ -151,10 +167,24 @@ int BLog_WouldLog (int channel, int level)
     return (level <= blog_global.channels[channel].loglevel);
 }
 
-void BLog_AppendVarArg (const char *fmt, va_list vl)
+void BLog_Begin (void)
 {
     ASSERT(blog_global.initialized)
     
+    BMutex_Lock(&blog_global.mutex);
+    
+#ifndef NDEBUG
+    ASSERT(!blog_global.logging)
+    blog_global.logging = 1;
+#endif
+}
+
+void BLog_AppendVarArg (const char *fmt, va_list vl)
+{
+    ASSERT(blog_global.initialized)
+#ifndef NDEBUG
+    ASSERT(blog_global.logging)
+#endif
     ASSERT(blog_global.logbuf_pos >= 0)
     ASSERT(blog_global.logbuf_pos < sizeof(blog_global.logbuf))
     
@@ -170,6 +200,9 @@ void BLog_AppendVarArg (const char *fmt, va_list vl)
 void BLog_Append (const char *fmt, ...)
 {
     ASSERT(blog_global.initialized)
+#ifndef NDEBUG
+    ASSERT(blog_global.logging)
+#endif
     
     va_list vl;
     va_start(vl, fmt);
@@ -180,7 +213,9 @@ void BLog_Append (const char *fmt, ...)
 void BLog_AppendBytes (const char *data, size_t len)
 {
     ASSERT(blog_global.initialized)
-    
+#ifndef NDEBUG
+    ASSERT(blog_global.logging)
+#endif
     ASSERT(blog_global.logbuf_pos >= 0)
     ASSERT(blog_global.logbuf_pos < sizeof(blog_global.logbuf))
     
@@ -195,6 +230,9 @@ void BLog_AppendBytes (const char *data, size_t len)
 void BLog_Finish (int channel, int level)
 {
     ASSERT(blog_global.initialized)
+#ifndef NDEBUG
+    ASSERT(blog_global.logging)
+#endif
     ASSERT(channel >= 0 && channel < BLOG_NUM_CHANNELS)
     ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
     ASSERT(BLog_WouldLog(channel, level))
@@ -205,8 +243,13 @@ void BLog_Finish (int channel, int level)
     
     blog_global.log_func(channel, level, blog_global.logbuf);
     
+#ifndef NDEBUG
+    blog_global.logging = 0;
+#endif
     blog_global.logbuf_pos = 0;
     blog_global.logbuf[0] = '\0';
+    
+    BMutex_Unlock(&blog_global.mutex);
 }
 
 void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list vl)
@@ -216,11 +259,10 @@ void BLog_LogToChannelVarArg (int channel, int level, const char *fmt, va_list v
     ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
     
     if (!BLog_WouldLog(channel, level)) {
-        blog_global.logbuf_pos = 0;
-        blog_global.logbuf[0] = '\0';
         return;
     }
     
+    BLog_Begin();
     BLog_AppendVarArg(fmt, vl);
     BLog_Finish(channel, level);
 }
@@ -232,14 +274,13 @@ void BLog_LogToChannel (int channel, int level, const char *fmt, ...)
     ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
     
     if (!BLog_WouldLog(channel, level)) {
-        blog_global.logbuf_pos = 0;
-        blog_global.logbuf[0] = '\0';
         return;
     }
     
     va_list vl;
     va_start(vl, fmt);
     
+    BLog_Begin();
     BLog_AppendVarArg(fmt, vl);
     BLog_Finish(channel, level);
     
@@ -253,11 +294,10 @@ void BLog_LogViaFuncVarArg (BLog_logfunc func, void *arg, int channel, int level
     ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
     
     if (!BLog_WouldLog(channel, level)) {
-        blog_global.logbuf_pos = 0;
-        blog_global.logbuf[0] = '\0';
         return;
     }
     
+    BLog_Begin();
     func(arg);
     BLog_AppendVarArg(fmt, vl);
     BLog_Finish(channel, level);
@@ -270,14 +310,13 @@ void BLog_LogViaFunc (BLog_logfunc func, void *arg, int channel, int level, cons
     ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
     
     if (!BLog_WouldLog(channel, level)) {
-        blog_global.logbuf_pos = 0;
-        blog_global.logbuf[0] = '\0';
         return;
     }
     
     va_list vl;
     va_start(vl, fmt);
     
+    BLog_Begin();
     func(arg);
     BLog_AppendVarArg(fmt, vl);
     BLog_Finish(channel, level);

+ 4 - 1
ncd/modules/socket.c

@@ -278,10 +278,13 @@ static void connection_log (struct connection *o, int level, const char *fmt, ..
         
         case CONNECTION_TYPE_LISTEN: {
             if (BLog_WouldLog(BLOG_CURRENT_CHANNEL, level)) {
+                BLog_Begin();
+                o->listen.listen_inst->i->params->logfunc(o->listen.listen_inst->i);
                 char addr_str[BADDR_MAX_PRINT_LEN];
                 BAddr_Print(&o->listen.addr, addr_str);
                 BLog_Append("client %s: ", addr_str);
-                NCDModuleInst_Backend_LogVarArg(o->listen.listen_inst->i, BLOG_CURRENT_CHANNEL, level, fmt, vl);
+                BLog_AppendVarArg(fmt, vl);
+                BLog_Finish(BLOG_CURRENT_CHANNEL, level);
             }
         } break;