Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Consolidate logger setup and add better handling of packets #1389

Merged
merged 7 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ add_subdirectory(dGame)
add_subdirectory(dZoneManager)
add_subdirectory(dNavigation)
add_subdirectory(dPhysics)
add_subdirectory(dServer)

# Create a list of common libraries shared between all binaries
set(COMMON_LIBRARIES "dCommon" "dDatabase" "dNet" "raknet" "mariadbConnCpp" "magic_enum")
Expand Down
25 changes: 6 additions & 19 deletions dAuthServer/AuthServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#include "eAuthMessageType.h"

#include "Game.h"
#include "Server.h"


namespace Game {
Logger* logger = nullptr;
dServer* server = nullptr;
Expand All @@ -33,7 +36,6 @@ namespace Game {
std::mt19937 randomEngine;
}

Logger* SetupLogger();
void HandlePacket(Packet* packet);

int main(int argc, char** argv) {
Expand All @@ -46,15 +48,12 @@ int main(int argc, char** argv) {
std::signal(SIGINT, Game::OnSignal);
std::signal(SIGTERM, Game::OnSignal);

Game::config = new dConfig("authconfig.ini");

//Create all the objects we need to run our service:
Game::logger = SetupLogger();
Server::SetupLogger("AuthServer");
if (!Game::logger) return EXIT_FAILURE;

//Read our config:
Game::config = new dConfig("authconfig.ini");
Game::logger->SetLogToConsole(Game::config->GetValue("log_to_console") != "0");
Game::logger->SetLogDebugStatements(Game::config->GetValue("log_debug_statements") == "1");

LOG("Starting Auth server...");
LOG("Version: %s", PROJECT_VERSION);
LOG("Compiled on: %s", __TIMESTAMP__);
Expand Down Expand Up @@ -162,18 +161,6 @@ int main(int argc, char** argv) {
return EXIT_SUCCESS;
}

Logger* SetupLogger() {
std::string logPath = (BinaryPathFinder::GetBinaryDir() / ("logs/AuthServer_" + std::to_string(time(nullptr)) + ".log")).string();
bool logToConsole = false;
bool logDebugStatements = false;
#ifdef _DEBUG
logToConsole = true;
logDebugStatements = true;
#endif

return new Logger(logPath, logToConsole, logDebugStatements);
}

void HandlePacket(Packet* packet) {
if (packet->length < 4) return;

Expand Down
6 changes: 5 additions & 1 deletion dAuthServer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
add_executable(AuthServer "AuthServer.cpp")
target_link_libraries(AuthServer ${COMMON_LIBRARIES})

target_link_libraries(AuthServer ${COMMON_LIBRARIES} dServer)

target_include_directories(AuthServer PRIVATE ${PROJECT_SOURCE_DIR}/dServer)

add_compile_definitions(AuthServer PRIVATE PROJECT_VERSION="\"${PROJECT_VERSION}\"")
4 changes: 3 additions & 1 deletion dChatServer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ set(DCHATSERVER_SOURCES

add_executable(ChatServer "ChatServer.cpp")
add_library(dChatServer ${DCHATSERVER_SOURCES})
target_include_directories(dChatServer PRIVATE ${PROJECT_SOURCE_DIR}/dServer)
add_compile_definitions(ChatServer PRIVATE PROJECT_VERSION="\"${PROJECT_VERSION}\"")

target_link_libraries(dChatServer ${COMMON_LIBRARIES} dChatFilter)
target_link_libraries(ChatServer ${COMMON_LIBRARIES} dChatFilter dChatServer)
target_link_libraries(ChatServer ${COMMON_LIBRARIES} dChatFilter dChatServer dServer)

21 changes: 4 additions & 17 deletions dChatServer/ChatServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ChatIgnoreList.h"

#include "Game.h"
#include "Server.h"

//RakNet includes:
#include "RakNetDefines.h"
Expand All @@ -38,7 +39,6 @@ namespace Game {
PlayerContainer playerContainer;
}

Logger* SetupLogger();
void HandlePacket(Packet* packet);

int main(int argc, char** argv) {
Expand All @@ -51,14 +51,13 @@ int main(int argc, char** argv) {
std::signal(SIGINT, Game::OnSignal);
std::signal(SIGTERM, Game::OnSignal);

Game::config = new dConfig("chatconfig.ini");

//Create all the objects we need to run our service:
Game::logger = SetupLogger();
Server::SetupLogger("ChatServer");
if (!Game::logger) return EXIT_FAILURE;

//Read our config:
Game::config = new dConfig("chatconfig.ini");
Game::logger->SetLogToConsole(Game::config->GetValue("log_to_console") != "0");
Game::logger->SetLogDebugStatements(Game::config->GetValue("log_debug_statements") == "1");

LOG("Starting Chat server...");
LOG("Version: %s", PROJECT_VERSION);
Expand Down Expand Up @@ -182,18 +181,6 @@ int main(int argc, char** argv) {
return EXIT_SUCCESS;
}

Logger* SetupLogger() {
std::string logPath = (BinaryPathFinder::GetBinaryDir() / ("logs/ChatServer_" + std::to_string(time(nullptr)) + ".log")).string();
bool logToConsole = false;
bool logDebugStatements = false;
#ifdef _DEBUG
logToConsole = true;
logDebugStatements = true;
#endif

return new Logger(logPath, logToConsole, logDebugStatements);
}

void HandlePacket(Packet* packet) {
if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION || packet->data[0] == ID_CONNECTION_LOST) {
LOG("A server has disconnected, erasing their connected players from the list.");
Expand Down
3 changes: 2 additions & 1 deletion dMasterServer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ add_executable(MasterServer "MasterServer.cpp")
add_compile_definitions(MasterServer PRIVATE PROJECT_VERSION="\"${PROJECT_VERSION}\"")

target_link_libraries(dMasterServer ${COMMON_LIBRARIES})
target_link_libraries(MasterServer ${COMMON_LIBRARIES} dMasterServer)
target_link_libraries(MasterServer ${COMMON_LIBRARIES} dMasterServer dServer)
target_include_directories(dMasterServer PRIVATE ${PROJECT_SOURCE_DIR}/dServer)

if(WIN32)
add_dependencies(MasterServer WorldServer AuthServer ChatServer)
Expand Down
22 changes: 4 additions & 18 deletions dMasterServer/MasterServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "FdbToSqlite.h"
#include "BitStreamUtils.h"
#include "Start.h"
#include "Server.h"

namespace Game {
Logger* logger = nullptr;
Expand All @@ -55,7 +56,6 @@ namespace Game {
bool shutdownSequenceStarted = false;
int ShutdownSequence(int32_t signal = -1);
int32_t FinalizeShutdown(int32_t signal = -1);
Logger* SetupLogger();
void HandlePacket(Packet* packet);
std::map<uint32_t, std::string> activeSessions;
SystemAddress authServerMasterPeerSysAddr;
Expand All @@ -77,8 +77,10 @@ int main(int argc, char** argv) {
std::signal(SIGINT, Game::OnSignal);
std::signal(SIGTERM, Game::OnSignal);

Game::config = new dConfig("masterconfig.ini");

//Create all the objects we need to run our service:
Game::logger = SetupLogger();
Server::SetupLogger("MasterServer");
if (!Game::logger) return EXIT_FAILURE;

if (!dConfig::Exists("authconfig.ini")) LOG("Could not find authconfig.ini, using default settings");
Expand All @@ -87,9 +89,6 @@ int main(int argc, char** argv) {
if (!dConfig::Exists("sharedconfig.ini")) LOG("Could not find sharedconfig.ini, using default settings");
if (!dConfig::Exists("worldconfig.ini")) LOG("Could not find worldconfig.ini, using default settings");

Game::config = new dConfig("masterconfig.ini");
Game::logger->SetLogToConsole(Game::config->GetValue("log_to_console") != "0");
Game::logger->SetLogDebugStatements(Game::config->GetValue("log_debug_statements") == "1");

uint32_t clientNetVersion = 171022;
const auto clientNetVersionString = Game::config->GetValue("client_net_version");
Expand Down Expand Up @@ -395,19 +394,6 @@ int main(int argc, char** argv) {
return ShutdownSequence(EXIT_SUCCESS);
}

Logger* SetupLogger() {
std::string logPath =
(BinaryPathFinder::GetBinaryDir() / ("logs/MasterServer_" + std::to_string(time(nullptr)) + ".log")).string();
bool logToConsole = false;
bool logDebugStatements = false;
#ifdef _DEBUG
logToConsole = true;
logDebugStatements = true;
#endif

return new Logger(logPath, logToConsole, logDebugStatements);
}

void HandlePacket(Packet* packet) {
if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION) {
LOG("A server has disconnected");
Expand Down
6 changes: 6 additions & 0 deletions dServer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
set(DSERVER_SOURCES
"Server.cpp")

add_library(dServer STATIC ${DSERVER_SOURCES})

target_include_directories(dServer PUBLIC ".")
29 changes: 29 additions & 0 deletions dServer/Server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "Server.h"

#include "BinaryPathFinder.h"
#include "Game.h"
#include "Logger.h"
#include "dConfig.h"

void Server::SetupLogger(const std::string_view serviceName) {
if (Game::logger) {
LOG("A logger has already been setup, skipping.");
return;
}

const auto logsDir = BinaryPathFinder::GetBinaryDir() / "logs";

if (!std::filesystem::exists(logsDir)) std::filesystem::create_directory(logsDir);

std::string logPath = (logsDir / serviceName).string() + "_" + std::to_string(time(nullptr)) + ".log";
bool logToConsole = false;
bool logDebugStatements = false;
#ifdef _DEBUG
logToConsole = true;
logDebugStatements = true;
#endif
Game::logger = new Logger(logPath, logToConsole, logDebugStatements);

Game::logger->SetLogToConsole(Game::config->GetValue("log_to_console") != "0");
Game::logger->SetLogDebugStatements(Game::config->GetValue("log_debug_statements") == "1");
}
10 changes: 10 additions & 0 deletions dServer/Server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __SERVER__H__
#define __SERVER__H__

#include <string_view>

namespace Server {
void SetupLogger(const std::string_view serviceName);
};

#endif //!__SERVER__H__
3 changes: 2 additions & 1 deletion dWorldServer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ add_executable(WorldServer "WorldServer.cpp")
add_compile_definitions(WorldServer PRIVATE PROJECT_VERSION="\"${PROJECT_VERSION}\"")

target_link_libraries(dWorldServer ${COMMON_LIBRARIES})
target_link_libraries(WorldServer ${COMMON_LIBRARIES} dChatFilter dGame dZoneManager dPhysics Detour Recast tinyxml2 dWorldServer dNavigation)
target_link_libraries(WorldServer ${COMMON_LIBRARIES} dChatFilter dGame dZoneManager dPhysics Detour Recast tinyxml2 dWorldServer dNavigation dServer)
target_include_directories(WorldServer PRIVATE ${PROJECT_SOURCE_DIR}/dServer)

Loading
Loading