BCESolve
bcelogstream.hpp
1// This file is part of the BCESolve library for games of incomplete
2// information
3// Copyright (C) 2022 Benjamin A. Brooks
4//
5// BCESolve free software: you can redistribute it and/or modify it
6// under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// BCESolve is distributed in the hope that it will be useful, but
11// WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13// General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see
17// <http://www.gnu.org/licenses/>.
18//
19// Benjamin A. Brooks
20// ben@benjaminbrooks.net
21// Chicago, IL
22
23#ifndef BCELOGSTREAM_HPP
24#define BCELOGSTREAM_HPP
25
26#include <iostream>
27#include <streambuf>
28#include <string>
29#include <QTextEdit>
30
32
40class BCELogStream : public std::basic_streambuf<char>
41{
42public:
44 BCELogStream(std::ostream &stream, QTextEdit* text_edit) : m_stream(stream)
45 {
46 log_window = text_edit;
47 m_old_buf = stream.rdbuf();
48 stream.rdbuf(this);
49 }
50
53 {
54 // output anything that is left
55 if (!m_string.empty()) {
56 QMetaObject::invokeMethod(log_window, "append", Qt::QueuedConnection,
57 Q_ARG(QString, m_string.c_str()));
58 // log_window->append(m_string.c_str());
59 }
60
61 m_stream.rdbuf(m_old_buf);
62 }
63
64protected:
65 virtual int_type overflow(int_type v)
66 {
67 if (v == '\n')
68 {
69 QMetaObject::invokeMethod(log_window, "append", Qt::QueuedConnection,
70 Q_ARG(QString, m_string.c_str()));
71 // log_window->append(m_string.c_str());
72 m_string.erase(m_string.begin(), m_string.end());
73 }
74 else
75 m_string += v;
76
77 return v;
78 }
79
80 virtual std::streamsize xsputn(const char *p, std::streamsize n)
81 {
82 m_string.append(p, p + n);
83
84 int pos = 0;
85 while (pos != std::string::npos)
86 {
87 pos = m_string.find('\n');
88 if (pos != std::string::npos)
89 {
90 std::string tmp(m_string.begin(), m_string.begin() + pos);
91 QMetaObject::invokeMethod(log_window, "append", Qt::QueuedConnection,
92 Q_ARG(QString, tmp.c_str()));
93 // log_window->append(tmp.c_str());
94 m_string.erase(m_string.begin(), m_string.begin() + pos + 1);
95 }
96 }
97
98 return n;
99 }
100
101private:
103 QTextEdit* log_window;
104
106 std::ostream &m_stream;
107 std::streambuf *m_old_buf;
108 std::string m_string;
109
110
111};
112
113#endif
Redirects all couts in the program to the log tab.
Definition: bcelogstream.hpp:41
~BCELogStream()
Destructor.
Definition: bcelogstream.hpp:52
QTextEdit * log_window
Text Edit where redirected stout/stderr will be sent.
Definition: bcelogstream.hpp:103
BCELogStream(std::ostream &stream, QTextEdit *text_edit)
Constructor.
Definition: bcelogstream.hpp:44
std::ostream & m_stream
Type of ostream redirected (e.g. "cout," "cerr," etc.)
Definition: bcelogstream.hpp:106