From 5bd5ee10e644d55d96ed756453d7f5782a7f865e Mon Sep 17 00:00:00 2001 From: Qudix <17361645+Qudix@users.noreply.github.com> Date: Wed, 13 Sep 2023 21:05:00 -0500 Subject: [PATCH] feat: add default logger (#41) When SFSE::Init is used, it will now initialize a default spdlog logger. --- CommonLibSF/include/SFSE/API.h | 2 +- CommonLibSF/include/SFSE/Interfaces.h | 2 ++ CommonLibSF/include/SFSE/Logger.h | 1 + CommonLibSF/src/SFSE/API.cpp | 5 ++++- CommonLibSF/src/SFSE/Interfaces.cpp | 9 +++++---- CommonLibSF/src/SFSE/Logger.cpp | 29 +++++++++++++++++++++++++++ 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/CommonLibSF/include/SFSE/API.h b/CommonLibSF/include/SFSE/API.h index 74227313..e83bc981 100644 --- a/CommonLibSF/include/SFSE/API.h +++ b/CommonLibSF/include/SFSE/API.h @@ -9,7 +9,7 @@ namespace SFSE { - void Init(const LoadInterface* a_intfc) noexcept; + void Init(const LoadInterface* a_intfc, bool a_log = true) noexcept; void RegisterForAPIInitEvent(std::function a_fn); PluginHandle GetPluginHandle() noexcept; diff --git a/CommonLibSF/include/SFSE/Interfaces.h b/CommonLibSF/include/SFSE/Interfaces.h index c8b25a54..eecd8795 100644 --- a/CommonLibSF/include/SFSE/Interfaces.h +++ b/CommonLibSF/include/SFSE/Interfaces.h @@ -134,6 +134,8 @@ namespace SFSE constexpr void MinimumRequiredXSEVersion(REL::Version a_version) noexcept { xseMinimum = a_version.pack(); } + [[nodiscard]] static const PluginVersionData* GetSingleton() noexcept; + const std::uint32_t dataVersion{ kVersion }; std::uint32_t pluginVersion = 0; char pluginName[256] = {}; diff --git a/CommonLibSF/include/SFSE/Logger.h b/CommonLibSF/include/SFSE/Logger.h index 00c0332d..61fd47b6 100644 --- a/CommonLibSF/include/SFSE/Logger.h +++ b/CommonLibSF/include/SFSE/Logger.h @@ -23,6 +23,7 @@ namespace SFSE::log SFSE_MAKE_SOURCE_LOGGER(critical, critical); [[nodiscard]] std::optional log_directory(); + void init(); } // namespace SFSE::log #undef SFSE_MAKE_SOURCE_LOGGER diff --git a/CommonLibSF/src/SFSE/API.cpp b/CommonLibSF/src/SFSE/API.cpp index a6e1b054..be33e530 100644 --- a/CommonLibSF/src/SFSE/API.cpp +++ b/CommonLibSF/src/SFSE/API.cpp @@ -47,10 +47,13 @@ namespace SFSE } } // namespace detail - void Init(const LoadInterface* a_intfc) noexcept + void Init(const LoadInterface* a_intfc, bool a_log) noexcept { stl_assert(a_intfc, "interface is null"sv); + if (a_log) + log::init(); + (void)REL::Module::get(); auto& storage = detail::APIStorage::get(); diff --git a/CommonLibSF/src/SFSE/Interfaces.cpp b/CommonLibSF/src/SFSE/Interfaces.cpp index 12654367..4fbf0916 100644 --- a/CommonLibSF/src/SFSE/Interfaces.cpp +++ b/CommonLibSF/src/SFSE/Interfaces.cpp @@ -2,10 +2,6 @@ #include "SFSE/API.h" #include "SFSE/Logger.h" -#include - -EXTERN_C IMAGE_DOS_HEADER __ImageBase; - namespace SFSE { REL::Version QueryInterface::RuntimeVersion() const @@ -71,4 +67,9 @@ namespace SFSE assert(this); return reinterpret_cast(this); } + + const PluginVersionData* PluginVersionData::GetSingleton() noexcept + { + return reinterpret_cast(WinAPI::GetProcAddress(WinAPI::GetCurrentModule(), "SFSEPlugin_Version")); + } } // namespace SFSE diff --git a/CommonLibSF/src/SFSE/Logger.cpp b/CommonLibSF/src/SFSE/Logger.cpp index 005683ba..7fa31801 100644 --- a/CommonLibSF/src/SFSE/Logger.cpp +++ b/CommonLibSF/src/SFSE/Logger.cpp @@ -1,6 +1,9 @@ #include "SFSE/Logger.h" #include "SFSE/API.h" +#include +#include + #include namespace SFSE @@ -21,5 +24,31 @@ namespace SFSE path /= "My Games\\Starfield\\SFSE\\Logs"; return path; } + + void init() + { + auto path = log_directory(); + if (!path) + return; + + const auto data = PluginVersionData::GetSingleton(); + *path /= std::format("{}.log", data->GetPluginName()); + + std::vector sinks{ + std::make_shared(path->string(), true), + std::make_shared() + }; + + auto logger = std::make_shared("global", sinks.begin(), sinks.end()); +#ifndef NDEBUG + logger->set_level(spdlog::level::debug); + logger->flush_on(spdlog::level::debug); +#else + logger->set_level(spdlog::level::info); + logger->flush_on(spdlog::level::info); +#endif + spdlog::set_default_logger(std::move(logger)); + spdlog::set_pattern("[%T.%e] [%=5t] [%L] %v"); + } } // namespace log } // namespace SFSE