From 2b9bed6635d95e15847c4d9b602e34d90e277d14 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Wed, 15 Sep 2021 11:27:48 +0200 Subject: Changed logging implementation * Code should no longer directly `#include spdlog/spdlog.h`, instead use `#include ` * Instead of explicit calls to `spdlog::info(...)` and such please use the logging macros defined in `zencore/logging.h`. I.e `ZEN_INFO`, `ZEN_DEBUG`, `ZEN_TRACE`, `ZEN_ERROR`, `ZEN_CRITITCAL` * The macros will pick up the "most local" logger via a `Log()` call to retrieve a logger instance. To override the default logger in a class please implement your own `Log()` function --- zencore/logging.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'zencore/logging.cpp') diff --git a/zencore/logging.cpp b/zencore/logging.cpp index 6441fc3bc..9d5a726f5 100644 --- a/zencore/logging.cpp +++ b/zencore/logging.cpp @@ -4,6 +4,16 @@ #include +namespace zen { + +spdlog::logger& +Log() +{ + return *spdlog::default_logger(); +} + +} // namespace zen + namespace zen::logging { spdlog::logger& -- cgit v1.2.3 From ffd45d6a5bd1e95065da71f00b6a9107b805c3ae Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Wed, 15 Sep 2021 15:22:36 +0200 Subject: Made logging macros always append `sv` string_view literal suffix Fixed up the few instances of explicit string_view arguments to make sure they compile properly with the new macros --- zencore/logging.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'zencore/logging.cpp') diff --git a/zencore/logging.cpp b/zencore/logging.cpp index 9d5a726f5..00ec845b4 100644 --- a/zencore/logging.cpp +++ b/zencore/logging.cpp @@ -9,7 +9,7 @@ namespace zen { spdlog::logger& Log() { - return *spdlog::default_logger(); + return *spdlog::default_logger_raw(); } } // namespace zen @@ -19,7 +19,7 @@ namespace zen::logging { spdlog::logger& Default() { - return *spdlog::default_logger(); + return *spdlog::default_logger_raw(); } spdlog::logger& -- cgit v1.2.3 From 2fc15e320c934424bc03601551f34f26a38e40a3 Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Wed, 15 Sep 2021 16:17:22 +0200 Subject: Tweaked logging to streamline access, and simplified setup code for new loggers --- zencore/logging.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'zencore/logging.cpp') diff --git a/zencore/logging.cpp b/zencore/logging.cpp index 00ec845b4..c5c0b6446 100644 --- a/zencore/logging.cpp +++ b/zencore/logging.cpp @@ -6,11 +6,8 @@ namespace zen { -spdlog::logger& -Log() -{ - return *spdlog::default_logger_raw(); -} +// We shadow the underlying spdlog default logger, in order to avoid a bunch of overhead +spdlog::logger* TheDefaultLogger; } // namespace zen @@ -19,20 +16,24 @@ namespace zen::logging { spdlog::logger& Default() { - return *spdlog::default_logger_raw(); + return *TheDefaultLogger; +} + +void +SetDefault(std::shared_ptr NewDefaultLogger) +{ + spdlog::set_default_logger(NewDefaultLogger); + TheDefaultLogger = spdlog::default_logger_raw(); } spdlog::logger& Get(std::string_view Name) { std::shared_ptr Logger = spdlog::get(std::string(Name)); + if (!Logger) { - Logger = std::make_shared(std::string(Name), - begin(spdlog::default_logger()->sinks()), - end(spdlog::default_logger()->sinks())); - - Logger->set_level(spdlog::default_logger()->level()); + Logger = Default().clone(std::string(Name)); spdlog::register_logger(Logger); } @@ -57,6 +58,7 @@ ConsoleLog() void InitializeLogging() { + TheDefaultLogger = spdlog::default_logger_raw(); } void -- cgit v1.2.3