Skip to content

Commit

Permalink
feat: add default logger (#41)
Browse files Browse the repository at this point in the history
When SFSE::Init is used, it will now initialize a default spdlog logger.
  • Loading branch information
qudix authored Sep 14, 2023
1 parent 99cf9ab commit 5bd5ee1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CommonLibSF/include/SFSE/API.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> a_fn);

PluginHandle GetPluginHandle() noexcept;
Expand Down
2 changes: 2 additions & 0 deletions CommonLibSF/include/SFSE/Interfaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {};
Expand Down
1 change: 1 addition & 0 deletions CommonLibSF/include/SFSE/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace SFSE::log
SFSE_MAKE_SOURCE_LOGGER(critical, critical);

[[nodiscard]] std::optional<std::filesystem::path> log_directory();
void init();
} // namespace SFSE::log

#undef SFSE_MAKE_SOURCE_LOGGER
5 changes: 4 additions & 1 deletion CommonLibSF/src/SFSE/API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 5 additions & 4 deletions CommonLibSF/src/SFSE/Interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
#include "SFSE/API.h"
#include "SFSE/Logger.h"

#include <Windows.h>

EXTERN_C IMAGE_DOS_HEADER __ImageBase;

namespace SFSE
{
REL::Version QueryInterface::RuntimeVersion() const
Expand Down Expand Up @@ -71,4 +67,9 @@ namespace SFSE
assert(this);
return reinterpret_cast<const detail::SFSETrampolineInterface*>(this);
}

const PluginVersionData* PluginVersionData::GetSingleton() noexcept
{
return reinterpret_cast<const PluginVersionData*>(WinAPI::GetProcAddress(WinAPI::GetCurrentModule(), "SFSEPlugin_Version"));
}
} // namespace SFSE
29 changes: 29 additions & 0 deletions CommonLibSF/src/SFSE/Logger.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "SFSE/Logger.h"
#include "SFSE/API.h"

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

#include <ShlObj.h>

namespace SFSE
Expand All @@ -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<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");
}
} // namespace log
} // namespace SFSE

0 comments on commit 5bd5ee1

Please sign in to comment.