util: Encapsulate logCategories within BCLog::Logger.

This commit is contained in:
Jim Posen 2018-04-11 13:02:01 -07:00
parent 6a6d764ca5
commit 3316a9ebb6
7 changed files with 55 additions and 28 deletions

View file

@ -364,8 +364,8 @@ bool InitHTTPServer()
// Update libevent's log handling. Returns false if our version of
// libevent doesn't support debug logging, in which case we should
// clear the BCLog::LIBEVENT flag.
if (!UpdateHTTPServerLogging(logCategories & BCLog::LIBEVENT)) {
logCategories &= ~BCLog::LIBEVENT;
if (!UpdateHTTPServerLogging(g_logger->WillLogCategory(BCLog::LIBEVENT))) {
g_logger->DisableCategory(BCLog::LIBEVENT);
}
#ifdef WIN32

View file

@ -32,7 +32,7 @@ void InterruptHTTPServer();
/** Stop HTTP server */
void StopHTTPServer();
/** Change logging level for libevent. Removes BCLog::LIBEVENT from logCategories if
/** Change logging level for libevent. Removes BCLog::LIBEVENT from log categories if
* libevent doesn't support debug logging.*/
bool UpdateHTTPServerLogging(bool enable);

View file

@ -968,7 +968,7 @@ bool AppInitParameterInteraction()
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debug", cat));
continue;
}
logCategories |= flag;
g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag));
}
}
}
@ -980,7 +980,7 @@ bool AppInitParameterInteraction()
InitWarning(strprintf(_("Unsupported logging category %s=%s."), "-debugexclude", cat));
continue;
}
logCategories &= ~flag;
g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag));
}
// Check for -debugnet
@ -1232,7 +1232,7 @@ bool AppInitMain()
CreatePidFile(GetPidFile(), getpid());
#endif
if (g_logger->fPrintToDebugLog) {
if (gArgs.GetBoolArg("-shrinkdebugfile", logCategories == BCLog::NONE)) {
if (gArgs.GetBoolArg("-shrinkdebugfile", g_logger->DefaultShrinkDebugFile())) {
// Do this first since it both loads a bunch of debug.log into memory,
// and because this needs to happen before any other debug.log printing
g_logger->ShrinkDebugFile();

View file

@ -60,7 +60,7 @@ class NodeImpl : public Node
void initLogging() override { InitLogging(); }
void initParameterInteraction() override { InitParameterInteraction(); }
std::string getWarnings(const std::string& type) override { return GetWarnings(type); }
uint32_t getLogCategories() override { return ::logCategories; }
uint32_t getLogCategories() override { return g_logger->GetCategoryMask(); }
bool baseInitialize() override
{
return AppInitBasicSetup() && AppInitParameterInteraction() && AppInitSanityChecks() &&

View file

@ -26,9 +26,6 @@ BCLog::Logger* const g_logger = new BCLog::Logger();
bool fLogIPs = DEFAULT_LOGIPS;
/** Log categories bitfield. */
std::atomic<uint32_t> logCategories(0);
static int FileWriteStr(const std::string &str, FILE *fp)
{
return fwrite(str.data(), 1, str.size(), fp);
@ -62,6 +59,26 @@ bool BCLog::Logger::OpenDebugLog()
return true;
}
void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
{
logCategories |= flag;
}
void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
{
logCategories &= ~flag;
}
bool BCLog::Logger::WillLogCategory(BCLog::LogFlags category) const
{
return (logCategories.load(std::memory_order_relaxed) & category) != 0;
}
bool BCLog::Logger::DefaultShrinkDebugFile() const
{
return logCategories == BCLog::NONE;
}
struct CLogCategoryDesc
{
uint32_t flag;

View file

@ -23,8 +23,6 @@ extern const char * const DEFAULT_DEBUGLOGFILE;
extern bool fLogIPs;
extern std::atomic<uint32_t> logCategories;
struct CLogCategoryActive
{
std::string category;
@ -72,6 +70,9 @@ namespace BCLog {
*/
std::atomic_bool fStartedNewLine{true};
/** Log categories bitfield. */
std::atomic<uint32_t> logCategories{0};
std::string LogTimestampStr(const std::string& str);
public:
@ -92,6 +93,13 @@ namespace BCLog {
fs::path GetDebugLogPath() const;
bool OpenDebugLog();
void ShrinkDebugFile();
uint32_t GetCategoryMask() const { return logCategories.load(); }
void EnableCategory(LogFlags flag);
void DisableCategory(LogFlags flag);
bool WillLogCategory(LogFlags category) const;
bool DefaultShrinkDebugFile() const;
};
} // namespace BCLog
@ -99,9 +107,9 @@ namespace BCLog {
extern BCLog::Logger* const g_logger;
/** Return true if log accepts specified category */
static inline bool LogAcceptCategory(uint32_t category)
static inline bool LogAcceptCategory(BCLog::LogFlags category)
{
return (logCategories.load(std::memory_order_relaxed) & category) != 0;
return g_logger->WillLogCategory(category);
}
/** Returns a string with the log categories. */

View file

@ -346,9 +346,8 @@ UniValue getmemoryinfo(const JSONRPCRequest& request)
}
}
uint32_t getCategoryMask(UniValue cats) {
void EnableOrDisableLogCategories(UniValue cats, bool enable) {
cats = cats.get_array();
uint32_t mask = 0;
for (unsigned int i = 0; i < cats.size(); ++i) {
uint32_t flag = 0;
std::string cat = cats[i].get_str();
@ -356,11 +355,14 @@ uint32_t getCategoryMask(UniValue cats) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "unknown logging category " + cat);
}
if (flag == BCLog::NONE) {
return 0;
return;
}
if (enable) {
g_logger->EnableCategory(static_cast<BCLog::LogFlags>(flag));
} else {
g_logger->DisableCategory(static_cast<BCLog::LogFlags>(flag));
}
mask |= flag;
}
return mask;
}
UniValue logging(const JSONRPCRequest& request)
@ -399,25 +401,25 @@ UniValue logging(const JSONRPCRequest& request)
);
}
uint32_t originalLogCategories = logCategories;
uint32_t original_log_categories = g_logger->GetCategoryMask();
if (request.params[0].isArray()) {
logCategories |= getCategoryMask(request.params[0]);
EnableOrDisableLogCategories(request.params[0], true);
}
if (request.params[1].isArray()) {
logCategories &= ~getCategoryMask(request.params[1]);
EnableOrDisableLogCategories(request.params[1], false);
}
uint32_t updated_log_categories = g_logger->GetCategoryMask();
uint32_t changed_log_categories = original_log_categories ^ updated_log_categories;
// Update libevent logging if BCLog::LIBEVENT has changed.
// If the library version doesn't allow it, UpdateHTTPServerLogging() returns false,
// in which case we should clear the BCLog::LIBEVENT flag.
// Throw an error if the user has explicitly asked to change only the libevent
// flag and it failed.
uint32_t changedLogCategories = originalLogCategories ^ logCategories;
if (changedLogCategories & BCLog::LIBEVENT) {
if (!UpdateHTTPServerLogging(logCategories & BCLog::LIBEVENT)) {
logCategories &= ~BCLog::LIBEVENT;
if (changedLogCategories == BCLog::LIBEVENT) {
if (changed_log_categories & BCLog::LIBEVENT) {
if (!UpdateHTTPServerLogging(g_logger->WillLogCategory(BCLog::LIBEVENT))) {
g_logger->DisableCategory(BCLog::LIBEVENT);
if (changed_log_categories == BCLog::LIBEVENT) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "libevent logging cannot be updated when using libevent before v2.1.1.");
}
}