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
Go to the documentation of this file.
1
5
7#include <ascii-chat/log/io.h>
9#include <libplatform/compiler.h>
10#include <libplatform/system.h>
11#include <libplatform/terminal.h>
12#include <string.h>
13
14#define LOG_IO_BUFFER_SIZE 8192
15
16/* stdout/stderr are process-global. Serialise descriptor redirection so a
17 * startup/shutdown worker cannot restore another capture's descriptors. */
18static static_mutex_t g_log_io_mutex = STATIC_MUTEX_INIT;
19
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}
62
63void log_io_stop(log_io_t capture, const char *prefix) {
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}
Platform initialization and static synchronization helpers.
FILE * platform_tmpfile(void)
Definition filesystem.c:32
int platform_close(int fd)
Definition filesystem.c:91
@ ASCIICHAT_OK
Definition error_codes.h:50
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:565
#define STATIC_MUTEX_INIT
log_io_t log_io_start(void)
Start capturing stdout and stderr.
Definition io.c:20
void log_io_stop(log_io_t capture, const char *prefix)
Stop capturing and log the accumulated output.
Definition io.c:63
#define LOG_IO_BUFFER_SIZE
Definition io.c:14
Capture stdout/stderr and redirect to logging system.
📝 Logging API with multiple log levels and terminal output control
🔤 String Manipulation and Shell Escaping Utilities
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
Static mutex structure for global mutexes requiring static initialization.
Definition common/sync.h:51