Chip8-CPP/include/logger.h

104 lines
2.6 KiB
C
Raw Normal View History

2023-09-09 17:57:57 +00:00
#ifndef CHIP8_LOGGER_H
#define CHIP8_LOGGER_H
#include <iostream>
#include <string>
#include "date.h"
/**
* The LogType of a log
*/
2023-09-09 17:57:57 +00:00
enum LogType {
INFO,
DEBUG,
WARN,
ERROR
};
/**
* The logger class allows logging to either the output stream or the error stream
*/
2023-09-09 17:57:57 +00:00
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
*/
2023-09-09 17:57:57 +00:00
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
*/
2023-09-09 17:57:57 +00:00
template<typename... Args>
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
*/
2023-09-09 17:57:57 +00:00
template<typename... Args>
static void info(Args&&... args) {
log(LogType::INFO, std::forward<Args>(args)...);
}
/**
* Writes an debug log to the cout stream
*
* @tparam Args @see self::log
* @param args @see self::log
*/
2023-09-09 17:57:57 +00:00
template<typename... Args>
static void debug(Args&&... args) {
log(LogType::DEBUG, std::forward<Args>(args)...);
}
/**
* Writes an warn log to the cout stream
*
* @tparam Args @see self::log
* @param args @see self::log
*/
2023-09-09 17:57:57 +00:00
template<typename... Args>
static void warn(Args&&... args) {
log(LogType::WARN, std::forward<Args>(args)...);
}
/**
* Writes an error log to the cerr stream
*
* @tparam Args @see self::log
* @param args @see self::log
*/
2023-09-09 17:57:57 +00:00
template<typename... Args>
static void error(Args&&... args) {
log(LogType::ERROR, std::forward<Args>(args)...);
}
};
#endif //CHIP8_LOGGER_H