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

@@ -24,7 +24,7 @@ Entity* GetPossessedEntity(const LWOOBJID& objId) {
void ReportCheat(User* user, const SystemAddress& sysAddr, const char* messageIfNotSender, va_list args) {
if (!user) {
Game::logger->Log("CheatDetection", "WARNING: User is null, using defaults.");
LOG("WARNING: User is null, using defaults.");
}
std::unique_ptr<sql::PreparedStatement> stmt(Database::CreatePreppedStmt(
"INSERT INTO player_cheat_detections (account_id, name, violation_msg, violation_system_address) VALUES (?, ?, ?, ?)")
@@ -39,7 +39,7 @@ void ReportCheat(User* user, const SystemAddress& sysAddr, const char* messageIf
stmt->setString(3, buffer);
stmt->setString(4, Game::config->GetValue("log_ip_addresses_for_anti_cheat") == "1" ? sysAddr.ToString() : "IP logging disabled.");
stmt->execute();
Game::logger->Log("CheatDetection", "Anti-cheat message: %s", buffer);
LOG("Anti-cheat message: %s", buffer);
}
void LogAndSaveFailedAntiCheatCheck(const LWOOBJID& id, const SystemAddress& sysAddr, const CheckType checkType, const char* messageIfNotSender, va_list args) {
@@ -51,20 +51,20 @@ void LogAndSaveFailedAntiCheatCheck(const LWOOBJID& id, const SystemAddress& sys
// If player exists and entity exists in world, use both for logging info.
if (entity && player) {
Game::logger->Log("CheatDetection", "Player (%s) (%llu) at system address (%s) with sending player (%s) (%llu) does not match their own.",
LOG("Player (%s) (%llu) at system address (%s) with sending player (%s) (%llu) does not match their own.",
player->GetCharacter()->GetName().c_str(), player->GetObjectID(),
sysAddr.ToString(),
entity->GetCharacter()->GetName().c_str(), entity->GetObjectID());
toReport = player->GetParentUser();
// In the case that the target entity id did not exist, just log the player info.
} else if (player) {
Game::logger->Log("CheatDetection", "Player (%s) (%llu) at system address (%s) with sending player (%llu) does not match their own.",
LOG("Player (%s) (%llu) at system address (%s) with sending player (%llu) does not match their own.",
player->GetCharacter()->GetName().c_str(), player->GetObjectID(),
sysAddr.ToString(), id);
toReport = player->GetParentUser();
// In the rare case that the player does not exist, just log the system address and who the target id was.
} else {
Game::logger->Log("CheatDetection", "Player at system address (%s) with sending player (%llu) does not match their own.",
LOG("Player at system address (%s) with sending player (%llu) does not match their own.",
sysAddr.ToString(), id);
}
break;
@@ -73,11 +73,11 @@ void LogAndSaveFailedAntiCheatCheck(const LWOOBJID& id, const SystemAddress& sys
auto* user = UserManager::Instance()->GetUser(sysAddr);
if (user) {
Game::logger->Log("CheatDetection", "User at system address (%s) (%s) (%llu) sent a packet as (%i) which is not an id they own.",
LOG("User at system address (%s) (%s) (%llu) sent a packet as (%i) which is not an id they own.",
sysAddr.ToString(), user->GetLastUsedChar()->GetName().c_str(), user->GetLastUsedChar()->GetObjectID(), static_cast<int32_t>(id));
// Can't know sending player. Just log system address for IP banning.
} else {
Game::logger->Log("CheatDetection", "No user found for system address (%s).", sysAddr.ToString());
LOG("No user found for system address (%s).", sysAddr.ToString());
}
toReport = user;
break;
@@ -110,7 +110,7 @@ bool CheatDetection::VerifyLwoobjidIsSender(const LWOOBJID& id, const SystemAddr
// Check here if the system address has a character with id matching the lwoobjid after unsetting the flag bits.
auto* sendingUser = UserManager::Instance()->GetUser(sysAddr);
if (!sendingUser) {
Game::logger->Log("CheatDetection", "No user found for system address (%s).", sysAddr.ToString());
LOG("No user found for system address (%s).", sysAddr.ToString());
return false;
}
invalidPacket = true;

View File

@@ -14,7 +14,7 @@
#include "Character.h"
#include "PacketUtils.h"
#include "BitStreamUtils.h"
#include "dLogger.h"
#include "Logger.h"
#include "EntityManager.h"
#include "InventoryComponent.h"
#include "GameMessages.h"
@@ -153,7 +153,7 @@ void Mail::HandleMailStuff(RakNet::BitStream* packet, const SystemAddress& sysAd
Mail::HandleSendMail(packet, sysAddr, entity);
break;
default:
Game::logger->Log("Mail", "Unhandled and possibly undefined MailStuffID: %i", int(stuffID));
LOG("Unhandled and possibly undefined MailStuffID: %i", int(stuffID));
}
});
}
@@ -262,10 +262,10 @@ void Mail::HandleSendMail(RakNet::BitStream* packet, const SystemAddress& sysAdd
Mail::SendSendResponse(sysAddr, Mail::MailSendResponse::Success);
entity->GetCharacter()->SetCoins(entity->GetCharacter()->GetCoins() - mailCost, eLootSourceType::MAIL);
Game::logger->Log("Mail", "Seeing if we need to remove item with ID/count/LOT: %i %i %i", itemID, attachmentCount, itemLOT);
LOG("Seeing if we need to remove item with ID/count/LOT: %i %i %i", itemID, attachmentCount, itemLOT);
if (inv && itemLOT != 0 && attachmentCount > 0 && item) {
Game::logger->Log("Mail", "Trying to remove item with ID/count/LOT: %i %i %i", itemID, attachmentCount, itemLOT);
LOG("Trying to remove item with ID/count/LOT: %i %i %i", itemID, attachmentCount, itemLOT);
inv->RemoveItem(itemLOT, attachmentCount, INVALID, true);
auto* missionCompoent = entity->GetComponent<MissionComponent>();

View File

@@ -1,7 +1,7 @@
#include "Preconditions.h"
#include "Game.h"
#include "dLogger.h"
#include "Logger.h"
#include <sstream>
@@ -28,7 +28,7 @@ Precondition::Precondition(const uint32_t condition) {
this->count = 1;
this->values = { 0 };
Game::logger->Log("Precondition", "Failed to find precondition of id (%i)!", condition);
LOG("Failed to find precondition of id (%i)!", condition);
return;
}

View File

@@ -29,7 +29,7 @@
#include "GeneralUtils.h"
#include "Entity.h"
#include "EntityManager.h"
#include "dLogger.h"
#include "Logger.h"
#include "WorldPackets.h"
#include "GameMessages.h"
#include "CDClientDatabase.h"
@@ -142,7 +142,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
WorldPackets::SendGMLevelChange(sysAddr, success, user->GetMaxGMLevel(), entity->GetGMLevel(), level);
GameMessages::SendChatModeUpdate(entity->GetObjectID(), level);
entity->SetGMLevel(level);
Game::logger->Log("SlashCommandHandler", "User %s (%i) has changed their GM level to %i for charID %llu", user->GetUsername().c_str(), user->GetAccountID(), level, entity->GetObjectID());
LOG("User %s (%i) has changed their GM level to %i for charID %llu", user->GetUsername().c_str(), user->GetAccountID(), level, entity->GetObjectID());
}
}
@@ -179,7 +179,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
auto* character = entity->GetComponent<CharacterComponent>();
if (character == nullptr) {
Game::logger->Log("SlashCommandHandler", "Failed to find character component!");
LOG("Failed to find character component!");
return;
}
@@ -263,7 +263,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
args.Insert("visible", true);
args.Insert("text", customText);
Game::logger->Log("SlashCommandHandler", "Sending %s", customText.c_str());
LOG("Sending %s", customText.c_str());
GameMessages::SendUIMessageServerToSingleClient(entity, entity->GetSystemAddress(), "ToggleStoryBox", args);
});
@@ -297,7 +297,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
const auto sysAddr = entity->GetSystemAddress();
Game::logger->Log("UserManager", "Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", entity->GetCharacter()->GetName().c_str(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
LOG("Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", entity->GetCharacter()->GetName().c_str(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
if (entity->GetCharacter()) {
entity->GetCharacter()->SetZoneID(zoneID);
@@ -316,7 +316,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
const auto& password = args[0];
ZoneInstanceManager::Instance()->RequestPrivateZone(Game::server, false, password, [=](bool mythranShift, uint32_t zoneID, uint32_t zoneInstance, uint32_t zoneClone, std::string serverIP, uint16_t serverPort) {
Game::logger->Log("UserManager", "Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", sysAddr.ToString(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
LOG("Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", sysAddr.ToString(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
if (entity->GetCharacter()) {
entity->GetCharacter()->SetZoneID(zoneID);
@@ -908,7 +908,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
pos.SetY(y);
pos.SetZ(z);
Game::logger->Log("SlashCommandHandler", "Teleporting objectID: %llu to %f, %f, %f", entity->GetObjectID(), pos.x, pos.y, pos.z);
LOG("Teleporting objectID: %llu to %f, %f, %f", entity->GetObjectID(), pos.x, pos.y, pos.z);
GameMessages::SendTeleport(entity->GetObjectID(), pos, NiQuaternion(), sysAddr);
} else if (args.size() == 2) {
@@ -928,7 +928,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
pos.SetY(0.0f);
pos.SetZ(z);
Game::logger->Log("SlashCommandHandler", "Teleporting objectID: %llu to X: %f, Z: %f", entity->GetObjectID(), pos.x, pos.z);
LOG("Teleporting objectID: %llu to X: %f, Z: %f", entity->GetObjectID(), pos.x, pos.z);
GameMessages::SendTeleport(entity->GetObjectID(), pos, NiQuaternion(), sysAddr);
} else {
ChatPackets::SendSystemMessage(sysAddr, u"Correct usage: /teleport <x> (<y>) <z> - if no Y given, will teleport to the height of the terrain (or any physics object).");
@@ -1552,7 +1552,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
ChatPackets::SendSystemMessage(sysAddr, u"Transfering map...");
Game::logger->Log("UserManager", "Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", sysAddr.ToString(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
LOG("Transferring %s to Zone %i (Instance %i | Clone %i | Mythran Shift: %s) with IP %s and Port %i", sysAddr.ToString(), zoneID, zoneInstance, zoneClone, mythranShift == true ? "true" : "false", serverIP.c_str(), serverPort);
if (entity->GetCharacter()) {
entity->GetCharacter()->SetZoneID(zoneID);
entity->GetCharacter()->SetZoneInstance(zoneInstance);
@@ -1819,7 +1819,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
if (!GeneralUtils::TryParse(args[0], inventoryType)) {
// In this case, we treat the input as a string and try to find it in the reflection list
std::transform(args[0].begin(), args[0].end(), args[0].begin(), ::toupper);
Game::logger->Log("SlashCommandHandler", "looking for inventory %s", args[0].c_str());
LOG("looking for inventory %s", args[0].c_str());
for (uint32_t index = 0; index < NUMBER_OF_INVENTORIES; index++) {
if (std::string_view(args[0]) == std::string_view(InventoryType::InventoryTypeToString(static_cast<eInventoryType>(index)))) inventoryType = static_cast<eInventoryType>(index);
}
@@ -1837,7 +1837,7 @@ void SlashCommandHandler::HandleChatCommand(const std::u16string& command, Entit
if (!inventoryToDelete) return;
inventoryToDelete->DeleteAllItems();
Game::logger->Log("SlashCommandHandler", "Deleted inventory %s for user %llu", args[0].c_str(), entity->GetObjectID());
LOG("Deleted inventory %s for user %llu", args[0].c_str(), entity->GetObjectID());
ChatPackets::SendSystemMessage(sysAddr, u"Deleted inventory " + GeneralUtils::UTF8ToUTF16(args[0]));
}

View File

@@ -12,7 +12,7 @@
#include "dServer.h"
#include "tinyxml2.h"
#include "Game.h"
#include "dLogger.h"
#include "Logger.h"
#include "BinaryPathFinder.h"
#include "EntityInfo.h"
@@ -49,7 +49,7 @@ void VanityUtilities::SpawnVanity() {
std::vector<VanityNPC> npcList = m_NPCs;
std::vector<uint32_t> taken = {};
Game::logger->Log("VanityUtilities", "Spawning party with %i locations", party.m_Locations.size());
LOG("Spawning party with %i locations", party.m_Locations.size());
// Loop through all locations
for (const auto& location : party.m_Locations) {
@@ -70,7 +70,7 @@ void VanityUtilities::SpawnVanity() {
taken.push_back(npcIndex);
// Spawn the NPC
Game::logger->Log("VanityUtilities", "ldf size is %i", npc.ldf.size());
LOG("ldf size is %i", npc.ldf.size());
if (npc.ldf.empty()) {
npc.ldf = {
new LDFData<std::vector<std::u16string>>(u"syncLDF", { u"custom_script_client" }),
@@ -194,7 +194,7 @@ void VanityUtilities::ParseXML(const std::string& file) {
auto* npcs = doc.FirstChildElement("npcs");
if (npcs == nullptr) {
Game::logger->Log("VanityUtilities", "Failed to parse NPCs");
LOG("Failed to parse NPCs");
return;
}
@@ -218,7 +218,7 @@ void VanityUtilities::ParseXML(const std::string& file) {
auto* locations = party->FirstChildElement("locations");
if (locations == nullptr) {
Game::logger->Log("VanityUtilities", "Failed to parse party locations");
LOG("Failed to parse party locations");
continue;
}
@@ -235,7 +235,7 @@ void VanityUtilities::ParseXML(const std::string& file) {
if (x == nullptr || y == nullptr || z == nullptr || rw == nullptr || rx == nullptr || ry == nullptr
|| rz == nullptr) {
Game::logger->Log("VanityUtilities", "Failed to parse party location data");
LOG("Failed to parse party location data");
continue;
}
@@ -253,7 +253,7 @@ void VanityUtilities::ParseXML(const std::string& file) {
auto* partyPhrases = npcs->FirstChildElement("partyphrases");
if (partyPhrases == nullptr) {
Game::logger->Log("VanityUtilities", "Failed to parse party phrases");
LOG("Failed to parse party phrases");
return;
}
@@ -263,7 +263,7 @@ void VanityUtilities::ParseXML(const std::string& file) {
auto* text = phrase->GetText();
if (text == nullptr) {
Game::logger->Log("VanityUtilities", "Failed to parse party phrase");
LOG("Failed to parse party phrase");
continue;
}
@@ -280,7 +280,7 @@ void VanityUtilities::ParseXML(const std::string& file) {
auto* lot = npc->Attribute("lot");
if (lot == nullptr) {
Game::logger->Log("VanityUtilities", "Failed to parse NPC lot");
LOG("Failed to parse NPC lot");
continue;
}
@@ -314,7 +314,7 @@ void VanityUtilities::ParseXML(const std::string& file) {
// Get the phrase
auto* text = phrase->GetText();
if (text == nullptr) {
Game::logger->Log("VanityUtilities", "Failed to parse NPC phrase");
LOG("Failed to parse NPC phrase");
continue;
}
phraseList.push_back(text);
@@ -367,7 +367,7 @@ void VanityUtilities::ParseXML(const std::string& file) {
auto* name = flag->Attribute("name");
if (name == nullptr) {
Game::logger->Log("VanityUtilities", "Failed to parse NPC flag name");
LOG("Failed to parse NPC flag name");
continue;
}
@@ -375,7 +375,7 @@ void VanityUtilities::ParseXML(const std::string& file) {
auto* value = flag->Attribute("value");
if (value == nullptr) {
Game::logger->Log("VanityUtilities", "Failed to parse NPC flag value");
LOG("Failed to parse NPC flag value");
continue;
}
@@ -389,7 +389,7 @@ void VanityUtilities::ParseXML(const std::string& file) {
auto* zoneID = zone->Attribute("id");
if (zoneID == nullptr) {
Game::logger->Log("VanityUtilities", "Failed to parse NPC zone ID");
LOG("Failed to parse NPC zone ID");
continue;
}
@@ -397,7 +397,7 @@ void VanityUtilities::ParseXML(const std::string& file) {
auto* locations = zone->FirstChildElement("locations");
if (locations == nullptr) {
Game::logger->Log("VanityUtilities", "Failed to parse NPC locations");
LOG("Failed to parse NPC locations");
continue;
}
@@ -414,7 +414,7 @@ void VanityUtilities::ParseXML(const std::string& file) {
if (x == nullptr || y == nullptr || z == nullptr || rw == nullptr || rx == nullptr || ry == nullptr
|| rz == nullptr) {
Game::logger->Log("VanityUtilities", "Failed to parse NPC location data");
LOG("Failed to parse NPC location data");
continue;
}
@@ -478,8 +478,10 @@ std::string VanityUtilities::ParseMarkdown(const std::string& file) {
while (std::getline(ss, line)) {
#define TOSTRING(x) #x
#define STRINGIFY(x) TOSTRING(x)
#ifndef STRINGIFY
#define STRINGIFY(x) TOSTRING(x)
#endif
// Replace "__TIMESTAMP__" with the __TIMESTAMP__
GeneralUtils::ReplaceInString(line, "__TIMESTAMP__", __TIMESTAMP__);
// Replace "__VERSION__" wit'h the PROJECT_VERSION