diff options
| author | Stefan Boberg <[email protected]> | 2021-09-08 19:10:59 +0200 |
|---|---|---|
| committer | Stefan Boberg <[email protected]> | 2021-09-08 19:10:59 +0200 |
| commit | f04eebe974a08922c74b28d3b08d1edc97496c47 (patch) | |
| tree | 7ced15c6aa2758d6fa52f69ecb545e743e6f4040 /zenserver/diag/logging.cpp | |
| parent | Merged from main (diff) | |
| download | zen-f04eebe974a08922c74b28d3b08d1edc97496c47.tar.xz zen-f04eebe974a08922c74b28d3b08d1edc97496c47.zip | |
Moved a bunch of logging code into zencore
Diffstat (limited to 'zenserver/diag/logging.cpp')
| -rw-r--r-- | zenserver/diag/logging.cpp | 66 |
1 files changed, 26 insertions, 40 deletions
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 |