mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-04 01:49:05 +00:00
Refactored how logging works
Made it central reusable logic. No longer needs to be passed around, opened or closed.
This commit is contained in:
@@ -158,7 +158,9 @@
|
||||
<ClInclude Include="LX-Common.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\Console.cpp" />
|
||||
<ClCompile Include="src\dllmain.cpp" />
|
||||
<ClCompile Include="src\Logger.cpp" />
|
||||
<ClCompile Include="src\pch.cpp">
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
|
||||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
|
||||
|
||||
@@ -37,5 +37,11 @@
|
||||
<ClCompile Include="src\pch.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Logger.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="src\Console.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -11,7 +11,8 @@
|
||||
#define NOMINMAX
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
|
||||
#undef NOMINMAX
|
||||
#undef WIN32_LEAN_AND_MEAN
|
||||
// Else alerts the user that their system is not supported //
|
||||
#else
|
||||
#error "This code is only designed to work on windows"
|
||||
@@ -22,6 +23,17 @@
|
||||
#error "This code is only designed to work with Visual Studio"
|
||||
#endif // _MSC_VER
|
||||
|
||||
// My commonly used macros //
|
||||
|
||||
#define RETURN_IF(condition) if (condition) { return; }
|
||||
#define RETURN_V_IF(value, condition) if (condition) { return value; }
|
||||
|
||||
#ifdef COMMON_EXPORTS
|
||||
#define COMMON_API __declspec(dllexport)
|
||||
#else
|
||||
#define COMMON_API __declspec(dllimport)
|
||||
#endif // COMMON_EXPORTS
|
||||
|
||||
// Includes commonly used STD files //
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
@@ -1,41 +1,30 @@
|
||||
namespace LX
|
||||
{
|
||||
// Enum of colors with their Win32 equivalent value //
|
||||
enum class Color
|
||||
{
|
||||
BLACK = 0,
|
||||
BLUE = 1,
|
||||
GREEN = 2,
|
||||
AQUA = 3,
|
||||
RED = 4,
|
||||
PURPLE = 5,
|
||||
YELLOW = 6,
|
||||
LIGHT_GRAY = 7,
|
||||
LIGHT_BLUE = 9,
|
||||
LIGHT_GREEN = 10,
|
||||
LIGHT_AQUA = 11,
|
||||
LIGHT_RED = 12,
|
||||
LIGHT_PURPLE = 13,
|
||||
LIGHT_YELLOW = 14,
|
||||
WHITE = 15
|
||||
BLACK = 0,
|
||||
BLUE = 1,
|
||||
GREEN = 2,
|
||||
AQUA = 3,
|
||||
RED = 4,
|
||||
PURPLE = 5,
|
||||
YELLOW = 6,
|
||||
LIGHT_GRAY = 7,
|
||||
LIGHT_BLUE = 9,
|
||||
LIGHT_GREEN = 10,
|
||||
LIGHT_AQUA = 11,
|
||||
LIGHT_RED = 12,
|
||||
LIGHT_PURPLE = 13,
|
||||
LIGHT_YELLOW = 14,
|
||||
WHITE = 15
|
||||
};
|
||||
|
||||
inline 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);
|
||||
}
|
||||
// Prints a string to std::cout with a certain color using Win32 API //
|
||||
extern "C" void COMMON_API PrintStringAsColor(const std::string& str, Color c);
|
||||
|
||||
// Util function for getting a line of the source at a given index (used for errors) //
|
||||
inline std::string GetLineAtIndexOf(const std::string src, const std::streamsize index)
|
||||
inline std::string GetLineAtIndexOf(const std::string& src, const std::streamsize index) // <- Has to be inline because of C++ types
|
||||
{
|
||||
// Finds the start of the line //
|
||||
size_t start = src.rfind('\n', index);
|
||||
|
||||
@@ -1,23 +1,51 @@
|
||||
namespace LX
|
||||
{
|
||||
template<typename... Args>
|
||||
inline void SafeLog(std::ofstream* log, Args... args)
|
||||
class COMMON_API Log
|
||||
{
|
||||
if (log != nullptr)
|
||||
{
|
||||
(*log << ... << args);
|
||||
(*log << '\n');
|
||||
}
|
||||
}
|
||||
public:
|
||||
// This class should never be constructed //
|
||||
Log() = delete;
|
||||
|
||||
inline void SafeFlush(std::ofstream* log)
|
||||
{
|
||||
if (log != nullptr)
|
||||
{
|
||||
log->flush();
|
||||
}
|
||||
}
|
||||
enum class Format
|
||||
{
|
||||
AUTO,
|
||||
NONE
|
||||
};
|
||||
|
||||
// Gives a standard way to mark a change between different sections within the log output //
|
||||
constexpr const char* LOG_BREAK = "\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n";
|
||||
template<Format format = Format::AUTO, typename... Args>
|
||||
static void out(Args... args)
|
||||
{
|
||||
if constexpr (format == Format::AUTO)
|
||||
{
|
||||
((*s_LogFile << ... << args) << "\n");
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
(*s_LogFile << ... << args);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
static void LogNewSection(Args... args)
|
||||
{
|
||||
static const char* BREAK = "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-";
|
||||
|
||||
*s_LogFile << '\n' << BREAK << '\n';
|
||||
(*s_LogFile << ... << args);
|
||||
*s_LogFile << '\n' << BREAK << '\n';
|
||||
}
|
||||
|
||||
private:
|
||||
// Allows ProcAttach to manage the log //
|
||||
friend bool ProcAttach(HMODULE hModule);
|
||||
|
||||
// Initalises the log (Called by DllMain) //
|
||||
static void Init();
|
||||
|
||||
// Closes the log (Called by DllMain) //
|
||||
static void Close();
|
||||
|
||||
static std::ofstream* s_LogFile;
|
||||
};
|
||||
}
|
||||
|
||||
19
Common/src/Console.cpp
Normal file
19
Common/src/Console.cpp
Normal 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
26
Common/src/Logger.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user