diff --git a/Engine/Misc/Logger.cpp b/Engine/Misc/Logger.cpp index e69de29..9a1594a 100644 --- a/Engine/Misc/Logger.cpp +++ b/Engine/Misc/Logger.cpp @@ -0,0 +1,58 @@ +#include "Logger.hpp" + +#include + + +enum LOG_TYPE{ + LOG_TYPE_INFO = 0, + LOG_TYPE_WARN, + LOG_TYPE_CRIT, + LOG_TYPE_DBG +}; + +Logger* Logger::smInstance = nullptr; + + +/* ------------------------------------------------------------------------------------------------------------------------------------------------- */ + +static void LogConsolePrint(const LOG_TYPE logType, const std::string& message, const double time) { + std::cout << std::fixed << std::setprecision(6) << '[' << time; + + switch (logType) { + case LOG_TYPE_DBG: + std::cout << "][DEBUG] "; + break; + case LOG_TYPE_CRIT: + std::cout << "][CRIT] "; + break; + case LOG_TYPE_WARN: + std::cout << "][WARN] "; + break; + case LOG_TYPE_INFO: + default: + std::cout << "][INFO] "; + break; + } + + std::cout << message << std::endl; +} + + +/* ------------------------------------------------------------------------------------------------------------------------------------------------- */ + +Logger::Logger() { + mLogTimer = std::make_unique(); + PrintInfo("Log module ready"); +} + +void Logger::PrintInfo(std::string _in) { LogConsolePrint(LOG_TYPE_INFO, _in, mLogTimer->GetElapsedTimeS()); } + +void Logger::PrintWarning(std::string _in) { LogConsolePrint(LOG_TYPE_WARN, _in, mLogTimer->GetElapsedTimeS()); } + +void Logger::PrintCritical(std::string _in) { LogConsolePrint(LOG_TYPE_CRIT, _in, mLogTimer->GetElapsedTimeS()); } + +void Logger::PrintDebug(std::string _in) { +#ifdef _DEBUG + LogConsolePrint(LOG_TYPE_DBG, _in, mLogTimer->GetElapsedTimeS()); +#endif +} \ No newline at end of file diff --git a/Engine/Misc/Logger.hpp b/Engine/Misc/Logger.hpp index e69de29..cb3589e 100644 --- a/Engine/Misc/Logger.hpp +++ b/Engine/Misc/Logger.hpp @@ -0,0 +1,34 @@ +#pragma once + +#include "../Utils/Timers.hpp" + + +//TODO: Should be upgraded using FMT lib +class Logger final { +public: + static Logger& getInstance() { + if (smInstance == nullptr) + smInstance = new Logger(); + + return *smInstance; + } + ~Logger() = default; + + Logger(Logger&&) = default; + Logger& operator= (Logger&&) = default; + Logger(Logger const&) = delete; + Logger& operator= (Logger const&) = delete; + + void PrintInfo(std::string inPrt); + void PrintWarning(std::string inPrt); + void PrintCritical(std::string inPrt); + void PrintDebug(std::string inPrt); + +private: + Logger() noexcept(false); + static Logger* smInstance; + std::unique_ptr mLogTimer = nullptr; + +}; + +#define LOGGER_MSG_FT(x) std::string(__FUNCTION__) + std::string("@") + std::to_string(__LINE__) + std::string(": ") + std::string(x) \ No newline at end of file diff --git a/ProtoTank.cpp b/ProtoTank.cpp index a392665..02ca9f5 100644 --- a/ProtoTank.cpp +++ b/ProtoTank.cpp @@ -1,15 +1,14 @@ -#include - +#include "Engine/Misc/Logger.hpp" #include "Game.hpp" int main(int argc, char** argv) { SysTimer mSysTimer; - std::cout << std::fixed << std::setprecision(6); + Logger& log = Logger::getInstance(); GAME_STATUS status = GAME_INIT; - std::cout << "[" << mSysTimer.GetElapsedTimeS() << "] Init game instance" << std::endl; + log.PrintInfo(LOGGER_MSG_FT("Init game instance")); Game& arcadeGame = Game::getInstance(true); for ( ;; ) { @@ -19,6 +18,6 @@ int main(int argc, char** argv) { break; } - std::cout << "[" << mSysTimer.GetElapsedTimeS() << "] Bye bye!" << std::endl; + log.PrintInfo(LOGGER_MSG_FT("Bye bye!")); return EXIT_SUCCESS; }