Serialize access to debug.log stream
authorMichael Hendricks <michael@ndrix.org>
Fri, 2 Mar 2012 19:24:38 +0000 (12:24 -0700)
committerLuke Dashjr <luke-jr+git@utopios.org>
Wed, 6 Jun 2012 19:29:28 +0000 (19:29 +0000)
Acquire an exclusive, advisory lock before sending output to debug.log
and release it when we're done. This should avoid output from multiple
threads being interspersed in the log file.

We can't use CRITICAL_SECTION machinery for this because the debug log
is written during startup and shutdown when that machinery is not
available.

(Thanks to Gavin for pointing out the CRITICAL_SECTION problems based
on his earlier work in this area)

src/util.cpp

index 6dc2f3b..ccd39aa 100644 (file)
@@ -22,6 +22,7 @@ namespace boost {
 #include <boost/interprocess/sync/interprocess_mutex.hpp>
 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
 #include <boost/foreach.hpp>
+#include <boost/thread.hpp>
 
 using namespace std;
 using namespace boost;
@@ -193,6 +194,8 @@ inline int OutputDebugStringF(const char* pszFormat, ...)
         if (fileout)
         {
             static bool fStartedNewLine = true;
+            static boost::mutex mutexDebugLog;
+            boost::mutex::scoped_lock scoped_lock(mutexDebugLog);
 
             // Debug print useful for profiling
             if (fLogTimestamps && fStartedNewLine)