From a7fd7821e96d499caa871a157d0b89023f9c9b0d Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Tue, 15 Mar 2016 00:04:48 +0300 Subject: [PATCH] Add "-logthreadnames" cmd-line option to add thread names to debug messages --- src/init.cpp | 4 +++- src/util.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- src/util.h | 3 +++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 10a632915b..c3838a2f41 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -519,6 +519,7 @@ std::string HelpMessage(HelpMessageMode mode) if (showDebug) { strUsage += HelpMessageOpt("-logtimemicros", strprintf("Add microsecond precision to debug timestamps (default: %u)", DEFAULT_LOGTIMEMICROS)); + strUsage += HelpMessageOpt("-logthreadnames", strprintf("Add thread names to debug messages (default: %u)", DEFAULT_LOGTHREADNAMES)); strUsage += HelpMessageOpt("-mocktime=", "Replace actual time with seconds since epoch (default: 0)"); strUsage += HelpMessageOpt("-limitfreerelay=", strprintf("Continuously rate-limit free transactions to *1000 bytes per minute (default: %u)", DEFAULT_LIMITFREERELAY)); strUsage += HelpMessageOpt("-relaypriority", strprintf("Require high priority for relaying free or low-fee transactions (default: %u)", DEFAULT_RELAYPRIORITY)); @@ -870,6 +871,7 @@ void InitLogging() fPrintToDebugLog = GetBoolArg("-printtodebuglog", true) && !fPrintToConsole; fLogTimestamps = GetBoolArg("-logtimestamps", DEFAULT_LOGTIMESTAMPS); fLogTimeMicros = GetBoolArg("-logtimemicros", DEFAULT_LOGTIMEMICROS); + fLogThreadNames = GetBoolArg("-logthreadnames", DEFAULT_LOGTHREADNAMES); fLogIPs = GetBoolArg("-logips", DEFAULT_LOGIPS); LogPrintf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); @@ -1700,7 +1702,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) pwalletMain->SetBestChain(chainActive.GetLocator()); } - LogPrintf("%s", strErrors.str()); + if(!strErrors.str().empty()) LogPrintf("%s", strErrors.str()); LogPrintf(" wallet %15dms\n", GetTimeMillis() - nStart); RegisterValidationInterface(pwalletMain); diff --git a/src/util.cpp b/src/util.cpp index 477463d81f..bcc9dbff79 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -134,6 +134,7 @@ bool fServer = false; string strMiscWarning; bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS; bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS; +bool fLogThreadNames = DEFAULT_LOGTHREADNAMES; bool fLogIPs = DEFAULT_LOGIPS; volatile bool fReopenDebugLog = false; CTranslationInterface translationInterface; @@ -315,12 +316,35 @@ static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine return strStamped; } +/** + * fStartedNewLine is a state variable held by the calling context that will + * suppress printing of the thread name when multiple calls are made that don't + * end in a newline. Initialize it to true, and hold it, in the calling context. + */ +static std::string LogThreadNameStr(const std::string &str, bool *fStartedNewLine) +{ + string strThreadLogged; + + if (!fLogThreadNames) + return str; + + std::string strThreadName = GetThreadName(); + + if (*fStartedNewLine) + strThreadLogged = strprintf("%16s | %s", strThreadName.c_str(), str.c_str()); + else + strThreadLogged = str; + + return strThreadLogged; +} + int LogPrintStr(const std::string &str) { int ret = 0; // Returns total number of characters written static bool fStartedNewLine = true; - string strTimestamped = LogTimestampStr(str, &fStartedNewLine); + std::string strThreadLogged = LogThreadNameStr(str, &fStartedNewLine); + std::string strTimestamped = LogTimestampStr(strThreadLogged, &fStartedNewLine); if (fPrintToConsole) { @@ -826,6 +850,21 @@ void RenameThread(const char* name) #endif } +std::string GetThreadName() +{ + char name[16]; +#if defined(PR_GET_NAME) + // Only the first 15 characters are used (16 - NUL terminator) + ::prctl(PR_GET_NAME, name, 0, 0, 0); +#elif defined(MAC_OSX) + pthread_getname_np(pthread_self(), name, 16); +// #elif (defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)) +// #else + // no get_name here +#endif + return std::string(name); +} + void SetupEnvironment() { // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale diff --git a/src/util.h b/src/util.h index e682a087a0..163d66eac3 100644 --- a/src/util.h +++ b/src/util.h @@ -51,6 +51,7 @@ extern std::string strBudgetMode; static const bool DEFAULT_LOGTIMEMICROS = false; static const bool DEFAULT_LOGIPS = false; static const bool DEFAULT_LOGTIMESTAMPS = true; +static const bool DEFAULT_LOGTHREADNAMES = false; /** Signals for translation. */ class CTranslationInterface @@ -69,6 +70,7 @@ extern bool fServer; extern std::string strMiscWarning; extern bool fLogTimestamps; extern bool fLogTimeMicros; +extern bool fLogThreadNames; extern bool fLogIPs; extern volatile bool fReopenDebugLog; extern CTranslationInterface translationInterface; @@ -239,6 +241,7 @@ int GetNumCores(); void SetThreadPriority(int nPriority); void RenameThread(const char* name); +std::string GetThreadName(); /** * .. and a wrapper that just calls func once