Skip to content

Commit 303700e

Browse files
committed
util: Refactor GetLogCategory.
Changing parameter types from pointers to references and uint32_t to BCLog::LogFlags simplifies calling code. backports bitcoin/bitcoin@1eac317
1 parent 0ae18c0 commit 303700e

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

src/init.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -941,23 +941,19 @@ bool AppInit2()
941941
if (!(GetBoolArg("-nodebug", false) ||
942942
find(categories.begin(), categories.end(), std::string("0")) != categories.end())) {
943943
for (const auto& cat : categories) {
944-
uint32_t flag;
945-
if (!GetLogCategory(&flag, &cat)) {
944+
if (!g_logger->EnableCategory(cat)) {
946945
UIWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
947946
}
948-
g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag));
949947
}
950948
}
951949

952950
// Now remove the logging categories which were explicitly excluded
953951
if (mapMultiArgs.count("-debugexclude") > 0) {
954952
const std::vector<std::string>& excludedCategories = mapMultiArgs.at("-debugexclude");
955953
for (const auto& cat : excludedCategories) {
956-
uint32_t flag;
957-
if (!GetLogCategory(&flag, &cat)) {
954+
if (!g_logger->DisableCategory(cat)) {
958955
UIWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
959956
}
960-
g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag));
961957
}
962958
}
963959

src/logging.cpp

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "chainparamsbase.h"
88
#include "logging.h"
99
#include "util.h"
10-
#include "utilstrencodings.h"
1110

1211
#include <boost/filesystem/fstream.hpp>
1312

@@ -72,11 +71,27 @@ void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
7271
logCategories |= flag;
7372
}
7473

74+
bool BCLog::Logger::EnableCategory(const std::string& str)
75+
{
76+
BCLog::LogFlags flag;
77+
if (!GetLogCategory(flag, str)) return false;
78+
EnableCategory(flag);
79+
return true;
80+
}
81+
7582
void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
7683
{
7784
logCategories &= ~flag;
7885
}
7986

87+
bool BCLog::Logger::DisableCategory(const std::string& str)
88+
{
89+
BCLog::LogFlags flag;
90+
if (!GetLogCategory(flag, str)) return false;
91+
DisableCategory(flag);
92+
return true;
93+
}
94+
8095
bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const
8196
{
8297
return (logCategories.load(std::memory_order_relaxed) & category) != 0;
@@ -89,7 +104,7 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
89104

90105
struct CLogCategoryDesc
91106
{
92-
uint32_t flag;
107+
BCLog::LogFlags flag;
93108
std::string category;
94109
};
95110

@@ -124,19 +139,17 @@ const CLogCategoryDesc LogCategories[] = {
124139
{BCLog::ALL, "all"},
125140
};
126141

127-
bool GetLogCategory(uint32_t *f, const std::string *str)
142+
bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
128143
{
129-
if (f && str) {
130-
if (*str == "") {
131-
*f = BCLog::ALL;
144+
if (str == "") {
145+
flag = BCLog::ALL;
146+
return true;
147+
}
148+
for (const CLogCategoryDesc& category_desc : LogCategories) {
149+
if (category_desc.category == str) {
150+
flag = category_desc.flag;
132151
return true;
133152
}
134-
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
135-
if (LogCategories[i].category == *str) {
136-
*f = LogCategories[i].flag;
137-
return true;
138-
}
139-
}
140153
}
141154
return false;
142155
}
@@ -145,11 +158,11 @@ std::string ListLogCategories()
145158
{
146159
std::string ret;
147160
int outcount = 0;
148-
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
161+
for (const CLogCategoryDesc& category_desc : LogCategories) {
149162
// Omit the special cases.
150-
if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) {
163+
if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
151164
if (outcount != 0) ret += ", ";
152-
ret += LogCategories[i].category;
165+
ret += category_desc.category;
153166
outcount++;
154167
}
155168
}
@@ -159,12 +172,12 @@ std::string ListLogCategories()
159172
std::vector<CLogCategoryActive> ListActiveLogCategories()
160173
{
161174
std::vector<CLogCategoryActive> ret;
162-
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
175+
for (const CLogCategoryDesc& category_desc : LogCategories) {
163176
// Omit the special cases.
164-
if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) {
177+
if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
165178
CLogCategoryActive catActive;
166-
catActive.category = LogCategories[i].category;
167-
catActive.active = LogAcceptCategory(static_cast<BCLog::LogFlags>(LogCategories[i].flag));
179+
catActive.category = category_desc.category;
180+
catActive.active = LogAcceptCategory(category_desc.flag);
168181
ret.push_back(catActive);
169182
}
170183
}

src/logging.h

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

106106
uint32_t GetCategoryMask() const { return logCategories.load(); }
107+
107108
void EnableCategory(LogFlags flag);
109+
bool EnableCategory(const std::string& str);
108110
void DisableCategory(LogFlags flag);
111+
bool DisableCategory(const std::string& str);
112+
109113
bool WillLogCategory(LogFlags category) const;
110114

111115
bool DefaultShrinkDebugFile() const;
@@ -127,8 +131,8 @@ std::string ListLogCategories();
127131
/** Returns a vector of the active log categories. */
128132
std::vector<CLogCategoryActive> ListActiveLogCategories();
129133

130-
/** Return true if str parses as a log category and set the flags in f */
131-
bool GetLogCategory(uint32_t *f, const std::string *str);
134+
/** Return true if str parses as a log category and set the flag */
135+
bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
132136

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

src/rpc/misc.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -590,19 +590,17 @@ UniValue setmocktime(const UniValue& params, bool fHelp)
590590
void EnableOrDisableLogCategories(UniValue cats, bool enable) {
591591
cats = cats.get_array();
592592
for (unsigned int i = 0; i < cats.size(); ++i) {
593-
uint32_t flag = 0;
594593
std::string cat = cats[i].get_str();
595-
if (!GetLogCategory(&flag, &cat)) {
596-
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat);
597-
}
598-
if (flag == BCLog::NONE) {
599-
return;
600-
}
594+
595+
bool success;
601596
if (enable) {
602-
g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag));
597+
success = g_logger->EnableCategory(cat);
603598
} else {
604-
g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag));
599+
success = g_logger->DisableCategory(cat);
605600
}
601+
602+
if (!success)
603+
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat);
606604
}
607605
}
608606

0 commit comments

Comments
 (0)