Skip to content

Commit

Permalink
util: Refactor GetLogCategory.
Browse files Browse the repository at this point in the history
Changing parameter types from pointers to references and uint32_t to
BCLog::LogFlags simplifies calling code.

backports bitcoin/bitcoin@1eac317
  • Loading branch information
random-zebra committed Apr 11, 2020
1 parent 0ae18c0 commit 303700e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 36 deletions.
8 changes: 2 additions & 6 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,23 +941,19 @@ bool AppInit2()
if (!(GetBoolArg("-nodebug", false) ||
find(categories.begin(), categories.end(), std::string("0")) != categories.end())) {
for (const auto& cat : categories) {
uint32_t flag;
if (!GetLogCategory(&flag, &cat)) {
if (!g_logger->EnableCategory(cat)) {
UIWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
}
g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag));
}
}

// Now remove the logging categories which were explicitly excluded
if (mapMultiArgs.count("-debugexclude") > 0) {
const std::vector<std::string>& excludedCategories = mapMultiArgs.at("-debugexclude");
for (const auto& cat : excludedCategories) {
uint32_t flag;
if (!GetLogCategory(&flag, &cat)) {
if (!g_logger->DisableCategory(cat)) {
UIWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
}
g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag));
}
}

Expand Down
51 changes: 32 additions & 19 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "chainparamsbase.h"
#include "logging.h"
#include "util.h"
#include "utilstrencodings.h"

#include <boost/filesystem/fstream.hpp>

Expand Down Expand Up @@ -72,11 +71,27 @@ void BCLog::Logger::EnableCategory(BCLog::LogFlags 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)
{
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
{
return (logCategories.load(std::memory_order_relaxed) & category) != 0;
Expand All @@ -89,7 +104,7 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const

struct CLogCategoryDesc
{
uint32_t flag;
BCLog::LogFlags flag;
std::string category;
};

Expand Down Expand Up @@ -124,19 +139,17 @@ const CLogCategoryDesc LogCategories[] = {
{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 == "") {
*f = BCLog::ALL;
if (str == "") {
flag = BCLog::ALL;
return true;
}
for (const CLogCategoryDesc& category_desc : LogCategories) {
if (category_desc.category == str) {
flag = category_desc.flag;
return true;
}
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
if (LogCategories[i].category == *str) {
*f = LogCategories[i].flag;
return true;
}
}
}
return false;
}
Expand All @@ -145,11 +158,11 @@ std::string ListLogCategories()
{
std::string ret;
int outcount = 0;
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
for (const CLogCategoryDesc& category_desc : LogCategories) {
// 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 += ", ";
ret += LogCategories[i].category;
ret += category_desc.category;
outcount++;
}
}
Expand All @@ -159,12 +172,12 @@ std::string ListLogCategories()
std::vector<CLogCategoryActive> ListActiveLogCategories()
{
std::vector<CLogCategoryActive> ret;
for (unsigned int i = 0; i < ARRAYLEN(LogCategories); i++) {
for (const CLogCategoryDesc& category_desc : LogCategories) {
// 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;
catActive.category = LogCategories[i].category;
catActive.active = LogAcceptCategory(static_cast<BCLog::LogFlags>(LogCategories[i].flag));
catActive.category = category_desc.category;
catActive.active = LogAcceptCategory(category_desc.flag);
ret.push_back(catActive);
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,12 @@ namespace BCLog {
void ShrinkDebugFile();

uint32_t GetCategoryMask() const { return logCategories.load(); }

void EnableCategory(LogFlags flag);
bool EnableCategory(const std::string& str);
void DisableCategory(LogFlags flag);
bool DisableCategory(const std::string& str);

bool WillLogCategory(LogFlags category) const;

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

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

/** Get format string from VA_ARGS for error reporting */
template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }
Expand Down
16 changes: 7 additions & 9 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,19 +590,17 @@ UniValue setmocktime(const UniValue& params, bool fHelp)
void EnableOrDisableLogCategories(UniValue cats, bool enable) {
cats = cats.get_array();
for (unsigned int i = 0; i < cats.size(); ++i) {
uint32_t flag = 0;
std::string cat = cats[i].get_str();
if (!GetLogCategory(&flag, &cat)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat);
}
if (flag == BCLog::NONE) {
return;
}

bool success;
if (enable) {
g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag));
success = g_logger->EnableCategory(cat);
} 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);
}
}

Expand Down

0 comments on commit 303700e

Please sign in to comment.