-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqdebugstream.h
67 lines (54 loc) · 1.54 KB
/
qdebugstream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* https://stackoverflow.com/questions/12978973/unknown-ouput-with-qdebugstream-and-qtextedit
*
* Joonhwan
*/
#ifndef Q_DEBUGSTREAM_H
#define Q_DEBUGSTREAM_H
#include <iostream>
#include <streambuf>
#include <string>
#include <QPlainTextEdit>
class QDebugStream : public std::basic_streambuf<char> {
public:
QDebugStream(std::ostream &stream, QPlainTextEdit *text_edit)
: m_stream(stream) {
log_window = text_edit;
m_old_buf = stream.rdbuf();
stream.rdbuf(this);
}
~QDebugStream() {
// output anything that is left
if (!m_string.empty())
log_window->appendPlainText(m_string.c_str());
m_stream.rdbuf(m_old_buf);
}
protected:
virtual int_type overflow(int_type v) {
if (v == '\n') {
log_window->appendPlainText(m_string.c_str());
m_string.erase(m_string.begin(), m_string.end());
} else
m_string += v;
return v;
}
virtual std::streamsize xsputn(const char *p, std::streamsize n) {
m_string.append(p, p + n);
size_t pos = 0;
while (pos != std::string::npos) {
pos = m_string.find('\n');
if (pos != std::string::npos) {
std::string tmp(m_string.begin(), m_string.begin() + pos);
log_window->appendPlainText(tmp.c_str());
m_string.erase(m_string.begin(), m_string.begin() + pos + 1);
}
}
return n;
}
private:
std::ostream &m_stream;
std::streambuf *m_old_buf;
std::string m_string;
QPlainTextEdit *log_window;
};
#endif // Q_DEBUGSTREAM_H