mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-09-30 18:03:45 +00:00
feat: logging structure improvements (#2037)
* feat: logging structure improvements tested that logs are sorted by the folder argument and that the directories are created recursively. tested that crash dumps follow the same exact name (+Crash_...name..._pid.log) so you can match the crash dump to the corresponding log file much easier * Potential fix for pull request finding 'Use parsed cloneID when constructing the log folder' Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * const --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -40,7 +40,9 @@ void HandlePacket(Packet* packet);
|
||||
int main(int argc, char** argv) {
|
||||
constexpr uint32_t authFramerate = mediumFramerate;
|
||||
constexpr uint32_t authFrameDelta = mediumFrameDelta;
|
||||
Diagnostics::SetProcessName("Auth");
|
||||
const auto curTimeStr = std::to_string(time(nullptr));
|
||||
const auto serviceName = "AuthServer_" + curTimeStr;
|
||||
Diagnostics::SetProcessName(serviceName);
|
||||
Diagnostics::SetProcessFileName(argv[0]);
|
||||
Diagnostics::Initialize();
|
||||
|
||||
@@ -50,7 +52,7 @@ int main(int argc, char** argv) {
|
||||
Game::config = new dConfig("authconfig.ini");
|
||||
|
||||
//Create all the objects we need to run our service:
|
||||
Server::SetupLogger("AuthServer");
|
||||
Server::SetupLogger(serviceName, "AuthServer");
|
||||
if (!Game::logger) return EXIT_FAILURE;
|
||||
Game::config->LogSettings();
|
||||
|
||||
@@ -62,7 +64,7 @@ int main(int argc, char** argv) {
|
||||
Database::Connect();
|
||||
} catch (std::exception& ex) {
|
||||
LOG("Got an error while connecting to the database: %s", ex.what());
|
||||
Database::Destroy("AuthServer");
|
||||
Database::Destroy(serviceName);
|
||||
delete Game::server;
|
||||
delete Game::logger;
|
||||
return EXIT_FAILURE;
|
||||
@@ -156,7 +158,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
LOG("Exited Main Loop! (signal %d)", Game::lastSignal);
|
||||
//Delete our objects here:
|
||||
Database::Destroy("AuthServer");
|
||||
Database::Destroy(serviceName);
|
||||
delete Game::server;
|
||||
delete Game::logger;
|
||||
delete Game::config;
|
||||
|
||||
@@ -47,7 +47,9 @@ void HandlePacket(Packet* packet);
|
||||
int main(int argc, char** argv) {
|
||||
constexpr uint32_t chatFramerate = mediumFramerate;
|
||||
constexpr uint32_t chatFrameDelta = mediumFrameDelta;
|
||||
Diagnostics::SetProcessName("Chat");
|
||||
const auto curTimeStr = std::to_string(time(nullptr));
|
||||
const auto serviceName = "ChatServer_" + curTimeStr;
|
||||
Diagnostics::SetProcessName(serviceName);
|
||||
Diagnostics::SetProcessFileName(argv[0]);
|
||||
Diagnostics::Initialize();
|
||||
|
||||
@@ -57,7 +59,7 @@ int main(int argc, char** argv) {
|
||||
Game::config = new dConfig("chatconfig.ini");
|
||||
|
||||
//Create all the objects we need to run our service:
|
||||
Server::SetupLogger("ChatServer");
|
||||
Server::SetupLogger(serviceName, "ChatServer");
|
||||
if (!Game::logger) return EXIT_FAILURE;
|
||||
Game::config->LogSettings();
|
||||
|
||||
@@ -88,7 +90,7 @@ int main(int argc, char** argv) {
|
||||
Database::Connect();
|
||||
} catch (std::exception& ex) {
|
||||
LOG("Got an error while connecting to the database: %s", ex.what());
|
||||
Database::Destroy("ChatServer");
|
||||
Database::Destroy(serviceName);
|
||||
delete Game::logger;
|
||||
delete Game::config;
|
||||
return EXIT_FAILURE;
|
||||
@@ -99,7 +101,7 @@ int main(int argc, char** argv) {
|
||||
if (Game::config->GetValue("web_server_enabled") == "1" && !Game::web.Startup("localhost", web_server_port)) {
|
||||
// if we want the web server and it fails to start, exit
|
||||
LOG("Failed to start web server, shutting down.");
|
||||
Database::Destroy("ChatServer");
|
||||
Database::Destroy(serviceName);
|
||||
delete Game::logger;
|
||||
delete Game::config;
|
||||
return EXIT_FAILURE;
|
||||
@@ -200,7 +202,7 @@ int main(int argc, char** argv) {
|
||||
Game::playerContainer.Shutdown();
|
||||
TeamContainer::Shutdown();
|
||||
//Delete our objects here:
|
||||
Database::Destroy("ChatServer");
|
||||
Database::Destroy(serviceName);
|
||||
delete Game::server;
|
||||
Game::server = nullptr;
|
||||
delete Game::logger;
|
||||
|
||||
@@ -82,7 +82,7 @@ struct bt_ctx {
|
||||
};
|
||||
|
||||
static inline void Bt(struct backtrace_state* state) {
|
||||
std::string fileName = Diagnostics::GetOutDirectory() + "crash_" + Diagnostics::GetProcessName() + "_" + std::to_string(getpid()) + ".log";
|
||||
std::string fileName = Diagnostics::GetOutDirectory() + "Crash_" + Diagnostics::GetProcessName() + "_" + std::to_string(getpid()) + ".log";
|
||||
LOG("backtrace is enabled, crash dump located at %s", fileName.c_str());
|
||||
FILE* file = fopen(fileName.c_str(), "w+");
|
||||
if (file != nullptr) {
|
||||
@@ -98,7 +98,7 @@ static void ErrorCallback(void* data, const char* msg, int errnum) {
|
||||
fprintf(stderr, "ERROR: %s (%d)", msg, errnum);
|
||||
ctx->error = 1;
|
||||
|
||||
std::string fileName = Diagnostics::GetOutDirectory() + "crash_" + Diagnostics::GetProcessName() + "_" + std::to_string(getpid()) + ".log";
|
||||
std::string fileName = Diagnostics::GetOutDirectory() + "Crash_" + Diagnostics::GetProcessName() + "_" + std::to_string(getpid()) + ".log";
|
||||
FILE* file = fopen(fileName.c_str(), "w+");
|
||||
if (file != nullptr) {
|
||||
fprintf(file, "ERROR: %s (%d)", msg, errnum);
|
||||
@@ -126,7 +126,7 @@ void CatchUnhandled(int sig) {
|
||||
|
||||
#ifndef INCLUDE_BACKTRACE
|
||||
|
||||
std::string fileName = Diagnostics::GetOutDirectory() + "crash_" + Diagnostics::GetProcessName() + "_" + std::to_string(getpid()) + ".log";
|
||||
std::string fileName = Diagnostics::GetOutDirectory() + "Crash_" + Diagnostics::GetProcessName() + "_" + std::to_string(getpid()) + ".log";
|
||||
LOG("Encountered signal %i, creating crash dump %s", sig, fileName.c_str());
|
||||
if (Diagnostics::GetProduceMemoryDump()) {
|
||||
GenerateDump();
|
||||
|
||||
@@ -61,6 +61,10 @@ namespace Game {
|
||||
std::mt19937 randomEngine;
|
||||
} //namespace Game
|
||||
|
||||
namespace {
|
||||
std::string g_ServiceName;
|
||||
}
|
||||
|
||||
bool shutdownSequenceStarted = false;
|
||||
int ShutdownSequence(int32_t signal = -1);
|
||||
int32_t FinalizeShutdown(int32_t signal = -1);
|
||||
@@ -80,7 +84,9 @@ int GenerateBCryptPassword(const std::string& password, const int workFactor, ch
|
||||
int main(int argc, char** argv) {
|
||||
constexpr uint32_t masterFramerate = mediumFramerate;
|
||||
constexpr uint32_t masterFrameDelta = mediumFrameDelta;
|
||||
Diagnostics::SetProcessName("Master");
|
||||
const auto curTimeStr = std::to_string(time(nullptr));
|
||||
g_ServiceName = "MasterServer_" + curTimeStr;
|
||||
Diagnostics::SetProcessName(g_ServiceName);
|
||||
Diagnostics::SetProcessFileName(argv[0]);
|
||||
Diagnostics::Initialize();
|
||||
|
||||
@@ -96,7 +102,7 @@ int main(int argc, char** argv) {
|
||||
Game::config = new dConfig("masterconfig.ini");
|
||||
|
||||
//Create all the objects we need to run our service:
|
||||
Server::SetupLogger("MasterServer");
|
||||
Server::SetupLogger(g_ServiceName, "MasterServer");
|
||||
if (!Game::logger) return EXIT_FAILURE;
|
||||
Game::config->LogSettings();
|
||||
|
||||
@@ -914,7 +920,7 @@ int ShutdownSequence(int32_t signal) {
|
||||
|
||||
int32_t FinalizeShutdown(int32_t signal) {
|
||||
//Delete our objects here:
|
||||
Database::Destroy("MasterServer");
|
||||
Database::Destroy(g_ServiceName);
|
||||
if (Game::config) delete Game::config;
|
||||
Game::config = nullptr;
|
||||
if (Game::im) delete Game::im;
|
||||
|
||||
@@ -5,17 +5,18 @@
|
||||
#include "Logger.h"
|
||||
#include "dConfig.h"
|
||||
|
||||
void Server::SetupLogger(const std::string_view serviceName) {
|
||||
void Server::SetupLogger(const std::string_view serviceName, const std::string_view folder) {
|
||||
if (Game::logger) {
|
||||
LOG("A logger has already been setup, skipping.");
|
||||
return;
|
||||
}
|
||||
|
||||
const auto logsDir = BinaryPathFinder::GetBinaryDir() / "logs";
|
||||
auto logsDir = BinaryPathFinder::GetBinaryDir() / "logs";
|
||||
if (!folder.empty()) logsDir /= folder;
|
||||
|
||||
if (!std::filesystem::exists(logsDir)) std::filesystem::create_directories(logsDir);
|
||||
|
||||
std::string logPath = (logsDir / serviceName).string() + "_" + std::to_string(time(nullptr)) + ".log";
|
||||
std::string logPath = (logsDir / serviceName).string() + ".log";
|
||||
bool logToConsole = false;
|
||||
bool logDebugStatements = false;
|
||||
#ifdef _DEBUG
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
#include <string_view>
|
||||
|
||||
namespace Server {
|
||||
void SetupLogger(const std::string_view serviceName);
|
||||
// takes in an optional argument of folder should you want to place the logs in a sub-folder
|
||||
void SetupLogger(const std::string_view serviceName, const std::string_view folder = "");
|
||||
};
|
||||
|
||||
#endif //!__SERVER__H__
|
||||
|
||||
@@ -99,6 +99,10 @@ namespace Game {
|
||||
std::string projectVersion = PROJECT_VERSION;
|
||||
} // namespace Game
|
||||
|
||||
namespace {
|
||||
std::string g_ServiceName;
|
||||
}
|
||||
|
||||
namespace {
|
||||
struct TempSessionInfo {
|
||||
SystemAddress sysAddr;
|
||||
@@ -125,7 +129,10 @@ void HandleMasterPacket(Packet* packet);
|
||||
void HandlePacket(Packet* packet);
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Diagnostics::SetProcessName("World");
|
||||
const auto curTimeStr = std::to_string(time(nullptr));
|
||||
g_ServiceName = "WorldServer";
|
||||
// Set this once here before we parse a bunch of options in case we crash early
|
||||
Diagnostics::SetProcessName(g_ServiceName);
|
||||
Diagnostics::SetProcessFileName(argv[0]);
|
||||
Diagnostics::Initialize();
|
||||
|
||||
@@ -158,7 +165,13 @@ int main(int argc, char** argv) {
|
||||
Game::config = new dConfig("worldconfig.ini");
|
||||
|
||||
//Create all the objects we need to run our service:
|
||||
Server::SetupLogger("WorldServer_" + std::to_string(zoneID) + "_" + std::to_string(g_InstanceID));
|
||||
const auto zoneStr = std::to_string(zoneID);
|
||||
const auto cloneStr = std::to_string(cloneID);
|
||||
const auto instanceStr = std::to_string(g_InstanceID);
|
||||
g_ServiceName += "_" + zoneStr + "_" + cloneStr + "_" + instanceStr + "_" + curTimeStr;
|
||||
// Here we re-set the process name since it'll have more info now
|
||||
Diagnostics::SetProcessName(g_ServiceName);
|
||||
Server::SetupLogger(g_ServiceName, "WorldServer/" + zoneStr + "/" + cloneStr + "/");
|
||||
if (!Game::logger) return EXIT_FAILURE;
|
||||
Game::config->LogSettings();
|
||||
|
||||
@@ -1586,7 +1599,7 @@ void FinalizeShutdown() {
|
||||
|
||||
//Delete our objects here:
|
||||
dpWorld::Shutdown();
|
||||
Database::Destroy("WorldServer");
|
||||
Database::Destroy(g_ServiceName);
|
||||
if (Game::chatFilter) delete Game::chatFilter;
|
||||
Game::chatFilter = nullptr;
|
||||
if (Game::zoneManager) delete Game::zoneManager;
|
||||
|
||||
Reference in New Issue
Block a user