ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
io.c File Reference

IO capture implementation. More...

Go to the source code of this file.

Macros

#define LOG_IO_BUFFER_SIZE   8192

Functions

log_io_t log_io_start (void)
 Start capturing stdout and stderr.
void log_io_stop (log_io_t capture, const char *prefix)
 Stop capturing and log the accumulated output.

Detailed Description

IO capture implementation.

Definition in file io.c.

Macro Definition Documentation

◆ LOG_IO_BUFFER_SIZE

#define LOG_IO_BUFFER_SIZE   8192

Definition at line 14 of file io.c.

Referenced by log_io_stop().

Function Documentation

◆ log_io_start()

log_io_t log_io_start ( void )

Start capturing stdout and stderr.

Redirects both stdout and stderr to a temporary file so their output can be captured and logged. Call log_io_stop() to end capture and log the accumulated output.

Returns
Capture handle (use with log_io_stop)
Note
If capture fails, returns a handle with saved_stdout_fd = -1
Do not use stdout/stderr directly while capturing

Definition at line 20 of file io.c.

20 {
21 log_io_t capture = {
22 .saved_stdout_fd = -1, .saved_stderr_fd = -1, .output = NULL};
23
24 static_mutex_lock_named(&g_log_io_mutex, "log_io");
25
26 // A pipe cannot be drained until the wrapped call returns. A temporary file
27 // lets backend worker threads finish even when diagnostics exceed pipe
28 // capacity.
29 capture.output = platform_tmpfile();
30 if (!capture.output) {
31 static_mutex_unlock(&g_log_io_mutex);
32 return capture;
33 }
34
35 fflush(stdout);
36 fflush(stderr);
37 capture.saved_stdout_fd = platform_dup(STDOUT_FILENO);
38 capture.saved_stderr_fd = platform_dup(STDERR_FILENO);
39 if (capture.saved_stdout_fd < 0 || capture.saved_stderr_fd < 0)
40 goto fail;
41
42 int output_fd = platform_fileno(capture.output);
43 if (platform_dup2(output_fd, STDOUT_FILENO) != ASCIICHAT_OK)
44 goto fail;
45 if (platform_dup2(output_fd, STDERR_FILENO) != ASCIICHAT_OK) {
46 // Restore stdout if only the first redirection succeeded.
47 platform_dup2(capture.saved_stdout_fd, STDOUT_FILENO);
48 goto fail;
49 }
50 return capture;
51
52fail:
53 if (capture.saved_stdout_fd >= 0)
55 if (capture.saved_stderr_fd >= 0)
57 fclose(capture.output);
58 static_mutex_unlock(&g_log_io_mutex);
59 return (log_io_t){
60 .saved_stdout_fd = -1, .saved_stderr_fd = -1, .output = NULL};
61}
FILE * platform_tmpfile(void)
Definition filesystem.c:32
int platform_close(int fd)
Definition filesystem.c:91
@ ASCIICHAT_OK
Definition error_codes.h:50
Saved descriptors and temporary file used during stdout/stderr capture.
Definition io.h:37
FILE * output
Temporary capture file, deleted on close.
Definition io.h:40
int saved_stdout_fd
Saved stdout file descriptor.
Definition io.h:38
int saved_stderr_fd
Saved stderr file descriptor.
Definition io.h:39

References ASCIICHAT_OK, log_io_t::output, platform_close(), platform_tmpfile(), log_io_t::saved_stderr_fd, and log_io_t::saved_stdout_fd.

◆ log_io_stop()

void log_io_stop ( log_io_t capture,
const char * prefix )

Stop capturing and log the accumulated output.

Restores stdout and stderr to their original state, reads all captured output from the temporary file, and logs it at DEBUG level with the given prefix.

Parameters
captureCapture handle returned by log_io_start()
prefixPrefix to use in log messages (e.g., "ffmpeg", "portaudio")
Note
Safe to call with invalid handle (saved_stdout_fd = -1)
After calling this, the capture handle is invalidated

Definition at line 63 of file io.c.

63 {
64 if (capture.saved_stdout_fd < 0 || capture.saved_stderr_fd < 0) {
65 return; // Capture was not started successfully
66 }
67
68 // Flush before restoring so buffered backend output is captured.
69 fflush(stdout);
70 fflush(stderr);
71
72 // Restore stdout and stderr
73 platform_dup2(capture.saved_stdout_fd, STDOUT_FILENO);
74 platform_dup2(capture.saved_stderr_fd, STDERR_FILENO);
77
78 // Read and log the captured output
79 if (capture.output) {
80 char buffer[LOG_IO_BUFFER_SIZE];
81 size_t bytes_read;
82
83 // Replay only after restoring the descriptors, so logger output cannot feed
84 // back into this capture. fseek synchronizes the stream with descriptor
85 // writes.
86 if (fseek(capture.output, 0, SEEK_SET) != 0) {
87 fclose(capture.output);
88 return;
89 }
90 while ((bytes_read = fread(buffer, 1, sizeof(buffer) - 1, capture.output)) >
91 0) {
92 buffer[bytes_read] = '\0';
93
94 // Split by lines and log each line
95 char *line_start = buffer;
96 char *line_end;
97
98 while ((line_end = strchr(line_start, '\n')) != NULL) {
99 // Null-terminate the line (overwriting the newline)
100 *line_end = '\0';
101
102 // Log the line if it's not empty
103 if (line_start[0] != '\0') {
104 if (prefix) {
105 log_debug("[%s] %s", prefix, line_start);
106 } else {
107 log_debug("%s", line_start);
108 }
109 }
110
111 // Move to the next line
112 line_start = line_end + 1;
113 }
114
115 // Log any remaining partial line
116 if (line_start[0] != '\0') {
117 if (prefix) {
118 log_debug("[%s] %s", prefix, line_start);
119 } else {
120 log_debug("%s", line_start);
121 }
122 }
123 }
124
125 fclose(capture.output);
126 }
127
128 static_mutex_unlock(&g_log_io_mutex);
129}
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
#define LOG_IO_BUFFER_SIZE
Definition io.c:14

References log_debug, LOG_IO_BUFFER_SIZE, log_io_t::output, platform_close(), log_io_t::saved_stderr_fd, and log_io_t::saved_stdout_fd.