Skip to content

Commit

Permalink
Reset local/static cache in LogAcceptCategory when categories change (#…
Browse files Browse the repository at this point in the history
…2804)

* Reset local/static cache in LogAcceptCategory when categories change

Without this, it is always required to first set debug to 0, wait a few
seconds (until LogAcceptCategory is called by all affected threads) and
then call "debug somecategory". Otherwise "ptrCategory" never gets updated.

This PR also stores a cache counter locally and globally and updates
"ptrCategory" when the counters do not match.

* Lock cs_args in LogAcceptCategory
  • Loading branch information
codablock authored and UdjinM6 committed Mar 25, 2019
1 parent 4a79f7a commit 4d8ef35
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ UniValue debug(const JSONRPCRequest& request)

fDebug = GetArg("-debug", "") != "0";

ResetLogAcceptCategoryCache();

return "Debug mode: " + (fDebug ? strMode : "off");
}

Expand Down
12 changes: 11 additions & 1 deletion src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ static boost::once_flag debugPrintInitFlag = BOOST_ONCE_INIT;
static FILE* fileout = NULL;
static boost::mutex* mutexDebugLog = NULL;
static std::list<std::string>* vMsgsBeforeOpenLog;
static std::atomic<int> logAcceptCategoryCacheCounter(0);

static int FileWriteStr(const std::string &str, FILE *fp)
{
Expand Down Expand Up @@ -260,6 +261,7 @@ bool LogAcceptCategory(const char* category)
// where mapMultiArgs might be deleted before another
// global destructor calls LogPrint()
static boost::thread_specific_ptr<std::set<std::string> > ptrCategory;
static boost::thread_specific_ptr<int> cacheCounter;

if (!fDebug) {
if (ptrCategory.get() != NULL) {
Expand All @@ -269,8 +271,11 @@ bool LogAcceptCategory(const char* category)
return false;
}

if (ptrCategory.get() == NULL)
if (ptrCategory.get() == NULL || *cacheCounter != logAcceptCategoryCacheCounter.load())
{
cacheCounter.reset(new int(logAcceptCategoryCacheCounter.load()));

LOCK(cs_args);
if (mapMultiArgs.count("-debug")) {
std::string strThreadName = GetThreadName();
LogPrintf("debug turned on:\n");
Expand Down Expand Up @@ -309,6 +314,11 @@ bool LogAcceptCategory(const char* category)
return true;
}

void ResetLogAcceptCategoryCache()
{
logAcceptCategoryCacheCounter++;
}

/**
* fStartedNewLine is a state variable held by the calling context that will
* suppress printing of the timestamp when multiple calls are made that don't
Expand Down
2 changes: 2 additions & 0 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ bool SetupNetworking();

/** Return true if log accepts specified category */
bool LogAcceptCategory(const char* category);
/** Reset internal log category caching (call this when debug categories have changed) */
void ResetLogAcceptCategoryCache();
/** Send a string to the log output */
int LogPrintStr(const std::string &str);

Expand Down

0 comments on commit 4d8ef35

Please sign in to comment.