From e634a4dfce823a3fea93ae5fb3862e93b7feb5bb Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Fri, 28 Oct 2022 18:49:29 -0400 Subject: [PATCH] Start working on logging --- CMakeLists.txt | 6 ++ examples/dev_testing/src/main.cpp | 9 +++ include/simpleengine/log/logger.h | 122 ++++++++++++++++++++++++++++++ shell.nix | 1 + src/game.cpp | 3 + src/log/logger.cpp | 32 ++++++++ 6 files changed, 173 insertions(+) create mode 100644 include/simpleengine/log/logger.h create mode 100644 src/log/logger.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 82bb540..ea9d78e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,12 +21,16 @@ find_package(GLEW REQUIRED) find_package(glfw3 CONFIG REQUIRED) find_package(glm CONFIG REQUIRED) find_package(OpenGL REQUIRED) +find_package(fmt REQUIRED) +find_package(spdlog REQUIRED) find_package(assimp CONFIG REQUIRED) # Link sources file(GLOB_RECURSE source_list src/*.cpp) add_library(simpleengine STATIC ${source_list}) +target_compile_definitions(simpleengine PUBLIC SPDLOG_FMT_EXTERNAL) + # Link headers target_include_directories(simpleengine PUBLIC include PRIVATE include/simpleengine) @@ -43,6 +47,8 @@ cmrc_add_resource_library( # Link dependencies target_link_libraries(simpleengine PUBLIC GLEW::GLEW) 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 ${OPENGL_LIBRARIES}) if(WIN32) diff --git a/examples/dev_testing/src/main.cpp b/examples/dev_testing/src/main.cpp index 7cdc2ba..e3a1f39 100644 --- a/examples/dev_testing/src/main.cpp +++ b/examples/dev_testing/src/main.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -29,6 +30,7 @@ #include #include #include +#include #include #include @@ -102,6 +104,11 @@ public: int main(int argc, char *argv[]) { se::Game game(640, 480, "SimpleEngine 3D OpenGL - Developer Testing", GLFW_OPENGL_CORE_PROFILE, 4, 4, false); + se::log::LoggerManager::set_level(spdlog::level::trace); + + se::log::LoggerPtr logger = se::log::LoggerManager::get_core_logger(); + logger->info("Hmmmm very cool"); + // Load core shaders from SimpleEngine resources se::gfx::shaders::Core3dShader core_shader; @@ -113,6 +120,8 @@ int main(int argc, char *argv[]) { renderer->initialize(); //game.add_renderable(renderer); + logger->error("AHHHHH SOMETHING BAD!"); + // Create a Scene and give it the renderer auto scene = std::make_shared(renderer, camera); //game.add_event(scene); diff --git a/include/simpleengine/log/logger.h b/include/simpleengine/log/logger.h new file mode 100644 index 0000000..0febd5b --- /dev/null +++ b/include/simpleengine/log/logger.h @@ -0,0 +1,122 @@ +//#define SPDLOG_FMT_EXTERNAL + +#include +#include +#include + +#include +#include +#include +#include + +namespace simpleengine::log { + class Logger { + private: + std::shared_ptr inner; + public: + Logger(std::string name) { + inner = spdlog::get(name); + + if (!inner) { + inner = spdlog::stderr_color_mt(name); + } + } + + Logger(std::shared_ptr logger) : inner(logger) { + + } + + std::shared_ptr get_inner() { + return inner; + } + + void set_level(spdlog::level::level_enum lvl) { + inner->set_level(lvl); + } + + void log(spdlog::level::level_enum lvl, spdlog::string_view_t msg) { + inner->log(spdlog::source_loc{}, lvl, msg); + } + + template + void trace(const T &msg) { + log(spdlog::level::trace, msg); + } + + template + void debug(const T &msg) { + log(spdlog::level::debug, msg); + } + + template + void info(const T &msg) { + log(spdlog::level::info, msg); + } + + template + void warn(const T &msg) { + log(spdlog::level::warn, msg); + } + + template + void error(const T &msg) { + log(spdlog::level::err, msg); + } + + template + void critical(const T &msg) { + log(spdlog::level::critical, msg); + } + + template + void trace(spdlog::format_string_t fmt, Args &&... args) { + log(spdlog::level::trace, fmt, std::forward(args)...); + } + + template + void debug(spdlog::format_string_t fmt, Args &&... args) { + log(spdlog::level::debug, fmt, std::forward(args)...); + } + + template + void info(spdlog::format_string_t fmt, Args &&... args) { + log(spdlog::level::info, fmt, std::forward(args)...); + } + + template + void warn(spdlog::format_string_t fmt, Args &&... args) { + log(spdlog::level::warn, fmt, std::forward(args)...); + } + + template + void error(spdlog::format_string_t fmt, Args &&... args) { + log(spdlog::level::err, fmt, std::forward(args)...); + } + + template + void critical(spdlog::format_string_t fmt, Args &&... args) { + log(spdlog::level::critical, fmt, std::forward(args)...); + } + }; + + using LoggerPtr = std::shared_ptr; + + class LoggerManager { + private: + static std::shared_ptr core_logger; + public: + /* LoggerManager() { + spdlog::set_pattern("[%R - %D] [%l] [thread %t] [%n] %v"); + + core_logger = std::make_shared("Engine"); + } */ + + static void init(); + static void set_level(spdlog::level::level_enum lvl); + + static std::shared_ptr get_core_logger(); + + static Logger create_logger(std::string name); + static Logger get_logger(std::string name); + }; +} \ No newline at end of file diff --git a/shell.nix b/shell.nix index 47146ae..0412ece 100644 --- a/shell.nix +++ b/shell.nix @@ -12,5 +12,6 @@ pkgs.mkShell { glfw glm assimp + spdlog ]; } \ No newline at end of file diff --git a/src/game.cpp b/src/game.cpp index b5e6e12..4f85cba 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,6 +1,7 @@ #include "game.h" #include "event/event.h" #include "renderable.h" +#include "log/logger.h" #include #include @@ -45,6 +46,8 @@ simpleengine::Game::Game(int w, int h, const std::string& window_name, const int update_enabled_vsync(); last_frame_time = std::chrono::high_resolution_clock::now(); + + log::LoggerManager::init(); } void simpleengine::Game::initialize(const int& gl_profile, const int& major_version, const int& minor_version, diff --git a/src/log/logger.cpp b/src/log/logger.cpp new file mode 100644 index 0000000..7ed2b9f --- /dev/null +++ b/src/log/logger.cpp @@ -0,0 +1,32 @@ +#include "log/logger.h" +#include +#include + +namespace simpleengine::log { + void LoggerManager::init() { + spdlog::set_pattern("[%T %D] [thread %t] [%n] [%l] %v"); + + core_logger = std::make_shared("engine"); + + spdlog::set_default_logger(core_logger->get_inner()); + set_level(spdlog::level::info); + } + + void LoggerManager::set_level(spdlog::level::level_enum lvl) { + spdlog::set_level(lvl); + } + + std::shared_ptr LoggerManager::core_logger; + + std::shared_ptr LoggerManager::get_core_logger() { + return core_logger; + } + + Logger LoggerManager::create_logger(std::string name) { + return Logger(name); + } + + Logger LoggerManager::get_logger(std::string name) { + return Logger(spdlog::get(name)); + } +} \ No newline at end of file