Skip to content

Commit dc43730

Browse files
committed
Redirect stderr to log window
1 parent b15daec commit dc43730

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

qt/logger.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ void Logger::logHandler(QtMsgType type, const QMessageLogContext &context ,
6060
Logger::Logger()
6161
{
6262
qInstallMessageHandler(logHandler);
63+
oldBuf = std::cerr.rdbuf();
64+
std::cerr.rdbuf(this);
6365
}
6466

6567
Logger::~Logger()
6668
{
69+
std::cerr.rdbuf(oldBuf);
6770
qInstallMessageHandler(nullptr);
6871
}
6972

@@ -97,3 +100,15 @@ void Logger::setTextEdit(QTextEdit *textEdit)
97100
logTextEdit = textEdit;
98101
}
99102

103+
std::basic_streambuf<char>::int_type Logger::overflow(int_type v)
104+
{
105+
return v;
106+
}
107+
108+
std::streamsize Logger::xsputn(const char *p, std::streamsize n)
109+
{
110+
qCritical() << p;
111+
112+
return n;
113+
}
114+

qt/logger.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@
77
#define LOGGER_H
88

99
#include <QTextEdit>
10+
#include <iostream>
11+
#include <streambuf>
1012

11-
class Logger
13+
class Logger: public std::basic_streambuf<char>
1214
{
1315
static QTextEdit *logTextEdit;
1416
static Logger *logger;
1517
static int refCount;
18+
std::streambuf *oldBuf;
1619

1720
explicit Logger();
1821
~Logger();
1922

2023
static void logHandler(QtMsgType type, const QMessageLogContext &context,
2124
const QString &msg);
25+
26+
protected:
27+
virtual std::basic_streambuf<char>::int_type overflow(int_type v) override;
28+
virtual std::streamsize xsputn(const char *p, std::streamsize n) override;
29+
2230
public:
2331
static Logger *getInstance();
2432
static void putInstance();

0 commit comments

Comments
 (0)