#ifndef CHIP8_LOGGER_H #define CHIP8_LOGGER_H #include #include #include "date.h" /** * The LogType of a log */ enum LogType { INFO, DEBUG, WARN, ERROR }; /** * The logger class allows logging to either the output stream or the error stream */ class Logger { private: /** * Converts the LogType enum to a string to display it in the log * @param logType the LogType enum to convert * @return the LogType as a string */ static const char* logTypeToString(LogType logType) { switch (logType) { case LogType::INFO: return "INFO"; case LogType::DEBUG: return "DEBUG"; case LogType::WARN: return "WARN"; case LogType::ERROR: return "ERROR"; } return ""; } public: /** * Writes log to either the 'cout' or 'cerr' streams * * @tparam Args Variadic template representing any number and type of arguments that * @param logType The type of the log. If logType is LogType::ERROR, output to 'cerr' instead of 'cout' * @param args The contents of the log, this can be any number of arguments */ template static void log(LogType logType, Args&&... args) { std::ostream& stream = (logType == LogType::ERROR) ? std::cerr : std::cout; stream << "[" << logTypeToString(logType) << "] [" << Date::now().to_string() << "]"; ((stream << " " << args), ...); stream << std::endl; } /** * Writes an info log to the cout stream * * @tparam Args @see self::log * @param args @see self::log */ template static void info(Args&&... args) { log(LogType::INFO, std::forward(args)...); } /** * Writes an debug log to the cout stream * * @tparam Args @see self::log * @param args @see self::log */ template static void debug(Args&&... args) { log(LogType::DEBUG, std::forward(args)...); } /** * Writes an warn log to the cout stream * * @tparam Args @see self::log * @param args @see self::log */ template static void warn(Args&&... args) { log(LogType::WARN, std::forward(args)...); } /** * Writes an error log to the cerr stream * * @tparam Args @see self::log * @param args @see self::log */ template static void error(Args&&... args) { log(LogType::ERROR, std::forward(args)...); } }; #endif //CHIP8_LOGGER_H