ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
tests/logging.c
Go to the documentation of this file.
1
7#include <stdint.h>
8#include <stdatomic.h>
9#include <stdbool.h>
10#include "log/logging.h"
11
12#ifndef _WIN32
13// POSIX-only test utilities for redirecting stdout/stderr
14
15#include "platform/file.h"
16#include "platform/util.h"
17#include <stdio.h>
18#include <unistd.h>
19#include <fcntl.h>
20
21// Global variables to store original file descriptors for restoration
22static int original_stdout_fd = -1;
23static int original_stderr_fd = -1;
24static int dev_null_fd = -1;
25static bool logging_disabled = false;
26
34int test_logging_disable(bool disable_stdout, bool disable_stderr) {
35 // If already disabled, return success
36 if (logging_disabled) {
37 return 0;
38 }
39
40 // Open /dev/null for writing
41 dev_null_fd = platform_open("/dev/null", PLATFORM_O_WRONLY);
42 if (dev_null_fd == -1) {
43 return -1;
44 }
45
46 // Save original file descriptors if we need to redirect them
47 if (disable_stdout) {
48 original_stdout_fd = dup(STDOUT_FILENO);
49 if (original_stdout_fd == -1) {
50 close(dev_null_fd);
51 dev_null_fd = -1;
52 return -1;
53 }
54 dup2(dev_null_fd, STDOUT_FILENO);
55 // Reopen stdout to use the redirected file descriptor
56 (void)freopen("/dev/null", "w", stdout);
57 // Make stdout unbuffered to ensure immediate redirection
58 (void)setvbuf(stdout, NULL, _IONBF, 0);
59 }
60
61 if (disable_stderr) {
62 original_stderr_fd = dup(STDERR_FILENO);
63 if (original_stderr_fd == -1) {
64 // Clean up stdout if it was redirected
65 if (disable_stdout && original_stdout_fd != -1) {
66 dup2(original_stdout_fd, STDOUT_FILENO);
67 close(original_stdout_fd);
68 original_stdout_fd = -1;
69 }
70 close(dev_null_fd);
71 dev_null_fd = -1;
72 return -1;
73 }
74 dup2(dev_null_fd, STDERR_FILENO);
75 // Reopen stderr to use the redirected file descriptor
76 (void)freopen("/dev/null", "w", stderr);
77 // Make stderr unbuffered to ensure immediate redirection
78 (void)setvbuf(stderr, NULL, _IONBF, 0);
79 }
80
81 logging_disabled = true;
82 return 0;
83}
84
91 // If not disabled, return success
92 if (!logging_disabled) {
93 return 0;
94 }
95
96 // Restore original stdout
97 if (original_stdout_fd != -1) {
98 dup2(original_stdout_fd, STDOUT_FILENO);
99 // Reopen stdout to use the restored file descriptor
100 (void)freopen("/dev/stdout", "w", stdout);
101 // Restore line buffering for stdout
102 (void)setvbuf(stdout, NULL, _IOLBF, 0);
103 close(original_stdout_fd);
104 original_stdout_fd = -1;
105 }
106
107 // Restore original stderr
108 if (original_stderr_fd != -1) {
109 dup2(original_stderr_fd, STDERR_FILENO);
110 // Reopen stderr to use the restored file descriptor
111 (void)freopen("/dev/stderr", "w", stderr);
112 // Restore unbuffered mode for stderr
113 (void)setvbuf(stderr, NULL, _IONBF, 0);
114 close(original_stderr_fd);
115 original_stderr_fd = -1;
116 }
117
118 // Close /dev/null file descriptor
119 if (dev_null_fd != -1) {
120 close(dev_null_fd);
121 dev_null_fd = -1;
122 }
123
124 logging_disabled = false;
125 return 0;
126}
127
134 return logging_disabled;
135}
136
137#else
138// Windows stub implementations
139
147int test_logging_disable(bool disable_stdout, bool disable_stderr) {
148 (void)disable_stdout;
149 (void)disable_stderr;
150 return 0; // Not implemented on Windows
151}
152
158int test_logging_restore(void) {
159 return 0; // Not implemented on Windows
160}
161
167bool test_logging_is_disabled(void) {
168 return false;
169}
170#endif // !_WIN32
Cross-platform file I/O interface for ascii-chat.
#define PLATFORM_O_WRONLY
Open file for writing only.
Definition file.h:45
int platform_open(const char *pathname, int flags,...)
Safe file open (open replacement)
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
Public platform utility API for string, memory, and file operations.