Finish writting the logger

This commit is contained in:
SeanOMik 2022-10-29 16:20:28 -04:00
parent e634a4dfce
commit 108382ed54
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
5 changed files with 291 additions and 21 deletions

View File

@ -1,6 +1,6 @@
--- ---
Checks: "clang-diagnostic-*,clang-analyzer-*,cppcoreguidelines-*,modernize-*,-modernize-use-trailing-return-type, Checks: "clang-diagnostic-*,clang-analyzer-*,cppcoreguidelines-*,modernize-*,-modernize-use-trailing-return-type,
-*-non-private-member-variables-in-classes,-*-magic-numbers" -*-non-private-member-variables-in-classes,-*-magic-numbers,-*-macro-usage"
WarningsAsErrors: true WarningsAsErrors: true
HeaderFilterRegex: "" HeaderFilterRegex: ""
AnalyzeTemporaryDtors: false AnalyzeTemporaryDtors: false

View File

@ -21,8 +21,8 @@ find_package(GLEW REQUIRED)
find_package(glfw3 CONFIG REQUIRED) find_package(glfw3 CONFIG REQUIRED)
find_package(glm CONFIG REQUIRED) find_package(glm CONFIG REQUIRED)
find_package(OpenGL REQUIRED) find_package(OpenGL REQUIRED)
find_package(fmt REQUIRED) find_package(fmt CONFIG REQUIRED)
find_package(spdlog REQUIRED) find_package(spdlog CONFIG REQUIRED)
find_package(assimp CONFIG REQUIRED) find_package(assimp CONFIG REQUIRED)
# Link sources # Link sources
@ -47,14 +47,16 @@ cmrc_add_resource_library(
# Link dependencies # Link dependencies
target_link_libraries(simpleengine PUBLIC GLEW::GLEW) target_link_libraries(simpleengine PUBLIC GLEW::GLEW)
target_link_libraries(simpleengine PUBLIC glfw) target_link_libraries(simpleengine PUBLIC glfw)
target_link_libraries(simpleengine PUBLIC fmt)
target_link_libraries(simpleengine PUBLIC spdlog)
target_link_libraries(simpleengine PUBLIC ${GLM_LIBRARIES}) target_link_libraries(simpleengine PUBLIC ${GLM_LIBRARIES})
target_link_libraries(simpleengine PUBLIC ${OPENGL_LIBRARIES}) target_link_libraries(simpleengine PUBLIC ${OPENGL_LIBRARIES})
if(WIN32) if(WIN32)
target_link_libraries(simpleengine PUBLIC assimp::assimp) target_link_libraries(simpleengine PUBLIC assimp::assimp)
target_link_libraries(simpleengine PUBLIC fmt::fmt)
target_link_libraries(simpleengine PUBLIC spdlog::spdlog)
else() else()
target_link_libraries(simpleengine PUBLIC assimp) target_link_libraries(simpleengine PUBLIC assimp)
target_link_libraries(simpleengine PUBLIC fmt)
target_link_libraries(simpleengine PUBLIC spdlog)
endif() endif()
# Link resources # Link resources

2
cmrc

@ -1 +1 @@
Subproject commit e386a629eb537d384811e598a3c96b9ca928f65e Subproject commit a64bea50c05594c8e7cf1f08e441bb9507742e2e

View File

@ -107,7 +107,10 @@ int main(int argc, char *argv[]) {
se::log::LoggerManager::set_level(spdlog::level::trace); se::log::LoggerManager::set_level(spdlog::level::trace);
se::log::LoggerPtr logger = se::log::LoggerManager::get_core_logger(); se::log::LoggerPtr logger = se::log::LoggerManager::get_core_logger();
logger->info("Hmmmm very cool"); logger->info("Hmmmm very cool. {}", "Yeah, I guess...");
SE_CLOG(spdlog::level::info, "This is a test. {}", "Did it work?");
SE_CINFO("This is a 2nd test. {}!", "Did it work?");
// Load core shaders from SimpleEngine resources // Load core shaders from SimpleEngine resources
se::gfx::shaders::Core3dShader core_shader; se::gfx::shaders::Core3dShader core_shader;

View File

@ -14,6 +14,13 @@ namespace simpleengine::log {
private: private:
std::shared_ptr<spdlog::logger> inner; std::shared_ptr<spdlog::logger> inner;
public: public:
/**
* @brief Construct a new Logger object from a name
*
* This will first check if the logger exists, if it does not it will create a new `color_mt` logger
*
* @param name The name of the logger.
*/
Logger(std::string name) { Logger(std::string name) {
inner = spdlog::get(name); inner = spdlog::get(name);
@ -22,79 +29,192 @@ namespace simpleengine::log {
} }
} }
/**
* @brief Construct a new Logger object using a spdlog::logger
*
* @param logger The inner spdlog::logger that this object will use
*/
Logger(std::shared_ptr<spdlog::logger> logger) : inner(logger) { Logger(std::shared_ptr<spdlog::logger> logger) : inner(logger) {
} }
/**
* @brief Get the inner spdlog logger object
*
* @return std::shared_ptr<spdlog::logger>
*/
std::shared_ptr<spdlog::logger> get_inner() { std::shared_ptr<spdlog::logger> get_inner() {
return inner; return inner;
} }
/**
* @brief Set the log level.
*
* @param lvl The level to set the log to
*/
void set_level(spdlog::level::level_enum lvl) { void set_level(spdlog::level::level_enum lvl) {
inner->set_level(lvl); inner->set_level(lvl);
} }
/**
* @brief Log a message.
*
* @param lvl Level to log at
* @param msg The message to log
*/
void log(spdlog::level::level_enum lvl, spdlog::string_view_t msg) { void log(spdlog::level::level_enum lvl, spdlog::string_view_t msg) {
inner->log(spdlog::source_loc{}, lvl, msg); inner->log(spdlog::source_loc{}, lvl, msg);
} }
/**
* @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)
{
inner->log(spdlog::source_loc{}, lvl, fmt, std::forward<Args>(args)...);
}
/**
* @brief Log a trace message.
*
* @tparam T
* @param msg The message that will be logged.
*/
template<typename T> template<typename T>
void trace(const T &msg) { void trace(const T &msg) {
log(spdlog::level::trace, msg); log(spdlog::level::trace, msg);
} }
/**
* @brief Log a debug message.
*
* @tparam T
* @param msg The message that will be logged.
*/
template<typename T> template<typename T>
void debug(const T &msg) { void debug(const T &msg) {
log(spdlog::level::debug, msg); log(spdlog::level::debug, msg);
} }
/**
* @brief Log an info message.
*
* @tparam T
* @param msg The message that will be logged.
*/
template<typename T> template<typename T>
void info(const T &msg) { void info(const T &msg) {
log(spdlog::level::info, msg); log(spdlog::level::info, msg);
} }
/**
* @brief Log a warning message.
*
* @tparam T
* @param msg The message that will be logged.
*/
template<typename T> template<typename T>
void warn(const T &msg) { void warn(const T &msg) {
log(spdlog::level::warn, msg); log(spdlog::level::warn, msg);
} }
/**
* @brief Log an error message.
*
* @tparam T
* @param msg The message that will be logged.
*/
template<typename T> template<typename T>
void error(const T &msg) { void error(const T &msg) {
log(spdlog::level::err, msg); log(spdlog::level::err, msg);
} }
/**
* @brief Log a critical message.
*
* @tparam T
* @param msg The message that will be logged.
*/
template<typename T> template<typename T>
void critical(const T &msg) { void critical(const T &msg) {
log(spdlog::level::critical, msg); log(spdlog::level::critical, msg);
} }
/**
* @brief Log a trace message.
*
* @tparam Args
* @param fmt String that will be formatted
* @param args Arguments to format into the message
*/
template<typename... Args> template<typename... Args>
void trace(spdlog::format_string_t<Args...> fmt, Args &&... args) { void trace(fmt::format_string<Args...> fmt, Args &&... args) {
log(spdlog::level::trace, fmt, std::forward<Args>(args)...); log(spdlog::level::trace, fmt, std::forward<Args>(args)...);
} }
/**
* @brief Log a debug message.
*
* @tparam Args
* @param fmt String that will be formatted
* @param args Arguments to format into the message
*/
template<typename... Args> template<typename... Args>
void debug(spdlog::format_string_t<Args...> fmt, Args &&... args) { void debug(fmt::format_string<Args...> fmt, Args &&... args) {
log(spdlog::level::debug, fmt, std::forward<Args>(args)...); log(spdlog::level::debug, fmt, std::forward<Args>(args)...);
} }
/**
* @brief Log an info message.
*
* @tparam Args
* @param fmt String that will be formatted
* @param args Arguments to format into the message
*/
template<typename... Args> template<typename... Args>
void info(spdlog::format_string_t<Args...> fmt, Args &&... args) { void info(fmt::format_string<Args...> fmt, Args &&... args) {
log(spdlog::level::info, fmt, std::forward<Args>(args)...); log(spdlog::level::info, fmt, std::forward<Args>(args)...);
} }
/**
* @brief Log a warning message.
*
* @tparam Args
* @param fmt String that will be formatted
* @param args Arguments to format into the message
*/
template<typename... Args> template<typename... Args>
void warn(spdlog::format_string_t<Args...> fmt, Args &&... args) { void warn(fmt::format_string<Args...> fmt, Args &&... args) {
log(spdlog::level::warn, fmt, std::forward<Args>(args)...); log(spdlog::level::warn, fmt, std::forward<Args>(args)...);
} }
/**
* @brief Log an error message.
*
* @tparam Args
* @param fmt String that will be formatted
* @param args Arguments to format into the message
*/
template<typename... Args> template<typename... Args>
void error(spdlog::format_string_t<Args...> fmt, Args &&... args) { void error(fmt::format_string<Args...> fmt, Args &&... args) {
log(spdlog::level::err, fmt, std::forward<Args>(args)...); log(spdlog::level::err, fmt, std::forward<Args>(args)...);
} }
/**
* @brief Log a critical message.
*
* @tparam Args
* @param fmt String that will be formatted
* @param args Arguments to format into the message
*/
template<typename... Args> template<typename... Args>
void critical(spdlog::format_string_t<Args...> fmt, Args &&... args) { void critical(fmt::format_string<Args...> fmt, Args &&... args) {
log(spdlog::level::critical, fmt, std::forward<Args>(args)...); log(spdlog::level::critical, fmt, std::forward<Args>(args)...);
} }
}; };
@ -103,20 +223,165 @@ namespace simpleengine::log {
class LoggerManager { class LoggerManager {
private: private:
static std::shared_ptr<Logger> core_logger; const static std::shared_ptr<Logger> core_logger;
public: public:
/* LoggerManager() { /**
spdlog::set_pattern("[%R - %D] [%l] [thread %t] [%n] %v"); * @brief Initialize the logger.
*
core_logger = std::make_shared<Logger>("Engine"); */
} */
static void init(); static void init();
/**
* @brief Set the core log level.
*
* @param lvl
*/
static void set_level(spdlog::level::level_enum lvl); static void set_level(spdlog::level::level_enum lvl);
/**
* @brief Get the core logger.
*
* @return std::shared_ptr<Logger>
*/
static std::shared_ptr<Logger> get_core_logger(); static std::shared_ptr<Logger> get_core_logger();
/**
* @brief Create a new logger.
*
* @param name The name of the logger
* @return Logger
*/
static Logger create_logger(std::string name); static Logger create_logger(std::string name);
/**
* @brief Get a logger using its name.
*
* @param name The name of the logger
* @return Logger
*/
static Logger get_logger(std::string name); static Logger get_logger(std::string name);
}; };
} }
// 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.
*/
#define SE_CLOG(level, message, ...) simpleengine::log::LoggerManager::get_core_logger()->log(level, fmt::format(message, ##__VA_ARGS__));
/**
* @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.
*/
#define SE_ERROR(logger_name, message, ...) SE_LOG(logger_name, spdlog::level::error, message, ##__VA_ARGS__);
/**
* @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.
*/
#define SE_CERROR(message, ...) SE_CLOG(spdlog::level::error, message, ##__VA_ARGS__);
/**
* @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__);