Cleaned up logging functions
This commit is contained in:
@@ -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>
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
{
|
{
|
||||||
@@ -44,7 +46,7 @@ namespace LXC::Internal
|
|||||||
template<typename... Args> concept AllLogable = (Logable<Args> && ...);
|
template<typename... Args> concept AllLogable = (Logable<Args> && ...);
|
||||||
|
|
||||||
// Checks if the type has a custom log method //
|
// Checks if the type has a custom log method //
|
||||||
template <typename T> concept HasLogStrFunc = requires(T obj)
|
template<typename T> concept HasLogStrFunc = requires(T obj)
|
||||||
{
|
{
|
||||||
// Checks the type has a function LogStr that returns a string //
|
// Checks the type has a function LogStr that returns a string //
|
||||||
{ obj.LogStr() } -> std::same_as<std::string>;
|
{ obj.LogStr() } -> std::same_as<std::string>;
|
||||||
@@ -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, '"');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
@@ -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 //
|
||||||
|
|||||||
Reference in New Issue
Block a user