Harden the guessed-stream sample-rate measurement that produced wrong rates (e.g. 44100 read as ~46205). New rate_estimator.hpp measures over longer ~0.5 s windows, rejects any window that doesn't snap to a standard rate (standard rates are >8% apart, so a quantization/burst error big enough to miss one lands in no-man's-land, never on a wrong neighbour), and requires consensus across windows before committing. If consensus isn't reached it commits a low-confidence estimate (new AudioFormat_LowConfidence, shown red) rather than spinning or publishing garbage. Pure logic, unit-tested with adversarial cadences (rate_estimator_test) incl. the real 46205 bug value. Add log severity levels: hook logw/loge set LogRecord.level; the host Log window colors warnings amber and errors red. The low-confidence rate logs a warning. Protocol -> v15 (new format states); also reserves AudioFormat_Override. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
138 lines
3.0 KiB
C++
138 lines
3.0 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);
|
|
}
|
|
|
|
namespace
|
|
{
|
|
const char* level_tag(std::uint32_t level)
|
|
{
|
|
switch (level)
|
|
{
|
|
case coop::LogLevel_Warn:
|
|
return "WARN ";
|
|
case coop::LogLevel_Error:
|
|
return "ERROR";
|
|
default:
|
|
return "info ";
|
|
}
|
|
}
|
|
|
|
void vlog(std::uint32_t level, const char* fmt, va_list args)
|
|
{
|
|
// Format the line once.
|
|
char line[coop::kLogMsgLen];
|
|
std::vsnprintf(line, sizeof(line), fmt, 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(), level, 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] %s\n", st.wHour, st.wMinute, st.wSecond,
|
|
st.wMilliseconds, GetCurrentProcessId(), level_tag(level), line);
|
|
std::fflush(f);
|
|
}
|
|
}
|
|
} // namespace
|
|
|
|
void logf(const char* fmt, ...)
|
|
{
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
vlog(coop::LogLevel_Info, fmt, args);
|
|
va_end(args);
|
|
}
|
|
|
|
void logw(const char* fmt, ...)
|
|
{
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
vlog(coop::LogLevel_Warn, fmt, args);
|
|
va_end(args);
|
|
}
|
|
|
|
void loge(const char* fmt, ...)
|
|
{
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
vlog(coop::LogLevel_Error, fmt, args);
|
|
va_end(args);
|
|
}
|
|
|
|
} // namespace coop::hook
|