Skip to content

Commit 467177f

Browse files
committed
Add rpc commands to control logging categories and initialization of categories in init
1 parent bbe4a76 commit 467177f

File tree

10 files changed

+162
-4
lines changed

10 files changed

+162
-4
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ GRIDCOIN_CORE_CPP = addrdb.cpp \
181181
rpcblockchain.cpp \
182182
rpcclient.cpp \
183183
rpcdump.cpp \
184+
rpcmisc.cpp \
184185
rpcmining.cpp \
185186
rpcnet.cpp \
186187
rpcprotocol.cpp \

src/init.cpp

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,9 @@ void InitLogging()
339339
LogInstance().m_log_time_micros = GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS);
340340
LogInstance().m_log_threadnames = GetBoolArg("-logthreadnames", DEFAULT_LOGTHREADNAMES);
341341

342-
BCLog::LogFlags flags = BCLog::LogFlags::ALL;
342+
//BCLog::LogFlags flags = BCLog::LogFlags::ALL;
343343

344-
LogInstance().EnableCategory(flags);
344+
//LogInstance().EnableCategory(flags);
345345

346346
//printf("LogInstance().GetCategoryMask() = %x\n", LogInstance().GetCategoryMask());
347347

@@ -357,6 +357,52 @@ void InitLogging()
357357
LogInstance().ShrinkDebugFile();
358358
}
359359
}
360+
361+
if (IsArgSet("-debug"))
362+
{
363+
// Special-case: if -debug=0/-nodebug is set, turn off debugging messages
364+
std::vector<std::string> categories;
365+
366+
if (mapArgs.count("-debug") && mapMultiArgs["-debug"].size() > 0)
367+
{
368+
for (auto const& sSubParam : mapMultiArgs["-debug"])
369+
{
370+
categories.push_back(sSubParam);
371+
}
372+
}
373+
374+
if (std::none_of(categories.begin(), categories.end(),
375+
[](std::string cat){return cat == "0" || cat == "none";}))
376+
{
377+
for (const auto& cat : categories)
378+
{
379+
if (!LogInstance().EnableCategory(cat))
380+
{
381+
InitWarning(strprintf("Unsupported logging category %s=%s.", "-debug", cat));
382+
}
383+
}
384+
}
385+
}
386+
387+
std::vector<std::string> excluded_categories;
388+
389+
if (mapArgs.count("-debugexclude") && mapMultiArgs["-debugexclude"].size() > 0)
390+
{
391+
for (auto const& sSubParam : mapMultiArgs["-debugexclude"])
392+
{
393+
excluded_categories.push_back(sSubParam);
394+
}
395+
}
396+
397+
// Now remove the logging categories which were explicitly excluded
398+
for (const std::string& cat : excluded_categories)
399+
{
400+
if (!LogInstance().DisableCategory(cat))
401+
{
402+
InitWarning(strprintf("Unsupported logging category %s=%s.", "-debugexclude", cat));
403+
}
404+
}
405+
360406
if (!LogInstance().StartLogging())
361407
{
362408
strprintf("Could not open debug log file %s", LogInstance().m_file_path.string());

src/logging.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ const CLogCategoryDesc LogCategories[] =
175175
{BCLog::COINDB, "coindb"},
176176
{BCLog::QT, "qt"},
177177
{BCLog::LEVELDB, "leveldb"},
178+
{BCLog::SCRAPER, "scraper"},
179+
{BCLog::MANIFEST, "manifest"},
180+
{BCLog::SB, "superblock"},
178181
{BCLog::ALL, "1"},
179182
{BCLog::ALL, "all"},
180183
};
@@ -456,7 +459,7 @@ bool BCLog::Logger::archive(bool fImmediate, fs::path pfile_out)
456459

