diff options
author | Gavin Andresen <gavinandresen@gmail.com> | 2013-12-10 13:19:18 +1000 |
---|---|---|
committer | Gavin Andresen <gavinandresen@gmail.com> | 2013-12-10 13:19:18 +1000 |
commit | 0b238b278686f8e0c6609b7c9360d36c3198105c (patch) | |
tree | f7e4e2b07029d7c87c28d4e050454dc6b848fca2 /src/util.cpp | |
parent | 962b1cf441ceb1de7791d89951a79a54a2fefcd4 (diff) |
Use thread-local storage for LogPrint(category...)
This prevents crashes at shutdown where a global destructor
calls LogPrint(category...) after mapMultiArgs has been
deleted.
Diffstat (limited to 'src/util.cpp')
-rw-r--r-- | src/util.cpp | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/src/util.cpp b/src/util.cpp index d975cc9bfb..bedf59767b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -242,17 +242,23 @@ int LogPrint(const char* category, const char* pszFormat, ...) if (!fDebug) return 0; - const vector<string>& categories = mapMultiArgs["-debug"]; - bool allCategories = count(categories.begin(), categories.end(), string("")); - - // Only look for categories, if not -debug/-debug=1 was passed, - // as that implies every category should be logged. - if (!allCategories) + // Give each thread quick access to -debug settings. + // This helps prevent issues debugging global destructors, + // where mapMultiArgs might be deleted before another + // global destructor calls LogPrint() + static boost::thread_specific_ptr<set<string> > ptrCategory; + if (ptrCategory.get() == NULL) { - // Category was not found (not supplied via -debug=<category>) - if (find(categories.begin(), categories.end(), string(category)) == categories.end()) - return 0; + const vector<string>& categories = mapMultiArgs["-debug"]; + ptrCategory.reset(new set<string>(categories.begin(), categories.end())); + // thread_specific_ptr automatically deletes the set when the thread ends. } + const set<string>& setCategories = *ptrCategory.get(); + + // if not debugging everything and not debugging specific category, LogPrint does nothing. + if (setCategories.count(string("")) == 0 && + setCategories.count(string(category)) == 0) + return 0; } int ret = 0; // Returns total number of characters written |