From f5bb46788cbb54b7c84311d4d3b6ed8bb7160463 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Sun, 20 Jul 2025 14:50:50 +0100 Subject: [PATCH] Updated logging --- Common/IO.h | 21 ++++++++++++++++++++- Common/Result.h | 18 +++++++++++++++++- LXC/LXC.cpp | 25 +++++++++++-------------- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/Common/IO.h b/Common/IO.h index acb8bd9..ea34dcc 100644 --- a/Common/IO.h +++ b/Common/IO.h @@ -18,7 +18,7 @@ namespace LXC::Util template concept AllLogable = (Logable && ...); // Enum to translate to the Win32 code for the colors // - enum class Color : WORD + enum Color : WORD { BLACK = 0x00, BLUE = 0x01, @@ -50,4 +50,23 @@ namespace LXC::Util (std::cout << ... << args); SetConsoleTextAttribute(hConsole, (WORD)Color::LIGHT_GRAY); } + + // Prints arguments to the console // + template + requires AllLogable + inline void Print(Args... args) + { + // Fowards the arguments to the console // + (std::cout << ... << args); + } + + // Prints arguments to the console with a new-line character at the end // + template + requires AllLogable + inline void PrintLn(Args... args) + { + // Fowards the arguments to the console // + (std::cout << ... << args); + std::cout << std::endl; + } } diff --git a/Common/Result.h b/Common/Result.h index 3bd476a..d1c5698 100644 --- a/Common/Result.h +++ b/Common/Result.h @@ -7,6 +7,22 @@ namespace LXC::Util { + // Util function to stop/ the program // + inline void Stop() + { + // Only checks for a debugger when compiled in Debug mode // + #ifdef _DEBUG + + // Triggers a breakpoint so the debugger can work out where the program exits // + if (IsDebuggerPresent()) + DebugBreak(); + + #endif // _DEBUG + + // Force exits the program // + std::exit(EXIT_FAILURE); + } + // Custom version of std::unexpected // template struct FunctionFail { @@ -20,7 +36,7 @@ namespace LXC::Util if (IsDebuggerPresent()) DebugBreak(); - #endif + #endif // _DEBUG } ErrorType error; diff --git a/LXC/LXC.cpp b/LXC/LXC.cpp index 47cec3c..4ee52a2 100644 --- a/LXC/LXC.cpp +++ b/LXC/LXC.cpp @@ -7,31 +7,28 @@ int main(int argc, char** argv) using namespace LXC; // Reads the given file to a string // - Util::ReturnVal fileContents = Util::ReadFile("example/example.jk.lx"); - if (fileContents.Failed()) + Util::ReturnVal fileContents = Util::ReadFile("example/example.lx"); + if (fileContents.Failed()) _UNLIKELY { // Stores the error for easier access // Util::FileReadError& err = fileContents.Error(); // Prints the error to the console // - Util::PrintAs("[LXC]"); - Util::PrintAs(" Error: "); + Util::PrintAs("[LXC]"); + Util::PrintAs(" Error: "); + Util::PrintLn(Util::FileReadError::ReasonStr(err.reason), " [", std::filesystem::absolute(err.path), ']'); - std::cout - << Util::FileReadError::ReasonStr(err.reason) << ' ' - << '[' << std::filesystem::absolute(err.path) << ']' - << std::endl; - - // Returns with default exit code // - return -1; + Util::Stop(); } // Turns the file contents into a vector of tokens // Util::ReturnVal tokens = Lexer::TokenizeFile(fileContents); - if (tokens.Failed()) + if (tokens.Failed()) _UNLIKELY { - // Returns with default error code // - return -1; + // Stores the error for easier access // + Lexer::LexerError& err = tokens.Error(); + + Util::Stop(); } return 0;