#include "Logger.hpp" #include #include //#include enum LOG_TYPE{ LOG_TYPE_INFO = 0, LOG_TYPE_WARN, LOG_TYPE_CRIT, LOG_TYPE_DBG }; Logger* Logger::smInstance = nullptr; /* ------------------------------------------------------------------------------------------------------------------------------------------------- */ inline static std::string GetLabelType(const LOG_TYPE logType) { switch (logType) { case LOG_TYPE_DBG: return "DEBUG"; break; case LOG_TYPE_CRIT: return "CRIT"; break; case LOG_TYPE_WARN: return "WARN"; break; case LOG_TYPE_INFO: default: return "INFO"; break; } } inline static void LogConsolePrint(const LOG_TYPE logType, const std::string& message, const double time) { fmt::print("[{:.5f}][{}] {}\n", time, GetLabelType(logType), message); } inline static void LogFilePrint(const LOG_TYPE logType, const std::string& message, const double time) { auto outLogFile = fmt::output_file("log.txt", fmt::file::APPEND | fmt::file::CREATE | fmt::file::WRONLY); outLogFile.print("[{:.5f}][{}] {}\n", time, GetLabelType(logType), message); outLogFile.flush(); outLogFile.close(); } /* ------------------------------------------------------------------------------------------------------------------------------------------------- */ Logger::Logger() { mLogTimer = std::make_unique(); LogConsolePrint(LOG_TYPE_INFO, "Log module ready", mLogTimer->GetElapsedTimeS()); } void Logger::PrintInfo(std::string _in, bool writeToFile) { LogConsolePrint(LOG_TYPE_INFO, _in, mLogTimer->GetElapsedTimeS()); if (writeToFile) LogFilePrint(LOG_TYPE_INFO, _in, mLogTimer->GetElapsedTimeS()); } void Logger::PrintWarning(std::string _in, bool writeToFile) { LogConsolePrint(LOG_TYPE_WARN, _in, mLogTimer->GetElapsedTimeS()); if (writeToFile) LogFilePrint(LOG_TYPE_WARN, _in, mLogTimer->GetElapsedTimeS()); } void Logger::PrintCritical(std::string _in, bool writeToFile) { LogConsolePrint(LOG_TYPE_CRIT, _in, mLogTimer->GetElapsedTimeS()); if (writeToFile) LogFilePrint(LOG_TYPE_CRIT, _in, mLogTimer->GetElapsedTimeS()); } void Logger::PrintDebug(std::string _in, bool writeToFile) { #ifdef DEBUG LogConsolePrint(LOG_TYPE_DBG, _in, mLogTimer->GetElapsedTimeS()); if (writeToFile) LogFilePrint(LOG_TYPE_DBG, _in, mLogTimer->GetElapsedTimeS()); #endif }