A C++ Framework for Optimization
Loading...
Searching...
No Matches
SilentMode.h
1//
2// Created by henri on 21.06.24.
3//
4
5#ifndef IDOL_SILENTMODE_H
6#define IDOL_SILENTMODE_H
7
8#include <cstdio>
9#include <fcntl.h>
10#include <csignal>
11#include <unistd.h>
12
13namespace idol {
14 class SilentMode;
15}
16
18 int m_original_stdout_fd;
19 int m_null_fd;
20 bool m_silent_mode;
21public:
22 explicit SilentMode(bool t_silent = true) : m_silent_mode(t_silent) {
23 if (!m_silent_mode) { return; }
24 m_original_stdout_fd = dup(fileno(stdout));
25 m_null_fd = open("/dev/null", O_WRONLY);
26 dup2(m_null_fd, fileno(stdout));
27 }
28
29 ~SilentMode() {
30 if (!m_silent_mode) { return; }
31 dup2(m_original_stdout_fd, fileno(stdout));
32 close(m_null_fd);
33 close(m_original_stdout_fd);
34 }
35};
36
37#endif //IDOL_SILENTMODE_H