mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2025-07-04 00:36:14 +00:00
Added thread names to logs
This commit is contained in:
parent
a42ab67d2f
commit
437a654b90
@ -6,8 +6,8 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
void InitLog();
|
void InitLog();
|
||||||
#define DebugPrintTID() DebugPrintTIDInternal(__func__)
|
#define DebugPrintTID() DebugPrintTIDInternal(__func__, false)
|
||||||
void DebugPrintTIDInternal(const std::string& func); // prints the current thread id in debug mode, to make tracing of crashes and asserts easier
|
void DebugPrintTIDInternal(const std::string& func, bool overwrite = true); // prints the current thread id in debug mode, to make tracing of crashes and asserts easier
|
||||||
void ConsoleOut(const std::string& msg);
|
void ConsoleOut(const std::string& msg);
|
||||||
void QueueAbort();
|
void QueueAbort();
|
||||||
void except(const std::string& toPrint);
|
void except(const std::string& toPrint);
|
||||||
|
@ -9,7 +9,37 @@
|
|||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <shared_mutex>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
using RWMutex = std::shared_mutex;
|
||||||
|
using ReadLock = std::shared_lock<RWMutex>;
|
||||||
|
using WriteLock = std::unique_lock<RWMutex>;
|
||||||
|
|
||||||
|
static RWMutex ThreadNameMapMutex;
|
||||||
|
static std::unordered_map<std::thread::id, std::string> ThreadNameMap;
|
||||||
|
|
||||||
|
std::string ThreadName() {
|
||||||
|
ReadLock lock(ThreadNameMapMutex);
|
||||||
|
std::string Name;
|
||||||
|
if (ThreadNameMap.find(std::this_thread::get_id()) != ThreadNameMap.end()) {
|
||||||
|
Name = ThreadNameMap.at(std::this_thread::get_id());
|
||||||
|
} else {
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << std::this_thread::get_id();
|
||||||
|
Name = ss.str();
|
||||||
|
}
|
||||||
|
lock.unlock();
|
||||||
|
return Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetThreadName(const std::string& Name, bool overwrite) {
|
||||||
|
WriteLock lock(ThreadNameMapMutex);
|
||||||
|
if (overwrite || ThreadNameMap.find(std::this_thread::get_id()) == ThreadNameMap.end()) {
|
||||||
|
ThreadNameMap[std::this_thread::get_id()] = Name;
|
||||||
|
}
|
||||||
|
lock.unlock();
|
||||||
|
}
|
||||||
|
|
||||||
std::string getDate() {
|
std::string getDate() {
|
||||||
typedef std::chrono::duration<int, std::ratio_multiply<std::chrono::hours::period, std::ratio<24>>::type> days;
|
typedef std::chrono::duration<int, std::ratio_multiply<std::chrono::hours::period, std::ratio<24>>::type> days;
|
||||||
@ -46,14 +76,11 @@ std::string getDate() {
|
|||||||
<< Min << ":"
|
<< Min << ":"
|
||||||
<< Secs
|
<< Secs
|
||||||
<< "] "
|
<< "] "
|
||||||
#ifdef DEBUG
|
<< ThreadName()
|
||||||
<< std::this_thread::get_id()
|
|
||||||
<< " ";
|
<< " ";
|
||||||
#else
|
|
||||||
;
|
|
||||||
#endif
|
|
||||||
return date.str();
|
return date.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
void InitLog() {
|
void InitLog() {
|
||||||
std::ofstream LFS;
|
std::ofstream LFS;
|
||||||
LFS.open(Sec("Server.log"));
|
LFS.open(Sec("Server.log"));
|
||||||
@ -64,9 +91,10 @@ void InitLog() {
|
|||||||
}
|
}
|
||||||
std::mutex LogLock;
|
std::mutex LogLock;
|
||||||
|
|
||||||
void DebugPrintTIDInternal(const std::string& func) {
|
void DebugPrintTIDInternal(const std::string& func, bool overwrite) {
|
||||||
// we need to print to cout here as we might crash before all console output is handled,
|
// we need to print to cout here as we might crash before all console output is handled,
|
||||||
// due to segfaults or asserts.
|
// due to segfaults or asserts.
|
||||||
|
SetThreadName(func, overwrite);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
LogLock.lock();
|
LogLock.lock();
|
||||||
std::stringstream Print;
|
std::stringstream Print;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user