1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-05-13 22:06:58 +02:00

Show human-readable thread name in log

This commit is contained in:
Ivan Savenko 2024-02-11 16:11:21 +02:00
parent 0fc0ad238b
commit cded8b1999
6 changed files with 44 additions and 13 deletions

View File

@ -189,6 +189,7 @@ int main(int argc, char * argv[])
console->start(); console->start();
#endif #endif
setThreadNameLoggingOnly("MainGUI");
const boost::filesystem::path logPath = VCMIDirs::get().userLogsPath() / "VCMI_Client_log.txt"; const boost::filesystem::path logPath = VCMIDirs::get().userLogsPath() / "VCMI_Client_log.txt";
logConfig = new CBasicLogConfigurator(logPath, console); logConfig = new CBasicLogConfigurator(logPath, console);
logConfig->configureDefault(); logConfig->configureDefault();
@ -397,7 +398,10 @@ void playIntro()
static void mainLoop() static void mainLoop()
{ {
#ifndef VCMI_UNIX
// on Linux, name of main thread is also name of our process. Which we don't want to change
setThreadName("MainGUI"); setThreadName("MainGUI");
#endif
while(1) //main SDL events loop while(1) //main SDL events loop
{ {

View File

@ -463,7 +463,7 @@
"properties" : { "properties" : {
"format" : { "format" : {
"type" : "string", "type" : "string",
"default" : "[%c] %l %n - %m" "default" : "[%c] %l [%t] %n - %m"
} }
} }
}, },

View File

@ -55,10 +55,25 @@ void CThreadHelper::processTasks()
} }
} }
// set name for this thread. static thread_local std::string threadNameForLogging;
// NOTE: on *nix string will be trimmed to 16 symbols
std::string getThreadName()
{
if (!threadNameForLogging.empty())
return threadNameForLogging;
return boost::lexical_cast<std::string>(boost::this_thread::get_id());
}
void setThreadNameLoggingOnly(const std::string &name)
{
threadNameForLogging = name;
}
void setThreadName(const std::string &name) void setThreadName(const std::string &name)
{ {
threadNameForLogging = name;
#ifdef VCMI_WINDOWS #ifdef VCMI_WINDOWS
#ifndef __GNUC__ #ifndef __GNUC__
//follows http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx //follows http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
@ -90,7 +105,7 @@ void setThreadName(const std::string &name)
//not supported //not supported
#endif #endif
#elif defined(__linux__) #elif defined(VCMI_UNIX)
prctl(PR_SET_NAME, name.c_str(), 0, 0, 0); prctl(PR_SET_NAME, name.c_str(), 0, 0, 0);
#elif defined(VCMI_APPLE) #elif defined(VCMI_APPLE)
pthread_setname_np(name.c_str()); pthread_setname_np(name.c_str());

View File

@ -85,7 +85,14 @@ private:
} }
}; };
/// Sets thread name that will be used for both logs and debugger (if supported)
/// WARNING: on Unix-like systems this method should not be used for main thread since it will also change name of the process
void DLL_LINKAGE setThreadName(const std::string &name); void DLL_LINKAGE setThreadName(const std::string &name);
/// Sets thread name for use in logging only
void DLL_LINKAGE setThreadNameLoggingOnly(const std::string &name);
/// Returns human-readable thread name that was set before, or string form of system-provided thread ID if no human-readable name was set
std::string DLL_LINKAGE getThreadName();
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -9,6 +9,7 @@
*/ */
#include "StdInc.h" #include "StdInc.h"
#include "CLogger.h" #include "CLogger.h"
#include "../CThreadHelper.h"
#ifdef VCMI_ANDROID #ifdef VCMI_ANDROID
#include <android/log.h> #include <android/log.h>
@ -427,8 +428,7 @@ void CLogConsoleTarget::setColorMapping(const CColorMapping & colorMapping) { th
CLogFileTarget::CLogFileTarget(const boost::filesystem::path & filePath, bool append): CLogFileTarget::CLogFileTarget(const boost::filesystem::path & filePath, bool append):
file(filePath.c_str(), append ? std::ios_base::app : std::ios_base::out) file(filePath.c_str(), append ? std::ios_base::app : std::ios_base::out)
{ {
// formatter.setPattern("%d %l %n [%t] - %m"); formatter.setPattern("[%c] %l %n [%t] - %m");
formatter.setPattern("%l %n [%t] - %m");
} }
void CLogFileTarget::write(const LogRecord & record) void CLogFileTarget::write(const LogRecord & record)
@ -446,4 +446,14 @@ CLogFileTarget::~CLogFileTarget()
file.close(); file.close();
} }
LogRecord::LogRecord(const CLoggerDomain & domain, ELogLevel::ELogLevel level, const std::string & message)
: domain(domain),
level(level),
message(message),
timeStamp(boost::posix_time::microsec_clock::local_time()),
threadId(getThreadName())
{
}
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -107,12 +107,7 @@ private:
/// The struct LogRecord holds the log message and additional logging information. /// The struct LogRecord holds the log message and additional logging information.
struct DLL_LINKAGE LogRecord struct DLL_LINKAGE LogRecord
{ {
LogRecord(const CLoggerDomain & domain, ELogLevel::ELogLevel level, const std::string & message) LogRecord(const CLoggerDomain & domain, ELogLevel::ELogLevel level, const std::string & message);
: domain(domain),
level(level),
message(message),
timeStamp(boost::posix_time::microsec_clock::local_time()),
threadId(boost::lexical_cast<std::string>(boost::this_thread::get_id())) { }
CLoggerDomain domain; CLoggerDomain domain;
ELogLevel::ELogLevel level; ELogLevel::ELogLevel level;