SST 16.1.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 confirm_(true)
60 {}
61
62 void reset()
63 {
64 if ( confirm_ )
65 paginate_ = true;
66 else
67 paginate_ = false;
68 quit_ = false;
69 curLines_ = 0;
70 curChars_ = 0;
71 }
72
73 void setConfirm(bool c)
74 {
75 confirm_ = c;
76 paginate_ = c;
77 }
78
79 void setLineWidth(const unsigned w) { charsPerLine_ = w; }
80 void setLineCount(const unsigned c) { linesPerScreen_ = c; }
81 unsigned getLineWidth() { return charsPerLine_; }
82};
83
84// ** This class is intended for use with the interactive console ONLY **
85// When
86
87class DebuggerStream : public std::ostream
88{
89private:
91
92public:
93 DebuggerStream(std::ostream& dest, unsigned linesPerScreen, unsigned charsPerLine) :
94 std::ostream(&buf_),
95 buf_(dest.rdbuf(), linesPerScreen, charsPerLine)
96 {}
97
98 void reset()
99 {
100 buf_.reset();
101 clear();
102 }
103
104 void setConfirm(bool c) { buf_.setConfirm(c); }
105
106
107 void setLineWidth(const unsigned w) { buf_.setLineWidth(w); }
108 void setLineCount(const unsigned c) { buf_.setLineCount(c); }
109 unsigned getLineWidth() { return buf_.getLineWidth(); }
110};
111
112// Overload << to cpature all function pointers, used in conjunction
113// with dreset.
114
115inline DebuggerStream&
116operator<<(DebuggerStream& stream, DebuggerStream& (*func)(DebuggerStream&))
117{
118 return func(stream);
119}
120
121// Call this function to reset the DebuggerStreamBuf back to a
122// known good state after each use. This is required to allow it
123// to recover from a potential `EOF` that has been inserted
124// when the user elects to stop output
125
126inline DebuggerStream&
127dreset(DebuggerStream& stream)
128{
129 stream.reset();
130 stream.flush();
131 return stream;
132}
133
134
135} // namespace SST::IMPL::Interactive
136
137#endif
Definition debugStream.h:88