Skip to content

Commit ae32e5c

Browse files
author
MarcoFalke
committed
Merge #18669: log: Use Join() helper when listing log categories
faec063 log: Use Join() helper when listing log categories (MarcoFalke) Pull request description: This removes the global `ListLogCategories` and replaces it with a one-line member function `LogCategoriesString`, which just calls `Join`. Should be a straightforward refactor to get rid of a few LOC. ACKs for top commit: laanwj: ACK faec063 promag: ACK faec063, I also think it's fine as it is (re bitcoin/bitcoin#18669 (comment)). Tree-SHA512: 2f51f9ce1246eda5630015f3a869e36953c7eb34f311baad576b92d7829e4e88051c6189436271cd0a13732a49698506345b446b98fd28e58edfb5b62169f1c9
2 parents 9ddfce6 + faec063 commit ae32e5c

File tree

4 files changed

+17
-31
lines changed

4 files changed

+17
-31
lines changed

src/init.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,8 @@ void SetupServerArgs(NodeContext& node)
520520
gArgs.AddArg("-limitdescendantsize=<n>", strprintf("Do not accept transactions if any ancestor would have more than <n> kilobytes of in-mempool descendants (default: %u).", DEFAULT_DESCENDANT_SIZE_LIMIT), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
521521
gArgs.AddArg("-addrmantest", "Allows to test address relay on localhost", ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::DEBUG_TEST);
522522
gArgs.AddArg("-debug=<category>", "Output debugging information (default: -nodebug, supplying <category> is optional). "
523-
"If <category> is not supplied or if <category> = 1, output all debugging information. <category> can be: " + ListLogCategories() + ".", ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
523+
"If <category> is not supplied or if <category> = 1, output all debugging information. <category> can be: " + LogInstance().LogCategoriesString() + ".",
524+
ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
524525
gArgs.AddArg("-debugexclude=<category>", strprintf("Exclude debugging information for a category. Can be used in conjunction with -debug=1 to output debug logs for all categories except one or more specified categories."), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
525526
gArgs.AddArg("-logips", strprintf("Include IP addresses in debug output (default: %u)", DEFAULT_LOGIPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);
526527
gArgs.AddArg("-logtimestamps", strprintf("Prepend debug output with timestamp (default: %u)", DEFAULT_LOGTIMESTAMPS), ArgsManager::ALLOW_ANY, OptionsCategory::DEBUG_TEST);

src/logging.cpp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -182,30 +182,15 @@ bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
182182
return false;
183183
}
184184

185-
std::string ListLogCategories()
185+
std::vector<LogCategory> BCLog::Logger::LogCategoriesList()
186186
{
187-
std::string ret;
188-
int outcount = 0;
187+
std::vector<LogCategory> ret;
189188
for (const CLogCategoryDesc& category_desc : LogCategories) {
190189
// Omit the special cases.
191190
if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
192-
if (outcount != 0) ret += ", ";
193-
ret += category_desc.category;
194-
outcount++;
195-
}
196-
}
197-
return ret;
198-
}
199-
200-
std::vector<CLogCategoryActive> ListActiveLogCategories()
201-
{
202-
std::vector<CLogCategoryActive> ret;
203-
for (const CLogCategoryDesc& category_desc : LogCategories) {
204-
// Omit the special cases.
205-
if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
206-
CLogCategoryActive catActive;
191+
LogCategory catActive;
207192
catActive.category = category_desc.category;
208-
catActive.active = LogAcceptCategory(category_desc.flag);
193+
catActive.active = WillLogCategory(category_desc.flag);
209194
ret.push_back(catActive);
210195
}
211196
}

src/logging.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <fs.h>
1010
#include <tinyformat.h>
11+
#include <util/string.h>
1112

1213
#include <atomic>
1314
#include <cstdint>
@@ -24,8 +25,7 @@ extern const char * const DEFAULT_DEBUGLOGFILE;
2425

2526
extern bool fLogIPs;
2627

27-
struct CLogCategoryActive
28-
{
28+
struct LogCategory {
2929
std::string category;
3030
bool active;
3131
};
@@ -132,6 +132,13 @@ namespace BCLog {
132132
bool DisableCategory(const std::string& str);
133133

134134
bool WillLogCategory(LogFlags category) const;
135+
/** Returns a vector of the log categories */
136+
std::vector<LogCategory> LogCategoriesList();
137+
/** Returns a string with the log categories */
138+
std::string LogCategoriesString()
139+
{
140+
return Join(LogCategoriesList(), ", ", [&](const LogCategory& i) { return i.category; });
141+
};
135142

136143
bool DefaultShrinkDebugFile() const;
137144
};
@@ -146,12 +153,6 @@ static inline bool LogAcceptCategory(BCLog::LogFlags category)
146153
return LogInstance().WillLogCategory(category);
147154
}
148155

149-
/** Returns a string with the log categories. */
150-
std::string ListLogCategories();
151-
152-
/** Returns a vector of the active log categories. */
153-
std::vector<CLogCategoryActive> ListActiveLogCategories();
154-
155156
/** Return true if str parses as a log category and set the flag */
156157
bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
157158

src/rpc/misc.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ UniValue logging(const JSONRPCRequest& request)
516516
"When called with arguments, adds or removes categories from debug logging and return the lists above.\n"
517517
"The arguments are evaluated in order \"include\", \"exclude\".\n"
518518
"If an item is both included and excluded, it will thus end up being excluded.\n"
519-
"The valid logging categories are: " + ListLogCategories() + "\n"
519+
"The valid logging categories are: " + LogInstance().LogCategoriesString() + "\n"
520520
"In addition, the following are available as category names with special meanings:\n"
521521
" - \"all\", \"1\" : represent all logging categories.\n"
522522
" - \"none\", \"0\" : even if other logging categories are specified, ignore all of them.\n"
@@ -568,8 +568,7 @@ UniValue logging(const JSONRPCRequest& request)
568568
}
569569

570570
UniValue result(UniValue::VOBJ);
571-
std::vector<CLogCategoryActive> vLogCatActive = ListActiveLogCategories();
572-
for (const auto& logCatActive : vLogCatActive) {
571+
for (const auto& logCatActive : LogInstance().LogCategoriesList()) {
573572
result.pushKV(logCatActive.category, logCatActive.active);
574573
}
575574

0 commit comments

Comments
 (0)