457460
if (fDeleteOldLogArchives)
458461
{
459-
unsigned int nRetention = (unsigned int)GetArg("-logarchiveretainnumfiles", 14);
462+
unsigned int nRetention = (unsigned int)GetArg("-logarchiveretainnumfiles", 30);
460463
LogPrintf ("INFO: Logger: nRetention %i.", nRetention);
461464

462465
std::set<fs::directory_entry, std::greater <fs::directory_entry>> SortedDirEntries;

src/logging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ namespace BCLog {
7676
COINDB = (1 << 18),
7777
QT = (1 << 19),
7878
LEVELDB = (1 << 20),
79+
SCRAPER = (1 << 21),
80+
MANIFEST = (1 << 22),
81+
SB = (1 << 23),
7982
ALL = ~(uint32_t)0,
8083
};
8184

src/rpcmisc.cpp

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) 2012-2019 The Bitcoin developers
2+
// Copyright (c) 2019 The Gridcoin developers.
3+
// Distributed under the MIT/X11 software license, see the accompanying
4+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
#include "rpcprotocol.h"
7+
#include "util.h"
8+
9+
#include <univalue.h>
10+
11+
using namespace std;
12+
13+
static void EnableOrDisableLogCategories(UniValue cats, bool enable) {
14+
15+
std::vector<std::string> vcats;
16+
17+
// The below is to allow non-array parameters. The current front-end for Gridcoin does not
18+
// understand JSON parameters. This will handle that when it does, and deal with single
19+
// parameters too.
20+
if (cats.isArray())
21+
{
22+
for (unsigned int i = 0; i < cats.size(); ++i)
23+
{
24+
vcats.push_back(cats[i].get_str());
25+
}
26+
}
27+
else
28+
{
29+
vcats.push_back(cats.get_str());
30+
}
31+
32+
for (const auto& cat : vcats)
33+
{
34+
bool success;
35+
if (enable) {
36+
LogPrintf("INFO: EnableOrDisableLogCategories: enabling category %s", cat);
37+
success = LogInstance().EnableCategory(cat);
38+
} else {
39+
LogPrintf("INFO: EnableOrDisableLogCategories: disabling category %s", cat);
40+
success = LogInstance().DisableCategory(cat);
41+
}
42+
43+
if (!success) {
44+
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat);
45+
}
46+
}
47+
}
48+
49+
UniValue logging(const UniValue& params, bool fHelp)
50+
{
51+
if (fHelp || params.size() > 2)
52+
{
53+
throw runtime_error(
54+
"logging [json array category adds] [json array category removes]\n"
55+
"Gets and sets the logging configuration.\n"
56+
"When called without an argument, returns the list of categories with status that are currently being debug logged or not.\n"
57+
"When called with arguments, adds or removes categories from debug logging and return the lists above.\n"
58+
"The arguments are evaluated in order \"include\", \"exclude\".\n"
59+
"If an item is both included and excluded, it will thus end up being excluded.\n"
60+
"The valid logging categories are: " + ListLogCategories() + "\n"
61+
"In addition, the following are available as category names with special meanings:\n"
62+
"all or 1: represent all logging categories.\n"
63+
"none or 0: even if other logging categories are specified, ignore all of them.\n\n"
64+
"Examples:\n"
65+
"logging all net: enables all and disables net.\n"
66+
"logging none: disables all.\n\n"
67+
"Note that unlike Bitcoin, we don't process JSON arrays correctly as arguments yet for the command line,\n"
68+
"so, for the rpc cli, it is limited to one enable and/or one disable category.\n"
69+
);
70+
}
71+
72+
if (params.size() >= 1) EnableOrDisableLogCategories(params[0], true);
73+
74+
if (params.size() == 2) EnableOrDisableLogCategories(params[1], false);
75+
76+
UniValue result(UniValue::VOBJ);
77+
std::vector<CLogCategoryActive> vLogCatActive = ListActiveLogCategories();
78+
for (const auto& logCatActive : vLogCatActive) {
79+
result.pushKV(logCatActive.category, logCatActive.active);
80+
}
81+
82+
return result;
83+
}
84+

src/rpcserver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ static const CRPCCommand vRPCCommands[] =
371371
{ "getsupervotes", &rpc_getsupervotes, cat_developer },
372372
{ "listdata", &listdata, cat_developer },
373373
{ "listprojects", &listprojects, cat_developer },
374+
{ "logging", &logging, cat_developer },
374375
{ "memorizekeys", &memorizekeys, cat_developer },
375376
{ "network", &network, cat_developer },
376377
{ "projects", &projects, cat_developer },

src/rpcserver.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ extern UniValue rpc_getblockstats(const UniValue& params, bool fHelp);
189189
extern UniValue getlistof(const UniValue& params, bool fHelp);
190190
extern UniValue listdata(const UniValue& params, bool fHelp);
191191
extern UniValue listprojects(const UniValue& params, bool fHelp);
192+
extern UniValue logging(const UniValue& params, bool fHelp);
192193
extern UniValue memorizekeys(const UniValue& params, bool fHelp);
193194
extern UniValue network(const UniValue& params, bool fHelp);
194195
extern UniValue projects(const UniValue& params, bool fHelp);

src/scraper/scraper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ class ScraperLogger
310310

311311
if (fDeleteOldLogArchives)
312312
{
313-
unsigned int nRetention = (unsigned int)GetArg("-logarchiveretainnumfiles", 14);
313+
unsigned int nRetention = (unsigned int)GetArg("-logarchiveretainnumfiles", 30);
314314
LogPrintf ("INFO: ScraperLogger: nRetention %i.", nRetention);
315315

316316
std::set<fs::directory_entry, std::greater <fs::directory_entry>> SortedDirEntries;

src/util.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,11 @@ bool GetBoolArg(const std::string& strArg, bool fDefault)
435435
return fDefault;
436436
}
437437

438+
bool IsArgSet(const std::string& strArg)
439+
{
440+
return !(GetArg(strArg, "never_used_as_argument") == "never_used_as_argument");
441+
}
442+
438443
bool IsArgNegated(const std::string& strArg)
439444
{
440445
return GetArg(strArg, "true") == "false";

src/util.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,20 @@ int64_t GetArg(const std::string& strArg, int64_t nDefault);
327327
*/
328328
bool GetBoolArg(const std::string& strArg, bool fDefault=false);
329329

330+
/**
331+
* Return true if argument is set
332+
*
333+
* @param strArg Argument to get (e.g. "-foo")
334+
* @return boolean
335+
*/
336+
bool IsArgSet(const std::string& strArg);
337+
338+
/**
339+
* Return true if argument is negated
340+
*
341+
* @param strArg Argument to get (e.g. "-foo")
342+
* @return boolean
343+
*/
330344
bool IsArgNegated(const std::string& strArg);
331345

332346
/**

0 commit comments

Comments
 (0)