Skip to content

Commit 6a6d764

Browse files
author
Jim Posen
committed
util: Move debug file management functions into Logger.
1 parent f55f4fc commit 6a6d764

File tree

3 files changed

+23
-57
lines changed

3 files changed

+23
-57
lines changed

src/init.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,10 +1235,11 @@ bool AppInitMain()
12351235
if (gArgs.GetBoolArg("-shrinkdebugfile", logCategories == BCLog::NONE)) {
12361236
// Do this first since it both loads a bunch of debug.log into memory,
12371237
// and because this needs to happen before any other debug.log printing
1238-
ShrinkDebugFile();
1238+
g_logger->ShrinkDebugFile();
12391239
}
1240-
if (!OpenDebugLog()) {
1241-
return InitError(strprintf("Could not open debug log file %s", GetDebugLogPath().string()));
1240+
if (!g_logger->OpenDebugLog()) {
1241+
return InitError(strprintf("Could not open debug log file %s",
1242+
g_logger->GetDebugLogPath().string()));
12421243
}
12431244
}
12441245

src/logging.cpp

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
#include <util.h>
88
#include <utilstrencodings.h>
99

10-
#include <list>
11-
#include <mutex>
12-
1310
const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
1411

1512
/**
@@ -31,57 +28,23 @@ bool fLogIPs = DEFAULT_LOGIPS;
3128

3229
/** Log categories bitfield. */
3330
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;
5931

6032
static int FileWriteStr(const std::string &str, FILE *fp)
6133
{
6234
return fwrite(str.data(), 1, str.size(), fp);
6335
}
6436

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
7338
{
7439
fs::path logfile(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
7540
return AbsPathForConfigVal(logfile);
7641
}
7742

78-
bool OpenDebugLog()
43+
bool BCLog::Logger::OpenDebugLog()
7944
{
80-
std::call_once(debugPrintInitFlag, &DebugPrintInit);
81-
std::lock_guard<std::mutex> scoped_lock(*mutexDebugLog);
45+
std::lock_guard<std::mutex> scoped_lock(mutexDebugLog);
8246

8347
assert(fileout == nullptr);
84-
assert(vMsgsBeforeOpenLog);
8548
fs::path pathDebug = GetDebugLogPath();
8649

8750
fileout = fsbridge::fopen(pathDebug, "a");
@@ -91,13 +54,11 @@ bool OpenDebugLog()
9154

9255
setbuf(fileout, nullptr); // unbuffered
9356
// 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();
9760
}
9861

99-
delete vMsgsBeforeOpenLog;
100-
vMsgsBeforeOpenLog = nullptr;
10162
return true;
10263
}
10364

@@ -225,14 +186,12 @@ int BCLog::Logger::LogPrintStr(const std::string &str)
225186
fflush(stdout);
226187
}
227188
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);
230190

231191
// buffer if we haven't opened the log yet
232192
if (fileout == nullptr) {
233-
assert(vMsgsBeforeOpenLog);
234193
ret = strTimestamped.length();
235-
vMsgsBeforeOpenLog->push_back(strTimestamped);
194+
vMsgsBeforeOpenLog.push_back(strTimestamped);
236195
}
237196
else
238197
{
@@ -250,7 +209,7 @@ int BCLog::Logger::LogPrintStr(const std::string &str)
250209
return ret;
251210
}
252211

253-
void ShrinkDebugFile()
212+
void BCLog::Logger::ShrinkDebugFile()
254213
{
255214
// Amount of debug.log to save at end when shrinking (must fit in memory)
256215
constexpr size_t RECENT_DEBUG_HISTORY_SIZE = 10 * 1000000;

src/logging.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <atomic>
1313
#include <cstdint>
14+
#include <list>
15+
#include <mutex>
1416
#include <string>
1517
#include <vector>
1618

@@ -59,6 +61,10 @@ namespace BCLog {
5961
class Logger
6062
{
6163
private:
64+
FILE* fileout = nullptr;
65+
std::mutex mutexDebugLog;
66+
std::list<std::string> vMsgsBeforeOpenLog;
67+
6268
/**
6369
* fStartedNewLine is a state variable that will suppress printing of
6470
* the timestamp when multiple calls are made that don't end in a
@@ -82,6 +88,10 @@ namespace BCLog {
8288

8389
/** Returns whether logs will be written to any output */
8490
bool Enabled() const { return fPrintToConsole || fPrintToDebugLog; }
91+
92+
fs::path GetDebugLogPath() const;
93+
bool OpenDebugLog();
94+
void ShrinkDebugFile();
8595
};
8696

8797
} // namespace BCLog
@@ -141,8 +151,4 @@ template<typename T, typename... Args> static inline void MarkUsed(const T& t, c
141151
} while(0)
142152
#endif
143153

144-
fs::path GetDebugLogPath();
145-
bool OpenDebugLog();
146-
void ShrinkDebugFile();
147-
148154
#endif // BITCOIN_LOGGING_H

0 commit comments

Comments
 (0)