7
7
#include < util.h>
8
8
#include < utilstrencodings.h>
9
9
10
- #include < list>
11
- #include < mutex>
12
-
13
10
const char * const DEFAULT_DEBUGLOGFILE = " debug.log" ;
14
11
15
12
/* *
@@ -31,57 +28,23 @@ bool fLogIPs = DEFAULT_LOGIPS;
31
28
32
29
/* * Log categories bitfield. */
33
30
std::atomic<uint32_t > logCategories (0 );
34
- /* *
35
- * LogPrintf() has been broken a couple of times now
36
- * by well-meaning people adding mutexes in the most straightforward way.
37
- * It breaks because it may be called by global destructors during shutdown.
38
- * Since the order of destruction of static/global objects is undefined,
39
- * defining a mutex as a global object doesn't work (the mutex gets
40
- * destroyed, and then some later destructor calls OutputDebugStringF,
41
- * maybe indirectly, and you get a core dump at shutdown trying to lock
42
- * the mutex).
43
- */
44
-
45
- static std::once_flag debugPrintInitFlag;
46
-
47
- /* *
48
- * We use std::call_once() to make sure mutexDebugLog and
49
- * vMsgsBeforeOpenLog are initialized in a thread-safe manner.
50
- *
51
- * NOTE: fileout, mutexDebugLog and sometimes vMsgsBeforeOpenLog
52
- * are leaked on exit. This is ugly, but will be cleaned up by
53
- * the OS/libc. When the shutdown sequence is fully audited and
54
- * tested, explicit destruction of these objects can be implemented.
55
- */
56
- static FILE* fileout = nullptr ;
57
- static std::mutex* mutexDebugLog = nullptr ;
58
- static std::list<std::string>* vMsgsBeforeOpenLog;
59
31
60
32
static int FileWriteStr (const std::string &str, FILE *fp)
61
33
{
62
34
return fwrite (str.data (), 1 , str.size (), fp);
63
35
}
64
36
65
- static void DebugPrintInit ()
66
- {
67
- assert (mutexDebugLog == nullptr );
68
- mutexDebugLog = new std::mutex ();
69
- vMsgsBeforeOpenLog = new std::list<std::string>;
70
- }
71
-
72
- fs::path GetDebugLogPath ()
37
+ fs::path BCLog::Logger::GetDebugLogPath () const
73
38
{
74
39
fs::path logfile (gArgs .GetArg (" -debuglogfile" , DEFAULT_DEBUGLOGFILE));
75
40
return AbsPathForConfigVal (logfile);
76
41
}
77
42
78
- bool OpenDebugLog ()
43
+ bool BCLog::Logger:: OpenDebugLog ()
79
44
{
80
- std::call_once (debugPrintInitFlag, &DebugPrintInit);
81
- std::lock_guard<std::mutex> scoped_lock (*mutexDebugLog);
45
+ std::lock_guard<std::mutex> scoped_lock (mutexDebugLog);
82
46
83
47
assert (fileout == nullptr );
84
- assert (vMsgsBeforeOpenLog);
85
48
fs::path pathDebug = GetDebugLogPath ();
86
49
87
50
fileout = fsbridge::fopen (pathDebug, " a" );
@@ -91,13 +54,11 @@ bool OpenDebugLog()
91
54
92
55
setbuf (fileout, nullptr ); // unbuffered
93
56
// dump buffered messages from before we opened the log
94
- while (!vMsgsBeforeOpenLog-> empty ()) {
95
- FileWriteStr (vMsgsBeforeOpenLog-> front (), fileout);
96
- vMsgsBeforeOpenLog-> pop_front ();
57
+ while (!vMsgsBeforeOpenLog. empty ()) {
58
+ FileWriteStr (vMsgsBeforeOpenLog. front (), fileout);
59
+ vMsgsBeforeOpenLog. pop_front ();
97
60
}
98
61
99
- delete vMsgsBeforeOpenLog;
100
- vMsgsBeforeOpenLog = nullptr ;
101
62
return true ;
102
63
}
103
64
@@ -225,14 +186,12 @@ int BCLog::Logger::LogPrintStr(const std::string &str)
225
186
fflush (stdout);
226
187
}
227
188
if (fPrintToDebugLog ) {
228
- std::call_once (debugPrintInitFlag, &DebugPrintInit);
229
- std::lock_guard<std::mutex> scoped_lock (*mutexDebugLog);
189
+ std::lock_guard<std::mutex> scoped_lock (mutexDebugLog);
230
190
231
191
// buffer if we haven't opened the log yet
232
192
if (fileout == nullptr ) {
233
- assert (vMsgsBeforeOpenLog);
234
193
ret = strTimestamped.length ();
235
- vMsgsBeforeOpenLog-> push_back (strTimestamped);
194
+ vMsgsBeforeOpenLog. push_back (strTimestamped);
236
195
}
237
196
else
238
197
{
@@ -250,7 +209,7 @@ int BCLog::Logger::LogPrintStr(const std::string &str)
250
209
return ret;
251
210
}
252
211
253
- void ShrinkDebugFile ()
212
+ void BCLog::Logger:: ShrinkDebugFile ()
254
213
{
255
214
// Amount of debug.log to save at end when shrinking (must fit in memory)
256
215
constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000 ;
0 commit comments