Log window: stream the hook's logs over IPC into an in-app Log panel
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>
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
#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
|
||||
{
|
||||
|
||||
@@ -16,6 +19,7 @@ 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
|
||||
@@ -60,27 +64,37 @@ FILE* log_file_locked()
|
||||
|
||||
} // namespace
|
||||
|
||||
void set_log_ring(coop::LogRing* ring)
|
||||
{
|
||||
g_log_ring.store(ring, std::memory_order_release);
|
||||
}
|
||||
|
||||
void logf(const char* fmt, ...)
|
||||
{
|
||||
std::scoped_lock lock(g_log_mutex);
|
||||
FILE* f = log_file_locked();
|
||||
if (f == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
SYSTEMTIME st;
|
||||
GetLocalTime(&st);
|
||||
std::fprintf(f, "[%02u:%02u:%02u.%03u pid=%lu] ", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds,
|
||||
GetCurrentProcessId());
|
||||
|
||||
// Format the line once.
|
||||
char line[coop::kLogMsgLen];
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
std::vfprintf(f, fmt, args);
|
||||
std::vsnprintf(line, sizeof(line), fmt, args);
|
||||
va_end(args);
|
||||
|
||||
std::fputc('\n', f);
|
||||
std::fflush(f);
|
||||
// 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
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
// Lightweight file logger for diagnosing the injected hook from inside a game.
|
||||
//
|
||||
// We can't see stdout from an injected DLL, so route diagnostics to a file in
|
||||
// %TEMP%\coop_hook.log. Thread-safe, opened lazily, append-only. Intended for
|
||||
// development / bring-up; cheap enough to leave compiled in.
|
||||
// Lightweight logger for the injected hook. We can't see stdout from an injected
|
||||
// DLL, so each line is (a) streamed to the host over the shared log ring for the
|
||||
// in-app Log window, and (b) optionally written to %TEMP%\coop_hook.log (opt-in;
|
||||
// see debug_log.cpp). Thread-safe; cheap enough to leave compiled in.
|
||||
#pragma once
|
||||
|
||||
namespace coop
|
||||
{
|
||||
struct LogRing;
|
||||
}
|
||||
|
||||
namespace coop::hook
|
||||
{
|
||||
|
||||
// Append a printf-style line to %TEMP%\coop_hook.log (prefixed with pid + time).
|
||||
// Append a printf-style line to the log ring (if attached) and the file (if on).
|
||||
void logf(const char* fmt, ...);
|
||||
|
||||
// Attach/detach the host's shared log ring so lines stream to the Log window.
|
||||
void set_log_ring(coop::LogRing* ring);
|
||||
|
||||
} // namespace coop::hook
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
#include "audio_hook.hpp"
|
||||
#include "coop/audio_ring.hpp"
|
||||
#include "coop/log_ring.hpp"
|
||||
#include "coop/shared_memory.hpp"
|
||||
#include "debug_log.hpp"
|
||||
#include "focus_spoof.hpp"
|
||||
@@ -27,6 +28,7 @@ namespace
|
||||
coop::hook::IpcClient g_ipc;
|
||||
std::atomic<bool> g_running{true};
|
||||
coop::SharedMemory g_audio_shm; // the host's audio ring, opened when present
|
||||
coop::SharedMemory g_log_shm; // the host's log ring, opened when present
|
||||
|
||||
DWORD WINAPI worker_thread(LPVOID)
|
||||
{
|
||||
@@ -38,6 +40,24 @@ DWORD WINAPI worker_thread(LPVOID)
|
||||
coop::hook::logf("worker_thread: IPC connect FAILED (no host mapping); exiting");
|
||||
return 0;
|
||||
}
|
||||
// Attach the host's log ring first so the rest of bring-up streams to the Log
|
||||
// window. The host creates it at injection time; it's normally already there.
|
||||
{
|
||||
const std::wstring log_name = coop::log_ring_name(GetCurrentProcessId());
|
||||
if (g_log_shm.open(log_name, coop::log_ring_total_size(coop::kLogCapacity)))
|
||||
{
|
||||
auto* lr = g_log_shm.as<coop::LogRing>();
|
||||
if (coop::log_ring_valid(*lr))
|
||||
{
|
||||
coop::hook::set_log_ring(lr);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_log_shm.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
coop::hook::logf("worker_thread: IPC connected");
|
||||
|
||||
// The audio render-hook instantiates a COM enumerator on this thread.
|
||||
@@ -161,6 +181,7 @@ BOOL APIENTRY DllMain(HMODULE module, DWORD reason, LPVOID reserved)
|
||||
if (reserved == nullptr)
|
||||
{
|
||||
g_running.store(false, std::memory_order_relaxed);
|
||||
coop::hook::set_log_ring(nullptr);
|
||||
coop::hook::remove_focus_spoof();
|
||||
coop::hook::remove_xinput_hooks();
|
||||
coop::hook::remove_audio_hooks();
|
||||
|
||||
Reference in New Issue
Block a user