Skip to content

Commit

Permalink
Add log
Browse files Browse the repository at this point in the history
  • Loading branch information
-m committed May 30, 2024
1 parent cacbd28 commit 65d4d72
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
80 changes: 80 additions & 0 deletions src/log/log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#pragma once

#include <utility>
#include <string>
#include <spdlog/spdlog.h>
#include <spdlog/async.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>

namespace mkr {
class log {
private:
static inline std::shared_ptr<spdlog::async_logger> core_logger_ = nullptr;
static inline std::shared_ptr<spdlog::async_logger> 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<spdlog::sinks::stdout_color_sink_mt>();
console_sink->set_level(spdlog::level::level_enum::trace);

auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(_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<spdlog::async_logger>("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<spdlog::async_logger>("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

0 comments on commit 65d4d72

Please sign in to comment.