Skip to content

feat(logserver): replay log tail to clients on connect#7

Merged
tkgstrator merged 1 commit into
developfrom
feat/logserver-replay
Jun 22, 2026
Merged

feat(logserver): replay log tail to clients on connect#7
tkgstrator merged 1 commit into
developfrom
feat/logserver-replay

Conversation

@tkgstrator

Copy link
Copy Markdown
Contributor

What

When a client connects to the TCP log server (nc <device-ip> 18082), it now receives the last 100 KB of the sandbox log file before the live stream begins.

Why

Without this, any nc session started after launch misses all prior log output — including login-time crashes and boot-sequence messages. The race was especially painful on Chinlan where AirDrop was the only fallback.

How

  • ls_replay_to(fd) — seeks to max(0, fileSize - IPA_LOG_REPLAY_BYTES), skips to the next newline (no partial first line), then streams the rest synchronously before the fd is added to g_clients[]
  • IPALogServerSetReplayPath(NSString *) — dispatched onto g_queue so g_replayPath is always touched on the serial queue
  • IPALoggingInit() calls IPALogServerSetReplayPath(g_logSandbox) right after the path is finalised
  • Knob: -D IPA_LOG_REPLAY_BYTES=N (default 102400)
  • Zero impact on FINAL_RELEASE builds — replay code is inside #ifndef FINAL_RELEASE

Test plan

  • nc <device-ip> 18082 immediately after launch → should see boot log lines
  • nc <device-ip> 18082 while the app is mid-session → should see ~100 KB of history then live output
  • FINAL_RELEASE=1 build → logserver.m compiles to nothing (existing behaviour)

🤖 Generated with Claude Code

When a client connects to the TCP log server, it now receives the last
IPA_LOG_REPLAY_BYTES (default 100 KB) of the sandbox log file before
switching to the live stream.  This avoids the race where logs written
at process startup — including login-time crashes — are lost because no
nc session was open yet.

Implementation:
- ls_replay_to(fd): opens the log file, seeks to max(0, size - 100KB),
  skips to the next newline to avoid a partial first line, then streams
  the remainder synchronously before the client is admitted to g_clients[].
- IPALogServerSetReplayPath(NSString *): dispatched onto g_queue so
  g_replayPath access stays single-threaded.
- IPALoggingInit() calls IPALogServerSetReplayPath(g_logSandbox) once
  the path is finalised (after IPALogServerStart, before first IPALog).
- Knob: -D IPA_LOG_REPLAY_BYTES=N to override at build time.
- No change to FINAL_RELEASE builds (replay code compiled out with the
  rest of logserver).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 22, 2026 03:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a debug-only “replay-on-connect” feature to the TCP log server so new clients receive recent log history (tail of the sandbox log file) before switching to live streaming.

Changes:

  • Implement log tail replay (default 100 KB, newline-aligned) for newly connected logserver clients.
  • Add IPALogServerSetReplayPath(NSString *) API and wire it from IPALoggingInit() after the sandbox log path is finalized.
  • Introduce build-time knob IPA_LOG_REPLAY_BYTES to control replay size.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
logserver.m Adds replay-on-connect implementation and stores a replay file path used by new clients.
logserver.h Exposes IPALogServerSetReplayPath() in the debug-only logserver API.
logging.m Registers the sandbox log file path with the log server for replay in non-release builds.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread logserver.m
Comment on lines +131 to +136
while (rem > 0) {
ssize_t sent = send(fd, p, rem, 0);
if (sent <= 0) { close(fileFd); return; }
p += sent;
rem -= (size_t)sent;
}
Comment thread logserver.m
Comment on lines +152 to 155
// Replay history before admitting to the live stream so the client sees
// events that happened before it connected.
ls_replay_to(fd);
g_clients[g_clientCount++] = fd;
@tkgstrator
tkgstrator merged commit e210d20 into develop Jun 22, 2026
3 checks passed
@tkgstrator
tkgstrator deleted the feat/logserver-replay branch June 22, 2026 03:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants