Skip to content

Commit 5255aca

Browse files
committed
[rpc] Add logging RPC
Adds an RPC to get and set currently active logging categories.
1 parent 4d9950d commit 5255aca

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
lines changed

src/rpc/client.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ static const CRPCConvertParam vRPCConvertParams[] =
113113
{ "getmempoolancestors", 1, "verbose" },
114114
{ "getmempooldescendants", 1, "verbose" },
115115
{ "bumpfee", 1, "options" },
116+
{ "logging", 0, "include" },
117+
{ "logging", 1, "exclude" },
116118
// Echo with conversion (For testing only)
117119
{ "echojson", 0, "arg0" },
118120
{ "echojson", 1, "arg1" },

src/rpc/misc.cpp

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,59 @@ UniValue getmemoryinfo(const JSONRPCRequest& request)
555555
}
556556
}
557557

558+
uint32_t getCategoryMask(UniValue cats) {
559+
cats = cats.get_array();
560+
uint32_t mask = 0;
561+
for (unsigned int i = 0; i < cats.size(); ++i) {
562+
uint32_t flag = 0;
563+
std::string cat = cats[i].get_str();
564+
if (!GetLogCategory(&flag, &cat)) {
565+
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat);
566+
}
567+
mask |= flag;
568+
}
569+
return mask;
570+
}
571+
572+
UniValue logging(const JSONRPCRequest& request)
573+
{
574+
if (request.fHelp || request.params.size() > 2) {
575+
throw std::runtime_error(
576+
"logging [include,...] <exclude>\n"
577+
"Gets and sets the logging configuration.\n"
578+
"When called without an argument, returns the list of categories that are currently being debug logged.\n"
579+
"When called with arguments, adds or removes categories from debug logging.\n"
580+
"The valid logging categories are: " + ListLogCategories() + "\n"
581+
"libevent logging is configured on startup and cannot be modified by this RPC during runtime."
582+
"Arguments:\n"
583+
"1. \"include\" (array of strings) add debug logging for these categories.\n"
584+
"2. \"exclude\" (array of strings) remove debug logging for these categories.\n"
585+
"\nResult: <categories> (string): a list of the logging categories that are active.\n"
586+
"\nExamples:\n"
587+
+ HelpExampleCli("logging", "\"[\\\"all\\\"]\" \"[\\\"http\\\"]\"")
588+
+ HelpExampleRpc("logging", "[\"all\"], \"[libevent]\"")
589+
);
590+
}
591+
592+
uint32_t originalLogCategories = logCategories;
593+
if (request.params.size() > 0 && request.params[0].isArray()) {
594+
logCategories |= getCategoryMask(request.params[0]);
595+
}
596+
597+
if (request.params.size() > 1 && request.params[1].isArray()) {
598+
logCategories &= ~getCategoryMask(request.params[1]);
599+
}
600+
601+
602+
UniValue result(UniValue::VOBJ);
603+
std::vector<CLogCategoryActive> vLogCatActive = ListActiveLogCategories();
604+
for (const auto& logCatActive : vLogCatActive) {
605+
result.pushKV(logCatActive.category, logCatActive.active);
606+
}
607+
608+
return result;
609+
}
610+
558611
UniValue echo(const JSONRPCRequest& request)
559612
{
560613
if (request.fHelp)
@@ -581,7 +634,8 @@ static const CRPCCommand commands[] =
581634
/* Not shown in help */
582635
{ "hidden", "setmocktime", &setmocktime, true, {"timestamp"}},
583636
{ "hidden", "echo", &echo, true, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
584-
{ "hidden", "echojson", &echo, true, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
637+
{ "hidden", "echojson", &echo, true, {"arg0","arg1","arg2","arg3","arg4","arg5","arg6","arg7","arg8","arg9"}},
638+
{ "hidden", "logging", &logging, true, {"include", "exclude"}},
585639
};
586640

587641
void RegisterMiscRPCCommands(CRPCTable &t)

src/util.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ bool fLogIPs = DEFAULT_LOGIPS;
118118
std::atomic<bool> fReopenDebugLog(false);
119119
CTranslationInterface translationInterface;
120120

121-
/** Log categories bitfield. Leveldb/libevent need special handling if their flags are changed at runtime. */
121+
/** Log categories bitfield. libevent needs special handling if their flags are changed at runtime. */
122122
std::atomic<uint32_t> logCategories(0);
123123

124124
/** Init OpenSSL library multithreading support */
@@ -295,6 +295,21 @@ std::string ListLogCategories()
295295
return ret;
296296
}
297297

298+
std::vector<CLogCategoryActive> ListActiveLogCategories()
299+
{
300+
std::vector<CLogCategoryActive> ret;
301+
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
302+
// Omit the special cases.
303+
if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) {
304+
CLogCategoryActive catActive;
305+
catActive.category = LogCategories[i].category;
306+
catActive.active = LogAcceptCategory(LogCategories[i].flag);
307+
ret.push_back(catActive);
308+
}
309+
}
310+
return ret;
311+
}
312+
298313
/**
299314
* fStartedNewLine is a state variable held by the calling context that will
300315
* suppress printing of the timestamp when multiple calls are made that don't

src/util.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ inline std::string _(const char* psz)
6969
void SetupEnvironment();
7070
bool SetupNetworking();
7171

72+
struct CLogCategoryActive
73+
{
74+
std::string category;
75+
bool active;
76+
};
77+
7278
namespace BCLog {
7379
enum LogFlags : uint32_t {
7480
NONE = 0,
@@ -102,9 +108,12 @@ static inline bool LogAcceptCategory(uint32_t category)
102108
return (logCategories.load(std::memory_order_relaxed) & category) != 0;
103109
}
104110

105-
/** Returns a string with the supported log categories */
111+
/** Returns a string with the log categories. */
106112
std::string ListLogCategories();
107113

114+
/** Returns a vector of the active log categories. */
115+
std::vector<CLogCategoryActive> ListActiveLogCategories();
116+
108117
/** Return true if str parses as a log category and set the flags in f */
109118
bool GetLogCategory(uint32_t *f, const std::string *str);
110119

0 commit comments

Comments
 (0)