diff --git a/src/C4Application.cpp b/src/C4Application.cpp index d17d1f05..a147eac1 100644 --- a/src/C4Application.cpp +++ b/src/C4Application.cpp @@ -79,10 +79,19 @@ void C4Application::DoInit() assert(AppState == C4AS_None); // Config overwrite by parameter StdStrBuf sConfigFilename; + bool verbose{false}; char szParameter[_MAX_PATH + 1]; for (int32_t iPar = 0; SGetParameter(GetCommandLine(), iPar, szParameter, _MAX_PATH); iPar++) + { if (SEqual2NoCase(szParameter, "/config:")) + { sConfigFilename.Copy(szParameter + 8); + } + else if (!verbose && SEqualNoCase(szParameter, "/verbose")) + { + verbose = true; + } + } // Config check Config.Init(); Config.Load(true, sConfigFilename.getData()); @@ -113,7 +122,7 @@ void C4Application::DoInit() C4Group_SetSortList(C4CFN_FLS); // Open log - LogSystem.OpenLog(); + LogSystem.OpenLog(verbose); // init system group if (!SystemGroup.Open(C4CFN_System)) @@ -204,7 +213,7 @@ void C4Application::DoInit() spdlog::info("Version: " C4VERSION " " C4_OS); // Initialize OpenGL - DDraw = DDrawInit(this, Config.Graphics.Engine); + DDraw = DDrawInit(this, LogSystem, Config.Graphics.Engine); if (!DDraw) { LogFatal(C4ResStrTableKey::IDS_ERR_DDRAW); Clear(); throw StartupException{LogSystem.GetFatalErrorString()}; } #if defined(_WIN32) && !defined(USE_CONSOLE) diff --git a/src/C4AudioSystem.cpp b/src/C4AudioSystem.cpp index 7a10cec7..25e608f7 100644 --- a/src/C4AudioSystem.cpp +++ b/src/C4AudioSystem.cpp @@ -13,9 +13,10 @@ * for the above references. */ +#include "C4Application.h" #include "C4AudioSystem.h" - #include "C4AudioSystemNone.h" +#include "C4Config.h" #include "C4Log.h" #ifdef USE_SDL_MIXER @@ -34,7 +35,7 @@ C4AudioSystem *C4AudioSystem::NewInstance( } catch (const std::runtime_error &e) { - CreateLogger("C4AudioSystem")->error(e.what()); + Application.LogSystem.CreateLogger(Config.Logging.AudioSystem)->error(e.what()); } #endif return new C4AudioSystemNone{}; diff --git a/src/C4AudioSystem.h b/src/C4AudioSystem.h index ac18a5ef..707154c2 100644 --- a/src/C4AudioSystem.h +++ b/src/C4AudioSystem.h @@ -17,6 +17,7 @@ #pragma once +#include "C4Log.h" #include "Standard.h" #include @@ -101,3 +102,5 @@ class C4AudioSystem public: static constexpr auto MaxChannels = 1024; }; + +C4LOGGERCONFIG_NAME_TYPE(C4AudioSystem); diff --git a/src/C4AudioSystemSdl.cpp b/src/C4AudioSystemSdl.cpp index d77cb1eb..e386eddc 100644 --- a/src/C4AudioSystemSdl.cpp +++ b/src/C4AudioSystemSdl.cpp @@ -13,8 +13,9 @@ * for the above references. */ +#include "C4Application.h" #include "C4AudioSystemSdl.h" - +#include "C4Config.h" #include "C4Log.h" #include "StdHelpers.h" @@ -144,7 +145,7 @@ C4AudioSystemSdl::C4AudioSystemSdl(const int maxChannels, const bool preferLinea assert(!instance); instance = this; - auto logger = CreateLogger("C4AudioSystemSdl"); + auto logger = Application.LogSystem.CreateLoggerWithDifferentName(Config.Logging.AudioSystem, "C4AudioSystem"); // Check SDL_mixer version SDL_version compile_version; diff --git a/src/C4Aul.h b/src/C4Aul.h index 603ec4c2..85a49485 100644 --- a/src/C4Aul.h +++ b/src/C4Aul.h @@ -431,6 +431,15 @@ class C4AulProfiler static void StopProfiling(); }; +C4LOGGERCONFIG_NAME_TYPE(C4AulProfiler); + +template<> +struct C4LoggerConfig::Defaults +{ + static constexpr spdlog::level::level_enum GuiLogLevel{spdlog::level::info}; + static constexpr bool ShowLoggerNameInGui{false}; +}; + // script class class C4AulScript { @@ -587,3 +596,14 @@ class C4AulScriptEngine : public C4AulScript friend class C4AulFunc; friend class C4AulParseState; }; + +class C4AulExec; + +C4LOGGERCONFIG_NAME_TYPE(C4AulExec); + +template<> +struct C4LoggerConfig::Defaults +{ + static constexpr auto GuiLogLevel = spdlog::level::info; + static constexpr bool ShowLoggerNameInGui{false}; +}; diff --git a/src/C4AulExec.cpp b/src/C4AulExec.cpp index 5c34d238..5ea2daea 100644 --- a/src/C4AulExec.cpp +++ b/src/C4AulExec.cpp @@ -1533,7 +1533,7 @@ void C4AulExec::StartTrace() if (iTraceStart < 0) { iTraceStart = ContextStackSize(); - traceLogger = CreateLogger("C4AulExec", {.GuiLogLevel = spdlog::level::info, .ShowLoggerNameInGui = false}); + traceLogger = Application.LogSystem.CreateLogger(Config.Logging.AulExec); } } @@ -1558,7 +1558,7 @@ void C4AulExec::StopProfiling() if (!fProfiling) return; fProfiling = false; // collect profiler times - C4AulProfiler Profiler{CreateLogger("C4AulProfiler", {.GuiLogLevel = spdlog::level::info, .ShowLoggerNameInGui = false})}; + C4AulProfiler Profiler{Application.LogSystem.CreateLogger(Config.Logging.AulProfiler)}; Profiler.CollectEntry(nullptr, tDirectExecTotal); pProfiledScript->CollectProfilerTimes(Profiler); Profiler.Show(); diff --git a/src/C4Config.cpp b/src/C4Config.cpp index 8a2600f4..975ed0ac 100644 --- a/src/C4Config.cpp +++ b/src/C4Config.cpp @@ -21,8 +21,12 @@ #include "C4Version.h" #ifdef C4ENGINE #include +#include "C4GameControl.h" #include #include +#include "C4Network2Reference.h" +#include "C4Network2UPnP.h" +#include "C4Record.h" #include "C4ResStrTable.h" #include #endif @@ -397,6 +401,21 @@ void C4ConfigToasts::CompileFunc(StdCompiler *comp) { comp->Value(mkNamingAdapt(ReadyCheck, "ReadyCheck", true)); } + +void C4ConfigLogging::CompileFunc(StdCompiler *const comp) +{ + comp->Value(mkNamingAdapt(LogLevelStdout, "LogLevelStdout", spdlog::level::info)); + + comp->Value(AudioSystem); + comp->Value(AulExec); + comp->Value(AulProfiler); + comp->Value(DDraw); + comp->Value(GameControl); + comp->Value(Network); + comp->Value(Network2HTTPClient); + comp->Value(Network2UPnP); + comp->Value(Playback); +} #endif C4Config::C4Config() @@ -849,6 +868,7 @@ void C4Config::CompileFunc(StdCompiler *pComp) pComp->Value(mkNamingAdapt(Startup, "Startup")); pComp->Value(mkNamingAdapt(Cooldowns, "Cooldowns")); pComp->Value(mkNamingAdapt(Toasts, "Toasts")); + pComp->Value(mkNamingAdapt(Logging, "Logging")); #endif } diff --git a/src/C4Config.h b/src/C4Config.h index f606994b..99486c4e 100644 --- a/src/C4Config.h +++ b/src/C4Config.h @@ -25,6 +25,7 @@ #ifdef C4ENGINE #include "C4AulScriptStrict.h" +#include "C4Log.h" #include "StdWindow.h" #endif @@ -299,6 +300,24 @@ class C4ConfigToasts void CompileFunc(StdCompiler *comp); }; +class C4ConfigLogging +{ +public: + spdlog::level::level_enum LogLevelStdout; + + C4LoggerConfig::Config AudioSystem; + C4LoggerConfig::Config AulExec; + C4LoggerConfig::Config AulProfiler; + C4LoggerConfig::Config DDraw; + C4LoggerConfig::Config GameControl; + C4LoggerConfig::Config Network; + C4LoggerConfig::Config Network2HTTPClient; + C4LoggerConfig::Config Network2UPnP; + C4LoggerConfig::Config Playback; + + void CompileFunc(StdCompiler *comp); +}; + #endif class C4Config @@ -321,6 +340,7 @@ class C4Config C4ConfigStartup Startup; C4ConfigCooldowns Cooldowns; C4ConfigToasts Toasts; + C4ConfigLogging Logging; #endif bool fConfigLoaded; // true if config has been successfully loaded StdStrBuf ConfigFilename; // set for configs loaded from a nondefault config file diff --git a/src/C4ForwardDeclarations.h b/src/C4ForwardDeclarations.h index eaa968e0..f0086237 100644 --- a/src/C4ForwardDeclarations.h +++ b/src/C4ForwardDeclarations.h @@ -15,6 +15,7 @@ #pragma once +class C4AudioSystem; class C4AulFunc; class C4Def; class C4DefList; diff --git a/src/C4Game.cpp b/src/C4Game.cpp index b7213a53..598dd43e 100644 --- a/src/C4Game.cpp +++ b/src/C4Game.cpp @@ -3270,10 +3270,6 @@ void C4Game::ParseCommandLine(const char *szCmdLine) if (SSearchNoCase(szCmdLine, "/console")) Application.isFullScreen = false; - // verbose - if (SSearchNoCase(szCmdLine, "/verbose")) - Application.LogSystem.SetVerbose(true); - // startup dialog required? Application.UseStartupDialog = Application.isFullScreen && !*DirectJoinAddress && !*ScenarioFilename && !RecordStream.getSize(); } diff --git a/src/C4GameControl.cpp b/src/C4GameControl.cpp index 3d8a7277..68d72c0c 100644 --- a/src/C4GameControl.cpp +++ b/src/C4GameControl.cpp @@ -43,7 +43,7 @@ C4GameControl::~C4GameControl() void C4GameControl::InitLogger() { - logger = CreateLogger("C4GameControl"); + logger = Application.LogSystem.CreateLogger(Config.Logging.GameControl); } bool C4GameControl::InitLocal(C4Client *pLocal) @@ -72,7 +72,7 @@ bool C4GameControl::InitNetwork(C4Client *pLocal) bool C4GameControl::InitReplay(C4Group &rGroup) { // open replay - pPlayback = new C4Playback(logger->clone("C4Playback")); + pPlayback = new C4Playback(Application.LogSystem.CreateLogger(Config.Logging.Playback)); if (!pPlayback->Open(rGroup)) { LogFatal(C4ResStrTableKey::IDS_ERR_REPLAYREAD); diff --git a/src/C4GameControl.h b/src/C4GameControl.h index 04076d9d..68ecf929 100644 --- a/src/C4GameControl.h +++ b/src/C4GameControl.h @@ -20,6 +20,7 @@ #include "C4Control.h" #include "C4GameControlNetwork.h" +#include "C4Log.h" #include "C4Network2Client.h" #include "C4Record.h" @@ -148,3 +149,5 @@ class C4GameControl C4ControlSyncCheck *GetSyncCheck(int32_t iTick); void RemoveOldSyncChecks(); }; + +C4LOGGERCONFIG_NAME_TYPE(C4GameControl); diff --git a/src/C4Log.cpp b/src/C4Log.cpp index 900448c5..224dbebb 100644 --- a/src/C4Log.cpp +++ b/src/C4Log.cpp @@ -296,20 +296,33 @@ C4LogSystem::C4LogSystem() logger->sinks().emplace_back(ringbufferSink); } -void C4LogSystem::OpenLog() +void C4LogSystem::OpenLog(const bool verbose) { #ifdef _WIN32 SetConsoleOutputCP(CP_UTF8); #endif - stdoutSink = std::static_pointer_cast(std::make_shared()); - stdoutSink->set_formatter(defaultPatternFormatter->clone()); - stdoutSink->set_level(spdlog::level::info); + this->verbose = verbose; - auto clonkLogSink = std::make_shared(defaultPatternFormatter->clone()); - clonkLogFD = clonkLogSink->GetFD(); + if (verbose) + { + loggerSilentGuiSink->set_level(spdlog::level::trace); + } - clonkToUtf8Sink = std::make_shared(std::initializer_list{stdoutSink, std::static_pointer_cast(clonkLogSink)}); + { + auto stdoutSink = std::make_shared(); + stdoutSink->set_formatter(defaultPatternFormatter->clone()); + stdoutSink->set_level(AdjustLevelIfVerbose(Config.Logging.LogLevelStdout)); + + auto clonkLogSink = std::make_shared(defaultPatternFormatter->clone()); + clonkLogFD = clonkLogSink->GetFD(); + + clonkToUtf8Sink = std::make_shared( + std::initializer_list{ + std::static_pointer_cast(std::move(stdoutSink)), + std::static_pointer_cast(std::move(clonkLogSink)) + }); + } loggerSilent->sinks().emplace_back(clonkToUtf8Sink); @@ -327,43 +340,6 @@ void C4LogSystem::OpenLog() loggerDebug->sinks().insert(loggerDebug->sinks().begin(), loggerDebugGuiSink); } -std::shared_ptr C4LogSystem::CreateLogger(std::string name, const C4LogSystemCreateLoggerOptions options) -{ - auto newLogger = std::make_shared(std::move(name)); - newLogger->set_level(spdlog::level::trace); - - if (options.GuiLogLevel == spdlog::level::n_levels && !options.ShowLoggerNameInGui) - { - newLogger->sinks().emplace_back(loggerSilentGuiSink); - } - else - { - const auto level = options.GuiLogLevel != spdlog::level::n_levels ? std::max(options.GuiLogLevel, loggerSilentGuiSink->level()) : loggerSilentGuiSink->level(); - newLogger->sinks().emplace_back(std::make_shared(level, options.ShowLoggerNameInGui)); - } - -#ifdef _WIN32 - newLogger->sinks().emplace_back(debugSink); -#endif - - newLogger->sinks().emplace_back(clonkToUtf8Sink); - - return newLogger; -} - -std::shared_ptr C4LogSystem::GetOrCreate(std::string name, C4LogSystemCreateLoggerOptions options) -{ - const std::lock_guard lock{getOrCreateMutex}; - if (const auto logger = spdlog::get(name); logger) - { - return logger; - } - - auto logger = CreateLogger(std::move(name), std::move(options)); - spdlog::register_logger(logger); - return logger; -} - void C4LogSystem::EnableDebugLog(const bool enable) { if (loggerDebugGuiSink) @@ -372,12 +348,6 @@ void C4LogSystem::EnableDebugLog(const bool enable) } } -void C4LogSystem::SetVerbose(const bool verbose) -{ - loggerSilentGuiSink->set_level(verbose ? spdlog::level::trace : spdlog::level::warn); - stdoutSink->set_level(verbose ? spdlog::level::trace : spdlog::level::info); -} - void C4LogSystem::AddFatalError(std::string message) { logger->critical("{}", LoadResStr(C4ResStrTableKey::IDS_ERR_FATAL, message)); @@ -427,6 +397,53 @@ void C4LogSystem::SetConsoleInputCharset(const std::int32_t charset) #endif +spdlog::level::level_enum C4LogSystem::AdjustLevelIfVerbose(const spdlog::level::level_enum level) const noexcept +{ + return verbose ? spdlog::level::trace : level; +} + +std::shared_ptr C4LogSystem::CreateLogger(std::string name, const C4LoggerConfig::ConfigBase config) +{ + auto newLogger = std::make_shared(std::move(name)); + newLogger->set_level(AdjustLevelIfVerbose(config.LogLevel)); + + spdlog::level::level_enum guiLogLevel; + if (verbose) + { + guiLogLevel = spdlog::level::trace; + } + else if (config.GuiLogLevel == spdlog::level::n_levels) + { + guiLogLevel = loggerSilentGuiSink->level(); + } + else + { + guiLogLevel = config.GuiLogLevel; + } + + newLogger->sinks().emplace_back(std::make_shared(guiLogLevel, config.ShowLoggerNameInGui)); + +#ifdef _WIN32 + newLogger->sinks().emplace_back(debugSink); +#endif + + newLogger->sinks().emplace_back(clonkToUtf8Sink); + + return newLogger; +} + +std::shared_ptr C4LogSystem::GetOrCreate(std::string name, C4LoggerConfig::ConfigBase config) +{ + const std::lock_guard lock{getOrCreateMutex}; + if (const auto logger = spdlog::get(name); logger) + { + return logger; + } + + auto logger = CreateLogger(std::move(name), std::move(config)); + spdlog::register_logger(logger); + return logger; +} void LogNTr(const spdlog::level::level_enum level, const std::string_view message) { @@ -444,11 +461,6 @@ bool DebugLog(const spdlog::level::level_enum level, const std::string_view mess return true; } -std::shared_ptr CreateLogger(std::string name, C4LogSystemCreateLoggerOptions options) -{ - return Application.LogSystem.CreateLogger(std::move(name), std::move(options)); -} - int GetLogFD() { return Application.LogSystem.GetLogFD(); diff --git a/src/C4Log.h b/src/C4Log.h index 02494e1c..3dc29089 100644 --- a/src/C4Log.h +++ b/src/C4Log.h @@ -20,6 +20,7 @@ #include "C4ResStrTable.h" +#include "StdAdaptors.h" #include #include @@ -31,6 +32,130 @@ #include #include +class C4ConfigLogging; + +namespace C4LoggerConfig +{ + template + struct Defaults + { + }; + + template + struct Name; + +#define C4LOGGERCONFIG_NAME_TYPE(type) template<> struct ::C4LoggerConfig::Name { static constexpr auto Value = #type; } + + template + concept HasName = requires { Name::Value; }; + + struct ConfigBase + { + spdlog::level::level_enum LogLevel; + spdlog::level::level_enum GuiLogLevel; + spdlog::level::level_enum FlushOn; + bool ShowLoggerNameInGui; + + constexpr bool operator==(const ConfigBase &other) const noexcept = default; + }; + + template + struct Config : ConfigBase + { + private: + struct Marker + { + explicit Marker() = default; + }; + + struct ConfigDefaults + { + static constexpr spdlog::level::level_enum LogLevel{spdlog::level::trace}; + static constexpr spdlog::level::level_enum GuiLogLevel{spdlog::level::n_levels}; + static constexpr spdlog::level::level_enum FlushOn{spdlog::level::warn}; + static constexpr bool ShowLoggerNameInGui{true}; + }; + + private: + Config() = default; + Config(const spdlog::level::level_enum logLevel, const spdlog::level::level_enum guiLogLevel, const spdlog::level::level_enum flushOn, const bool showLoggerNameInGui) + : ConfigBase{logLevel, guiLogLevel, flushOn, showLoggerNameInGui} + { + } + + public: + void CompileFunc(StdCompiler *const comp) requires HasName + { + comp->Value(mkNamingAdapt(mkParAdapt(*this, Marker{}), C4LoggerConfig::Name::Value, Default())); + } + + void CompileFunc(StdCompiler *const comp, Marker) requires HasName + { + comp->Value(mkNamingAdapt(LogLevel, "LogLevel", DefaultLogLevel())); + comp->Value(mkNamingAdapt(GuiLogLevel, "GuiLogLevel", DefaultGuiLogLevel())); + comp->Value(mkNamingAdapt(FlushOn, "FlushOn", DefaultFlushOn())); + comp->Value(mkNamingAdapt(ShowLoggerNameInGui, "ShowLoggerNameInGui", DefaultShowLoggerNameInGui())); + } + + constexpr bool operator==(const Config &other) const noexcept = default; + + private: + static consteval spdlog::level::level_enum DefaultLogLevel() noexcept + { + if constexpr (requires { Defaults::LogLevel; }) + { + return Defaults::LogLevel; + } + else + { + return ConfigDefaults::LogLevel; + } + } + + static consteval spdlog::level::level_enum DefaultGuiLogLevel() noexcept + { + if constexpr (requires { Defaults::GuiLogLevel; }) + { + return Defaults::GuiLogLevel; + } + else + { + return ConfigDefaults::GuiLogLevel; + } + } + + static consteval spdlog::level::level_enum DefaultFlushOn() noexcept + { + if constexpr (requires { Defaults::FlushOn; }) + { + return Defaults::FlushOn; + } + else + { + return ConfigDefaults::FlushOn; + } + } + + static consteval bool DefaultShowLoggerNameInGui() noexcept + { + if constexpr (requires { Defaults::ShowLoggerNameInGui; }) + { + return Defaults::ShowLoggerNameInGui; + } + else + { + return ConfigDefaults::ShowLoggerNameInGui; + } + } + + static constexpr Config Default() noexcept + { + return {DefaultLogLevel(), DefaultGuiLogLevel(), DefaultFlushOn(), DefaultShowLoggerNameInGui()}; + } + + friend class ::C4ConfigLogging; + }; +}; struct C4LogSystemCreateLoggerOptions { @@ -40,8 +165,6 @@ struct C4LogSystemCreateLoggerOptions class C4LogSystem { -public: - class LogSink : public spdlog::sinks::base_sink { public: @@ -122,7 +245,7 @@ class C4LogSystem C4LogSystem &operator=(C4LogSystem &&) = delete; public: - void OpenLog(); + void OpenLog(bool verbose); int GetLogFD() const noexcept { return clonkLogFD; } public: @@ -136,16 +259,36 @@ class C4LogSystem std::vector GetRingbufferLogEntries(); void ClearRingbuffer(); - std::shared_ptr CreateLogger(std::string name, C4LogSystemCreateLoggerOptions options = {}); - std::shared_ptr GetOrCreate(std::string name, C4LogSystemCreateLoggerOptions options = {}); + template + std::shared_ptr CreateLogger(C4LoggerConfig::Config &config) + { + return CreateLogger(C4LoggerConfig::Name::Value, config); + } + + + template + std::shared_ptr CreateLoggerWithDifferentName(C4LoggerConfig::Config &config, std::string name) + { + return CreateLogger(std::move(name), config); + } + + template + std::shared_ptr GetOrCreate(C4LoggerConfig::Config &config) + { + return GetOrCreate(C4LoggerConfig::Name::Value, config); + } void EnableDebugLog(bool enable); - void SetVerbose(bool verbose); #ifdef _WIN32 void SetConsoleInputCharset(std::int32_t charset); #endif +private: + spdlog::level::level_enum AdjustLevelIfVerbose(const spdlog::level::level_enum level) const noexcept; + std::shared_ptr CreateLogger(std::string name, C4LoggerConfig::ConfigBase config); + std::shared_ptr GetOrCreate(std::string name, C4LoggerConfig::ConfigBase config); + private: std::unique_ptr defaultPatternFormatter; std::shared_ptr logger; @@ -156,13 +299,13 @@ class C4LogSystem spdlog::sink_ptr debugSink; #endif std::shared_ptr clonkToUtf8Sink; - spdlog::sink_ptr stdoutSink; std::shared_ptr loggerSilentGuiSink; std::shared_ptr loggerDebugGuiSink; std::shared_ptr ringbufferSink; int clonkLogFD{-1}; + bool verbose{false}; std::vector fatalErrors; std::mutex getOrCreateMutex; }; @@ -231,8 +374,21 @@ void LogFatal(const C4ResStrTableKeyFormat...> id, Ar LogFatalNTr(LoadResStr(id, std::forward(args)...)); } - -std::shared_ptr CreateLogger(std::string name, C4LogSystemCreateLoggerOptions options = {}); - // Used to print a backtrace after a crash int GetLogFD(); + +inline void CompileFunc(spdlog::level::level_enum &level, StdCompiler *const comp) +{ + constexpr StdEnumEntry Values[] + { + {"trace", spdlog::level::trace}, + {"debug", spdlog::level::debug}, + {"info", spdlog::level::info}, + {"warn", spdlog::level::warn}, + {"error", spdlog::level::err}, + {"critical", spdlog::level::critical}, + {"off", spdlog::level::off}, + }; + + comp->Value(mkEnumAdaptT(level, Values)); +} diff --git a/src/C4Network2.cpp b/src/C4Network2.cpp index 00dcf897..e4a47a61 100644 --- a/src/C4Network2.cpp +++ b/src/C4Network2.cpp @@ -224,7 +224,7 @@ bool C4Network2::InitHost(bool fLobby) if (isEnabled()) Clear(); if (!Logger) { - Logger = CreateLogger("Network"); + Logger = Application.LogSystem.CreateLogger(Config.Logging.Network); } // initialize everything Status.Set(fLobby ? GS_Lobby : GS_Go, Game.Control.ControlTick); @@ -283,7 +283,7 @@ C4Network2::InitResult C4Network2::InitClient(const C4Network2Reference &Ref, bo if (isEnabled()) Clear(); if (!Logger) { - Logger = CreateLogger("Network"); + Logger = Application.LogSystem.CreateLogger(Config.Logging.Network); } // Get host core const C4ClientCore &HostCore = Ref.Parameters.Clients.getHost()->getCore(); diff --git a/src/C4Network2.h b/src/C4Network2.h index 0f0e04b5..62c8857b 100644 --- a/src/C4Network2.h +++ b/src/C4Network2.h @@ -469,6 +469,12 @@ class C4PacketJoinData : public C4PacketBase virtual void CompileFunc(StdCompiler *pComp) override; }; +template<> +struct C4LoggerConfig::Name +{ + static constexpr auto Value = "Network"; +}; + // shows ready check dialog or sends back information class C4PacketReadyCheck : public C4PacketBase diff --git a/src/C4Network2Reference.cpp b/src/C4Network2Reference.cpp index d96b1b03..02b45aa1 100644 --- a/src/C4Network2Reference.cpp +++ b/src/C4Network2Reference.cpp @@ -406,14 +406,18 @@ class C4Network2HTTPClientImplNetIO : public C4Network2HTTPClient::Impl, public static constexpr int C4Network2HTTPQueryTimeout{20}; // (s) }; +#define IMPL(type) static_cast>(std::make_unique(Application.LogSystem.CreateLoggerWithDifferentName(Config.Logging.Network2HTTPClient, #type))) + C4Network2HTTPClient::C4Network2HTTPClient() : impl{Config.Network.UseCurl - ? static_cast>(std::make_unique(CreateLogger("C4Network2HTTPClientImplCurl"))) - : static_cast>(std::make_unique(CreateLogger("C4Network2HTTPClientImplNetIO"))) + ? IMPL(C4Network2HTTPClientImplCurl) + : IMPL(C4Network2HTTPClientImplNetIO) } { } +#undef IMPL + C4Network2HTTPClient::~C4Network2HTTPClient() { Cancel({}); diff --git a/src/C4Network2Reference.h b/src/C4Network2Reference.h index 33e307ea..49eab32e 100644 --- a/src/C4Network2Reference.h +++ b/src/C4Network2Reference.h @@ -20,6 +20,7 @@ #include "C4Coroutine.h" #include "C4ForwardDeclarations.h" #include "C4HTTPClient.h" +#include "C4Log.h" #include "C4Network2.h" #include "C4Network2Client.h" @@ -166,6 +167,8 @@ class C4Network2HTTPClient : public StdSchedulerProc std::unique_ptr impl; }; +C4LOGGERCONFIG_NAME_TYPE(C4Network2HTTPClient); + // Loads references (mini-HTTP-client) class C4Network2RefClient : public C4Network2HTTPClient { diff --git a/src/C4Network2UPnP.h b/src/C4Network2UPnP.h index 805562eb..e17aca57 100644 --- a/src/C4Network2UPnP.h +++ b/src/C4Network2UPnP.h @@ -16,6 +16,7 @@ #pragma once +#include "C4Log.h" #include "C4Network2IO.h" #include @@ -35,3 +36,11 @@ class C4Network2UPnP private: std::unique_ptr impl; }; + +C4LOGGERCONFIG_NAME_TYPE(C4Network2UPnP); + +template<> +struct C4LoggerConfig::Defaults +{ + static constexpr spdlog::level::level_enum GuiLogLevel{spdlog::level::off}; +}; diff --git a/src/C4Network2UPnPMiniUPnPc.cpp b/src/C4Network2UPnPMiniUPnPc.cpp index 42c7b74e..52c48382 100644 --- a/src/C4Network2UPnPMiniUPnPc.cpp +++ b/src/C4Network2UPnPMiniUPnPc.cpp @@ -52,7 +52,7 @@ struct C4Network2UPnP::Impl }; public: - Impl() : logger{CreateLogger("C4Network2UPnP", {.GuiLogLevel = spdlog::level::off})} + Impl() : logger{Application.LogSystem.CreateLogger(Config.Logging.Network2UPnP)} { task = Init(); } diff --git a/src/C4Record.cpp b/src/C4Record.cpp index 34ceceeb..ac033e4e 100644 --- a/src/C4Record.cpp +++ b/src/C4Record.cpp @@ -1065,7 +1065,7 @@ void C4Playback::DebugRecError(const std::string_view error) bool C4Playback::StreamToRecord(const char *szStream, StdStrBuf *pRecordFile) { - auto logger = CreateLogger("C4Playback"); + auto logger = Application.LogSystem.CreateLogger(Config.Logging.Playback); // Load data StdBuf CompressedData; logger->info("Reading stream..."); diff --git a/src/C4Record.h b/src/C4Record.h index ea8c2613..0d5d7fc8 100644 --- a/src/C4Record.h +++ b/src/C4Record.h @@ -23,6 +23,7 @@ class C4Record; #include "C4Group.h" #include "C4Control.h" +#include "C4Log.h" #include "CStdFile.h" #include "Fixed.h" @@ -327,3 +328,5 @@ class C4Playback // demo playback #endif static bool StreamToRecord(const char *szStream, StdStrBuf *pRecord); }; + +C4LOGGERCONFIG_NAME_TYPE(C4Playback); diff --git a/src/StdDDraw2.cpp b/src/StdDDraw2.cpp index 1095f306..30bdaeb7 100644 --- a/src/StdDDraw2.cpp +++ b/src/StdDDraw2.cpp @@ -1297,7 +1297,7 @@ void CStdDDraw::SetBlitMode(uint32_t dwBlitMode) this->dwBlitMode = dwBlitMode & Config.Graphics.AllowedBlitModes; } -CStdDDraw *DDrawInit(CStdApp *pApp, int Engine) +CStdDDraw *DDrawInit(CStdApp *pApp, C4LogSystem &logSystem, int Engine) { // create engine switch (iGfxEngine = Engine) @@ -1310,7 +1310,7 @@ CStdDDraw *DDrawInit(CStdApp *pApp, int Engine) } if (!lpDDraw) return nullptr; // init it - if (!lpDDraw->Init(pApp)) + if (!lpDDraw->Init(pApp, logSystem)) { delete lpDDraw; return nullptr; @@ -1319,10 +1319,10 @@ CStdDDraw *DDrawInit(CStdApp *pApp, int Engine) return lpDDraw; } -bool CStdDDraw::Init(CStdApp *pApp) +bool CStdDDraw::Init(CStdApp *pApp, C4LogSystem &logSystem) { this->pApp = pApp; - logger = CreateLogger(std::string{GetEngineName()}); + logger = logSystem.CreateLoggerWithDifferentName(Config.Logging.DDraw, std::string{GetEngineName()}); logger->debug("Init DDraw"); logger->debug(" Create DirectDraw..."); diff --git a/src/StdDDraw2.h b/src/StdDDraw2.h index 01087f57..0ced7915 100644 --- a/src/StdDDraw2.h +++ b/src/StdDDraw2.h @@ -302,7 +302,7 @@ class CStdDDraw public: // General - bool Init(CStdApp *pApp); + bool Init(CStdApp *pApp, C4LogSystem &logSystem); virtual void Clear(); virtual void Default(); virtual CStdGLCtx *CreateContext(CStdWindow *, CStdApp *) { return nullptr; } @@ -426,4 +426,6 @@ class CStdDDraw friend class CPattern; }; -CStdDDraw *DDrawInit(CStdApp *pApp, int Engine); +C4LOGGERCONFIG_NAME_TYPE(CStdDDraw); + +CStdDDraw *DDrawInit(CStdApp *pApp, C4LogSystem &logSystem, int Engine);