Added proper logging capabilities
This commit is contained in:
85
Common/IO.h
85
Common/IO.h
@@ -3,8 +3,20 @@
|
||||
#include <OS.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
#include <ostream>
|
||||
|
||||
namespace LXC::Internal
|
||||
{
|
||||
// Returns a reference to the log file //
|
||||
// To be used only by LXC components //
|
||||
inline std::ofstream& Log()
|
||||
{
|
||||
static std::ofstream sLog;
|
||||
return sLog;
|
||||
}
|
||||
}
|
||||
|
||||
namespace LXC::Util
|
||||
{
|
||||
// Checks if a type can be outputted to std::ostream //
|
||||
@@ -17,6 +29,13 @@ namespace LXC::Util
|
||||
// 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>;
|
||||
};
|
||||
|
||||
// Enum to translate to the Win32 code for the colors //
|
||||
enum Color : WORD
|
||||
{
|
||||
@@ -69,4 +88,70 @@ namespace LXC::Util
|
||||
(std::cout << ... << args);
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// 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();
|
||||
});
|
||||
}
|
||||
|
||||
// Logs all the arguments to the file (automatically flushes) //
|
||||
template<typename... Args>
|
||||
requires AllLogable<Args...> && (sizeof...(Args) > 1)
|
||||
inline void Log(Args... args)
|
||||
{
|
||||
std::ofstream& log = Internal::Log();
|
||||
|
||||
// Only attempts to write to an open log //
|
||||
if (log.is_open()) _UNLIKELY
|
||||
{
|
||||
// Opening symbol to make the log look fancy //
|
||||
log << "[LXC-M] \"";
|
||||
|
||||
// Fowards the arguments to the log and flushes //
|
||||
(log << ... << args);
|
||||
log << "\"\n";
|
||||
log.flush();
|
||||
}
|
||||
}
|
||||
|
||||
// Logs a singular argument to the log, calls Log() if it can on the object //
|
||||
template<typename T>
|
||||
requires HasLogStrFunc<T> || Logable<T>
|
||||
inline void Log(T arg)
|
||||
{
|
||||
std::ofstream& log = Internal::Log();
|
||||
|
||||
// Only attempts to write to an open log //
|
||||
if (log.is_open()) _UNLIKELY
|
||||
{
|
||||
// Opening symbol to make log look fancy //
|
||||
log << "[LXC-S] ";
|
||||
|
||||
// Logic if it can call LogStr() //
|
||||
if constexpr (HasLogStrFunc<T>)
|
||||
{
|
||||
// Prints the generated string //
|
||||
log << '{' << arg.LogStr() << "}\n";
|
||||
log.flush();
|
||||
}
|
||||
|
||||
// Default logic //
|
||||
else
|
||||
{
|
||||
// Prints the singular arg //
|
||||
log << '"' << arg << "\"\n";
|
||||
log.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user