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

feat: Abstract Logger and simplify code #1207

Merged
merged 9 commits into from
Oct 21, 2023
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
18 changes: 9 additions & 9 deletions dAuthServer/AuthServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//DLU Includes:
#include "dCommonVars.h"
#include "dServer.h"
#include "dLogger.h"
#include "Logger.h"
#include "Database.h"
#include "dConfig.h"
#include "Diagnostics.h"
Expand All @@ -25,14 +25,14 @@

#include "Game.h"
namespace Game {
dLogger* logger = nullptr;
Logger* logger = nullptr;
dServer* server = nullptr;
dConfig* config = nullptr;
bool shouldShutdown = false;
std::mt19937 randomEngine;
}

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

int main(int argc, char** argv) {
Expand All @@ -51,9 +51,9 @@ int main(int argc, char** argv) {
Game::logger->SetLogToConsole(Game::config->GetValue("log_to_console") != "0");
Game::logger->SetLogDebugStatements(Game::config->GetValue("log_debug_statements") == "1");

Game::logger->Log("AuthServer", "Starting Auth server...");
Game::logger->Log("AuthServer", "Version: %i.%i", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR);
Game::logger->Log("AuthServer", "Compiled on: %s", __TIMESTAMP__);
LOG("Starting Auth server...");
LOG("Version: %i.%i", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR);
LOG("Compiled on: %s", __TIMESTAMP__);

//Connect to the MySQL Database
std::string mysql_host = Game::config->GetValue("mysql_host");
Expand All @@ -64,7 +64,7 @@ int main(int argc, char** argv) {
try {
Database::Connect(mysql_host, mysql_database, mysql_username, mysql_password);
} catch (sql::SQLException& ex) {
Game::logger->Log("AuthServer", "Got an error while connecting to the database: %s", ex.what());
LOG("Got an error while connecting to the database: %s", ex.what());
Database::Destroy("AuthServer");
delete Game::server;
delete Game::logger;
Expand Down Expand Up @@ -161,7 +161,7 @@ int main(int argc, char** argv) {
return EXIT_SUCCESS;
}

dLogger* SetupLogger() {
Logger* SetupLogger() {
std::string logPath = (BinaryPathFinder::GetBinaryDir() / ("logs/AuthServer_" + std::to_string(time(nullptr)) + ".log")).string();
bool logToConsole = false;
bool logDebugStatements = false;
Expand All @@ -170,7 +170,7 @@ dLogger* SetupLogger() {
logDebugStatements = true;
#endif

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

void HandlePacket(Packet* packet) {
Expand Down
2 changes: 1 addition & 1 deletion dChatFilter/dChatFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <regex>

#include "dCommonVars.h"
#include "dLogger.h"
#include "Logger.h"
#include "dConfig.h"
#include "Database.h"
#include "Game.h"
Expand Down
20 changes: 10 additions & 10 deletions dChatServer/ChatPacketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "Game.h"
#include "dServer.h"
#include "GeneralUtils.h"
#include "dLogger.h"
#include "Logger.h"
#include "eAddFriendResponseCode.h"
#include "eAddFriendResponseType.h"
#include "RakString.h"
Expand Down Expand Up @@ -397,7 +397,7 @@ void ChatPacketHandler::HandleChatMessage(Packet* packet) {

std::string message = PacketUtils::ReadString(0x66, packet, true, 512);

Game::logger->Log("ChatPacketHandler", "Got a message from (%s) [%d]: %s", senderName.c_str(), channel, message.c_str());
LOG("Got a message from (%s) [%d]: %s", senderName.c_str(), channel, message.c_str());

if (channel != 8) return;

Expand Down Expand Up @@ -527,13 +527,13 @@ void ChatPacketHandler::HandleTeamInvite(Packet* packet) {
if (team->memberIDs.size() > 3) {
// no more teams greater than 4

Game::logger->Log("ChatPacketHandler", "Someone tried to invite a 5th player to a team");
LOG("Someone tried to invite a 5th player to a team");
return;
}

SendTeamInvite(other, player);

Game::logger->Log("ChatPacketHandler", "Got team invite: %llu -> %s", playerID, invitedPlayer.c_str());
LOG("Got team invite: %llu -> %s", playerID, invitedPlayer.c_str());
}

void ChatPacketHandler::HandleTeamInviteResponse(Packet* packet) {
Expand All @@ -547,7 +547,7 @@ void ChatPacketHandler::HandleTeamInviteResponse(Packet* packet) {
LWOOBJID leaderID = LWOOBJID_EMPTY;
inStream.Read(leaderID);

Game::logger->Log("ChatPacketHandler", "Accepted invite: %llu -> %llu (%d)", playerID, leaderID, declined);
LOG("Accepted invite: %llu -> %llu (%d)", playerID, leaderID, declined);

if (declined) {
return;
Expand All @@ -556,13 +556,13 @@ void ChatPacketHandler::HandleTeamInviteResponse(Packet* packet) {
auto* team = playerContainer.GetTeam(leaderID);

if (team == nullptr) {
Game::logger->Log("ChatPacketHandler", "Failed to find team for leader (%llu)", leaderID);
LOG("Failed to find team for leader (%llu)", leaderID);

team = playerContainer.GetTeam(playerID);
}

if (team == nullptr) {
Game::logger->Log("ChatPacketHandler", "Failed to find team for player (%llu)", playerID);
LOG("Failed to find team for player (%llu)", playerID);
return;
}

Expand All @@ -578,7 +578,7 @@ void ChatPacketHandler::HandleTeamLeave(Packet* packet) {

auto* team = playerContainer.GetTeam(playerID);

Game::logger->Log("ChatPacketHandler", "(%llu) leaving team", playerID);
LOG("(%llu) leaving team", playerID);

if (team != nullptr) {
playerContainer.RemoveMember(team, playerID, false, false, true);
Expand All @@ -592,7 +592,7 @@ void ChatPacketHandler::HandleTeamKick(Packet* packet) {

std::string kickedPlayer = PacketUtils::ReadString(0x14, packet, true);

Game::logger->Log("ChatPacketHandler", "(%llu) kicking (%s) from team", playerID, kickedPlayer.c_str());
LOG("(%llu) kicking (%s) from team", playerID, kickedPlayer.c_str());

auto* kicked = playerContainer.GetPlayerData(kickedPlayer);

Expand Down Expand Up @@ -622,7 +622,7 @@ void ChatPacketHandler::HandleTeamPromote(Packet* packet) {

std::string promotedPlayer = PacketUtils::ReadString(0x14, packet, true);

Game::logger->Log("ChatPacketHandler", "(%llu) promoting (%s) to team leader", playerID, promotedPlayer.c_str());
LOG("(%llu) promoting (%s) to team leader", playerID, promotedPlayer.c_str());

auto* promoted = playerContainer.GetPlayerData(promotedPlayer);

Expand Down
34 changes: 17 additions & 17 deletions dChatServer/ChatServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//DLU Includes:
#include "dCommonVars.h"
#include "dServer.h"
#include "dLogger.h"
#include "Logger.h"
#include "Database.h"
#include "dConfig.h"
#include "dChatFilter.h"
Expand All @@ -27,7 +27,7 @@
#include <MessageIdentifiers.h>

namespace Game {
dLogger* logger = nullptr;
Logger* logger = nullptr;
dServer* server = nullptr;
dConfig* config = nullptr;
dChatFilter* chatFilter = nullptr;
Expand All @@ -37,7 +37,7 @@ namespace Game {
}


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

PlayerContainer playerContainer;
Expand All @@ -58,9 +58,9 @@ int main(int argc, char** argv) {
Game::logger->SetLogToConsole(Game::config->GetValue("log_to_console") != "0");
Game::logger->SetLogDebugStatements(Game::config->GetValue("log_debug_statements") == "1");

Game::logger->Log("ChatServer", "Starting Chat server...");
Game::logger->Log("ChatServer", "Version: %i.%i", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR);
Game::logger->Log("ChatServer", "Compiled on: %s", __TIMESTAMP__);
LOG("Starting Chat server...");
LOG("Version: %i.%i", PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR);
LOG("Compiled on: %s", __TIMESTAMP__);

try {
std::string clientPathStr = Game::config->GetValue("client_location");
Expand All @@ -72,7 +72,7 @@ int main(int argc, char** argv) {

Game::assetManager = new AssetManager(clientPath);
} catch (std::runtime_error& ex) {
Game::logger->Log("ChatServer", "Got an error while setting up assets: %s", ex.what());
LOG("Got an error while setting up assets: %s", ex.what());

return EXIT_FAILURE;
}
Expand All @@ -86,7 +86,7 @@ int main(int argc, char** argv) {
try {
Database::Connect(mysql_host, mysql_database, mysql_username, mysql_password);
} catch (sql::SQLException& ex) {
Game::logger->Log("ChatServer", "Got an error while connecting to the database: %s", ex.what());
LOG("Got an error while connecting to the database: %s", ex.what());
Database::Destroy("ChatServer");
delete Game::server;
delete Game::logger;
Expand Down Expand Up @@ -185,7 +185,7 @@ int main(int argc, char** argv) {
return EXIT_SUCCESS;
}

dLogger* SetupLogger() {
Logger* SetupLogger() {
std::string logPath = (BinaryPathFinder::GetBinaryDir() / ("logs/ChatServer_" + std::to_string(time(nullptr)) + ".log")).string();
bool logToConsole = false;
bool logDebugStatements = false;
Expand All @@ -194,16 +194,16 @@ dLogger* SetupLogger() {
logDebugStatements = true;
#endif

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

void HandlePacket(Packet* packet) {
if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION || packet->data[0] == ID_CONNECTION_LOST) {
Game::logger->Log("ChatServer", "A server has disconnected, erasing their connected players from the list.");
LOG("A server has disconnected, erasing their connected players from the list.");
}

if (packet->data[0] == ID_NEW_INCOMING_CONNECTION) {
Game::logger->Log("ChatServer", "A server is connecting, awaiting user list.");
LOG("A server is connecting, awaiting user list.");
}

if (packet->length < 4) return; // Nothing left to process. Need 4 bytes to continue.
Expand Down Expand Up @@ -234,7 +234,7 @@ void HandlePacket(Packet* packet) {
}

default:
Game::logger->Log("ChatServer", "Unknown CHAT_INTERNAL id: %i", int(packet->data[3]));
LOG("Unknown CHAT_INTERNAL id: %i", int(packet->data[3]));
}
}

Expand All @@ -245,7 +245,7 @@ void HandlePacket(Packet* packet) {
break;

case eChatMessageType::GET_IGNORE_LIST:
Game::logger->Log("ChatServer", "Asked for ignore list, but is unimplemented right now.");
LOG("Asked for ignore list, but is unimplemented right now.");
break;

case eChatMessageType::TEAM_GET_STATUS:
Expand Down Expand Up @@ -303,19 +303,19 @@ void HandlePacket(Packet* packet) {
break;

default:
Game::logger->Log("ChatServer", "Unknown CHAT id: %i", int(packet->data[3]));
LOG("Unknown CHAT id: %i", int(packet->data[3]));
}
}

if (static_cast<eConnectionType>(packet->data[1]) == eConnectionType::WORLD) {
switch (static_cast<eWorldMessageType>(packet->data[3])) {
case eWorldMessageType::ROUTE_PACKET: {
Game::logger->Log("ChatServer", "Routing packet from world");
LOG("Routing packet from world");
break;
}

default:
Game::logger->Log("ChatServer", "Unknown World id: %i", int(packet->data[3]));
LOG("Unknown World id: %i", int(packet->data[3]));
}
}
}
10 changes: 5 additions & 5 deletions dChatServer/PlayerContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <iostream>
#include <algorithm>
#include "Game.h"
#include "dLogger.h"
#include "Logger.h"
#include "ChatPacketHandler.h"
#include "GeneralUtils.h"
#include "BitStreamUtils.h"
Expand Down Expand Up @@ -44,7 +44,7 @@ void PlayerContainer::InsertPlayer(Packet* packet) {
mNames[data->playerID] = GeneralUtils::UTF8ToUTF16(data->playerName);

mPlayers.insert(std::make_pair(data->playerID, data));
Game::logger->Log("PlayerContainer", "Added user: %s (%llu), zone: %i", data->playerName.c_str(), data->playerID, data->zoneID.GetMapID());
LOG("Added user: %s (%llu), zone: %i", data->playerName.c_str(), data->playerID, data->zoneID.GetMapID());

auto* insertLog = Database::CreatePreppedStmt("INSERT INTO activity_log (character_id, activity, time, map_id) VALUES (?, ?, ?, ?);");

Expand Down Expand Up @@ -87,7 +87,7 @@ void PlayerContainer::RemovePlayer(Packet* packet) {
}
}

Game::logger->Log("PlayerContainer", "Removed user: %llu", playerID);
LOG("Removed user: %llu", playerID);
mPlayers.erase(playerID);

auto* insertLog = Database::CreatePreppedStmt("INSERT INTO activity_log (character_id, activity, time, map_id) VALUES (?, ?, ?, ?);");
Expand All @@ -110,7 +110,7 @@ void PlayerContainer::MuteUpdate(Packet* packet) {
auto* player = this->GetPlayerData(playerID);

if (player == nullptr) {
Game::logger->Log("PlayerContainer", "Failed to find user: %llu", playerID);
LOG("Failed to find user: %llu", playerID);

return;
}
Expand Down Expand Up @@ -214,7 +214,7 @@ TeamData* PlayerContainer::GetTeam(LWOOBJID playerID) {

void PlayerContainer::AddMember(TeamData* team, LWOOBJID playerID) {
if (team->memberIDs.size() >= 4){
Game::logger->Log("PlayerContainer", "Tried to add player to team that already had 4 players");
LOG("Tried to add player to team that already had 4 players");
auto* player = GetPlayerData(playerID);
if (!player) return;
ChatPackets::SendSystemMessage(player->sysAddr, u"The teams is full! You have not been added to a team!");
Expand Down
2 changes: 1 addition & 1 deletion dCommon/Amf3.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define __AMF3__H__

#include "dCommonVars.h"
#include "dLogger.h"
#include "Logger.h"
#include "Game.h"

#include <unordered_map>
Expand Down
4 changes: 2 additions & 2 deletions dCommon/AmfSerialize.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "AmfSerialize.h"

#include "Game.h"
#include "dLogger.h"
#include "Logger.h"

// Writes an AMFValue pointer to a RakNet::BitStream
template<>
Expand Down Expand Up @@ -29,7 +29,7 @@ void RakNet::BitStream::Write<AMFBaseValue&>(AMFBaseValue& value) {
break;
}
default: {
Game::logger->Log("AmfSerialize", "Encountered unwritable AMFType %i!", type);
LOG("Encountered unwritable AMFType %i!", type);
}
case eAmf::Undefined:
case eAmf::Null:
Expand Down
Loading
Loading