idol
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
12namespace idol {
13 class SilentMode;
14}
15
17 int m_original_stdout_fd;
18 int m_null_fd;
19 bool m_silent_mode;
20public:
21 explicit SilentMode(bool t_silent = true) : m_silent_mode(t_silent) {
22 if (!m_silent_mode) { return; }
23 m_original_stdout_fd = dup(fileno(stdout));
24 m_null_fd = open("/dev/null", O_WRONLY);
25 dup2(m_null_fd, fileno(stdout));
26 }
27
28 ~SilentMode() {
29 if (!m_silent_mode) { return; }
30 dup2(m_original_stdout_fd, fileno(stdout));
31 close(m_null_fd);
32 close(m_original_stdout_fd);
33 }
34};
35
36#endif //IDOL_SILENTMODE_H