Skip to content

Commit 0b238b2

Browse files
committed
Use thread-local storage for LogPrint(category...)
This prevents crashes at shutdown where a global destructor calls LogPrint(category...) after mapMultiArgs has been deleted.
1 parent 962b1cf commit 0b238b2

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/util.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,17 +242,23 @@ int LogPrint(const char* category, const char* pszFormat, ...)
242242
if (!fDebug)
243243
return 0;
244244

245-
const vector<string>& categories = mapMultiArgs["-debug"];
246-
bool allCategories = count(categories.begin(), categories.end(), string(""));
247-
248-
// Only look for categories, if not -debug/-debug=1 was passed,
249-
// as that implies every category should be logged.
250-
if (!allCategories)
245+
// Give each thread quick access to -debug settings.
246+
// This helps prevent issues debugging global destructors,
247+
// where mapMultiArgs might be deleted before another
248+
// global destructor calls LogPrint()
249+
static boost::thread_specific_ptr<set<string> > ptrCategory;
250+
if (ptrCategory.get() == NULL)
251251
{
252-
// Category was not found (not supplied via -debug=<category>)
253-
if (find(categories.begin(), categories.end(), string(category)) == categories.end())
254-
return 0;
252+
const vector<string>& categories = mapMultiArgs["-debug"];
253+
ptrCategory.reset(new set<string>(categories.begin(), categories.end()));
254+
// thread_specific_ptr automatically deletes the set when the thread ends.
255255
}
256+
const set<string>& setCategories = *ptrCategory.get();
257+
258+
// if not debugging everything and not debugging specific category, LogPrint does nothing.
259+
if (setCategories.count(string("")) == 0 &&
260+
setCategories.count(string(category)) == 0)
261+
return 0;
256262
}
257263

258264
int ret = 0; // Returns total number of characters written

0 commit comments

Comments
 (0)