From b20edc482a398c348dc830cbf4534cf00ea9ccb3 Mon Sep 17 00:00:00 2001 From: Qudix <17361645+Qudix@users.noreply.github.com> Date: Wed, 13 Sep 2023 13:54:54 -0500 Subject: [PATCH 1/4] feat: add default logging --- 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 | 30 +++++++++++++++++++++++++++ 5 files changed, 42 insertions(+), 5 deletions(-) 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..a6dfffbf 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 = true) 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..1e1f0421 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,32 @@ 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 /= data->GetPluginName(); + *path /= L".log"; + + 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("[%^%L%$] %v"); + } } // namespace log } // namespace SFSE From 2906f3b983a229804b24c0da881bf443f5452d81 Mon Sep 17 00:00:00 2001 From: Qudix <17361645+Qudix@users.noreply.github.com> Date: Wed, 13 Sep 2023 14:01:10 -0500 Subject: [PATCH 2/4] fix: missing args --- CommonLibSF/include/SFSE/API.h | 2 +- CommonLibSF/src/SFSE/API.cpp | 2 +- 2 files changed, 2 insertions(+), 2 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/src/SFSE/API.cpp b/CommonLibSF/src/SFSE/API.cpp index a6dfffbf..be33e530 100644 --- a/CommonLibSF/src/SFSE/API.cpp +++ b/CommonLibSF/src/SFSE/API.cpp @@ -47,7 +47,7 @@ namespace SFSE } } // namespace detail - void Init(const LoadInterface* a_intfc, bool a_log = true) noexcept + void Init(const LoadInterface* a_intfc, bool a_log) noexcept { stl_assert(a_intfc, "interface is null"sv); From 25c38f00f33f1b2e63db1ab8ef00b56a2ef7ec8f Mon Sep 17 00:00:00 2001 From: Qudix <17361645+Qudix@users.noreply.github.com> Date: Wed, 13 Sep 2023 14:16:47 -0500 Subject: [PATCH 3/4] fix: use std format for log file name --- CommonLibSF/src/SFSE/Logger.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CommonLibSF/src/SFSE/Logger.cpp b/CommonLibSF/src/SFSE/Logger.cpp index 1e1f0421..95e26062 100644 --- a/CommonLibSF/src/SFSE/Logger.cpp +++ b/CommonLibSF/src/SFSE/Logger.cpp @@ -32,8 +32,7 @@ namespace SFSE return; const auto data = PluginVersionData::GetSingleton(); - *path /= data->GetPluginName(); - *path /= L".log"; + *path /= std::format("{}.log", data->GetPluginName()); std::vector sinks{ std::make_shared(path->string(), true), From b634213214121fc660ff112a6c97ed962cbabb10 Mon Sep 17 00:00:00 2001 From: Qudix <17361645+Qudix@users.noreply.github.com> Date: Wed, 13 Sep 2023 15:32:31 -0500 Subject: [PATCH 4/4] feat: show time and thread id in log --- CommonLibSF/src/SFSE/Logger.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CommonLibSF/src/SFSE/Logger.cpp b/CommonLibSF/src/SFSE/Logger.cpp index 95e26062..7fa31801 100644 --- a/CommonLibSF/src/SFSE/Logger.cpp +++ b/CommonLibSF/src/SFSE/Logger.cpp @@ -48,7 +48,7 @@ namespace SFSE logger->flush_on(spdlog::level::info); #endif spdlog::set_default_logger(std::move(logger)); - spdlog::set_pattern("[%^%L%$] %v"); + spdlog::set_pattern("[%T.%e] [%=5t] [%L] %v"); } } // namespace log } // namespace SFSE