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.h File Reference

Capture stdout/stderr and redirect to logging system. More...

Go to the source code of this file.

Data Structures

struct  log_io_t
 Saved descriptors and temporary file used during stdout/stderr capture. More...

Macros

#define LOG_IO_IF(condition, prefix, block)
 Capture output from a code block and log it.
#define LOG_IO(prefix, block)

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

Capture stdout/stderr and redirect to logging system.

Provides functions and macros to capture stdout and stderr output and redirect it to the logging system instead of suppressing it.

Usage: // Capture specific code: log_io_t capture = log_io_start(); ffmpeg_function_that_prints_stuff(); log_io_stop(capture, "ffmpeg");

// Capture a code block (recommended): LOG_IO("ffmpeg", { ffmpeg_function_that_prints_stuff(); });

Definition in file io.h.

Macro Definition Documentation

◆ LOG_IO

#define LOG_IO ( prefix,
block )
Value:
LOG_IO_IF(true, prefix, block)
#define LOG_IO_IF(condition, prefix, block)
Capture output from a code block and log it.
Definition io.h:95

Definition at line 106 of file io.h.

Referenced by audio_start_duplex(), audio_stop_duplex(), audio_terminate_portaudio_final(), ffmpeg_decoder_create_stdin(), ffmpeg_decoder_create_with_request_options(), gpg_sign_with_key(), h265_decoder_create(), h265_encoder_destroy(), and yt_dlp_is_available().

◆ LOG_IO_IF

#define LOG_IO_IF ( condition,
prefix,
block )
Value:
do { \
bool _log_io_enabled = (condition); \
log_io_t _log_io = {0}; \
if (_log_io_enabled) \
_log_io = log_io_start(); \
block; \
if (_log_io_enabled) \
log_io_stop(_log_io, (prefix)); \
} while (asciichat_macro_block_continue())
log_io_t log_io_start(void)
Start capturing stdout and stderr.
Definition io.c:20
Saved descriptors and temporary file used during stdout/stderr capture.
Definition io.h:37

Capture output from a code block and log it.

This macro redirects stdout and stderr for the duration of the code block, then logs any captured output with the given prefix.

Example: LOG_IO("ffmpeg", { av_log_set_callback(NULL); // Use default ffmpeg logging process_file("input.mp4"); });

Parameters
prefixPrefix for log messages (const char *)
blockCode block to capture (must be valid C statement block)
Note
Prefix and block are only evaluated once
Variables in the enclosing scope are accessible in the block

Capture a block only when condition is true; execute the block once either way.

Note
The condition is evaluated once, before executing the block.

Definition at line 95 of file io.h.

95#define LOG_IO_IF(condition, prefix, block) \
96 do { \
97 bool _log_io_enabled = (condition); \
98 log_io_t _log_io = {0}; \
99 if (_log_io_enabled) \
100 _log_io = log_io_start(); \
101 block; \
102 if (_log_io_enabled) \
103 log_io_stop(_log_io, (prefix)); \
104 } while (asciichat_macro_block_continue())

Referenced by yt_dlp_extract_with_options().

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
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.