Add a shared log ring (common/coop/log_ring.hpp): a lossy multi-producer / single-consumer ring named coop_log_<pid>. The hook logs from several threads, so producers claim a slot with fetch_add and publish each record with a release store of its sequence; the consumer reads in order and tolerates losing the oldest lines if it falls a whole ring behind. The DLL's logf() now formats once and pushes every line to the ring (the file trace stays as an opt-in mirror); the worker attaches the ring right after IPC connect so bring-up is captured. The host (IpcServer) creates the ring at injection time and exposes drain_logs(); a new LogPanel pulls new lines each frame into a bounded rolling buffer and renders them with auto-scroll, a filter, and clear. Added to the View menu (and UiState.show_log). Verified against Phantom Brave via coop_audio_probe, which now also creates the ring and drains it: the full hook bring-up trace streamed over IPC. All four tests pass. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
101 lines
2.4 KiB
C++
101 lines
2.4 KiB
C++
#define _CRT_SECURE_NO_WARNINGS
|
|
#include "debug_log.hpp"
|
|
|
|
#include <atomic>
|
|
#include <cstdarg>
|
|
#include <cstdio>
|
|
#include <mutex>
|
|
|
|
#include <windows.h>
|
|
|
|
#include "coop/log_ring.hpp"
|
|
|
|
namespace coop::hook
|
|
{
|
|
|
|
namespace
|
|
{
|
|
|
|
std::mutex g_log_mutex;
|
|
FILE* g_log_file = nullptr;
|
|
bool g_log_tried = false;
|
|
std::atomic<coop::LogRing*> g_log_ring{nullptr};
|
|
|
|
// Logging is opt-in so an injected DLL doesn't write to disk in normal use.
|
|
// Enable it by setting the COOP_HOOK_LOG environment variable for the target, or
|
|
// (more practical for a Steam-launched game we can't set env on) by creating the
|
|
// sentinel file %TEMP%\coop_hook.log.on — every process sees the same %TEMP%, so
|
|
// the probe / debugger can flip it without touching the game's environment.
|
|
bool logging_enabled()
|
|
{
|
|
wchar_t buf[8] = {};
|
|
if (GetEnvironmentVariableW(L"COOP_HOOK_LOG", buf, 8) > 0)
|
|
{
|
|
return true;
|
|
}
|
|
wchar_t dir[MAX_PATH] = {};
|
|
const DWORD n = GetTempPathW(MAX_PATH, dir);
|
|
if (n != 0 && n < MAX_PATH)
|
|
{
|
|
const std::wstring sentinel = std::wstring(dir) + L"coop_hook.log.on";
|
|
return GetFileAttributesW(sentinel.c_str()) != INVALID_FILE_ATTRIBUTES;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
FILE* log_file_locked()
|
|
{
|
|
if (!g_log_tried)
|
|
{
|
|
g_log_tried = true;
|
|
if (logging_enabled())
|
|
{
|
|
wchar_t dir[MAX_PATH] = {};
|
|
const DWORD n = GetTempPathW(MAX_PATH, dir);
|
|
if (n != 0 && n < MAX_PATH)
|
|
{
|
|
std::wstring path = std::wstring(dir) + L"coop_hook.log";
|
|
g_log_file = _wfopen(path.c_str(), L"a");
|
|
}
|
|
}
|
|
}
|
|
return g_log_file;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void set_log_ring(coop::LogRing* ring)
|
|
{
|
|
g_log_ring.store(ring, std::memory_order_release);
|
|
}
|
|
|
|
void logf(const char* fmt, ...)
|
|
{
|
|
// Format the line once.
|
|
char line[coop::kLogMsgLen];
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
std::vsnprintf(line, sizeof(line), fmt, args);
|
|
va_end(args);
|
|
|
|
// Stream to the host's Log window over the shared ring (the primary sink).
|
|
if (coop::LogRing* ring = g_log_ring.load(std::memory_order_acquire))
|
|
{
|
|
coop::log_ring_push(*ring, GetCurrentProcessId(), GetTickCount64(), line);
|
|
}
|
|
|
|
// Also mirror to the file when the opt-in trace is enabled.
|
|
std::scoped_lock lock(g_log_mutex);
|
|
FILE* f = log_file_locked();
|
|
if (f != nullptr)
|
|
{
|
|
SYSTEMTIME st;
|
|
GetLocalTime(&st);
|
|
std::fprintf(f, "[%02u:%02u:%02u.%03u pid=%lu] %s\n", st.wHour, st.wMinute, st.wSecond,
|
|
st.wMilliseconds, GetCurrentProcessId(), line);
|
|
std::fflush(f);
|
|
}
|
|
}
|
|
|
|
} // namespace coop::hook
|