From 8cbcec09fa062a3ffc43664882e5af6410a564a3 Mon Sep 17 00:00:00 2001 From: lauchmelder Date: Fri, 3 Jan 2025 14:48:36 +0100 Subject: [PATCH] add logging module --- CMakeLists.txt | 12 +++++- quark/CMakeLists.txt | 27 +++++++++++- quark/src/quark.hpp | 2 +- quark/src/quark/Application.cpp | 3 +- .../quark/{Entrypoint.hpp => Entrypoint.cpp} | 7 +-- quark/src/quark/Logger.cpp | 19 ++++++++ quark/src/quark/Logger.hpp | 43 +++++++++++++++++++ 7 files changed, 106 insertions(+), 7 deletions(-) rename quark/src/quark/{Entrypoint.hpp => Entrypoint.cpp} (72%) create mode 100644 quark/src/quark/Logger.cpp create mode 100644 quark/src/quark/Logger.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b2abb9a..b8752dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,16 @@ -cmake_minimum_required(VERSION 3.14) +cmake_minimum_required(VERSION 3.24) project(quark) +include(FetchContent) + +set(SPDLOG_BUILD_SHARED ON CACHE INTERNAL "") +FetchContent_Declare( + spdlog + GIT_REPOSITORY https://github.com/gabime/spdlog + GIT_TAG 8e5613379f5140fefb0b60412fbf1f5406e7c7f8 + FIND_PACKAGE_ARGS +) + add_subdirectory(quark) add_subdirectory(sandbox) diff --git a/quark/CMakeLists.txt b/quark/CMakeLists.txt index e44359b..60831e4 100644 --- a/quark/CMakeLists.txt +++ b/quark/CMakeLists.txt @@ -1,7 +1,32 @@ project(quark CXX) -add_library(quark SHARED +FetchContent_MakeAvailable(spdlog) + +add_library(quark SHARED) + +target_sources(quark + PRIVATE + src/quark/Entrypoint.cpp src/quark/Application.cpp + src/quark/Logger.cpp + + PUBLIC FILE_SET HEADERS + BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/src FILES + src/quark.hpp + src/quark/Application.hpp + src/quark/Logger.hpp ) target_include_directories(quark INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/src) + +target_link_libraries(quark + spdlog::spdlog +) + +include(GNUInstallDirs) +install( + TARGETS quark + RUNTIME_DEPENDENCIES + LIBRARY + FILE_SET HEADERS +) diff --git a/quark/src/quark.hpp b/quark/src/quark.hpp index 81474fc..1f6198e 100644 --- a/quark/src/quark.hpp +++ b/quark/src/quark.hpp @@ -3,5 +3,5 @@ // IWYU pragma: begin_exports #include "quark/Application.hpp" -#include "quark/Entrypoint.hpp" +#include "quark/Logger.hpp" // IWYU pragma: end_exports diff --git a/quark/src/quark/Application.cpp b/quark/src/quark/Application.cpp index e88a96b..d9dc027 100644 --- a/quark/src/quark/Application.cpp +++ b/quark/src/quark/Application.cpp @@ -1,10 +1,11 @@ #include "Application.hpp" +#include namespace qk { void Application::Run() const { for(;;) { - + } } diff --git a/quark/src/quark/Entrypoint.hpp b/quark/src/quark/Entrypoint.cpp similarity index 72% rename from quark/src/quark/Entrypoint.hpp rename to quark/src/quark/Entrypoint.cpp index 63cbcda..4424b3f 100644 --- a/quark/src/quark/Entrypoint.hpp +++ b/quark/src/quark/Entrypoint.cpp @@ -1,11 +1,12 @@ -#pragma once -// IWYU pragma: private, include - #include "Application.hpp" +#include "Logger.hpp" extern std::unique_ptr qk::CreateApplication(); int main(int argc, char** argv) { + qk::Logger::Init(); + QK_CORE_INFO("Logger initialised"); + auto application = qk::CreateApplication(); application->Run(); diff --git a/quark/src/quark/Logger.cpp b/quark/src/quark/Logger.cpp new file mode 100644 index 0000000..b34473b --- /dev/null +++ b/quark/src/quark/Logger.cpp @@ -0,0 +1,19 @@ +#include "Logger.hpp" + +#include + +namespace qk { + std::shared_ptr Logger::coreLogger; + std::shared_ptr Logger::clientLogger; + + void Logger::Init() { + spdlog::set_pattern("[%^%l%$] %T.%e (%t) %n: %v"); + + coreLogger = spdlog::stdout_color_mt("quark"); + coreLogger->set_level(spdlog::level::trace); + + clientLogger = spdlog::stdout_color_mt("app"); + clientLogger->set_level(spdlog::level::trace); + } +} + diff --git a/quark/src/quark/Logger.hpp b/quark/src/quark/Logger.hpp new file mode 100644 index 0000000..ac4f37b --- /dev/null +++ b/quark/src/quark/Logger.hpp @@ -0,0 +1,43 @@ +#pragma once +// IWYU pragma: private + +#include +#include + +namespace qk { + class Logger { + public: + static void Init(); + + inline static std::shared_ptr GetCoreLogger() { + return coreLogger; + } + + inline static std::shared_ptr GetClientLogger() { + return clientLogger; + } + + private: + static std::shared_ptr coreLogger; + static std::shared_ptr clientLogger; + }; +} + +#ifndef NDEBUG +# define QK_CORE_TRACE(...) ::qk::Logger::GetCoreLogger()->trace(__VA_ARGS__) +#else +# define QK_CORE_TRACE +#endif +#define QK_CORE_INFO(...) ::qk::Logger::GetCoreLogger()->info(__VA_ARGS__) +#define QK_CORE_WARN(...) ::qk::Logger::GetCoreLogger()->warn(__VA_ARGS__) +#define QK_CORE_ERROR(...) ::qk::Logger::GetCoreLogger()->error(__VA_ARGS__) +#define QK_CORE_CRITICAL(...) ::qk::Logger::GetCoreLogger()->critical(__VA_ARGS__) + +#ifndef NDEBUG +# define QK_TRACE(...) ::qk::Logger::GetClientLogger()->trace(__VA_ARGS__) +#else +# define QK_INFO(...) ::qk::Logger::GetClientLogger()->info(__VA_ARGS__) +#endif +#define QK_WARN(...) ::qk::Logger::GetClientLogger()->warn(__VA_ARGS__) +#define QK_ERROR(...) ::qk::Logger::GetClientLogger()->error(__VA_ARGS__) +#define QK_CRITICAL(...) ::qk::Logger::GetClientLogger()->critical(__VA_ARGS__)