From e6ba0249a89ac582f665cd1f47aed126eb108ce4 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Sun, 3 Aug 2025 19:16:54 +0100 Subject: [PATCH] Added container logging --- Common/modules/IO.h | 50 +++++++++++++++++++++++++++++++++++++++++ Common/modules/Result.h | 2 +- LXC/LXC.cpp | 8 ++----- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/Common/modules/IO.h b/Common/modules/IO.h index d7214ab..3db3953 100644 --- a/Common/modules/IO.h +++ b/Common/modules/IO.h @@ -177,6 +177,29 @@ namespace LXC::Util Internal::WriteImpl(std::cout, "Nullptr to: [", typeid(Raw).name(), "]"); } + // Prints a container to the console // + template> + requires (Internal::HasLogStrFunc || Internal::Logable) && (!std::is_pointer_v) && + (Internal::HasLogStrFunc || Internal::Logable) && (!std::is_pointer_v) + inline void PrintContainer(T&& containerName, const Container& container) + { + // Prints a starting section for the container // + Internal::WriteImpl(std::cout, "[LXC] \"", containerName, "\":\n{"); + + // Iterates over each of the items in the container and prints them // + for (const auto& item : container) + { + if constexpr (Internal::HasLogStrFunc) + Internal::WriteImpl(std::cout, '\t', item.LogStr()); + + else + Internal::WriteImpl(std::cout, '\t', item); + } + + // Prints the ending bracket // + Internal::WriteImpl(std::cout, '}'); + } + // Logs all the arguments to the file (automatically flushes) // template requires Internal::AllLogable && (sizeof...(Args) > 1) @@ -219,6 +242,33 @@ namespace LXC::Util } } + // Prints the contents of a container to the log // + template> + requires (Internal::HasLogStrFunc || Internal::Logable) && (!std::is_pointer_v) && + (Internal::HasLogStrFunc || Internal::Logable) && (!std::is_pointer_v) + inline void LogContainer(T&& containerName, const Container& container) + { + // Gets the log if it is open // + std::ofstream& log = Internal::Log(); + if (!log.is_open()) _LIKELY { return; } + + // Prints a starting section for the container // + Internal::WriteImpl(log, "[LXC] \"", containerName, "\":\n{"); + + // Iterates over each of the items in the container and prints them // + for (const auto& item : container) + { + if constexpr (Internal::HasLogStrFunc) + Internal::WriteImpl(log, '\t', item.LogStr()); + + else + Internal::WriteImpl(log, '\t', item); + } + + // Prints the ending bracket // + Internal::WriteImpl(log, '}'); + } + // Intitalises the log with the given file name // inline void CreateLog(const std::filesystem::path& path) { diff --git a/Common/modules/Result.h b/Common/modules/Result.h index e55af12..7d264b6 100644 --- a/Common/modules/Result.h +++ b/Common/modules/Result.h @@ -105,7 +105,7 @@ namespace LXC::Util // Operator overloads // explicit operator bool() const { return !m_FunctionFailed; } - operator ResultType() { return Result(); } + operator ResultType&() { return Result(); } private: // Union to hold either the result or the error // diff --git a/LXC/LXC.cpp b/LXC/LXC.cpp index 1ca1ebe..f93b139 100644 --- a/LXC/LXC.cpp +++ b/LXC/LXC.cpp @@ -60,13 +60,9 @@ int main(int argc, char** argv) } else { - // Prints all of the tokensto the log // - for (const auto& token : tokens.Result()) - { - Util::Log(token); - } - + // Prints all of the tokens to the log // Lexer::LexerOutput lexerOutput = tokens.Result(); + Util::LogContainer("Lexer output", lexerOutput); } // Turns the tokens into into an abstract syntax tree //