implement thread names in debug mode

This commit is contained in:
Lion Kortlepel 2021-03-30 00:30:24 +02:00
parent 940a39ed4e
commit 3094d382ff
9 changed files with 61 additions and 36 deletions

View File

@ -60,29 +60,13 @@ private:
static inline std::deque<TShutdownHandler> mShutdownHandlers {};
};
std::string ThreadName();
void RegisterThread(const std::string str);
#define RegisterThreadAuto() RegisterThread(__func__)
#define KB 1024
#define MB (KB * 1024)
#ifndef DEBUG
static inline void warn(const std::string& str) {
Application::Console().Write(std::string("[WARN] ") + str);
}
static inline void error(const std::string& str) {
Application::Console().Write(std::string("[ERROR] ") + str);
}
static inline void info(const std::string& str) {
Application::Console().Write(std::string("[INFO] ") + str);
}
static inline void debug(const std::string& str) {
if (Application::Settings.DebugModeEnabled) {
Application::Console().Write(std::string("[DEBUG] ") + str);
}
}
static inline void luaprint(const std::string& str) {
Application::Console().WriteRaw(str);
}
#else // DEBUG
#define _file_basename std::filesystem::path(__FILE__).filename().string()
#define _line std::to_string(__LINE__)
#define _in_lambda (std::string(__func__) == "operator()")
@ -98,26 +82,34 @@ static inline void luaprint(const std::string& str) {
#define _function_name std::string(__func__)
#endif
#if defined(DEBUG)
// if this is defined, we will show the full function signature infront of
// each info/debug/warn... call instead of the 'filename:line' format.
#if defined(BMP_FULL_FUNCTION_NAMES)
#define _this_location (_function_name)
#define _this_location (ThreadName() + _function_name)
#else
#define _this_location (_file_basename + ":" + _line)
#define _this_location (ThreadName() + _file_basename + ":" + _line)
#endif
#define warn(x) Application::Console().Write(_this_location + std::string(" [WARN] ") + (x))
#define info(x) Application::Console().Write(_this_location + std::string(" [INFO] ") + (x))
#define error(x) Application::Console().Write(_this_location + std::string(" [ERROR] ") + (x))
#define luaprint(x) Application::Console().Write(_this_location + std::string(" [LUA] ") + (x))
#else // !defined(DEBUG)
#define _this_location (ThreadName())
#endif // defined(DEBUG)
#define warn(x) Application::Console().Write(_this_location + std::string("[WARN] ") + (x))
#define info(x) Application::Console().Write(_this_location + std::string("[INFO] ") + (x))
#define error(x) Application::Console().Write(_this_location + std::string("[ERROR] ") + (x))
#define luaprint(x) Application::Console().Write(_this_location + std::string("[LUA] ") + (x))
#define debug(x) \
do { \
if (Application::Settings.DebugModeEnabled) { \
Application::Console().Write(_this_location + std::string(" [DEBUG] ") + (x)); \
Application::Console().Write(_this_location + std::string("[DEBUG] ") + (x)); \
} \
} while (false)
#endif // DEBUG
#define Biggest 30000
std::string Comp(std::string Data);
std::string DeComp(std::string Compressed);

View File

@ -3,6 +3,8 @@
#include "TConsole.h"
#include <array>
#include <iostream>
#include <map>
#include <thread>
#include <zlib.h>
std::unique_ptr<TConsole> Application::mConsole = std::make_unique<TConsole>();
@ -65,3 +67,22 @@ std::string DeComp(std::string Compressed) {
std::copy_n(C.begin(), TO, Ret.begin());
return Ret;
}
// thread name stuff
std::map<std::thread::id, std::string> threadNameMap;
std::string ThreadName() {
if (Application::Settings.DebugModeEnabled) {
auto id = std::this_thread::get_id();
if (threadNameMap.find(id) != threadNameMap.end()) {
// found
return threadNameMap.at(id) + " ";
}
}
return "";
}
void RegisterThread(const std::string str) {
threadNameMap[std::this_thread::get_id()] = str;
}

View File

@ -6,6 +6,7 @@
#include <sstream>
void THeartbeatThread::operator()() {
RegisterThread("Heartbeat");
std::string Body;
std::string T;

View File

@ -32,6 +32,7 @@ TLuaEngine::TLuaEngine(TServer& Server, TNetwork& Network)
}
void TLuaEngine::operator()() {
RegisterThread("LuaEngine");
info("Lua system online");
while (!mShutdown) {
if (!mLuaFiles.empty()) {

View File

@ -44,6 +44,7 @@ void ClearStack(lua_State* L) {
}
std::any Trigger(TLuaFile* lua, const std::string& R, std::shared_ptr<TLuaArg> arg) {
RegisterThread(lua->GetFileName());
std::lock_guard<std::mutex> lockGuard(lua->Lock);
std::packaged_task<std::any(std::shared_ptr<TLuaArg>)> task([lua, R](std::shared_ptr<TLuaArg> arg) { return CallFunction(lua, R, arg); });
std::future<std::any> f1 = task.get_future();
@ -180,7 +181,7 @@ void ExecuteAsync(TLuaFile* lua, const std::string& FuncName) {
}
void CallAsync(TLuaFile* lua, const std::string& Func, int U) {
//DebugPrintTID();
RegisterThread(lua->GetFileName());
lua->SetStopThread(false);
int D = 1000 / U;
while (!lua->GetStopThread()) {
@ -623,6 +624,7 @@ std::string TLuaFile::GetOrigin() {
}
std::any CallFunction(TLuaFile* lua, const std::string& FuncName, std::shared_ptr<TLuaArg> Arg) {
RegisterThread(lua->GetFileName());
lua_State* luaState = lua->GetState();
lua_getglobal(luaState, FuncName.c_str());
if (lua_isfunction(luaState, -1)) {

View File

@ -2,8 +2,8 @@
#include "Client.h"
#include <CustomAssert.h>
#include <Http.h>
#include <cstring>
#include <array>
#include <cstring>
TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& ResourceManager)
: mServer(Server)
@ -30,6 +30,7 @@ TNetwork::TNetwork(TServer& Server, TPPSMonitor& PPSMonitor, TResourceManager& R
}
void TNetwork::UDPServerMain() {
RegisterThread("UDPServer");
#ifdef WIN32
WSADATA data;
if (WSAStartup(514, &data)) {
@ -99,6 +100,7 @@ void TNetwork::UDPServerMain() {
}
void TNetwork::TCPServerMain() {
RegisterThread("TCPServer");
#ifdef WIN32
WSADATA wsaData;
if (WSAStartup(514, &wsaData)) {
@ -201,6 +203,7 @@ void TNetwork::TCPServerMain() {
namespace json = rapidjson;
void TNetwork::Identify(SOCKET TCPSock) {
RegisterThreadAuto();
char Code;
if (recv(TCPSock, &Code, 1, 0) != 1) {
CloseSocketProper(TCPSock);
@ -293,6 +296,7 @@ void TNetwork::Authentication(SOCKET TCPSock) {
return;
}
RegisterThread(Client->GetName());
debug("Name -> " + Client->GetName() + ", Guest -> " + std::to_string(Client->IsGuest()) + ", Roles -> " + Client->GetRoles());
debug("There are " + std::to_string(mServer.ClientCount()) + " known clients");
mServer.ForEachClient([&](const std::weak_ptr<TClient>& ClientPtr) -> bool {

View File

@ -16,6 +16,7 @@ TPPSMonitor::TPPSMonitor(TServer& Server)
Start();
}
void TPPSMonitor::operator()() {
RegisterThread("PPSMonitor");
while (!mNetwork) {
// hard spi
std::this_thread::sleep_for(std::chrono::milliseconds(1));

View File

@ -1,8 +1,8 @@
#include "TServer.h"
#include "Client.h"
#include "Common.h"
#include "TPPSMonitor.h"
#include "TNetwork.h"
#include "TPPSMonitor.h"
#include <TLuaFile.h>
#include <any>
#include <sstream>
@ -16,7 +16,7 @@
namespace json = rapidjson;
TServer::TServer(int argc, char** argv) {
info("BeamMP Server running version " + Application::ServerVersion());
info("BeamMP Server v" + Application::ServerVersion());
if (argc > 1) {
Application::Settings.CustomIP = argv[1];
size_t n = std::count(Application::Settings.CustomIP.begin(), Application::Settings.CustomIP.end(), '.');

View File

@ -33,7 +33,9 @@ void UnixSignalHandler(int sig) {
int main(int argc, char** argv) {
#ifdef __unix
#if DEBUG
info("registering handlers for SIGINT, SIGTERM, SIGPIPE");
#endif // DEBUG
signal(SIGPIPE, UnixSignalHandler);
signal(SIGTERM, UnixSignalHandler);
#ifndef DEBUG
@ -48,6 +50,7 @@ int main(int argc, char** argv) {
TServer Server(argc, argv);
[[maybe_unused]] TConfig Config("Server.cfg");
RegisterThread("Main");
TResourceManager ResourceManager;
TPPSMonitor PPSMonitor(Server);
THeartbeatThread Heartbeat(ResourceManager, Server);