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:
75
host/src/log_panel.cpp
Normal file
75
host/src/log_panel.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "log_panel.hpp"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
#include "imgui.h"
|
||||
|
||||
#include "injection_panel.hpp"
|
||||
|
||||
namespace coop
|
||||
{
|
||||
|
||||
void LogPanel::add_line(const LogRecord& rec)
|
||||
{
|
||||
if (first_millis_ == 0)
|
||||
{
|
||||
first_millis_ = rec.millis;
|
||||
}
|
||||
const double secs = static_cast<double>(rec.millis - first_millis_) / 1000.0;
|
||||
|
||||
char buf[256];
|
||||
std::snprintf(buf, sizeof(buf), "[%8.3f] %s", secs, rec.text);
|
||||
lines_.emplace_back(buf);
|
||||
while (lines_.size() > kMaxLines)
|
||||
{
|
||||
lines_.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void LogPanel::pull(InjectionPanel& injection)
|
||||
{
|
||||
injection.drain_logs([this](const LogRecord& rec) { add_line(rec); });
|
||||
}
|
||||
|
||||
void LogPanel::draw()
|
||||
{
|
||||
ImGui::SetNextWindowPos(ImVec2(24, 760), ImGuiCond_FirstUseEver);
|
||||
ImGui::SetNextWindowSize(ImVec2(720, 240), ImGuiCond_FirstUseEver);
|
||||
ImGui::Begin("Log");
|
||||
|
||||
if (ImGui::Button("Clear"))
|
||||
{
|
||||
lines_.clear();
|
||||
first_millis_ = 0;
|
||||
}
|
||||
ImGui::SameLine();
|
||||
ImGui::Checkbox("Auto-scroll", &autoscroll_);
|
||||
ImGui::SameLine();
|
||||
ImGui::SetNextItemWidth(-1.0f);
|
||||
ImGui::InputTextWithHint("##logfilter", "filter...", filter_, sizeof(filter_));
|
||||
|
||||
ImGui::Separator();
|
||||
if (ImGui::BeginChild("loglines", ImVec2(0, 0), ImGuiChildFlags_None, ImGuiWindowFlags_HorizontalScrollbar))
|
||||
{
|
||||
const bool has_filter = filter_[0] != '\0';
|
||||
for (const std::string& line : lines_)
|
||||
{
|
||||
if (has_filter && line.find(filter_) == std::string::npos)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
ImGui::TextUnformatted(line.c_str());
|
||||
}
|
||||
// Stick to the bottom while new lines arrive (unless the user scrolled up).
|
||||
if (autoscroll_ && ImGui::GetScrollY() >= ImGui::GetScrollMaxY() - 1.0f)
|
||||
{
|
||||
ImGui::SetScrollHereY(1.0f);
|
||||
}
|
||||
}
|
||||
ImGui::EndChild();
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
} // namespace coop
|
||||
Reference in New Issue
Block a user