ascii-chat 0.11.33
Video chat in your terminal
Loading...
Searching...
No Matches
tests/logging.c
Go to the documentation of this file.
1
7#include <stdint.h>
8#include <ascii-chat/atomic.h>
9#include <stdbool.h>
10#include <ascii-chat/log/log.h>
11
12#ifndef _WIN32
13// POSIX-only test utilities for redirecting stdout/stderr
14
16#include <stdio.h>
17#include <unistd.h>
18#include <fcntl.h>
19
20// Global variables to store original file descriptors for restoration
21static int original_stdout_fd = -1;
22static int original_stderr_fd = -1;
23static int dev_null_fd = -1;
24static bool logging_disabled = false;
25
33int test_logging_disable(bool disable_stdout, bool disable_stderr) {
34 // If already disabled, return success
35 if (logging_disabled) {
36 return 0;
37 }
38
39 // Open /dev/null for writing
40 dev_null_fd = platform_open("test-logging", "/dev/null", PLATFORM_O_WRONLY);
41 if (dev_null_fd == -1) {
42 return -1;
43 }
44
45 // Save original file descriptors if we need to redirect them
46 if (disable_stdout) {
47 original_stdout_fd = dup(STDOUT_FILENO);
48 if (original_stdout_fd == -1) {
49 close(dev_null_fd);
50 dev_null_fd = -1;
51 return -1;
52 }
53 dup2(dev_null_fd, STDOUT_FILENO);
54 // Reopen stdout to use the redirected file descriptor
55 (void)freopen("/dev/null", "w", stdout);
56 // Make stdout unbuffered to ensure immediate redirection
57 (void)setvbuf(stdout, NULL, _IONBF, 0);
58 }
59
60 if (disable_stderr) {
61 original_stderr_fd = dup(STDERR_FILENO);
62 if (original_stderr_fd == -1) {
63 // Clean up stdout if it was redirected
64 if (disable_stdout && original_stdout_fd != -1) {
65 dup2(original_stdout_fd, STDOUT_FILENO);
66 close(original_stdout_fd);
67 original_stdout_fd = -1;
68 }
69 close(dev_null_fd);
70 dev_null_fd = -1;
71 return -1;
72 }
73 dup2(dev_null_fd, STDERR_FILENO);
74 // Reopen stderr to use the redirected file descriptor
75 (void)freopen("/dev/null", "w", stderr);
76 // Make stderr unbuffered to ensure immediate redirection
77 (void)setvbuf(stderr, NULL, _IONBF, 0);
78 }
79
80 logging_disabled = true;
81 return 0;
82}
83
90 // If not disabled, return success
91 if (!logging_disabled) {
92 return 0;
93 }
94
95 // Restore original stdout
96 if (original_stdout_fd != -1) {
97 dup2(original_stdout_fd, STDOUT_FILENO);
98 // Reopen stdout to use the restored file descriptor
99 (void)freopen("/dev/stdout", "w", stdout);
100 // Restore line buffering for stdout
101 (void)setvbuf(stdout, NULL, _IOLBF, 0);
102 close(original_stdout_fd);
103 original_stdout_fd = -1;
104 }
105
106 // Restore original stderr
107 if (original_stderr_fd != -1) {
108 dup2(original_stderr_fd, STDERR_FILENO);
109 // Reopen stderr to use the restored file descriptor
110 (void)freopen("/dev/stderr", "w", stderr);
111 // Restore unbuffered mode for stderr
112 (void)setvbuf(stderr, NULL, _IONBF, 0);
113 close(original_stderr_fd);
114 original_stderr_fd = -1;
115 }
116
117 // Close /dev/null file descriptor
118 if (dev_null_fd != -1) {
119 close(dev_null_fd);
120 dev_null_fd = -1;
121 }
122
123 logging_disabled = false;
124 return 0;
125}
126
133 return logging_disabled;
134}
135
136#else
137// Windows stub implementations
138
146int test_logging_disable(bool disable_stdout, bool disable_stderr) {
147 (void)disable_stdout;
148 (void)disable_stderr;
149 return 0; // Not implemented on Windows
150}
151
157int test_logging_restore(void) {
158 return 0; // Not implemented on Windows
159}
160
166bool test_logging_is_disabled(void) {
167 return false;
168}
169#endif // !_WIN32
⚛️ Atomic operations abstraction layer with debug tracking
Cross-platform filesystem operations.
int platform_open(const char *name, const char *pathname, int flags,...)
Safe file open (open replacement)
#define PLATFORM_O_WRONLY
Definition filesystem.h:83
int test_logging_disable(bool disable_stdout, bool disable_stderr)
Disable stdout/stderr output for quiet test execution.
int test_logging_restore(void)
Restore stdout/stderr output after test logging disable.
bool test_logging_is_disabled(void)
Check if logging is currently disabled.
📝 Logging API with multiple log levels and terminal output control
ClangTool/LibTooling compatibility shim for stdbool.h.