SST 16.0.0
Structural Simulation Toolkit
debugStream.h
1// Copyright 2009-2026 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2026, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_IMPL_INTERACTIVE_DEBUGSTREAM_H
13#define SST_CORE_IMPL_INTERACTIVE_DEBUGSTREAM_H
14
15#include <cstddef>
16#include <cstdint>
17#include <ostream>
18#include <sstream>
19#include <streambuf>
20#include <string>
21#include <utility>
22#include <vector>
23
24namespace SST::IMPL::Interactive {
25
26// Overloaded streambuffer for use with DebuggerStream class
27// ** This class is intended for use with the interactive console ONLY **
28// This streambuffer will limit the size, in terms of line width and
29// lines per screen, of output. Wide output (wider than charsPerLine_)
30// will be truncated with a '...' and output that goes beyond
31// linesPerScreen will prompt the user to continue or cancel
32
33class DebuggerStreamBuf : public std::streambuf
34{
35private:
36 std::streambuf* dest_;
37 unsigned linesPerScreen_;
38 unsigned curLines_;
39 bool paginate_;
40 bool quit_;
41 unsigned charsPerLine_;
42 unsigned curChars_;
43 bool confirm_;
44
45protected:
46 virtual int_type overflow(int_type c) override;
47 virtual int sync() override;
48
49
50public:
51 DebuggerStreamBuf(std::streambuf* dest, unsigned linesPerScreen, unsigned charsPerLine) :
52 dest_(dest),
53 linesPerScreen_(linesPerScreen),
54 curLines_(0),
55 paginate_(true),
56 quit_(false),
57 charsPerLine_(charsPerLine),
58 curChars_(0)
59 {}
60
61 void reset()
62 {
63 if ( confirm_ )
64 paginate_ = true;
65 else
66 paginate_ = false;
67 quit_ = false;
68 curLines_ = 0;
69 curChars_ = 0;
70 }
71
72 void setConfirm(bool c)
73 {
74 confirm_ = c;
75 paginate_ = c;
76 }
77
78 void setLineWidth(const unsigned w) { charsPerLine_ = w; }
79 void setLineCount(const unsigned c) { linesPerScreen_ = c; }
80 unsigned getLineWidth() { return charsPerLine_; }
81};
82
83// ** This class is intended for use with the interactive console ONLY **
84// When
85
86class DebuggerStream : public std::ostream
87{
88private:
90
91public:
92 DebuggerStream(std::ostream& dest, unsigned linesPerScreen, unsigned charsPerLine) :
93 std::ostream(&buf_),
94 buf_(dest.rdbuf(), linesPerScreen, charsPerLine)
95 {}
96
97 void reset()
98 {
99 buf_.reset();
100 clear();
101 }
102
103 void setConfirm(bool c) { buf_.setConfirm(c); }
104
105
106 void setLineWidth(const unsigned w) { buf_.setLineWidth(w); }
107 void setLineCount(const unsigned c) { buf_.setLineCount(c); }
108 unsigned getLineWidth() { return buf_.getLineWidth(); }
109};
110
111// Overload << to cpature all function pointers, used in conjunction
112// with dreset.
113
114inline DebuggerStream&
115operator<<(DebuggerStream& stream, DebuggerStream& (*func)(DebuggerStream&))
116{
117 return func(stream);
118}
119
120// Call this function to reset the DebuggerStreamBuf back to a
121// known good state after each use. This is required to allow it
122// to recover from a potential `EOF` that has been inserted
123// when the user elects to stop output
124
125inline DebuggerStream&
126dreset(DebuggerStream& stream)
127{
128 stream.reset();
129 stream.flush();
130 return stream;
131}
132
133
134} // namespace SST::IMPL::Interactive
135
136#endif
Definition debugStream.h:87