From fb29ca07d547fad736f68b20b7c27a07916c5896 Mon Sep 17 00:00:00 2001 From: karl Date: Sat, 20 Mar 2021 16:55:43 +0100 Subject: [PATCH] Add logging using spdlog --- .gitmodules | 3 +++ SConstruct | 8 ++++++++ cpp/Logger.cpp | 17 +++++++++++++--- cpp/vendor/spdlog | 1 + include/Gedeng/EntryPoint.h | 4 ++++ include/Gedeng/Logger.h | 40 ++++++++++++++++++++++++++++++++++++- test/test-app/main.cpp | 2 ++ 7 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 .gitmodules create mode 160000 cpp/vendor/spdlog diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e10e895 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "cpp/vendor/spdlog"] + path = cpp/vendor/spdlog + url = https://github.com/gabime/spdlog diff --git a/SConstruct b/SConstruct index f03673c..627c757 100644 --- a/SConstruct +++ b/SConstruct @@ -1,5 +1,9 @@ #!python +def add_third_party_includes(env): + env.Append(CPPPATH=['cpp/', 'cpp/vendor/spdlog/include']) + + # Create the environment and create a Compilation Database for use in VSCodium env = Environment(tools=['default', 'compilation_db']) env.CompilationDatabase() @@ -7,6 +11,8 @@ env.CompilationDatabase() env.Append(CCFLAGS=["-Wall", "-Wextra", "-Werror", "-pedantic"]) env.Append(CPPPATH=['cpp/', 'include/']) +add_third_party_includes(env) + # Build the library gedeng = env.SharedLibrary('lib/gedeng', Glob('cpp/*.cpp')) env.Install('test/bin/lib/', gedeng) @@ -25,6 +31,8 @@ testEnv.Append(LINKFLAGS=[ '-Wl,--disable-new-dtags,-rpath,\'$$ORIGIN/lib/\'' ]) +add_third_party_includes(testEnv) + # Build the test programs catch_cpp = "test/catch_amalgamated.cpp" testEnv.Program('test/bin/vector-test.out', [catch_cpp, 'test/vector/vector-test.cpp']) diff --git a/cpp/Logger.cpp b/cpp/Logger.cpp index 6e71c79..958cc4f 100644 --- a/cpp/Logger.cpp +++ b/cpp/Logger.cpp @@ -1,10 +1,21 @@ #include "Gedeng/Logger.h" -#include +#include "spdlog/common.h" +#include "spdlog/sinks/stdout_color_sinks.h" +#include "spdlog/spdlog.h" namespace Gedeng { -void Logger::debug(const String &text) { - std::cout << "Debug: " << text << std::endl; +std::shared_ptr Logger::core_logger; +std::shared_ptr Logger::client_logger; + +void Logger::init() { + spdlog::set_pattern("%^[%T] %n: %v%$"); + + core_logger = spdlog::stdout_color_mt("GEDENG"); + core_logger->set_level(spdlog::level::trace); + + client_logger = spdlog::stdout_color_mt("APP"); + client_logger->set_level(spdlog::level::trace); } } // namespace Gedeng diff --git a/cpp/vendor/spdlog b/cpp/vendor/spdlog new file mode 160000 index 0000000..44e1f9f --- /dev/null +++ b/cpp/vendor/spdlog @@ -0,0 +1 @@ +Subproject commit 44e1f9f6829cd8acbfea282d390560452e889aff diff --git a/include/Gedeng/EntryPoint.h b/include/Gedeng/EntryPoint.h index 4060924..e85c9e7 100644 --- a/include/Gedeng/EntryPoint.h +++ b/include/Gedeng/EntryPoint.h @@ -7,6 +7,10 @@ extern Gedeng::Application *Gedeng::create_application(); // If the user program defines GEDENG_MAIN, supply this main function #ifdef GEDENG_MAIN int main() { + // Initialize logging + Gedeng::Logger::init(); + GG_CORE_WARN("Logger initialized"); + Gedeng::Application *app = Gedeng::create_application(); app->run(); diff --git a/include/Gedeng/Logger.h b/include/Gedeng/Logger.h index 4b335a0..4949bd8 100644 --- a/include/Gedeng/Logger.h +++ b/include/Gedeng/Logger.h @@ -1,11 +1,49 @@ #pragma once #include "Gedeng/String.h" +#include "spdlog/spdlog.h" +#include namespace Gedeng { class Logger { + public: + static void init(); + + inline static std::shared_ptr &get_core_logger() { + return core_logger; + } + inline static std::shared_ptr &get_client_logger() { + return client_logger; + } + static void debug(const String &text); + + private: + static std::shared_ptr core_logger; + static std::shared_ptr client_logger; }; -} // namespace Gedeng \ No newline at end of file +} // namespace Gedeng + +// Core log macros +#define GG_CORE_FATAL(...) \ + ::Gedeng::Logger::get_core_logger()->fatal(__VA_ARGS__) +#define GG_CORE_ERROR(...) \ + ::Gedeng::Logger::get_core_logger()->error(__VA_ARGS__) +#define GG_CORE_WARN(...) ::Gedeng::Logger::get_core_logger()->warn(__VA_ARGS__) +#define GG_CORE_INFO(...) ::Gedeng::Logger::get_core_logger()->info(__VA_ARGS__) +#define GG_CORE_TRACE(...) \ + ::Gedeng::Logger::get_core_logger()->trace(__VA_ARGS__) + +// Client log macros +#define GG_CLIENT_FATAL(...) \ + ::Gedeng::Logger::get_client_logger()->fatal(__VA_ARGS__) +#define GG_CLIENT_ERROR(...) \ + ::Gedeng::Logger::get_client_logger()->error(__VA_ARGS__) +#define GG_CLIENT_WARN(...) \ + ::Gedeng::Logger::get_client_logger()->warn(__VA_ARGS__) +#define GG_CLIENT_INFO(...) \ + ::Gedeng::Logger::get_client_logger()->info(__VA_ARGS__) +#define GG_CLIENT_TRACE(...) \ + ::Gedeng::Logger::get_client_logger()->trace(__VA_ARGS__) diff --git a/test/test-app/main.cpp b/test/test-app/main.cpp index 345f10c..7d2e221 100644 --- a/test/test-app/main.cpp +++ b/test/test-app/main.cpp @@ -1,3 +1,4 @@ +#include "Gedeng/Logger.h" #define GEDENG_MAIN #include @@ -9,5 +10,6 @@ class TestApp : public Gedeng::Application { }; Gedeng::Application *Gedeng::create_application() { + GG_CLIENT_INFO("Creating Application"); return new TestApp(); }