From 094101ffdd51b208a3e17ef6522594932b1bb6bd Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Thu, 8 May 2025 14:32:33 +0100 Subject: [PATCH] Improved logging capabilities --- Common/inc/Logger.h | 22 +++++++++++++++++++--- Common/src/Logger.cpp | 9 +++++++-- IR-Generator/src/Generator.cpp | 2 +- Lexer/src/Lexer.cpp | 2 +- Parser/src/AST/AST-Loggers.cpp | 22 +++++++++++----------- README.md | 8 +------- 6 files changed, 40 insertions(+), 25 deletions(-) diff --git a/Common/inc/Logger.h b/Common/inc/Logger.h index 581b4e9..2193231 100644 --- a/Common/inc/Logger.h +++ b/Common/inc/Logger.h @@ -18,13 +18,26 @@ namespace LX NONE }; + // Different priorities in the logger // + enum class Priority + { + HIGH, // Shows in all logs + LOW // Default + }; + // Variadic template to allow an undefined ammount of arguments // - template + template requires AllLogable // <- Checks all types can be outputted to the console // Logs information (if the log is initalised) // static void out(Args... args) { + // Returns if not high enough priority // + if constexpr (priority == Priority::LOW) + { + RETURN_IF(s_Priority == Priority::HIGH); + } + // Prints out the args ending with a new line unless specified // if constexpr (format == Format::AUTO) { ((*s_LogFile << ... << args) << "\n"); } @@ -48,11 +61,14 @@ namespace LX *s_LogFile << '\n' << BREAK << '\n'; } - // Initalises the log (Called by DllMain) // - static void Init(); + // Initalises the log // + static void Init(Priority _default); private: // Keeps the pointer hidden to stop accidental changes // static std::ofstream* s_LogFile; + + // The current priority of the log output // + static Priority s_Priority; }; } diff --git a/Common/src/Logger.cpp b/Common/src/Logger.cpp index 8dc18c8..d01d985 100644 --- a/Common/src/Logger.cpp +++ b/Common/src/Logger.cpp @@ -3,9 +3,11 @@ namespace LX { // Allocates memory for the log file pointer // - std::ofstream* Log::s_LogFile = nullptr; - void Log::Init() + std::ofstream* Log::s_LogFile = nullptr; + Log::Priority Log::s_Priority; + + void Log::Init(Priority _default) { // The actual object holding the log file // // Has to be done like this to stop MSVC complaining // @@ -14,5 +16,8 @@ namespace LX // Opens the log file and assigns it to the log pointer // actualLog.open("log"); s_LogFile = &actualLog; + + // Assigns the priority // + s_Priority = _default; } } diff --git a/IR-Generator/src/Generator.cpp b/IR-Generator/src/Generator.cpp index e9172c1..6e57112 100644 --- a/IR-Generator/src/Generator.cpp +++ b/IR-Generator/src/Generator.cpp @@ -8,7 +8,7 @@ extern "C" int __declspec(dllexport) GenIR(const char* a_inpPath, const char* a_ try { // Initalises the log // - LX::Log::Init(); + LX::Log::Init(LX::Log::Priority::HIGH); // Turns the file paths into the C++ type for handling them // std::filesystem::path inpPath = a_inpPath; diff --git a/Lexer/src/Lexer.cpp b/Lexer/src/Lexer.cpp index d4e1f8b..59356e3 100644 --- a/Lexer/src/Lexer.cpp +++ b/Lexer/src/Lexer.cpp @@ -247,7 +247,7 @@ namespace LX for (auto& token : tokens) { - Log::out + Log::out ( std::left, "{ Line: ", std::setw(3), token.line, diff --git a/Parser/src/AST/AST-Loggers.cpp b/Parser/src/AST/AST-Loggers.cpp index 5c76688..f4a2222 100644 --- a/Parser/src/AST/AST-Loggers.cpp +++ b/Parser/src/AST/AST-Loggers.cpp @@ -11,23 +11,23 @@ namespace LX::AST void NumberLiteral::Log(unsigned depth) { - Log::out(std::string(depth, '\t'), "Number: ", m_Number); + Log::out(std::string(depth, '\t'), "Number: ", m_Number); } void Operation::Log(unsigned depth) { - Log::out(std::string(depth, '\t'), "Operation {", ToString(m_Operand), "}:"); + Log::out(std::string(depth, '\t'), "Operation {", ToString(m_Operand), "}:"); - Log::out(std::string(depth + 1, '\t'), "LHS:"); + Log::out(std::string(depth + 1, '\t'), "LHS:"); m_Lhs->Log(depth + 2); - Log::out(std::string(depth + 1, '\t'), "RHS:"); + Log::out(std::string(depth + 1, '\t'), "RHS:"); m_Rhs->Log(depth + 2); } void ReturnStatement::Log(unsigned depth) { - Log::out(std::string(depth, '\t'), "Return"); + Log::out(std::string(depth, '\t'), "Return"); if (m_Val != nullptr) { @@ -37,26 +37,26 @@ namespace LX::AST else { - Log::out('\n'); + Log::out('\n'); } } void VariableDeclaration::Log(unsigned depth) { - Log::out(std::string(depth, '\t'), "Variable declaration: ", m_Name); + Log::out(std::string(depth, '\t'), "Variable declaration: ", m_Name); } void VariableAssignment::Log(unsigned depth) { - Log::out(std::string(depth, '\t'), "Variable assignment:"); + Log::out(std::string(depth, '\t'), "Variable assignment:"); - Log::out(std::string(depth + 1, '\t'), "To: ", m_Name); - Log::out(std::string(depth + 1, '\t'), "Value:"); + Log::out(std::string(depth + 1, '\t'), "To: ", m_Name); + Log::out(std::string(depth + 1, '\t'), "Value:"); m_Value->Log(depth + 2); } void VariableAccess::Log(unsigned depth) { - Log::out(std::string(depth, '\t'), "Variable: ", m_Name); + Log::out(std::string(depth, '\t'), "Variable: ", m_Name); } } diff --git a/README.md b/README.md index e9b50fa..59ab556 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,7 @@ This is my custom compiled language written in C++ based off of the LLVM toolcha - Structs / Classes (Polymorphism + vtables) ### Codebase -- Errors - - All simple errors (no members) use the same type -- Logging - - Choose what is logged (if any) -- Refactor - - Parser needs folders - - Better .h files +- New features ### Stuff I want to do later (unordered) - I/O manager (Console, Files)