feat: Abstract Logger and simplify code (#1207)

* Logger: Rename logger to Logger from dLogger

* Logger: Add compile time filename

Fix include issues
Add writers
Add macros
Add macro to force compilation

* Logger: Replace calls with macros

Allows for filename and line number to be logged

* Logger: Add comments

and remove extra define

Logger: Replace with unique_ptr

also flush console at exit. regular file writer should be flushed on file close.

Logger: Remove constexpr on variable

* Logger: Simplify code

* Update Logger.cpp
This commit is contained in:
David Markowitz
2023-10-21 16:31:55 -07:00
committed by GitHub
parent 131239538b
commit 5942182486
160 changed files with 1013 additions and 985 deletions

View File

@@ -4,7 +4,7 @@
#include "dNetCommon.h"
#include "dServer.h"
#include "dLogger.h"
#include "Logger.h"
#include "Database.h"
#include "ZoneInstanceManager.h"
#include "MD5.h"
@@ -34,7 +34,7 @@ void AuthPackets::HandleHandshake(dServer* server, Packet* packet) {
uint32_t clientVersion = 0;
inStream.Read(clientVersion);
server->GetLogger()->Log("AuthPackets", "Received client version: %i", clientVersion);
LOG("Received client version: %i", clientVersion);
SendHandshake(server, packet->systemAddress, server->GetIP(), server->GetPort(), server->GetServerType());
}
@@ -43,7 +43,7 @@ void AuthPackets::SendHandshake(dServer* server, const SystemAddress& sysAddr, c
BitStreamUtils::WriteHeader(bitStream, eConnectionType::SERVER, eServerMessageType::VERSION_CONFIRM);
uint32_t netVersion;
if (!GeneralUtils::TryParse(Game::config->GetValue("client_net_version"), netVersion)) {
Game::logger->Log("AuthPackets", "Failed to parse client_net_version. Cannot authenticate to %s:%i", nextServerIP.c_str(), nextServerPort);
LOG("Failed to parse client_net_version. Cannot authenticate to %s:%i", nextServerIP.c_str(), nextServerPort);
return;
}
bitStream.Write<uint32_t>(netVersion);
@@ -70,7 +70,7 @@ void AuthPackets::HandleLoginRequest(dServer* server, Packet* packet) {
sql::ResultSet* res = stmt->executeQuery();
if (res->rowsCount() == 0) {
server->GetLogger()->Log("AuthPackets", "No user found!");
LOG("No user found!");
AuthPackets::SendLoginResponse(server, packet->systemAddress, eLoginResponse::INVALID_USER, "", "", 2001, username);
return;
}
@@ -103,7 +103,7 @@ void AuthPackets::HandleLoginRequest(dServer* server, Packet* packet) {
//Check to see if we have a play key:
if (sqlPlayKey == 0 && sqlGmLevel == 0) {
AuthPackets::SendLoginResponse(server, packet->systemAddress, eLoginResponse::PERMISSIONS_NOT_HIGH_ENOUGH, "Your account doesn't have a play key associated with it!", "", 2001, username);
server->GetLogger()->Log("AuthPackets", "User %s tried to log in, but they don't have a play key.", username.c_str());
LOG("User %s tried to log in, but they don't have a play key.", username.c_str());
return;
}
@@ -124,7 +124,7 @@ void AuthPackets::HandleLoginRequest(dServer* server, Packet* packet) {
if (!isKeyActive && sqlGmLevel == 0) {
AuthPackets::SendLoginResponse(server, packet->systemAddress, eLoginResponse::PERMISSIONS_NOT_HIGH_ENOUGH, "Your play key has been disabled.", "", 2001, username);
server->GetLogger()->Log("AuthPackets", "User %s tried to log in, but their play key was disabled", username.c_str());
LOG("User %s tried to log in, but their play key was disabled", username.c_str());
return;
}
}
@@ -181,7 +181,7 @@ void AuthPackets::HandleLoginRequest(dServer* server, Packet* packet) {
if (!loginSuccess) {
AuthPackets::SendLoginResponse(server, packet->systemAddress, eLoginResponse::WRONG_PASS, "", "", 2001, username);
server->GetLogger()->Log("AuthPackets", "Wrong password used");
LOG("Wrong password used");
} else {
SystemAddress system = packet->systemAddress; //Copy the sysAddr before the Packet gets destroyed from main
@@ -268,6 +268,6 @@ void AuthPackets::SendLoginResponse(dServer* server, const SystemAddress& sysAdd
bitStream.Write(LUString(username, 66));
server->SendToMaster(&bitStream);
server->GetLogger()->Log("AuthPackets", "Set sessionKey: %i for user %s", sessionKey, username.c_str());
LOG("Set sessionKey: %i for user %s", sessionKey, username.c_str());
}
}

View File

@@ -11,7 +11,7 @@
#include "Entity.h"
#include "ControllablePhysicsComponent.h"
#include "Game.h"
#include "dLogger.h"
#include "Logger.h"
#include "WorldPackets.h"
#include "NiPoint3.h"
#include "NiQuaternion.h"
@@ -38,7 +38,7 @@
void ClientPackets::HandleChatMessage(const SystemAddress& sysAddr, Packet* packet) {
User* user = UserManager::Instance()->GetUser(sysAddr);
if (!user) {
Game::logger->Log("ClientPackets", "Unable to get user to parse chat message");
LOG("Unable to get user to parse chat message");
return;
}
@@ -67,7 +67,7 @@ void ClientPackets::HandleChatMessage(const SystemAddress& sysAddr, Packet* pack
std::string playerName = user->GetLastUsedChar()->GetName();
bool isMythran = user->GetLastUsedChar()->GetGMLevel() > eGameMasterLevel::CIVILIAN;
bool isOk = Game::chatFilter->IsSentenceOkay(GeneralUtils::UTF16ToWTF8(message), user->GetLastUsedChar()->GetGMLevel()).empty();
Game::logger->LogDebug("ClientPackets", "Msg: %s was approved previously? %i", GeneralUtils::UTF16ToWTF8(message).c_str(), user->GetLastChatMessageApproved());
LOG_DEBUG("Msg: %s was approved previously? %i", GeneralUtils::UTF16ToWTF8(message).c_str(), user->GetLastChatMessageApproved());
if (!isOk) {
// Add a limit to the string converted by general utils because it is a user received string and may be a bad actor.
CheatDetection::ReportCheat(
@@ -80,14 +80,14 @@ void ClientPackets::HandleChatMessage(const SystemAddress& sysAddr, Packet* pack
if (!isOk && !isMythran) return;
std::string sMessage = GeneralUtils::UTF16ToWTF8(message);
Game::logger->Log("Chat", "%s: %s", playerName.c_str(), sMessage.c_str());
LOG("%s: %s", playerName.c_str(), sMessage.c_str());
ChatPackets::SendChatMessage(sysAddr, chatChannel, playerName, user->GetLoggedInChar(), isMythran, message);
}
void ClientPackets::HandleClientPositionUpdate(const SystemAddress& sysAddr, Packet* packet) {
User* user = UserManager::Instance()->GetUser(sysAddr);
if (!user) {
Game::logger->Log("ClientPackets", "Unable to get user to parse position update");
LOG("Unable to get user to parse position update");
return;
}
@@ -286,14 +286,14 @@ void ClientPackets::HandleClientPositionUpdate(const SystemAddress& sysAddr, Pac
void ClientPackets::HandleChatModerationRequest(const SystemAddress& sysAddr, Packet* packet) {
User* user = UserManager::Instance()->GetUser(sysAddr);
if (!user) {
Game::logger->Log("ClientPackets", "Unable to get user to parse chat moderation request");
LOG("Unable to get user to parse chat moderation request");
return;
}
auto* entity = Player::GetPlayer(sysAddr);
if (entity == nullptr) {
Game::logger->Log("ClientPackets", "Unable to get player to parse chat moderation request");
LOG("Unable to get player to parse chat moderation request");
return;
}

View File

@@ -1,7 +1,7 @@
#include "PacketUtils.h"
#include <vector>
#include <fstream>
#include "dLogger.h"
#include "Logger.h"
#include "Game.h"
uint16_t PacketUtils::ReadU16(uint32_t startLoc, Packet* packet) {
@@ -62,7 +62,7 @@ std::string PacketUtils::ReadString(uint32_t startLoc, Packet* packet, bool wide
//! Saves a packet to the filesystem
void PacketUtils::SavePacket(const std::string& filename, const char* data, size_t length) {
//If we don't log to the console, don't save the bin files either. This takes up a lot of time.
if (!Game::logger->GetIsLoggingToConsole()) return;
if (!Game::logger->GetLogToConsole()) return;
std::string path = "packets/" + filename;

View File

@@ -5,7 +5,7 @@
#include "GeneralUtils.h"
#include "User.h"
#include "Character.h"
#include "dLogger.h"
#include "Logger.h"
#include <iostream>
#include "Game.h"
#include "LDFFormat.h"
@@ -137,7 +137,7 @@ void WorldPackets::SendCreateCharacter(const SystemAddress& sysAddr, Entity* ent
auto character = entity->GetComponent<CharacterComponent>();
if (!character) {
Game::logger->Log("WorldPackets", "Entity is not a character?? what??");
LOG("Entity is not a character?? what??");
return;
}
@@ -194,7 +194,7 @@ void WorldPackets::SendCreateCharacter(const SystemAddress& sysAddr, Entity* ent
// PacketUtils::SavePacket("chardata.bin", (const char*)bitStream.GetData(), static_cast<uint32_t>(bitStream.GetNumberOfBytesUsed()));
SEND_PACKET;
delete[] compressedData;
Game::logger->Log("WorldPackets", "Sent CreateCharacter for ID: %llu", entity->GetObjectID());
LOG("Sent CreateCharacter for ID: %llu", entity->GetObjectID());
}
void WorldPackets::SendChatModerationResponse(const SystemAddress& sysAddr, bool requestAccepted, uint32_t requestID, const std::string& receiver, std::vector<std::pair<uint8_t, uint8_t>> unacceptedItems) {

View File

@@ -1,7 +1,7 @@
#define _VARIADIC_MAX 10
#include "dServer.h"
#include "dNetCommon.h"
#include "dLogger.h"
#include "Logger.h"
#include "dConfig.h"
#include "RakNetworkFactory.h"
@@ -39,7 +39,7 @@ public:
}
} ReceiveDownloadCompleteCB;
dServer::dServer(const std::string& ip, int port, int instanceID, int maxConnections, bool isInternal, bool useEncryption, dLogger* logger, const std::string masterIP, int masterPort, ServerType serverType, dConfig* config, bool* shouldShutdown, unsigned int zoneID) {
dServer::dServer(const std::string& ip, int port, int instanceID, int maxConnections, bool isInternal, bool useEncryption, Logger* logger, const std::string masterIP, int masterPort, ServerType serverType, dConfig* config, bool* shouldShutdown, unsigned int zoneID) {
mIP = ip;
mPort = port;
mZoneID = zoneID;
@@ -60,15 +60,15 @@ dServer::dServer(const std::string& ip, int port, int instanceID, int maxConnect
mIsOkay = Startup();
//Forcibly log to both the console and our file what ip, port and possibly zoneID / instanceID we're running on:
bool prevLogSetting = mLogger->GetIsLoggingToConsole();
bool prevLogSetting = mLogger->GetLogToConsole();
mLogger->SetLogToConsole(true);
if (mIsOkay) {
if (zoneID == 0)
mLogger->Log("dServer", "Server is listening on %s:%i with encryption: %i", ip.c_str(), port, int(useEncryption));
LOG("Server is listening on %s:%i with encryption: %i", ip.c_str(), port, int(useEncryption));
else
mLogger->Log("dServer", "Server is listening on %s:%i with encryption: %i, running zone %i / %i", ip.c_str(), port, int(useEncryption), zoneID, instanceID);
} else { mLogger->Log("dServer", "FAILED TO START SERVER ON IP/PORT: %s:%i", ip.c_str(), port); return; }
LOG("Server is listening on %s:%i with encryption: %i, running zone %i / %i", ip.c_str(), port, int(useEncryption), zoneID, instanceID);
} else { LOG("FAILED TO START SERVER ON IP/PORT: %s:%i", ip.c_str(), port); return; }
mLogger->SetLogToConsole(prevLogSetting);
@@ -108,13 +108,13 @@ Packet* dServer::ReceiveFromMaster() {
if (packet->length < 1) { mMasterPeer->DeallocatePacket(packet); return nullptr; }
if (packet->data[0] == ID_DISCONNECTION_NOTIFICATION || packet->data[0] == ID_CONNECTION_LOST) {
mLogger->Log("dServer", "Lost our connection to master, shutting DOWN!");
LOG("Lost our connection to master, shutting DOWN!");
mMasterConnectionActive = false;
//ConnectToMaster(); //We'll just shut down now
}
if (packet->data[0] == ID_CONNECTION_REQUEST_ACCEPTED) {
mLogger->Log("dServer", "Established connection to master, zone (%i), instance (%i)", this->GetZoneID(), this->GetInstanceID());
LOG("Established connection to master, zone (%i), instance (%i)", this->GetZoneID(), this->GetInstanceID());
mMasterConnectionActive = true;
mMasterSystemAddress = packet->systemAddress;
MasterPackets::SendServerInfo(this, packet);

View File

@@ -4,7 +4,7 @@
#include "ReplicaManager.h"
#include "NetworkIDManager.h"
class dLogger;
class Logger;
class dConfig;
enum class eServerDisconnectIdentifiers : uint32_t;
@@ -26,7 +26,7 @@ public:
int maxConnections,
bool isInternal,
bool useEncryption,
dLogger* logger,
Logger* logger,
const std::string masterIP,
int masterPort,
ServerType serverType,
@@ -51,7 +51,7 @@ public:
const bool GetIsEncrypted() const { return mUseEncryption; }
const bool GetIsInternal() const { return mIsInternal; }
const bool GetIsOkay() const { return mIsOkay; }
dLogger* GetLogger() const { return mLogger; }
Logger* GetLogger() const { return mLogger; }
const bool GetIsConnectedToMaster() const { return mMasterConnectionActive; }
const unsigned int GetZoneID() const { return mZoneID; }
const int GetInstanceID() const { return mInstanceID; }
@@ -74,7 +74,7 @@ private:
bool ConnectToMaster();
private:
dLogger* mLogger = nullptr;
Logger* mLogger = nullptr;
dConfig* mConfig = nullptr;
RakPeerInterface* mPeer = nullptr;
ReplicaManager* mReplicaManager = nullptr;