Cleaned up logging functions

This commit is contained in:
Pasha Bibko
2025-08-03 17:03:39 +01:00
parent 611c5012a7
commit 1411839e38
4 changed files with 39 additions and 24 deletions

View File

@@ -19,6 +19,7 @@
#include <cstring> #include <cstring>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <ranges>
#include <array> #include <array>
#include <span> #include <span>

View File

@@ -3,8 +3,10 @@
#include <modules/OS.h> #include <modules/OS.h>
#include <iostream> #include <iostream>
#include <utility>
#include <cstdlib> #include <cstdlib>
#include <ostream> #include <ostream>
#include <ranges>
namespace LXC::Util namespace LXC::Util
{ {
@@ -153,23 +155,26 @@ namespace LXC::Util
// Prints argument to the console with a new line character at the end // // Prints argument to the console with a new line character at the end //
template<typename T> template<typename T>
requires Internal::HasLogStrFunc<T> || Internal::Logable<T> requires (Internal::HasLogStrFunc<T> || Internal::Logable<T>) && (!std::is_pointer_v<T>)
inline void PrintLn(T&& arg) inline void PrintLn(T&& arg)
{ {
if constexpr (std::is_pointer_v<T>) if constexpr (Internal::HasLogStrFunc<T>)
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, arg.LogStr());
else
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, arg);
}
// Prints the value of a pointer to the console, prints an error if null //
template<typename T, typename Raw = std::remove_pointer_t<T>>
requires (Internal::HasLogStrFunc<Raw> || Internal::Logable<Raw>) && std::is_pointer_v<T>
inline void PrintLn(T&& arg)
{ {
if (arg != nullptr) _LIKELY if (arg != nullptr) _LIKELY
PrintLn(*arg); PrintLn(*arg);
else else
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, "Nullptr to: [", typeid(std::remove_pointer_t<T>).name(), "]"); Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, "Nullptr to: [", typeid(Raw).name(), "]");
}
else if constexpr (Internal::HasLogStrFunc<T>)
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, arg.LogStr());
else
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, arg);
} }
// Logs all the arguments to the file (automatically flushes) // // Logs all the arguments to the file (automatically flushes) //
@@ -184,26 +189,33 @@ namespace LXC::Util
// Logs a singular argument to the log, calls Log() if it can on the object // // Logs a singular argument to the log, calls Log() if it can on the object //
template<typename T> template<typename T>
requires Internal::HasLogStrFunc<T> || Internal::Logable<T> requires (Internal::HasLogStrFunc<T> || Internal::Logable<T>) && (!std::is_pointer_v<T>)
inline void Log(T&& arg) inline void Log(T&& arg)
{ {
std::ofstream& log = Internal::Log(); std::ofstream& log = Internal::Log();
if (log.is_open()) _UNLIKELY if (log.is_open()) _UNLIKELY
{ {
if constexpr (std::is_pointer_v<T>) if constexpr (Internal::HasLogStrFunc<T>)
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] ", '{', arg.LogStr(), '}');
else
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] ", '"', arg, '"');
}
}
// Prints the value of a pointer to the log, prints an error to the log if null //
template<typename T, typename Raw = std::remove_pointer_t<T>>
requires (Internal::HasLogStrFunc<Raw> || Internal::Logable<Raw>) && std::is_pointer_v<T>
inline void Log(T&& arg)
{
std::ofstream& log = Internal::Log();
if (log.is_open()) _UNLIKELY
{ {
if (arg != nullptr) _LIKELY if (arg != nullptr) _LIKELY
Log(*arg); Log(*arg);
else else
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] Nullptr to: [", typeid(std::remove_pointer_t<T>).name(), "]"); Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] Nullptr to: [", typeid(Raw).name(), ']');
}
else if constexpr (Internal::HasLogStrFunc<T>)
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] ", '{', arg.LogStr(), '}');
else
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] ", '"', arg, '"');
} }
} }

View File

@@ -104,7 +104,7 @@ namespace LXC::Util
// Operator overloads // // Operator overloads //
operator bool() const { return !m_FunctionFailed; } explicit operator bool() const { return !m_FunctionFailed; }
operator ResultType() { return Result(); } operator ResultType() { return Result(); }
private: private:

View File

@@ -65,6 +65,8 @@ int main(int argc, char** argv)
{ {
Util::Log(token); Util::Log(token);
} }
Lexer::LexerOutput lexerOutput = tokens.Result();
} }
// Turns the tokens into into an abstract syntax tree // // Turns the tokens into into an abstract syntax tree //