19
19
20
20
#include < iostream>
21
21
#include < sstream>
22
+ #include < stdexcept>
22
23
#include < string>
23
24
24
- // NOLINTNEXTLINE(readability-redundant-declaration) - TODO: fix this
25
- extern std::ostringstream errout;
26
- // NOLINTNEXTLINE(readability-redundant-declaration) - TODO: fix this
27
- extern std::ostringstream output;
28
25
/* *
29
26
* @brief Utility class for capturing cout and cerr to ostringstream buffers
30
27
* for later use. Uses RAII to stop redirection when the object goes out of
@@ -47,34 +44,35 @@ class RedirectOutputError {
47
44
}
48
45
49
46
/* * Revert cout and cerr behaviour */
50
- ~RedirectOutputError () {
47
+ ~RedirectOutputError () noexcept ( false ) {
51
48
std::cout.rdbuf (_oldCout); // restore cout's original streambuf
52
49
std::cerr.rdbuf (_oldCerr); // restore cerrs's original streambuf
53
50
54
- errout << _err.str ();
55
- output << _out.str ();
51
+ {
52
+ const std::string s = _out.str ();
53
+ if (!s.empty ())
54
+ throw std::runtime_error (" unconsumed stdout: " + s); // cppcheck-suppress exceptThrowInDestructor - FP #11031
55
+ }
56
+
57
+ {
58
+ const std::string s = _err.str ();
59
+ if (!s.empty ())
60
+ throw std::runtime_error (" consumed stderr: " + s);
61
+ }
56
62
}
57
63
58
- /* * Return what would be printed to cout. See also clearOutput() */
59
- std::string getOutput () const {
60
- return _out.str ();
61
- }
62
-
63
- /* * Normally called after getOutput() to prevent same text to be returned
64
- twice. */
65
- void clearOutput () {
64
+ /* * Return what would be printed to cout. */
65
+ std::string getOutput () {
66
+ std::string s = _out.str ();
66
67
_out.str (" " );
68
+ return s;
67
69
}
68
70
69
- /* * Return what would be printed to cerr. See also clearErrout() */
70
- std::string getErrout () const {
71
- return _err.str ();
72
- }
73
-
74
- /* * Normally called after getErrout() to prevent same text to be returned
75
- twice. */
76
- void clearErrout () {
71
+ /* * Return what would be printed to cerr. */
72
+ std::string getErrout () {
73
+ std::string s = _err.str ();
77
74
_err.str (" " );
75
+ return s;
78
76
}
79
77
80
78
private:
@@ -112,9 +110,7 @@ class SuppressOutput {
112
110
113
111
#define REDIRECT RedirectOutputError redir
114
112
#define GET_REDIRECT_OUTPUT redir.getOutput()
115
- #define CLEAR_REDIRECT_OUTPUT redir.clearOutput()
116
113
#define GET_REDIRECT_ERROUT redir.getErrout()
117
- #define CLEAR_REDIRECT_ERROUT redir.clearErrout()
118
114
119
115
#define SUPPRESS SuppressOutput supprout
120
116
0 commit comments