diff options
| author | Stefan Boberg <[email protected]> | 2021-09-15 16:17:22 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-09-15 16:17:22 +0200 |
| commit | 2fc15e320c934424bc03601551f34f26a38e40a3 (patch) | |
| tree | afaf187ad71c5111bf928355c10099b938f4846a /zencore | |
| parent | Changed `std::exception` into `std::runtime_error` since `std::exception` doe... (diff) | |
| download | zen-2fc15e320c934424bc03601551f34f26a38e40a3.tar.xz zen-2fc15e320c934424bc03601551f34f26a38e40a3.zip | |
Tweaked logging to streamline access, and simplified setup code for new loggers
Diffstat (limited to 'zencore')
| -rw-r--r-- | zencore/include/zencore/logging.h | 10 | ||||
| -rw-r--r-- | zencore/logging.cpp | 24 |
2 files changed, 22 insertions, 12 deletions
diff --git a/zencore/include/zencore/logging.h b/zencore/include/zencore/logging.h index 4996463fd..4eee20414 100644 --- a/zencore/include/zencore/logging.h +++ b/zencore/include/zencore/logging.h @@ -13,6 +13,7 @@ namespace zen::logging { spdlog::logger& Default(); +void SetDefault(std::shared_ptr<spdlog::logger> NewDefaultLogger); spdlog::logger& ConsoleLog(); spdlog::logger& Get(std::string_view Name); @@ -22,7 +23,14 @@ void ShutdownLogging(); } // namespace zen::logging namespace zen { -spdlog::logger& Log(); +extern spdlog::logger* TheDefaultLogger; + +inline spdlog::logger& +Log() +{ + return *TheDefaultLogger; +} + using logging::ConsoleLog; } // namespace zen 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<spdlog::logger> NewDefaultLogger) +{ + spdlog::set_default_logger(NewDefaultLogger); + TheDefaultLogger = spdlog::default_logger_raw(); } spdlog::logger& Get(std::string_view Name) { std::shared_ptr<spdlog::logger> Logger = spdlog::get(std::string(Name)); + if (!Logger) { - Logger = std::make_shared<spdlog::logger>(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 |