From d7a2ab760e06f87fd0158602e34ce3c2236ed82c Mon Sep 17 00:00:00 2001 From: Marcel Keller Date: Tue, 20 Oct 2020 15:30:47 +1100 Subject: [PATCH] Fix multithreaded output. --- Tools/SwitchableOutput.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Tools/SwitchableOutput.h b/Tools/SwitchableOutput.h index 90278d1b..51f6ad04 100644 --- a/Tools/SwitchableOutput.h +++ b/Tools/SwitchableOutput.h @@ -12,7 +12,7 @@ using namespace std; class SwitchableOutput { - bool on; + ostream* out; public: SwitchableOutput(bool on = true) @@ -22,21 +22,22 @@ public: void activate(bool on) { - this->on = on; + if (on) + out = &cout; + else + out = 0; } void redirect_to_file(ofstream& out_file) { - if (!on) - this->on = true; - cout.rdbuf(out_file.rdbuf()); + out = &out_file; } template SwitchableOutput& operator<<(const T& value) { - if (on) - cout << value; + if (out) + *out << value; return *this; cout << flush; @@ -44,8 +45,8 @@ public: SwitchableOutput& operator<<(ostream& (*__pf)(ostream&)) { - if (on) - cout << __pf; + if (out) + *out << __pf; return *this; } };