diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d87843..6888814 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,15 +43,15 @@ FetchContent_MakeAvailable(mkr_common) FetchContent_Declare(mkr_maths GIT_REPOSITORY https://github.com/TypeDefinition/mkr_maths.git GIT_TAG main) FetchContent_MakeAvailable(mkr_maths) -FetchContent_Declare(mkr_log GIT_REPOSITORY https://github.com/TypeDefinition/mkr_log.git GIT_TAG main) -FetchContent_MakeAvailable(mkr_log) - FetchContent_Declare(mkr_glsl_include GIT_REPOSITORY https://github.com/TypeDefinition/mkr_glsl_include.git GIT_TAG main) FetchContent_MakeAvailable(mkr_glsl_include) FetchContent_Declare(flecs GIT_REPOSITORY https://github.com/SanderMertens/flecs.git GIT_TAG 37233f127d5006ceb0087fbfcd2f3e36f5b77a23) FetchContent_MakeAvailable(flecs) +FetchContent_Declare(spdlog GIT_REPOSITORY https://github.com/gabime/spdlog.git GIT_TAG 1253a57db644a07a13c202ad73f00f9ed66f9aed) +FetchContent_MakeAvailable(spdlog) + # Dependencies set(SDL2_INC_DIR ${PROJECT_SOURCE_DIR}/dep/sdl2/include) set(SDL2_LIB_DIR ${PROJECT_SOURCE_DIR}/dep/sdl2/lib) diff --git a/src/log/log.h b/src/log/log.h new file mode 100644 index 0000000..36cc13a --- /dev/null +++ b/src/log/log.h @@ -0,0 +1,80 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace mkr { +class log { + private: + static inline std::shared_ptr core_logger_ = nullptr; + static inline std::shared_ptr client_logger_ = nullptr; + + public: + log() = delete; + + static void init(const std::string& _log_file = "./log/log.txt") { + spdlog::init_thread_pool(8192, 1); + + auto console_sink = std::make_shared(); + console_sink->set_level(spdlog::level::level_enum::trace); + + auto file_sink = std::make_shared(_log_file, 5242880, 3, true); + file_sink->set_level(spdlog::level::level_enum::trace); + + spdlog::sinks_init_list sink_list = {console_sink, file_sink}; + + core_logger_ = std::make_shared("Core Logger", sink_list.begin(), sink_list.end(), spdlog::thread_pool()); + core_logger_->set_level(spdlog::level::level_enum::trace); // Set the logger's log level to the lowest so that it does not override the sinks. + core_logger_->flush_on(spdlog::level::level_enum::trace); // Set the logger's flush level to the lowest so that everything gets written to the file. + + client_logger_ = std::make_shared("Client Logger", sink_list.begin(), sink_list.end(), spdlog::thread_pool()); + client_logger_->set_level(spdlog::level::level_enum::trace); + client_logger_->flush_on(spdlog::level::level_enum::trace); + } + + static void exit() { + core_logger_->flush(); + core_logger_ = nullptr; + client_logger_->flush(); + client_logger_ = nullptr; + } + + inline static spdlog::async_logger* core_logger() { return core_logger_.get(); } + + inline static spdlog::async_logger* client_logger() { return client_logger_.get(); } +}; +} + +#ifndef NDEBUG +#define MKR_CORE_TRACE(...) ::mkr::log::core_logger()->trace(__VA_ARGS__) + #define MKR_CORE_DEBUG(...) ::mkr::log::core_logger()->debug(__VA_ARGS__) + #define MKR_CORE_INFO(...) ::mkr::log::core_logger()->info(__VA_ARGS__) + #define MKR_CORE_WARN(...) ::mkr::log::core_logger()->warn(__VA_ARGS__) + #define MKR_CORE_ERROR(...) ::mkr::log::core_logger()->error(__VA_ARGS__) + #define MKR_CORE_CRITICAL(...) ::mkr::log::core_logger()->critical(__VA_ARGS__) + + #define MKR_TRACE(...) ::mkr::log::client_logger()->trace(__VA_ARGS__) + #define MKR_DEBUG(...) ::mkr::log::client_logger()->debug(__VA_ARGS__) + #define MKR_INFO(...) ::mkr::log::client_logger()->info(__VA_ARGS__) + #define MKR_WARN(...) ::mkr::log::client_logger()->warn(__VA_ARGS__) + #define MKR_ERROR(...) ::mkr::log::client_logger()->error(__VA_ARGS__) + #define MKR_CRITICAL(...) ::mkr::log::client_logger()->critical(__VA_ARGS__) +#else +#define MKR_CORE_TRACE(...) +#define MKR_CORE_DEBUG(...) +#define MKR_CORE_INFO(...) +#define MKR_CORE_WARN(...) +#define MKR_CORE_ERROR(...) +#define MKR_CORE_CRITICAL(...) + +#define MKR_TRACE(...) +#define MKR_DEBUG(...) +#define MKR_INFO(...) +#define MKR_WARN(...) +#define MKR_ERROR(...) +#define MKR_CRITICAL(...) +#endif