Refactored how logging works

Made it central reusable logic. No longer needs to be passed around, opened or closed.
This commit is contained in:
Pasha Bibko
2025-05-05 23:55:22 +01:00
parent 0f11fe006b
commit 5339df9b36
22 changed files with 231 additions and 193 deletions

19
Common/src/Console.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include <LX-Common.h>
namespace LX
{
void PrintStringAsColor(const std::string& str, Color c)
{
// Gets a handle to the console //
static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// Sets the color of the console to the desired color //
SetConsoleTextAttribute(hConsole, (WORD)c);
// Outputs the text //
std::cout << str;
// Resets the color //
SetConsoleTextAttribute(hConsole, (WORD)Color::LIGHT_GRAY);
}
}

26
Common/src/Logger.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include <LX-Common.h>
namespace LX
{
// Allocates memory for the log file pointer //
std::ofstream* Log::s_LogFile = nullptr;
void Log::Init()
{
// The actual object holding the log file //
// Has to be done like this to stop MSVC complaining //
static std::ofstream actualLog;
// Opens the log file and assigns it to the log pointer //
actualLog.open("log");
s_LogFile = &actualLog;
}
void Log::Close()
{
// Flushes and closes the log //
// Yes I know closing automatically flushes but this function looked empty //
s_LogFile->flush();
s_LogFile->close();
}
}

View File

@@ -3,36 +3,38 @@
namespace LX
{
// All the different functions that can be called by the system //
// Currently all empty but here for easier future development //
using DllFunc = bool(HMODULE);
static bool ProcAttach(HMODULE hModule)
{
Log::Init(); // Initalises the log before the main process starts
return true;
}
static bool ProcDetach(HMODULE hModule)
{
return true;
}
static bool ThreadAttach(HMODULE hModule)
{
}
static bool ThreadDetach(HMODULE hModule)
{
}
static bool ThreadAttach(HMODULE hModule) { return true; }
static bool ThreadDetach(HMODULE hModule) { return true; }
}
BOOL __stdcall DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
// All the functions that might be called with their relevant state //
static const std::unordered_map<DWORD, LX::DllFunc&> funcs =
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
{ DLL_PROCESS_ATTACH, LX::ProcAttach },
{ DLL_PROCESS_DETACH, LX::ProcDetach },
{ DLL_THREAD_ATTACH , LX::ThreadAttach },
{ DLL_THREAD_DETACH , LX::ThreadDetach }
};
return true;
// Returns the output of the relavant function //
return funcs.at(ul_reason_for_call)(hModule);
}