Add RED4ext logging shim
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
#include <RED4ext/Api/ApiVersion.hpp>
|
||||
#include <RED4ext/Api/v1/EMainReason.hpp>
|
||||
#include <RED4ext/Api/v1/PluginHandle.hpp>
|
||||
#include <RED4ext/Api/v1/PluginInfo.hpp>
|
||||
#include <RED4ext/Api/v1/Runtime.hpp>
|
||||
#include <RED4ext/Api/v1/Version.hpp>
|
||||
#include <RED4ext/Common.hpp>
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
#include <mutex>
|
||||
#include <sstream>
|
||||
#include <string_view>
|
||||
|
||||
namespace RED4ext::v1
|
||||
{
|
||||
struct Sdk;
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
HMODULE gModule = nullptr;
|
||||
std::mutex gLogMutex;
|
||||
|
||||
std::filesystem::path GetLogPath()
|
||||
{
|
||||
wchar_t modulePath[MAX_PATH]{};
|
||||
if (gModule && GetModuleFileNameW(gModule, modulePath, MAX_PATH) > 0)
|
||||
{
|
||||
auto path = std::filesystem::path(modulePath);
|
||||
return path.parent_path() / L"EdgeWeightGPS.log";
|
||||
}
|
||||
|
||||
return std::filesystem::path(L"red4ext") / L"plugins" / L"EdgeWeightGPS" / L"EdgeWeightGPS.log";
|
||||
}
|
||||
|
||||
void Log(std::string_view aMessage)
|
||||
{
|
||||
std::lock_guard lock(gLogMutex);
|
||||
|
||||
auto now = std::chrono::system_clock::now();
|
||||
auto time = std::chrono::system_clock::to_time_t(now);
|
||||
std::tm localTime{};
|
||||
localtime_s(&localTime, &time);
|
||||
|
||||
std::ofstream out(GetLogPath(), std::ios::app);
|
||||
if (!out)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
out << std::put_time(&localTime, "%Y-%m-%d %H:%M:%S") << " " << aMessage << "\n";
|
||||
}
|
||||
|
||||
const char* ReasonToString(RED4ext::v1::EMainReason aReason)
|
||||
{
|
||||
switch (aReason)
|
||||
{
|
||||
case RED4ext::v1::EMainReason::Load:
|
||||
return "Load";
|
||||
case RED4ext::v1::EMainReason::Unload:
|
||||
return "Unload";
|
||||
}
|
||||
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
BOOL APIENTRY DllMain(HMODULE aModule, DWORD aReason, LPVOID)
|
||||
{
|
||||
if (aReason == DLL_PROCESS_ATTACH)
|
||||
{
|
||||
gModule = aModule;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
RED4EXT_C_EXPORT bool RED4EXT_CALL Main(RED4ext::v1::PluginHandle aHandle, RED4ext::v1::EMainReason aReason,
|
||||
const RED4ext::v1::Sdk* aSdk)
|
||||
{
|
||||
std::ostringstream line;
|
||||
line << "EdgeWeightGPS Main reason=" << ReasonToString(aReason) << " handle=" << aHandle << " sdk=" << aSdk;
|
||||
Log(line.str());
|
||||
return true;
|
||||
}
|
||||
|
||||
RED4EXT_C_EXPORT void RED4EXT_CALL Query(RED4ext::v1::PluginInfo* aInfo)
|
||||
{
|
||||
aInfo->name = L"EdgeWeightGPS";
|
||||
aInfo->author = L"salt+Codex";
|
||||
aInfo->version = RED4ext::v1::SemVer{0, 1, 0, {0, 0}};
|
||||
aInfo->runtime = RED4ext::v1::FileVer{3, 0, 80, 51928};
|
||||
aInfo->sdk = RED4ext::v1::SemVer{RED4EXT_VER_MAJOR, RED4EXT_VER_MINOR, RED4EXT_VER_PATCH, {0, 0}};
|
||||
}
|
||||
|
||||
RED4EXT_C_EXPORT uint32_t RED4EXT_CALL Supports()
|
||||
{
|
||||
return RED4EXT_API_VERSION_1;
|
||||
}
|
||||
Reference in New Issue
Block a user