Skip to content

Commit 3316a9e

Browse files
author
Jim Posen
committed
util: Encapsulate logCategories within BCLog::Logger.
1 parent 6a6d764 commit 3316a9e

File tree

7 files changed

+55
-28
lines changed

7 files changed

+55
-28
lines changed

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -968,7 +968,7 @@ bool AppInitParameterInteraction()
968968
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
969969
continue;
970970
}
971-
logCategories |= flag;
971+
g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag));
972972
}
973973
}
974974
}
@@ -980,7 +980,7 @@ bool AppInitParameterInteraction()
980980
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
981981
continue;
982982
}
983-
logCategories &= ~flag;
983+
g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag));
984984
}
985985

986986
// Check for -debugnet
@@ -1232,7 +1232,7 @@ bool AppInitMain()
12321232
CreatePidFile(GetPidFile(), getpid());
12331233
#endif
12341234
if (g_logger->fPrintToDebugLog) {
1235-
if (gArgs.GetBoolArg("-shrinkdebugfile", logCategories == BCLog::NONE)) {
1235+
if (gArgs.GetBoolArg("-shrinkdebugfile", g_logger->DefaultShrinkDebugFile())) {
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
12381238
g_logger->ShrinkDebugFile();

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() &&

src/logging.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@ BCLog::Logger* const g_logger = new BCLog::Logger();
2626

2727
bool fLogIPs = DEFAULT_LOGIPS;
2828

29-
/** Log categories bitfield. */
30-
std::atomic<uint32_t> logCategories(0);
31-
3229
static int FileWriteStr(const std::string &str, FILE *fp)
3330
{
3431
return fwrite(str.data(), 1, str.size(), fp);
@@ -62,6 +59,26 @@ bool BCLog::Logger::OpenDebugLog()
6259
return true;
6360
}
6461

62+
void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
63+
{
64+
logCategories |= flag;
65+
}
66+
67+
void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
68+
{
69+
logCategories &= ~flag;
70+
}
71+
72+
bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const
73+
{
74+
return (logCategories.load(std::memory_order_relaxed) & category) != 0;
75+
}
76+
77+
bool BCLog::Logger::DefaultShrinkDebugFile() const
78+
{
79+
return logCategories == BCLog::NONE;
80+
}
81+
6582
struct CLogCategoryDesc
6683
{
6784
uint32_t flag;

src/logging.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ extern const char * const DEFAULT_DEBUGLOGFILE;
2323

2424
extern bool fLogIPs;
2525

26-
extern std::atomic<uint32_t> logCategories;
27-
2826
struct CLogCategoryActive
2927
{
3028
std::string category;
@@ -72,6 +70,9 @@ namespace BCLog {
7270
*/
7371
std::atomic_bool fStartedNewLine{true};
7472

73+
/** Log categories bitfield. */
74+
std::atomic<uint32_t> logCategories{0};
75+
7576
std::string LogTimestampStr(const std::string& str);
7677

7778
public:
@@ -92,16 +93,23 @@ namespace BCLog {
9293
fs::path GetDebugLogPath() const;
9394
bool OpenDebugLog();
9495
void ShrinkDebugFile();
96+
97+
uint32_t GetCategoryMask() const { return logCategories.load(); }
98+
void EnableCategory(LogFlags flag);
99+
void DisableCategory(LogFlags flag);
100+
bool WillLogCategory(LogFlags category) const;
101+
102+
bool DefaultShrinkDebugFile() const;
95103
};
96104

97105
} // namespace BCLog
98106

99107
extern BCLog::Logger* const g_logger;
100108

101109
/** Return true if log accepts specified category */
102-
static inline bool LogAcceptCategory(uint32_t category)
110+
static inline bool LogAcceptCategory(BCLog::LogFlags category)
103111
{
104-
return (logCategories.load(std::memory_order_relaxed) & category) != 0;
112+
return g_logger->WillLogCategory(category);
105113
}
106114

107115
/** Returns a string with the log categories. */

src/rpc/misc.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -346,21 +346,23 @@ UniValue getmemoryinfo(const JSONRPCRequest& request)
346346
}
347347
}
348348

349-
uint32_t getCategoryMask(UniValue cats) {
349+
void EnableOrDisableLogCategories(UniValue cats, bool enable) {
350350
cats = cats.get_array();
351-
uint32_t mask = 0;
352351
for (unsigned int i = 0; i < cats.size(); ++i) {
353352
uint32_t flag = 0;
354353
std::string cat = cats[i].get_str();
355354
if (!GetLogCategory(&flag, &cat)) {
356355
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat);
357356
}
358357
if (flag == BCLog::NONE) {
359-
return 0;
358+
return;
359+
}
360+
if (enable) {
361+
g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag));
362+
} else {
363+
g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag));
360364
}
361-
mask |= flag;
362365
}
363-
return mask;
364366
}
365367

366368
UniValue logging(const JSONRPCRequest& request)
@@ -399,25 +401,25 @@ UniValue logging(const JSONRPCRequest& request)
399401
);
400402
}
401403

402-
uint32_t originalLogCategories = logCategories;
404+
uint32_t original_log_categories = g_logger->GetCategoryMask();
403405
if (request.params[0].isArray()) {
404-
logCategories |= getCategoryMask(request.params[0]);
406+
EnableOrDisableLogCategories(request.params[0], true);
405407
}
406-
407408
if (request.params[1].isArray()) {
408-
logCategories &= ~getCategoryMask(request.params[1]);
409+
EnableOrDisableLogCategories(request.params[1], false);
409410
}
411+
uint32_t updated_log_categories = g_logger->GetCategoryMask();
412+
uint32_t changed_log_categories = original_log_categories ^ updated_log_categories;
410413

411414
// Update libevent logging if BCLog::LIBEVENT has changed.
412415
// If the library version doesn't allow it, UpdateHTTPServerLogging() returns false,
413416
// in which case we should clear the BCLog::LIBEVENT flag.
414417
// Throw an error if the user has explicitly asked to change only the libevent
415418
// flag and it failed.
416-
uint32_t changedLogCategories = originalLogCategories ^ logCategories;
417-
if (changedLogCategories & BCLog::LIBEVENT) {
418-
if (!UpdateHTTPServerLogging(logCategories & BCLog::LIBEVENT)) {
419-
logCategories &= ~BCLog::LIBEVENT;
420-
if (changedLogCategories == BCLog::LIBEVENT) {
419+
if (changed_log_categories & BCLog::LIBEVENT) {
420+
if (!UpdateHTTPServerLogging(g_logger->WillLogCategory(BCLog::LIBEVENT))) {
421+
g_logger->DisableCategory(BCLog::LIBEVENT);
422+
if (changed_log_categories == BCLog::LIBEVENT) {
421423
throw JSONRPCError(RPC_INVALID_PARAMETER, "libevent logging cannot be updated when using libevent before v2.1.1.");
422424
}
423425
}

0 commit comments

Comments
 (0)