util: Refactor GetLogCategory.

Changing parameter types from pointers to references and uint32_t to
BCLog::LogFlags simplies calling code.
This commit is contained in:
Jim Posen 2018-04-11 14:06:35 -07:00
parent 3316a9ebb6
commit 1eac317f25
4 changed files with 48 additions and 38 deletions

View file

@ -963,24 +963,18 @@ bool AppInitParameterInteraction()
if (std::none_of(categories.begin(), categories.end(), if (std::none_of(categories.begin(), categories.end(),
[](std::string cat){return cat == "0" || cat == "none";})) { [](std::string cat){return cat == "0" || cat == "none";})) {
for (const auto& cat : categories) { for (const auto& cat : categories) {
uint32_t flag = 0; if (!g_logger->EnableCategory(cat)) {
if (!GetLogCategory(&flag, &cat)) {
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat)); InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
continue;
} }
g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag));
} }
} }
} }
// Now remove the logging categories which were explicitly excluded // Now remove the logging categories which were explicitly excluded
for (const std::string& cat : gArgs.GetArgs("-debugexclude")) { for (const std::string& cat : gArgs.GetArgs("-debugexclude")) {
uint32_t flag = 0; if (!g_logger->DisableCategory(cat)) {
if (!GetLogCategory(&flag, &cat)) {
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat)); InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
continue;
} }
g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag));
} }
// Check for -debugnet // Check for -debugnet

View file

@ -5,7 +5,6 @@
#include <logging.h> #include <logging.h>
#include <util.h> #include <util.h>
#include <utilstrencodings.h>
const char * const DEFAULT_DEBUGLOGFILE = "debug.log"; const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
@ -64,11 +63,27 @@ void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
logCategories |= flag; logCategories |= flag;
} }
bool BCLog::Logger::EnableCategory(const std::string& str)
{
BCLog::LogFlags flag;
if (!GetLogCategory(flag, str)) return false;
EnableCategory(flag);
return true;
}
void BCLog::Logger::DisableCategory(BCLog::LogFlags flag) void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
{ {
logCategories &= ~flag; logCategories &= ~flag;
} }
bool BCLog::Logger::DisableCategory(const std::string& str)
{
BCLog::LogFlags flag;
if (!GetLogCategory(flag, str)) return false;
DisableCategory(flag);
return true;
}
bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const
{ {
return (logCategories.load(std::memory_order_relaxed) & category) != 0; return (logCategories.load(std::memory_order_relaxed) & category) != 0;
@ -81,7 +96,7 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
struct CLogCategoryDesc struct CLogCategoryDesc
{ {
uint32_t flag; BCLog::LogFlags flag;
std::string category; std::string category;
}; };
@ -114,19 +129,17 @@ const CLogCategoryDesc LogCategories[] =
{BCLog::ALL, "all"}, {BCLog::ALL, "all"},
}; };
bool GetLogCategory(uint32_t *f, const std::string *str) bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
{ {
if (f && str) { if (str == "") {
if (*str == "") { flag = BCLog::ALL;
*f = BCLog::ALL; return true;
}
for (const CLogCategoryDesc& category_desc : LogCategories) {
if (category_desc.category == str) {
flag = category_desc.flag;
return true; return true;
} }
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
if (LogCategories[i].category == *str) {
*f = LogCategories[i].flag;
return true;
}
}
} }
return false; return false;
} }
@ -135,11 +148,11 @@ std::string ListLogCategories()
{ {
std::string ret; std::string ret;
int outcount = 0; int outcount = 0;
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) { for (const CLogCategoryDesc& category_desc : LogCategories) {
// Omit the special cases. // Omit the special cases.
if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) { if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
if (outcount != 0) ret += ", "; if (outcount != 0) ret += ", ";
ret += LogCategories[i].category; ret += category_desc.category;
outcount++; outcount++;
} }
} }
@ -149,12 +162,12 @@ std::string ListLogCategories()
std::vector<CLogCategoryActive> ListActiveLogCategories() std::vector<CLogCategoryActive> ListActiveLogCategories()
{ {
std::vector<CLogCategoryActive> ret; std::vector<CLogCategoryActive> ret;
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) { for (const CLogCategoryDesc& category_desc : LogCategories) {
// Omit the special cases. // Omit the special cases.
if (LogCategories[i].flag != BCLog::NONE && LogCategories[i].flag != BCLog::ALL) { if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
CLogCategoryActive catActive; CLogCategoryActive catActive;
catActive.category = LogCategories[i].category; catActive.category = category_desc.category;
catActive.active = LogAcceptCategory(LogCategories[i].flag); catActive.active = LogAcceptCategory(category_desc.flag);
ret.push_back(catActive); ret.push_back(catActive);
} }
} }

View file

@ -95,8 +95,12 @@ namespace BCLog {
void ShrinkDebugFile(); void ShrinkDebugFile();
uint32_t GetCategoryMask() const { return logCategories.load(); } uint32_t GetCategoryMask() const { return logCategories.load(); }
void EnableCategory(LogFlags flag); void EnableCategory(LogFlags flag);
bool EnableCategory(const std::string& str);
void DisableCategory(LogFlags flag); void DisableCategory(LogFlags flag);
bool DisableCategory(const std::string& str);
bool WillLogCategory(LogFlags category) const; bool WillLogCategory(LogFlags category) const;
bool DefaultShrinkDebugFile() const; bool DefaultShrinkDebugFile() const;
@ -118,8 +122,8 @@ std::string ListLogCategories();
/** Returns a vector of the active log categories. */ /** Returns a vector of the active log categories. */
std::vector<CLogCategoryActive> ListActiveLogCategories(); std::vector<CLogCategoryActive> ListActiveLogCategories();
/** Return true if str parses as a log category and set the flags in f */ /** Return true if str parses as a log category and set the flag */
bool GetLogCategory(uint32_t *f, const std::string *str); bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
/** Get format string from VA_ARGS for error reporting */ /** Get format string from VA_ARGS for error reporting */
template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; } template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }

View file

@ -349,18 +349,17 @@ UniValue getmemoryinfo(const JSONRPCRequest& request)
void EnableOrDisableLogCategories(UniValue cats, bool enable) { void EnableOrDisableLogCategories(UniValue cats, bool enable) {
cats = cats.get_array(); cats = cats.get_array();
for (unsigned int i = 0; i < cats.size(); ++i) { for (unsigned int i = 0; i < cats.size(); ++i) {
uint32_t flag = 0;
std::string cat = cats[i].get_str(); std::string cat = cats[i].get_str();
if (!GetLogCategory(&flag, &cat)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat); bool success;
}
if (flag == BCLog::NONE) {
return;
}
if (enable) { if (enable) {
g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag)); success = g_logger->EnableCategory(cat);
} else { } else {
g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag)); success = g_logger->DisableCategory(cat);
}
if (!success) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat);
} }
} }
} }