287 lines
10 KiB
C++
287 lines
10 KiB
C++
#pragma once
|
|
|
|
#include <modules/OS.h>
|
|
|
|
#include <iostream>
|
|
#include <utility>
|
|
#include <cstdlib>
|
|
#include <ostream>
|
|
#include <ranges>
|
|
|
|
namespace LXC::Util
|
|
{
|
|
// Enum to translate to the Win32 code for the colors //
|
|
enum Color
|
|
{
|
|
DEFAULT = 0x07,
|
|
|
|
BLACK = 0x00,
|
|
BLUE = 0x01,
|
|
GREEN = 0x02,
|
|
AQUA = 0x03,
|
|
RED = 0x04,
|
|
PURPLE = 0x05,
|
|
YELLOW = 0x06,
|
|
LIGHT_GRAY = 0x07,
|
|
LIGHT_BLUE = 0x09,
|
|
LIGHT_GREEN = 0x0a,
|
|
LIGHT_AQUA = 0x0b,
|
|
LIGHT_RED = 0x0c,
|
|
LIGHT_PURPLE = 0x0d,
|
|
LIGHT_YELLOW = 0x0e,
|
|
WHITE = 0x0f
|
|
};
|
|
}
|
|
|
|
namespace LXC::Internal
|
|
{
|
|
// Checks if a type can be outputted to std::ostream //
|
|
template<typename T> concept Logable = requires(std::ostream & os, T t)
|
|
{
|
|
// I have no idea what this part does at all //
|
|
{ os << t } -> std::same_as<std::ostream&>;
|
|
};
|
|
|
|
// Checks if a list of types can be outputted to std::ostream //
|
|
template<typename... Args> concept AllLogable = (Logable<Args> && ...);
|
|
|
|
// Checks if the type has a custom log method //
|
|
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>;
|
|
};
|
|
|
|
// Returns a reference to the log file //
|
|
inline std::ofstream& Log()
|
|
{
|
|
static std::ofstream sLog;
|
|
return sLog;
|
|
}
|
|
|
|
#if defined(_WIN32)
|
|
|
|
// Base function for all derived Print() functions //
|
|
template<Util::Color col, bool newLine, typename... Args>
|
|
inline void WriteImpl(std::ostream& os, Args&&... args)
|
|
{
|
|
static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
if constexpr (col != Util::Color::DEFAULT)
|
|
SetConsoleTextAttribute(hConsole, static_cast<WORD>(col));
|
|
|
|
(os << ... << std::forward<Args>(args));
|
|
|
|
if constexpr (newLine)
|
|
os << std::endl;
|
|
|
|
if constexpr (col != Util::Color::DEFAULT)
|
|
SetConsoleTextAttribute(hConsole, static_cast<WORD>(Util::Color::DEFAULT));
|
|
}
|
|
|
|
#elif defined(__unix__)
|
|
|
|
static const char* GetAnsiCode(Util::Color color)
|
|
{
|
|
switch (color)
|
|
{
|
|
case Util::Color::BLACK: return "\x1b[30m";
|
|
case Util::Color::BLUE: return "\x1b[34m";
|
|
case Util::Color::GREEN: return "\x1b[32m";
|
|
case Util::Color::AQUA: return "\x1b[36m";
|
|
case Util::Color::RED: return "\x1b[31m";
|
|
case Util::Color::PURPLE: return "\x1b[35m";
|
|
case Util::Color::YELLOW: return "\x1b[33m";
|
|
case Util::Color::LIGHT_GRAY: return "\x1b[0m";
|
|
case Util::Color::LIGHT_BLUE: return "\x1b[94m";
|
|
case Util::Color::LIGHT_GREEN: return "\x1b[92m";
|
|
case Util::Color::LIGHT_AQUA: return "\x1b[96m";
|
|
case Util::Color::LIGHT_RED: return "\x1b[91m";
|
|
case Util::Color::LIGHT_PURPLE: return "\x1b[95m";
|
|
case Util::Color::LIGHT_YELLOW: return "\x1b[93m";
|
|
case Util::Color::WHITE: return "\x1b[97m";
|
|
default: return "\x1b[0m";
|
|
}
|
|
}
|
|
|
|
template<Util::Color col, bool newLine, typename... Args>
|
|
inline void WriteImpl(std::ostream& os, Args&&... args)
|
|
{
|
|
if constexpr (col != Util::Color::DEFAULT)
|
|
os << GetAnsiCode(col);
|
|
|
|
(os << ... << std::forward<Args>(args));
|
|
|
|
if constexpr (newLine)
|
|
os << std::endl;
|
|
|
|
if constexpr (col != Util::Color::DEFAULT)
|
|
os << GetAnsiCode(Util::Color::DEFAULT);
|
|
}
|
|
|
|
#endif
|
|
}
|
|
|
|
namespace LXC::Util
|
|
{
|
|
// Prints arguments to the console with the given color //
|
|
template<Color col, typename... Args>
|
|
requires Internal::AllLogable<Args...>
|
|
inline void PrintAs(Args&&... args)
|
|
{
|
|
Internal::WriteImpl<col, false>(std::cout, std::forward<Args>(args)...);
|
|
}
|
|
|
|
// Prints arguments to the console //
|
|
template<typename... Args>
|
|
requires Internal::AllLogable<Args...>
|
|
inline void Print(Args&&... args)
|
|
{
|
|
Internal::WriteImpl<Util::Color::DEFAULT, false>(std::cout, std::forward<Args>(args)...);
|
|
}
|
|
|
|
// Prints arguments to the console with a new-line character at the end //
|
|
template<typename... Args>
|
|
requires Internal::AllLogable<Args...> && (sizeof...(Args) > 1)
|
|
inline void PrintLn(Args&&... args)
|
|
{
|
|
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, std::forward<Args>(args)...);
|
|
}
|
|
|
|
// Prints a new line within the console //
|
|
inline void PrintLn()
|
|
{
|
|
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout);
|
|
}
|
|
|
|
// Prints argument to the console with a new line character at the end //
|
|
template<typename T>
|
|
requires (Internal::HasLogStrFunc<T> || Internal::Logable<T>) && (!std::is_pointer_v<T>)
|
|
inline void PrintLn(T&& arg)
|
|
{
|
|
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(), "]");
|
|
}
|
|
|
|
// Prints a container to the console //
|
|
template<typename T, std::ranges::range Container, typename CargoType = std::ranges::range_value_t<Container>>
|
|
requires (Internal::HasLogStrFunc<T> || Internal::Logable<T>) && (!std::is_pointer_v<T>) &&
|
|
(Internal::HasLogStrFunc<CargoType> || Internal::Logable<CargoType>) && (!std::is_pointer_v<CargoType>)
|
|
inline void PrintContainer(T&& containerName, const Container& container)
|
|
{
|
|
// Prints a starting section for the container //
|
|
Internal::WriteImpl<Util::Color::DEFAULT, true>(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<CargoType>)
|
|
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, '\t', item.LogStr());
|
|
|
|
else
|
|
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, '\t', item);
|
|
}
|
|
|
|
// Prints the ending bracket //
|
|
Internal::WriteImpl<Util::Color::DEFAULT, true>(std::cout, '}');
|
|
}
|
|
|
|
// Logs all the arguments to the file (automatically flushes) //
|
|
template<typename... Args>
|
|
requires Internal::AllLogable<Args...> && (sizeof...(Args) > 1)
|
|
inline void Log(Args&&... args)
|
|
{
|
|
std::ofstream& log = Internal::Log();
|
|
if (log.is_open()) _UNLIKELY
|
|
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, R"([LXC] ")", std::forward<Args>(args)..., '"');
|
|
}
|
|
|
|
// 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>) && (!std::is_pointer_v<T>)
|
|
inline void Log(T&& arg)
|
|
{
|
|
std::ofstream& log = Internal::Log();
|
|
if (log.is_open()) _UNLIKELY
|
|
{
|
|
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
|
|
Log(*arg);
|
|
|
|
else
|
|
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, "[LXC] Nullptr to: [", typeid(Raw).name(), ']');
|
|
}
|
|
}
|
|
|
|
// Prints the contents of a container to the log //
|
|
template<typename T, std::ranges::range Container, typename CargoType = std::ranges::range_value_t<Container>>
|
|
requires (Internal::HasLogStrFunc<T> || Internal::Logable<T>) && (!std::is_pointer_v<T>) &&
|
|
(Internal::HasLogStrFunc<CargoType> || Internal::Logable<CargoType>) && (!std::is_pointer_v<CargoType>)
|
|
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<Util::Color::DEFAULT, true>(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<CargoType>)
|
|
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, '\t', item.LogStr());
|
|
|
|
else
|
|
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, '\t', item);
|
|
}
|
|
|
|
// Prints the ending bracket //
|
|
Internal::WriteImpl<Util::Color::DEFAULT, true>(log, '}');
|
|
}
|
|
|
|
// Intitalises the log with the given file name //
|
|
inline void CreateLog(const std::filesystem::path& path)
|
|
{
|
|
// Opens the log file with the given path //
|
|
std::ofstream& log = Internal::Log();
|
|
log.open(path);
|
|
|
|
// Assigns a function to close the log file on program exit //
|
|
std::atexit([]()
|
|
{
|
|
std::ofstream& log = Internal::Log();
|
|
log.close();
|
|
});
|
|
}
|
|
}
|