#pragma once #include #include #include #include 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 // template concept Logable = requires(std::ostream& os, T t) { // I have no idea what this part does at all // { os << t } -> std::same_as; }; // Checks if a list of types can be outputted to std::ostream // template concept AllLogable = (Logable && ...); // Checks if the type has a custom log method // template concept HasLogStrFunc = requires(T obj) { // Checks the type has a function LogStr that returns a string // { obj.LogStr() } -> std::same_as; }; // Enum to translate to the Win32 code for the colors // enum Color : WORD { 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 }; // Prints arguments to the console with the given color // template requires AllLogable inline void PrintAs(Args... args) { // Permenant handle to the console // static HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Prints it to the console and resets the color // SetConsoleTextAttribute(hConsole, (WORD)col); (std::cout << ... << args); SetConsoleTextAttribute(hConsole, (WORD)Color::LIGHT_GRAY); } // Prints arguments to the console // template requires AllLogable inline void Print(Args... args) { // Fowards the arguments to the console // (std::cout << ... << args); } // Prints arguments to the console with a new-line character at the end // template requires AllLogable inline void PrintLn(Args... args) { // Fowards the arguments to the console // (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 requires AllLogable && (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 requires HasLogStrFunc || Logable 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) { // Prints the generated string // log << '{' << arg.LogStr() << "}\n"; log.flush(); } // Default logic // else { // Prints the singular arg // log << '"' << arg << "\"\n"; log.flush(); } } } }