Updated logging

This commit is contained in:
Pasha Bibko
2025-07-20 14:50:50 +01:00
parent ac656b7b0f
commit f5bb46788c
3 changed files with 48 additions and 16 deletions

View File

@@ -18,7 +18,7 @@ namespace LXC::Util
template<typename... Args> concept AllLogable = (Logable<Args> && ...); template<typename... Args> concept AllLogable = (Logable<Args> && ...);
// Enum to translate to the Win32 code for the colors // // Enum to translate to the Win32 code for the colors //
enum class Color : WORD enum Color : WORD
{ {
BLACK = 0x00, BLACK = 0x00,
BLUE = 0x01, BLUE = 0x01,
@@ -50,4 +50,23 @@ namespace LXC::Util
(std::cout << ... << args); (std::cout << ... << args);
SetConsoleTextAttribute(hConsole, (WORD)Color::LIGHT_GRAY); SetConsoleTextAttribute(hConsole, (WORD)Color::LIGHT_GRAY);
} }
// Prints arguments to the console //
template<typename... Args>
requires AllLogable<Args...>
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<typename... Args>
requires AllLogable<Args...>
inline void PrintLn(Args... args)
{
// Fowards the arguments to the console //
(std::cout << ... << args);
std::cout << std::endl;
}
} }

View File

@@ -7,6 +7,22 @@
namespace LXC::Util 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 // // Custom version of std::unexpected //
template<typename ErrorType> struct FunctionFail template<typename ErrorType> struct FunctionFail
{ {
@@ -20,7 +36,7 @@ namespace LXC::Util
if (IsDebuggerPresent()) if (IsDebuggerPresent())
DebugBreak(); DebugBreak();
#endif #endif // _DEBUG
} }
ErrorType error; ErrorType error;

View File

@@ -7,31 +7,28 @@ int main(int argc, char** argv)
using namespace LXC; using namespace LXC;
// Reads the given file to a string // // Reads the given file to a string //
Util::ReturnVal fileContents = Util::ReadFile("example/example.jk.lx"); Util::ReturnVal fileContents = Util::ReadFile("example/example.lx");
if (fileContents.Failed()) if (fileContents.Failed()) _UNLIKELY
{ {
// Stores the error for easier access // // Stores the error for easier access //
Util::FileReadError& err = fileContents.Error(); Util::FileReadError& err = fileContents.Error();
// Prints the error to the console // // Prints the error to the console //
Util::PrintAs<Util::Color::WHITE>("[LXC]"); Util::PrintAs<Util::WHITE>("[LXC]");
Util::PrintAs<Util::Color::LIGHT_RED>(" Error: "); Util::PrintAs<Util::LIGHT_RED>(" Error: ");
Util::PrintLn(Util::FileReadError::ReasonStr(err.reason), " [", std::filesystem::absolute(err.path), ']');
std::cout Util::Stop();
<< Util::FileReadError::ReasonStr(err.reason) << ' '
<< '[' << std::filesystem::absolute(err.path) << ']'
<< std::endl;
// Returns with default exit code //
return -1;
} }
// Turns the file contents into a vector of tokens // // Turns the file contents into a vector of tokens //
Util::ReturnVal tokens = Lexer::TokenizeFile(fileContents); Util::ReturnVal tokens = Lexer::TokenizeFile(fileContents);
if (tokens.Failed()) if (tokens.Failed()) _UNLIKELY
{ {
// Returns with default error code // // Stores the error for easier access //
return -1; Lexer::LexerError& err = tokens.Error();
Util::Stop();
} }
return 0; return 0;