Skip to content

Commit 5a66642

Browse files
committed
Merge #12954: util: Refactor logging code into a global object
8c2d695 util: Store debug log file path in BCLog::Logger member. (Jim Posen) 8e7b961 scripted-diff: Rename BCLog::Logger member variables. (Jim Posen) 1eac317 util: Refactor GetLogCategory. (Jim Posen) 3316a9e util: Encapsulate logCategories within BCLog::Logger. (Jim Posen) 6a6d764 util: Move debug file management functions into Logger. (Jim Posen) f55f4fc util: Establish global logger object. (Jim Posen) Pull request description: This is purely a refactor with no behavior changes. This creates a new class `BCLog::Logger` to encapsulate all global logging configuration and state. Tree-SHA512: b34811f54a53b7375d7b6f84925453c6f2419d21179379ee28b3843d0f4ff8e22020de84a5e783453ea927e9074e32de8ecd05a6fa50d7bb05502001aaed8e53
2 parents 3186ad4 + 8c2d695 commit 5a66642

File tree

10 files changed

+204
-179
lines changed

10 files changed

+204
-179
lines changed

src/bench/bench_bitcoin.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ main(int argc, char** argv)
4646
RandomInit();
4747
ECC_Start();
4848
SetupEnvironment();
49-
fPrintToDebugLog = false; // don't want to write to debug.log file
5049

5150
int64_t evaluations = gArgs.GetArg("-evals", DEFAULT_BENCH_EVALUATIONS);
5251
std::string regex_filter = gArgs.GetArg("-filter", DEFAULT_BENCH_FILTER);

src/httpserver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,8 @@ bool InitHTTPServer()
364364
// Update libevent's log handling. Returns false if our version of
365365
// libevent doesn't support debug logging, in which case we should
366366
// clear the BCLog::LIBEVENT flag.
367-
if (!UpdateHTTPServerLogging(logCategories & BCLog::LIBEVENT)) {
368-
logCategories &= ~BCLog::LIBEVENT;
367+
if (!UpdateHTTPServerLogging(g_logger->WillLogCategory(BCLog::LIBEVENT))) {
368+
g_logger->DisableCategory(BCLog::LIBEVENT);
369369
}
370370

371371
#ifdef WIN32

src/httpserver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void InterruptHTTPServer();
3232
/** Stop HTTP server */
3333
void StopHTTPServer();
3434

35-
/** Change logging level for libevent. Removes BCLog::LIBEVENT from logCategories if
35+
/** Change logging level for libevent. Removes BCLog::LIBEVENT from log categories if
3636
* libevent doesn't support debug logging.*/
3737
bool UpdateHTTPServerLogging(bool enable);
3838

src/init.cpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ static void HandleSIGTERM(int)
305305

306306
static void HandleSIGHUP(int)
307307
{
308-
fReopenDebugLog = true;
308+
g_logger->m_reopen_file = true;
309309
}
310310

311311
#ifndef WIN32
@@ -826,15 +826,18 @@ static std::string ResolveErrMsg(const char * const optname, const std::string&
826826
*/
827827
void InitLogging()
828828
{
829+
g_logger->m_print_to_file = !gArgs.IsArgNegated("-debuglogfile");
830+
g_logger->m_file_path = AbsPathForConfigVal(gArgs.GetArg("-debuglogfile", DEFAULT_DEBUGLOGFILE));
831+
829832
// Add newlines to the logfile to distinguish this execution from the last
830833
// one; called before console logging is set up, so this is only sent to
831834
// debug.log.
832835
LogPrintf("\n\n\n\n\n");
833836

834-
fPrintToConsole = gArgs.GetBoolArg("-printtoconsole", !gArgs.GetBoolArg("-daemon", false));
835-
fPrintToDebugLog = !gArgs.IsArgNegated("-debuglogfile");
836-
fLogTimestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
837-
fLogTimeMicros = gArgs.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
837+
g_logger->m_print_to_console = gArgs.GetBoolArg("-printtoconsole", !gArgs.GetBoolArg("-daemon", false));
838+
g_logger->m_log_timestamps = gArgs.GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS);
839+
g_logger->m_log_time_micros = gArgs.GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
840+
838841
fLogIPs = gArgs.GetBoolArg("-logips", DEFAULT_LOGIPS);
839842

840843
std::string version_string = FormatFullVersion();
@@ -962,24 +965,18 @@ bool AppInitParameterInteraction()
962965
if (std::none_of(categories.begin(), categories.end(),
963966
[](std::string cat){return cat == "0" || cat == "none";})) {
964967
for (const auto& cat : categories) {
965-
uint32_t flag = 0;
966-
if (!GetLogCategory(&flag, &cat)) {
968+
if (!g_logger->EnableCategory(cat)) {
967969
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
968-
continue;
969970
}
970-
logCategories |= flag;
971971
}
972972
}
973973
}
974974

975975
// Now remove the logging categories which were explicitly excluded
976976
for (const std::string& cat : gArgs.GetArgs("-debugexclude")) {
977-
uint32_t flag = 0;
978-
if (!GetLogCategory(&flag, &cat)) {
977+
if (!g_logger->DisableCategory(cat)) {
979978
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
980-
continue;
981979
}
982-
logCategories &= ~flag;
983980
}
984981

985982
// Check for -debugnet
@@ -1230,18 +1227,19 @@ bool AppInitMain()
12301227
#ifndef WIN32
12311228
CreatePidFile(GetPidFile(), getpid());
12321229
#endif
1233-
if (fPrintToDebugLog) {
1234-
if (gArgs.GetBoolArg("-shrinkdebugfile", logCategories == BCLog::NONE)) {
1230+
if (g_logger->m_print_to_file) {
1231+
if (gArgs.GetBoolArg("-shrinkdebugfile", g_logger->DefaultShrinkDebugFile())) {
12351232
// Do this first since it both loads a bunch of debug.log into memory,
12361233
// and because this needs to happen before any other debug.log printing
1237-
ShrinkDebugFile();
1234+
g_logger->ShrinkDebugFile();
12381235
}
1239-
if (!OpenDebugLog()) {
1240-
return InitError(strprintf("Could not open debug log file %s", GetDebugLogPath().string()));
1236+
if (!g_logger->OpenDebugLog()) {
1237+
return InitError(strprintf("Could not open debug log file %s",
1238+
g_logger->m_file_path.string()));
12411239
}
12421240
}
12431241

1244-
if (!fLogTimestamps)
1242+
if (!g_logger->m_log_timestamps)
12451243
LogPrintf("Startup time: %s\n", FormatISO8601DateTime(GetTime()));
12461244
LogPrintf("Default data directory %s\n", GetDefaultDataDir().string());
12471245
LogPrintf("Using data directory %s\n", GetDataDir().string());

src/interfaces/node.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class NodeImpl : public Node
6060
void initLogging() override { InitLogging(); }
6161
void initParameterInteraction() override { InitParameterInteraction(); }
6262
std::string getWarnings(const std::string& type) override { return GetWarnings(type); }
63-
uint32_t getLogCategories() override { return ::logCategories; }
63+
uint32_t getLogCategories() override { return g_logger->GetCategoryMask(); }
6464
bool baseInitialize() override
6565
{
6666
return AppInitBasicSetup() && AppInitParameterInteraction() && AppInitSanityChecks() &&

0 commit comments

Comments
 (0)