Cleaned up logging functions
This commit is contained in:
@@ -3,8 +3,10 @@
|
||||
#include <modules/OS.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
#include <cstdlib>
|
||||
#include <ostream>
|
||||
#include <ranges>
|
||||
|
||||
namespace LXC::Util
|
||||
{
|
||||
@@ -44,7 +46,7 @@ namespace LXC::Internal
|
||||
template<typename... Args> concept AllLogable = (Logable<Args> && ...);
|
||||
|
||||
// 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 //
|
||||
{ obj.LogStr() } -> std::same_as<std::string>;
|
||||
@@ -153,25 +155,28 @@ namespace LXC::Util
|
||||
|
||||
// Prints argument to the console with a new line character at the end //
|
||||
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)
|
||||
{
|
||||
if constexpr (std::is_pointer_v<T>)
|
||||
{
|
||||
if (arg != nullptr) _LIKELY
|
||||
PrintLn(*arg);
|
||||
|
||||
else
|
||||
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, "Nullptr to: [", typeid(std::remove_pointer_t<T>).name(), "]");
|
||||
}
|
||||
|
||||
else if constexpr (Internal::HasLogStrFunc<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
|
||||
PrintLn(*arg);
|
||||
|
||||
else
|
||||
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, "Nullptr to: [", typeid(Raw).name(), "]");
|
||||
}
|
||||
|
||||
// Logs all the arguments to the file (automatically flushes) //
|
||||
template<typename... Args>
|
||||
requires Internal::AllLogable<Args...> && (sizeof...(Args) > 1)
|
||||
@@ -184,22 +189,13 @@ namespace LXC::Util
|
||||
|
||||
// Logs a singular argument to the log, calls Log() if it can on the object //
|
||||
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)
|
||||
{
|
||||
std::ofstream& log = Internal::Log();
|
||||
if (log.is_open()) _UNLIKELY
|
||||
{
|
||||
if constexpr (std::is_pointer_v<T>)
|
||||
{
|
||||
if (arg != nullptr) _LIKELY
|
||||
Log(*arg);
|
||||
|
||||
else
|
||||
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] Nullptr to: [", typeid(std::remove_pointer_t<T>).name(), "]");
|
||||
}
|
||||
|
||||
else if constexpr (Internal::HasLogStrFunc<T>)
|
||||
if constexpr (Internal::HasLogStrFunc<T>)
|
||||
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] ", '{', arg.LogStr(), '}');
|
||||
|
||||
else
|
||||
@@ -207,6 +203,22 @@ namespace LXC::Util
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
Log(*arg);
|
||||
|
||||
else
|
||||
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] Nullptr to: [", typeid(Raw).name(), ']');
|
||||
}
|
||||
}
|
||||
|
||||
// Intitalises the log with the given file name //
|
||||
inline void CreateLog(const std::filesystem::path& path)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user