refactor(daemon): share the output ring-window helper
The embedded daemon and the xmrig manager each kept their own copy of the console output ring: append new bytes, trim the front to a cap at a newline boundary, and hand a reader everything since its offset. The two copies had drifted. The daemon tracked an absolute byte total so a reader's offset stayed valid across a front-trim, while the miner keyed its reader off the live buffer size, so a trim could silently rewind the console feed. - The append/trim and the since-offset logic now live in one small header, outputWindowSince and appendBounded, included by both. The trim keeps the newline-boundary cut and the guard that stops a lone trailing newline from erasing the whole buffer. - The xmrig manager now carries the same absolute byte total, so its reader resolves against the window the same way and survives a trim (and a process restart that clears the buffer but keeps the total). Tests cover feeding the ring well past the trim point while a reader polls each chunk (the feed is continuous and never dies at the trim), the lone-trailing-newline guard, and a cleared buffer with a retained total. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
// DragonX Wallet - ImGui Edition
|
||||
// Shared absolute-offset output ring helpers (daemon debug.log feed, xmrig console feed).
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
|
||||
namespace dragonx {
|
||||
namespace daemon {
|
||||
|
||||
// getOutputSince() over a buffer holding the LAST buffer.size() of `total` bytes ever appended.
|
||||
inline std::string outputWindowSince(const std::string& buffer, std::size_t total, std::size_t& offset)
|
||||
{
|
||||
const std::size_t base = total - buffer.size(); // absolute position of buffer[0]
|
||||
if (offset < base) offset = base; // trimmed before read: resume at what's left
|
||||
if (offset >= total) {
|
||||
offset = total;
|
||||
return {};
|
||||
}
|
||||
std::string result = buffer.substr(offset - base);
|
||||
offset = total;
|
||||
return result;
|
||||
}
|
||||
|
||||
// Append `n` bytes, advance absolute `total`, and trim to ~cap at a newline once size exceeds 1.25x cap.
|
||||
// The `nl < size-1` guard keeps a lone trailing newline from erasing the whole buffer.
|
||||
inline void appendBounded(std::string& buffer, std::size_t& total, const char* data, std::size_t n, std::size_t cap)
|
||||
{
|
||||
buffer.append(data, n);
|
||||
total += n;
|
||||
if (buffer.size() > cap + cap / 4) {
|
||||
const std::size_t trim_to = buffer.size() - cap;
|
||||
const std::size_t nl = buffer.find('\n', trim_to);
|
||||
if (nl != std::string::npos && nl < buffer.size() - 1) buffer.erase(0, nl + 1);
|
||||
else buffer.erase(0, trim_to);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace daemon
|
||||
} // namespace dragonx
|
||||
Reference in New Issue
Block a user