SimpleEngine/include/simpleengine/log/logger.h

385 lines
11 KiB
C
Raw Permalink Normal View History

2022-11-11 05:44:41 +00:00
#pragma once
2022-10-28 22:49:29 +00:00
#include <memory>
#include <string>
#include <unordered_map>
#include <spdlog/logger.h>
#include <spdlog/spdlog.h>
#include <spdlog/common.h>
#include <spdlog/sinks/stdout_color_sinks.h>
namespace simpleengine::log {
class Logger {
private:
std::shared_ptr<spdlog::logger> inner;
public:
2022-10-29 20:20:28 +00:00
/**
* @brief Construct a new Logger object from a name
*
2022-11-11 22:58:10 +00:00
* This will first check if the spdlog logger exists, if it does not it will create a new `color_mt` logger
2022-10-29 20:20:28 +00:00
*
* @param name The name of the logger.
*/
2022-10-28 22:49:29 +00:00
Logger(std::string name) {
inner = spdlog::get(name);
if (!inner) {
inner = spdlog::stderr_color_mt(name);
}
}
2022-10-29 20:20:28 +00:00
/**
* @brief Construct a new Logger object using a spdlog::logger
*
* @param logger The inner spdlog::logger that this object will use
*/
2022-10-28 22:49:29 +00:00
Logger(std::shared_ptr<spdlog::logger> logger) : inner(logger) {
}
2022-10-29 20:20:28 +00:00
/**
* @brief Get the inner spdlog logger object
*
* @return std::shared_ptr<spdlog::logger>
*/
2022-10-28 22:49:29 +00:00
std::shared_ptr<spdlog::logger> get_inner() {
return inner;
}
2022-10-29 20:20:28 +00:00
/**
* @brief Set the log level.
*
* @param lvl The level to set the log to
*/
2022-10-28 22:49:29 +00:00
void set_level(spdlog::level::level_enum lvl) {
inner->set_level(lvl);
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log a message.
*
* @param lvl Level to log at
* @param msg The message to log
*/
2022-10-28 22:49:29 +00:00
void log(spdlog::level::level_enum lvl, spdlog::string_view_t msg) {
2022-11-11 22:58:10 +00:00
inner->log(lvl, msg);
2022-10-28 22:49:29 +00:00
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log a formatted message.
*
* @tparam Args
* @param lvl Level to log at
* @param fmt The string that will be formatted
* @param args The arguments to format into the message.
*/
template<typename... Args>
void log(spdlog::level::level_enum lvl, fmt::format_string<Args...> fmt, Args &&...args)
{
2022-11-11 22:58:10 +00:00
inner->log(lvl, fmt, std::forward<Args>(args)...);
2022-10-29 20:20:28 +00:00
}
/**
* @brief Log a trace message.
*
* @tparam T
* @param msg The message that will be logged.
*/
2022-10-28 22:49:29 +00:00
template<typename T>
void trace(const T &msg) {
log(spdlog::level::trace, msg);
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log a debug message.
*
* @tparam T
* @param msg The message that will be logged.
*/
2022-10-28 22:49:29 +00:00
template<typename T>
void debug(const T &msg) {
log(spdlog::level::debug, msg);
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log an info message.
*
* @tparam T
* @param msg The message that will be logged.
*/
2022-10-28 22:49:29 +00:00
template<typename T>
void info(const T &msg) {
log(spdlog::level::info, msg);
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log a warning message.
*
* @tparam T
* @param msg The message that will be logged.
*/
2022-10-28 22:49:29 +00:00
template<typename T>
void warn(const T &msg) {
log(spdlog::level::warn, msg);
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log an error message.
*
* @tparam T
* @param msg The message that will be logged.
*/
2022-10-28 22:49:29 +00:00
template<typename T>
void error(const T &msg) {
log(spdlog::level::err, msg);
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log a critical message.
*
* @tparam T
* @param msg The message that will be logged.
*/
2022-10-28 22:49:29 +00:00
template<typename T>
void critical(const T &msg) {
log(spdlog::level::critical, msg);
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log a trace message.
*
* @tparam Args
* @param fmt String that will be formatted
* @param args Arguments to format into the message
*/
2022-10-28 22:49:29 +00:00
template<typename... Args>
2022-10-29 20:20:28 +00:00
void trace(fmt::format_string<Args...> fmt, Args &&... args) {
2022-10-28 22:49:29 +00:00
log(spdlog::level::trace, fmt, std::forward<Args>(args)...);
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log a debug message.
*
* @tparam Args
* @param fmt String that will be formatted
* @param args Arguments to format into the message
*/
2022-10-28 22:49:29 +00:00
template<typename... Args>
2022-10-29 20:20:28 +00:00
void debug(fmt::format_string<Args...> fmt, Args &&... args) {
2022-10-28 22:49:29 +00:00
log(spdlog::level::debug, fmt, std::forward<Args>(args)...);
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log an info message.
*
* @tparam Args
* @param fmt String that will be formatted
* @param args Arguments to format into the message
*/
2022-10-28 22:49:29 +00:00
template<typename... Args>
2022-10-29 20:20:28 +00:00
void info(fmt::format_string<Args...> fmt, Args &&... args) {
2022-10-28 22:49:29 +00:00
log(spdlog::level::info, fmt, std::forward<Args>(args)...);
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log a warning message.
*
* @tparam Args
* @param fmt String that will be formatted
* @param args Arguments to format into the message
*/
2022-10-28 22:49:29 +00:00
template<typename... Args>
2022-10-29 20:20:28 +00:00
void warn(fmt::format_string<Args...> fmt, Args &&... args) {
2022-10-28 22:49:29 +00:00
log(spdlog::level::warn, fmt, std::forward<Args>(args)...);
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log an error message.
*
* @tparam Args
* @param fmt String that will be formatted
* @param args Arguments to format into the message
*/
2022-10-28 22:49:29 +00:00
template<typename... Args>
2022-10-29 20:20:28 +00:00
void error(fmt::format_string<Args...> fmt, Args &&... args) {
2022-10-28 22:49:29 +00:00
log(spdlog::level::err, fmt, std::forward<Args>(args)...);
}
2022-10-29 20:20:28 +00:00
/**
* @brief Log a critical message.
*
* @tparam Args
* @param fmt String that will be formatted
* @param args Arguments to format into the message
*/
2022-10-28 22:49:29 +00:00
template<typename... Args>
2022-10-29 20:20:28 +00:00
void critical(fmt::format_string<Args...> fmt, Args &&... args) {
2022-10-28 22:49:29 +00:00
log(spdlog::level::critical, fmt, std::forward<Args>(args)...);
}
};
class LoggerManager {
private:
2022-11-11 22:58:10 +00:00
static Logger core_logger;
2022-10-28 22:49:29 +00:00
public:
2022-10-29 20:20:28 +00:00
/**
* @brief Initialize the logger.
*
*/
2022-10-28 22:49:29 +00:00
static void init();
2022-10-29 20:20:28 +00:00
/**
* @brief Set the core log level.
*
* @param lvl
*/
2022-10-28 22:49:29 +00:00
static void set_level(spdlog::level::level_enum lvl);
2022-10-29 20:20:28 +00:00
/**
* @brief Get the core logger.
*
2022-11-11 22:58:10 +00:00
* @return Logger&
2022-10-29 20:20:28 +00:00
*/
2022-11-11 22:58:10 +00:00
static Logger& get_core_logger();
2022-10-28 22:49:29 +00:00
2022-10-29 20:20:28 +00:00
/**
* @brief Create a new logger.
*
* @param name The name of the logger
* @return Logger
*/
2022-10-28 22:49:29 +00:00
static Logger create_logger(std::string name);
2022-10-29 20:20:28 +00:00
/**
* @brief Get a logger using its name.
*
* @param name The name of the logger
* @return Logger
*/
2022-10-28 22:49:29 +00:00
static Logger get_logger(std::string name);
};
2022-10-29 20:20:28 +00:00
}
// Log macros
/**
* @brief Log to a logger.
*
* @param logger_name The name of the logger to log to.
* @param level The level to log at.
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
#define SE_LOG(logger_name, level, message, ...) simpleengine::log::LoggerManager::get_logger(logger_name).log(level, fmt::format(message, ##__VA_ARGS__));
/**
* @brief Log to the core logger.
*
* @param level The level to log at.
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
2022-11-11 22:58:10 +00:00
#define SE_CLOG(level, message, ...) simpleengine::log::LoggerManager::get_core_logger().log(level, fmt::format(message, ##__VA_ARGS__));
2022-10-29 20:20:28 +00:00
/**
* @brief Log a critical message to a logger.
*
* @param logger_name The name of the logger to log to.
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
#define SE_CRIT(logger_name, message, ...) SE_LOG(logger_name, spdlog::level::critical, message, ##__VA_ARGS__);
/**
* @brief Log a critical message to the core logger.
*
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
#define SE_CCRIT(message, ...) SE_CLOG(spdlog::level::critical, message, ##__VA_ARGS__);
/**
* @brief Log an error message to a logger.
*
* @param logger_name The name of the logger to log to.
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
2022-11-11 05:44:41 +00:00
#define SE_ERROR(logger_name, message, ...) SE_LOG(logger_name, spdlog::level::err, message, ##__VA_ARGS__);
2022-10-29 20:20:28 +00:00
/**
* @brief Log an error message to the core logger.
*
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
2022-11-11 05:44:41 +00:00
#define SE_CERROR(message, ...) SE_CLOG(spdlog::level::err, message, ##__VA_ARGS__);
2022-10-29 20:20:28 +00:00
/**
* @brief Log a warning message to a logger.
*
* @param logger_name The name of the logger to log to.
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
#define SE_WARN(logger_name, message, ...) SE_LOG(logger_name, spdlog::level::warn, message, ##__VA_ARGS__);
/**
* @brief Log a warning message to the core logger.
*
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
#define SE_CWARN(message, ...) SE_CLOG(spdlog::level::warn, message, ##__VA_ARGS__);
/**
* @brief Log an info message to a logger.
*
* @param logger_name The name of the logger to log to.
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
#define SE_INFO(logger_name, message, ...) SE_LOG(logger_name, spdlog::level::info, message, ##__VA_ARGS__);
/**
* @brief Log an info message to the core logger.
*
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
#define SE_CINFO(message, ...) SE_CLOG(spdlog::level::info, message, ##__VA_ARGS__);
/**
* @brief Log a debug message to a logger.
*
* @param logger_name The name of the logger to log to.
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
#define SE_DEBUG(logger_name, message, ...) SE_LOG(logger_name, spdlog::level::debug, message, ##__VA_ARGS__);
/**
* @brief Log a debug message to the core logger.
*
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
#define SE_CDEBUG(message, ...) SE_CLOG(spdlog::level::debug, message, ##__VA_ARGS__);
/**
* @brief Log a trace message to a logger.
*
* @param logger_name The name of the logger to log to.
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
#define SE_TRACE(logger_name, message, ...) SE_LOG(logger_name, spdlog::level::trace, message, ##__VA_ARGS__);
/**
* @brief Log a trace message to the core logger.
*
* @param message The (to be) formatted message to log.
* @param ... The variables that will be formatted in the text.
*/
#define SE_CTRACE(message, ...) SE_CLOG(spdlog::level::trace, message, ##__VA_ARGS__);