Skip to content

Commit

Permalink
Merge pull request #131 from shad0wshayd3-TES5/default-log
Browse files Browse the repository at this point in the history
optional SKSE::Init log, PluginVersionData::GetSingleton
  • Loading branch information
powerof3 authored Sep 7, 2024
2 parents 20276e9 + 2b9e07c commit 7292da8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
2 changes: 1 addition & 1 deletion include/SKSE/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace SKSE
{
void Init(const LoadInterface* a_intfc) noexcept;
void Init(const LoadInterface* a_intfc, const bool a_log = false) noexcept;
void RegisterForAPIInitEvent(std::function<void()> a_fn);

PluginHandle GetPluginHandle() noexcept;
Expand Down
35 changes: 26 additions & 9 deletions include/SKSE/Interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,30 @@ namespace SKSE
kVersionIndependentEx_NoStructUse = 1 << 0,
};

constexpr void AuthorEmail(std::string_view a_email) noexcept { SetCharBuffer(a_email, std::span{ supportEmail }); }
constexpr void PluginVersion(REL::Version a_version) noexcept { pluginVersion = a_version.pack(); }

[[nodiscard]] constexpr REL::Version GetPluginVersion() const noexcept { return REL::Version::unpack(pluginVersion); }

constexpr void PluginName(std::string_view a_plugin) noexcept { SetCharBuffer(a_plugin, std::span{ pluginName }); }

[[nodiscard]] constexpr std::string_view GetPluginName() const noexcept { return std::string_view{ pluginName }; }

constexpr void AuthorName(std::string_view a_name) noexcept { SetCharBuffer(a_name, std::span{ author }); }

[[nodiscard]] constexpr std::string_view GetAuthorName() const noexcept { return std::string_view{ author }; }

constexpr void AuthorEmail(std::string_view a_email) noexcept { SetCharBuffer(a_email, std::span{ supportEmail }); }

[[nodiscard]] constexpr std::string_view GetAuthorEmail() const noexcept { return std::string_view{ supportEmail }; }

constexpr void UsesAddressLibrary() noexcept { versionIndependence |= kVersionIndependent_AddressLibraryPostAE; }
constexpr void UsesSigScanning() noexcept { versionIndependence |= kVersionIndependent_Signatures; }
constexpr void UsesUpdatedStructs() noexcept { versionIndependence |= kVersionIndependent_StructsPost629; }

constexpr void UsesNoStructs() noexcept { versionIndependenceEx |= kVersionIndependentEx_NoStructUse; }

constexpr void MinimumRequiredXSEVersion(REL::Version a_version) noexcept { xseMinimum = a_version.pack(); }

constexpr void CompatibleVersions(std::initializer_list<REL::Version> a_versions) noexcept
{
assert(a_versions.size() < std::size(compatibleVersions) - 1);
Expand All @@ -394,14 +415,7 @@ namespace SKSE
[](const REL::Version& a_version) noexcept { return a_version.pack(); });
}

constexpr void MinimumRequiredXSEVersion(REL::Version a_version) noexcept { xseMinimum = a_version.pack(); }
constexpr void PluginName(std::string_view a_plugin) noexcept { SetCharBuffer(a_plugin, std::span{ pluginName }); }
constexpr void PluginVersion(REL::Version a_version) noexcept { pluginVersion = a_version.pack(); }
constexpr void UsesAddressLibrary() noexcept { versionIndependence |= kVersionIndependent_AddressLibraryPostAE; }
constexpr void UsesSigScanning() noexcept { versionIndependence |= kVersionIndependent_Signatures; }
constexpr void UsesUpdatedStructs() noexcept { versionIndependence |= kVersionIndependent_StructsPost629; }

constexpr void UsesNoStructs() noexcept { versionIndependenceEx |= kVersionIndependentEx_NoStructUse; }
[[nodiscard]] static const PluginVersionData* GetSingleton() noexcept;

const std::uint32_t dataVersion{ kVersion };
std::uint32_t pluginVersion = 0;
Expand Down Expand Up @@ -435,3 +449,6 @@ namespace SKSE
static_assert(sizeof(PluginVersionData) == 0x350);
#endif
}

#define SKSEPluginLoad(...) extern "C" [[maybe_unused]] __declspec(dllexport) bool SKSEPlugin_Load(__VA_ARGS__)
#define SKSEPluginVersion extern "C" [[maybe_unused]] __declspec(dllexport) constinit SKSE::PluginVersionData SKSEPlugin_Version
2 changes: 2 additions & 0 deletions include/SKSE/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ namespace SKSE::log

void add_papyrus_sink(std::regex a_filter);
void remove_papyrus_sink();

void init();
}

#undef SKSE_MAKE_SOURCE_LOGGER
6 changes: 5 additions & 1 deletion src/SKSE/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,16 @@ namespace SKSE
}
}

void Init(const LoadInterface* a_intfc) noexcept
void Init(const LoadInterface* a_intfc, const bool a_log) noexcept
{
if (!a_intfc) {
stl::report_and_fail("interface is null"sv);
}

if (a_log) {
log::init();
}

(void)REL::Module::get();
(void)REL::IDDatabase::get();

Expand Down
5 changes: 5 additions & 0 deletions src/SKSE/Interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,9 @@ namespace SKSE
assert(this);
return reinterpret_cast<const detail::SKSETrampolineInterface*>(this);
}

const PluginVersionData* PluginVersionData::GetSingleton() noexcept
{
return reinterpret_cast<const PluginVersionData*>(REX::W32::GetProcAddress(REX::W32::GetCurrentModule(), "SKSEPlugin_Version"));
}
}
30 changes: 30 additions & 0 deletions src/SKSE/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#include "SKSE/API.h"

#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/msvc_sink.h>

namespace SKSE
{
namespace Impl
Expand Down Expand Up @@ -113,5 +116,32 @@ namespace SKSE
}
});
}

void init()
{
auto path = log_directory();
if (!path) {
return;
}

const auto data = PluginVersionData::GetSingleton();
*path /= std::format("{}.log", data->GetPluginName());

std::vector<spdlog::sink_ptr> sinks{
std::make_shared<spdlog::sinks::basic_file_sink_mt>(path->string(), true),
std::make_shared<spdlog::sinks::msvc_sink_mt>()
};

auto logger = std::make_shared<spdlog::logger>("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");
}
}
}

0 comments on commit 7292da8

Please sign in to comment.