add logging module

This commit is contained in:
lauchmelder 2025-01-03 14:48:36 +01:00
parent 31c2b61d20
commit 8cbcec09fa
7 changed files with 106 additions and 7 deletions

View file

@ -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)

View file

@ -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
)

View file

@ -3,5 +3,5 @@
// IWYU pragma: begin_exports
#include "quark/Application.hpp"
#include "quark/Entrypoint.hpp"
#include "quark/Logger.hpp"
// IWYU pragma: end_exports

View file

@ -1,10 +1,11 @@
#include "Application.hpp"
#include <spdlog/spdlog.h>
namespace qk {
void Application::Run() const {
for(;;) {
}
}

View file

@ -1,11 +1,12 @@
#pragma once
// IWYU pragma: private, include <quark.hpp>
#include "Application.hpp"
#include "Logger.hpp"
extern std::unique_ptr<qk::Application> qk::CreateApplication();
int main(int argc, char** argv) {
qk::Logger::Init();
QK_CORE_INFO("Logger initialised");
auto application = qk::CreateApplication();
application->Run();

View file

@ -0,0 +1,19 @@
#include "Logger.hpp"
#include <spdlog/sinks/stdout_color_sinks.h>
namespace qk {
std::shared_ptr<spdlog::logger> Logger::coreLogger;
std::shared_ptr<spdlog::logger> 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);
}
}

View file

@ -0,0 +1,43 @@
#pragma once
// IWYU pragma: private <quark.h>
#include <memory>
#include <spdlog/spdlog.h>
namespace qk {
class Logger {
public:
static void Init();
inline static std::shared_ptr<spdlog::logger> GetCoreLogger() {
return coreLogger;
}
inline static std::shared_ptr<spdlog::logger> GetClientLogger() {
return clientLogger;
}
private:
static std::shared_ptr<spdlog::logger> coreLogger;
static std::shared_ptr<spdlog::logger> 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__)