aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2021-09-08 19:10:59 +0200
committerStefan Boberg <[email protected]>2021-09-08 19:10:59 +0200
commitf04eebe974a08922c74b28d3b08d1edc97496c47 (patch)
tree7ced15c6aa2758d6fa52f69ecb545e743e6f4040
parentMerged from main (diff)
downloadzen-f04eebe974a08922c74b28d3b08d1edc97496c47.tar.xz
zen-f04eebe974a08922c74b28d3b08d1edc97496c47.zip
Moved a bunch of logging code into zencore
-rw-r--r--zencore/include/zencore/logging.h14
-rw-r--r--zencore/logging.cpp57
-rw-r--r--zenserver/diag/logging.cpp66
-rw-r--r--zenserver/diag/logging.h10
4 files changed, 97 insertions, 50 deletions
diff --git a/zencore/include/zencore/logging.h b/zencore/include/zencore/logging.h
index e77314178..7a08cc48b 100644
--- a/zencore/include/zencore/logging.h
+++ b/zencore/include/zencore/logging.h
@@ -7,3 +7,17 @@
#include <spdlog/spdlog.h>
#include "postwindows.h"
// clang-format on
+
+#include <string_view>
+
+namespace zen::logging {
+
+spdlog::logger& Default();
+spdlog::logger& ConsoleLog();
+spdlog::logger& Get(std::string_view Name);
+
+void InitializeLogging();
+void ShutdownLogging();
+
+
+} // namespace zen::logging
diff --git a/zencore/logging.cpp b/zencore/logging.cpp
new file mode 100644
index 000000000..89d588650
--- /dev/null
+++ b/zencore/logging.cpp
@@ -0,0 +1,57 @@
+#include "zencore/logging.h"
+
+#include <spdlog/sinks/stdout_color_sinks.h>
+
+namespace zen::logging {
+
+spdlog::logger&
+Default()
+{
+ return *spdlog::default_logger();
+}
+
+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());
+ spdlog::register_logger(Logger);
+ }
+
+ return *Logger;
+}
+
+std::once_flag ConsoleInitFlag;
+std::shared_ptr<spdlog::logger> ConLogger;
+
+spdlog::logger&
+ConsoleLog()
+{
+ std::call_once(ConsoleInitFlag, [&] {
+ ConLogger = spdlog::stdout_color_mt("console");
+
+ ConLogger->set_pattern("%v");
+ });
+
+ return *ConLogger;
+}
+
+void
+InitializeLogging()
+{
+}
+
+void
+ShutdownLogging()
+{
+ spdlog::drop_all();
+ spdlog::shutdown();
+}
+
+} // namespace zen::logging
diff --git a/zenserver/diag/logging.cpp b/zenserver/diag/logging.cpp
index 89ed4ac6a..796a15d01 100644
--- a/zenserver/diag/logging.cpp
+++ b/zenserver/diag/logging.cpp
@@ -4,6 +4,8 @@
#include "config.h"
+#include <spdlog/async.h>
+#include <spdlog/async_logger.h>
#include <spdlog/pattern_formatter.h>
#include <spdlog/sinks/ansicolor_sink.h>
#include <spdlog/sinks/basic_file_sink.h>
@@ -187,13 +189,33 @@ EnableVTMode()
void
InitializeLogging(const ZenServerOptions& GlobalOptions)
{
+ zen::logging::InitializeLogging();
+
EnableVTMode();
std::filesystem::path LogPath = GlobalOptions.DataDir / "logs/zenserver.log";
- spdlog::level::level_enum LogLevel = spdlog::level::debug;
+ bool IsAsync = true;
+ spdlog::level::level_enum LogLevel = spdlog::level::info;
+
+ if (GlobalOptions.IsDebug)
+ {
+ LogLevel = spdlog::level::debug;
+ IsAsync = false;
+ }
+
+ if (IsAsync)
+ {
+ const int QueueSize = 8192;
+ const int ThreadCount = 1;
+ spdlog::init_thread_pool(QueueSize, ThreadCount);
+
+ auto AsyncLogger = spdlog::create_async<spdlog::sinks::ansicolor_stdout_sink_mt>("main");
+ spdlog::set_default_logger(AsyncLogger);
+ }
// Sinks
+
auto ConsoleSink = std::make_shared<spdlog::sinks::ansicolor_stdout_sink_mt>();
auto FileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(zen::WideToUtf8(LogPath.c_str()), /* truncate */ true);
@@ -201,9 +223,11 @@ InitializeLogging(const ZenServerOptions& GlobalOptions)
auto DefaultLogger = spdlog::default_logger();
auto& Sinks = spdlog::default_logger()->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);
@@ -221,43 +245,5 @@ InitializeLogging(const ZenServerOptions& GlobalOptions)
void
ShutdownLogging()
{
- spdlog::drop_all();
- spdlog::shutdown();
+ zen::logging::ShutdownLogging();
}
-
-spdlog::logger&
-ConsoleLog()
-{
- static auto ConLogger = spdlog::stdout_color_mt("console");
-
- ConLogger->set_pattern("%v");
-
- return *ConLogger;
-}
-
-namespace zen::logging {
-
-spdlog::logger&
-Default()
-{
- return *spdlog::default_logger();
-}
-
-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());
- spdlog::register_logger(Logger);
- }
-
- return *Logger;
-}
-
-} // namespace zen::logging
diff --git a/zenserver/diag/logging.h b/zenserver/diag/logging.h
index bc93898ad..8df49f842 100644
--- a/zenserver/diag/logging.h
+++ b/zenserver/diag/logging.h
@@ -8,13 +8,3 @@ struct ZenServerOptions;
void InitializeLogging(const ZenServerOptions& GlobalOptions);
void ShutdownLogging();
-
-spdlog::logger& ConsoleLog();
-
-namespace zen::logging {
-
-spdlog::logger& Default();
-
-spdlog::logger& Get(std::string_view Name);
-
-} // namespace zen::logging