aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-09-15 16:17:22 +0200
committerStefan Boberg <[email protected]>2021-09-15 16:17:22 +0200
commit2fc15e320c934424bc03601551f34f26a38e40a3 (patch)
treeafaf187ad71c5111bf928355c10099b938f4846a
parentChanged `std::exception` into `std::runtime_error` since `std::exception` doe... (diff)
downloadzen-2fc15e320c934424bc03601551f34f26a38e40a3.tar.xz
zen-2fc15e320c934424bc03601551f34f26a38e40a3.zip
Tweaked logging to streamline access, and simplified setup code for new loggers
-rw-r--r--zencore/include/zencore/logging.h10
-rw-r--r--zencore/logging.cpp24
-rw-r--r--zenserver/diag/logging.cpp15
3 files changed, 31 insertions, 18 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
diff --git a/zenserver/diag/logging.cpp b/zenserver/diag/logging.cpp
index 5782ce582..48eda7512 100644
--- a/zenserver/diag/logging.cpp
+++ b/zenserver/diag/logging.cpp
@@ -211,7 +211,7 @@ InitializeLogging(const ZenServerOptions& GlobalOptions)
spdlog::init_thread_pool(QueueSize, ThreadCount);
auto AsyncLogger = spdlog::create_async<spdlog::sinks::ansicolor_stdout_sink_mt>("main");
- spdlog::set_default_logger(AsyncLogger);
+ zen::logging::SetDefault(AsyncLogger);
}
// Sinks
@@ -220,16 +220,16 @@ InitializeLogging(const ZenServerOptions& GlobalOptions)
auto FileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(zen::WideToUtf8(LogPath.c_str()), /* truncate */ true);
// Default
- auto DefaultLogger = spdlog::default_logger();
- auto& Sinks = spdlog::default_logger()->sinks();
- Sinks.clear();
+ auto& DefaultLogger = zen::logging::Default();
+ auto& Sinks = DefaultLogger.sinks();
+
+ Sinks.clear();
Sinks.push_back(ConsoleSink);
Sinks.push_back(FileSink);
- DefaultLogger->set_level(LogLevel);
- DefaultLogger->flush_on(spdlog::level::err);
// Jupiter - only log HTTP traffic to file
+
auto JupiterLogger = std::make_shared<spdlog::logger>("jupiter", FileSink);
spdlog::register_logger(JupiterLogger);
JupiterLogger->set_level(LogLevel);
@@ -238,7 +238,10 @@ InitializeLogging(const ZenServerOptions& GlobalOptions)
spdlog::register_logger(ZenClientLogger);
ZenClientLogger->set_level(LogLevel);
+ // Configure all registered loggers according to settings
+
spdlog::set_level(LogLevel);
+ spdlog::flush_on(spdlog::level::err);
spdlog::set_formatter(std::make_unique<logging::full_formatter>(GlobalOptions.LogId, std::chrono::system_clock::now()));
}