Skip to content

Commit 1eac317

Browse files
author
Jim Posen
committed
util: Refactor GetLogCategory.
Changing parameter types from pointers to references and uint32_t to BCLog::LogFlags simplies calling code.
1 parent 3316a9e commit 1eac317

File tree

4 files changed

+48
-38
lines changed

4 files changed

+48
-38
lines changed

src/init.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -963,24 +963,18 @@ bool AppInitParameterInteraction()
963963
if (std::none_of(categories.begin(), categories.end(),
964964
[](std::string cat){return cat == "0" || cat == "none";})) {
965965
for (const auto& cat : categories) {
966-
uint32_t flag = 0;
967-
if (!GetLogCategory(&flag, &cat)) {
966+
if (!g_logger->EnableCategory(cat)) {
968967
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
969-
continue;
970968
}
971-
g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag));
972969
}
973970
}
974971
}
975972

976973
// Now remove the logging categories which were explicitly excluded
977974
for (const std::string& cat : gArgs.GetArgs("-debugexclude")) {
978-
uint32_t flag = 0;
979-
if (!GetLogCategory(&flag, &cat)) {
975+
if (!g_logger->DisableCategory(cat)) {
980976
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
981-
continue;
982977
}
983-
g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag));
984978
}
985979

986980
// Check for -debugnet

src/logging.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
#include <logging.h>
77
#include <util.h>
8-
#include <utilstrencodings.h>
98

109
const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
1110

@@ -64,11 +63,27 @@ void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
6463
logCategories |= flag;
6564
}
6665

66+
bool BCLog::Logger::EnableCategory(const std::string& str)
67+
{
68+
BCLog::LogFlags flag;
69+
if (!GetLogCategory(flag, str)) return false;
70+
EnableCategory(flag);
71+
return true;
72+
}
73+
6774
void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
6875
{
6976
logCategories &= ~flag;
7077
}
7178

79+
bool BCLog::Logger::DisableCategory(const std::string& str)
80+
{
81+
BCLog::LogFlags flag;
82+
if (!GetLogCategory(flag, str)) return false;
83+
DisableCategory(flag);
84+
return true;
85+
}
86+
7287
bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const
7388
{
7489
return (logCategories.load(std::memory_order_relaxed) & category) != 0;
@@ -81,7 +96,7 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
8196

8297
struct CLogCategoryDesc
8398
{
84-
uint32_t flag;
99+
BCLog::LogFlags flag;
85100
std::string category;
86101
};
87102

@@ -114,19 +129,17 @@ const CLogCategoryDesc LogCategories[] =
114129
{BCLog::ALL, "all"},
115130
};
116131

117-
bool GetLogCategory(uint32_t *f, const std::string *str)
132+
bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
118133
{
119-
if (f && str) {
120-
if (*str == "") {
121-
*f = BCLog::ALL;
134+
if (str == "") {
135+
flag = BCLog::ALL;
136+
return true;
137+
}
138+
for (const CLogCategoryDesc& category_desc : LogCategories) {
139+
if (category_desc.category == str) {
140+
flag = category_desc.flag;
122141
return true;
123142
}
124-
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
125-
if (LogCategories[i].category == *str) {
126-
*f = LogCategories[i].flag;
127-
return true;
128-
}
129-
}
130143
}
131144
return false;
132145
}
@@ -135,11 +148,11 @@ std::string ListLogCategories()
135148
{
136149
std::string ret;
137150
int outcount = 0;
138-
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
151+
for (const CLogCategoryDesc& category_desc : LogCategories) {
139152
// Omit the special cases.
140-
if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) {
153+
if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
141154
if (outcount != 0) ret += ", ";
142-
ret += LogCategories[i].category;
155+
ret += category_desc.category;
143156
outcount++;
144157
}
145158
}
@@ -149,12 +162,12 @@ std::string ListLogCategories()
149162
std::vector<CLogCategoryActive> ListActiveLogCategories()
150163
{
151164
std::vector<CLogCategoryActive> ret;
152-
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
165+
for (const CLogCategoryDesc& category_desc : LogCategories) {
153166
// Omit the special cases.
154-
if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) {
167+
if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
155168
CLogCategoryActive catActive;
156-
catActive.category = LogCategories[i].category;
157-
catActive.active = LogAcceptCategory(LogCategories[i].flag);
169+
catActive.category = category_desc.category;
170+
catActive.active = LogAcceptCategory(category_desc.flag);
158171
ret.push_back(catActive);
159172
}
160173
}

src/logging.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,12 @@ namespace BCLog {
9595
void ShrinkDebugFile();
9696

9797
uint32_t GetCategoryMask() const { return logCategories.load(); }
98+
9899
void EnableCategory(LogFlags flag);
100+
bool EnableCategory(const std::string& str);
99101
void DisableCategory(LogFlags flag);
102+
bool DisableCategory(const std::string& str);
103+
100104
bool WillLogCategory(LogFlags category) const;
101105

102106
bool DefaultShrinkDebugFile() const;
@@ -118,8 +122,8 @@ std::string ListLogCategories();
118122
/** Returns a vector of the active log categories. */
119123
std::vector<CLogCategoryActive> ListActiveLogCategories();
120124

121-
/** Return true if str parses as a log category and set the flags in f */
122-
bool GetLogCategory(uint32_t *f, const std::string *str);
125+
/** Return true if str parses as a log category and set the flag */
126+
bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
123127

124128
/** Get format string from VA_ARGS for error reporting */
125129
template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }

src/rpc/misc.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,18 +349,17 @@ UniValue getmemoryinfo(const JSONRPCRequest& request)
349349
void EnableOrDisableLogCategories(UniValue cats, bool enable) {
350350
cats = cats.get_array();
351351
for (unsigned int i = 0; i < cats.size(); ++i) {
352-
uint32_t flag = 0;
353352
std::string cat = cats[i].get_str();
354-
if (!GetLogCategory(&flag, &cat)) {
355-
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat);
356-
}
357-
if (flag == BCLog::NONE) {
358-
return;
359-
}
353+
354+
bool success;
360355
if (enable) {
361-
g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag));
356+
success = g_logger->EnableCategory(cat);
362357
} else {
363-
g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag));
358+
success = g_logger->DisableCategory(cat);
359+
}
360+
361+
if (!success) {
362+
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat);
364363
}
365364
}
366365
}

0 commit comments

Comments
 (0)