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 <memory>
#include <vector>
#include <ranges>
#include <array>
#include <span>

View File

@@ -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)
{

View File

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