NAIA
1.1.1
src
Utility
Logging
Logging.cpp
Go to the documentation of this file.
1
#include "
Utility/Logging/Logging.h
"
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
NAIA::Logging::UnmuteOutput
void UnmuteOutput()
Definition:
Logging.cpp:36
NAIA::Logging::isMuted
bool isMuted
Definition:
Logging.cpp:20
NAIA::Logging::stdoutSave
FILE * stdoutSave
Definition:
Logging.cpp:15
NAIA
Definition:
Event.h:13
NAIA::Logging::MuteOutput
void MuteOutput()
Definition:
Logging.cpp:22
NAIA::Logging::fout
std::ofstream fout("/dev/null")
NAIA::Logging::stderrSave
FILE * stderrSave
Definition:
Logging.cpp:16
NAIA::Logging::cerr_sbuf
std::streambuf * cerr_sbuf
Definition:
Logging.cpp:18
NAIA::Logging::cout_sbuf
std::streambuf * cout_sbuf
Definition:
Logging.cpp:17
Logging.h
NAIA::Logging::muteOut
FILE * muteOut(fopen("/dev/null", "w"))
Generated by
1.8.18