File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ #include " utl/SuppressOutput.h"
2+
3+ #include < fcntl.h>
4+ #include < unistd.h>
5+
6+ #include " utl/Logger.h"
7+
8+ namespace utl {
9+
10+ SuppressStdout::SuppressStdout (utl::Logger *logger) : logger_(logger) {
11+ fflush (stdout);
12+
13+ saved_fd_ = dup (STDOUT_FILENO);
14+ if (saved_fd_ < 0 ) {
15+ logger_->error (UTL, 73 , " Failed to dup STDOUT_FILENO" );
16+ return ;
17+ }
18+
19+ const int dev_null_fd = open (" /dev/null" , O_WRONLY);
20+ if (dev_null_fd < 0 ) {
21+ close (saved_fd_);
22+ saved_fd_ = -1 ;
23+ logger_->error (UTL, 74 , " Failed to open /dev/null" );
24+ return ;
25+ }
26+
27+ if (dup2 (dev_null_fd, STDOUT_FILENO) < 0 ) {
28+ close (saved_fd_);
29+ saved_fd_ = -1 ;
30+ logger_->error (UTL, 75 , " dup2 failed to redirect STDOUT_FILENO" );
31+ }
32+
33+ close (dev_null_fd);
34+ }
35+
36+ SuppressStdout::~SuppressStdout () {
37+ fflush (stdout);
38+ if (saved_fd_ == -1 ) {
39+ return ;
40+ }
41+
42+ if (dup2 (saved_fd_, STDOUT_FILENO) < 0 ) {
43+ logger_->error (UTL, 76 ,
44+ " dup2 failed to restore stdout, all stdout is likely broken "
45+ " from this point foward." );
46+ } else {
47+ close (saved_fd_);
48+ }
49+ }
50+
51+ } // namespace utl
You can’t perform that action at this time.
0 commit comments