NAIA  1.0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
Logging.cpp
Go to the documentation of this file.
2 
3 #include <fstream>
4 #include <iostream>
5 
6 namespace NAIA {
7 namespace Logging {
8 // Mute-unmute routines.
9 // From: https://bbs.archlinux.org/viewtopic.php?id=79378
10 // Modified to mute also C-style output.
11 
12 FILE *muteOut(fopen("/dev/null", "w"));
13 std::ofstream fout("/dev/null");
14 
15 FILE *stdoutSave = nullptr;
16 FILE *stderrSave = nullptr;
17 std::streambuf *cout_sbuf = nullptr;
18 std::streambuf *cerr_sbuf = nullptr;
19 
20 bool isMuted = false;
21 
22 void MuteOutput() {
23  if (!std::exchange(isMuted, true)) {
24  stdoutSave = stdout;
25  stdout = muteOut;
26  stderrSave = stderr;
27  stderr = muteOut;
28 
29  cout_sbuf = std::cout.rdbuf();
30  cerr_sbuf = std::cerr.rdbuf();
31  std::cout.rdbuf(fout.rdbuf());
32  std::cerr.rdbuf(fout.rdbuf());
33  }
34 }
35 
36 void UnmuteOutput() {
37  if (std::exchange(isMuted, false)) {
38  if (stdoutSave) {
39  stdout = stdoutSave;
40  stdoutSave = nullptr;
41  }
42  if (stderrSave) {
43  stderr = stderrSave;
44  stderrSave = nullptr;
45  }
46 
47  if (cout_sbuf) {
48  std::cout.rdbuf(cout_sbuf);
49  cout_sbuf = nullptr;
50  }
51  if (cerr_sbuf) {
52  std::cerr.rdbuf(cerr_sbuf);
53  cerr_sbuf = nullptr;
54  }
55  }
56 }
57 } // namespace Logging
58 } // namespace NAIA
void UnmuteOutput()
Definition: Logging.cpp:36
bool isMuted
Definition: Logging.cpp:20
FILE * stderrSave
Definition: Logging.cpp:16
std::streambuf * cout_sbuf
Definition: Logging.cpp:17
std::streambuf * cerr_sbuf
Definition: Logging.cpp:18
void MuteOutput()
Definition: Logging.cpp:22
FILE * stdoutSave
Definition: Logging.cpp:15
std::ofstream fout("/dev/null")
FILE * muteOut(fopen("/dev/null","w"))