Add "-logthreadnames" cmd-line option to add thread names to debug messages

This commit is contained in:
UdjinM6 2016-03-15 00:04:48 +03:00 committed by Holger Schinzel
parent e0c4dd0129
commit a7fd7821e9
3 changed files with 46 additions and 2 deletions

View File

@ -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=<n>", "Replace actual time with <n> seconds since epoch (default: 0)");
strUsage += HelpMessageOpt("-limitfreerelay=<n>", strprintf("Continuously rate-limit free transactions to <n>*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);

View File

@ -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

View File

@ -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