PRISMS-PF Manual v3.0-pre
All Classes Functions Variables Enumerations Pages
tee_stream.h
1// SPDX-FileCopyrightText: © 2025 PRISMS Center at the University of Michigan
2// SPDX-License-Identifier: GNU Lesser General Public Version 2.1
3
4#ifndef tee_stream_h
5#define tee_stream_h
6
7#include <prismspf/config.h>
8
9#include <ostream>
10
11PRISMS_PF_BEGIN_NAMESPACE
12
17class TeeStream : public std::ostream
18{
19public:
20 TeeStream(std::ostream &stream1, std::ostream &stream2)
21 : std::ostream(&tee_buffer)
22 , tee_buffer(stream1, stream2)
23 {}
24
25private:
26 class TeeBuffer : public std::streambuf
27 {
28 public:
29 TeeBuffer(std::ostream &stream1, std::ostream &stream2)
30 : stream1(stream1.rdbuf())
31 , stream2(stream2.rdbuf())
32 {}
33
34 protected:
35 int
36 overflow(int character) override
37 {
38 if (character == EOF)
39 {
40 return static_cast<int>(!EOF);
41 }
42
43 const int result1 = stream1->sputc(static_cast<char_type>(character));
44 const int result2 = stream2->sputc(static_cast<char_type>(character));
45 return result1 == EOF || result2 == EOF ? EOF : character;
46 }
47
48 int
49 sync() override
50 {
51 const int result1 = stream1->pubsync();
52 const int result2 = stream2->pubsync();
53 return result1 == 0 && result2 == 0 ? 0 : -1;
54 }
55
56 private:
57 std::streambuf *stream1;
58 std::streambuf *stream2;
59 };
60
61 TeeBuffer tee_buffer;
62};
63
64PRISMS_PF_END_NAMESPACE
65
66#endif
Combined output streams so we can output to terminal and the summary.log with a single statement.
Definition tee_stream.h:18