updated console/logging system (fixed scrolling issues)
This commit is contained in:
parent
ade52a90b9
commit
aeae6770d9
4 changed files with 41 additions and 21 deletions
25
console.cpp
25
console.cpp
|
@ -11,7 +11,8 @@ Console::Console(QWidget *parent) :
|
|||
DebugStream::registerConsole(this);
|
||||
DebugStream::registerHandler();
|
||||
|
||||
|
||||
setCenterOnScroll(true);
|
||||
setMaximumBlockCount(1000);
|
||||
}
|
||||
|
||||
void Console::setup()
|
||||
|
@ -23,3 +24,25 @@ void Console::setup()
|
|||
appendHtml(intro.readAll());
|
||||
moveCursor(QTextCursor::Start);
|
||||
}
|
||||
|
||||
void Console::log(QString msg, QtMsgType type)
|
||||
{
|
||||
appendHtml(format(msg, type));
|
||||
ensureCursorVisible();
|
||||
moveCursor(QTextCursor::End);
|
||||
moveCursor(QTextCursor::StartOfLine);
|
||||
}
|
||||
|
||||
QString Console::format(QString msg, QtMsgType type)
|
||||
{
|
||||
QString out("<pre><div style='font-weight: bold; color: %1'>[%2]</div> %3</pre>");
|
||||
|
||||
switch (type) {
|
||||
case QtDebugMsg: out = out.arg("Gray").arg("Debug "); break;
|
||||
case QtWarningMsg: out = out.arg("Orange").arg("Warning "); break;
|
||||
case QtCriticalMsg: out = out.arg("DarkRed").arg("Critical"); break;
|
||||
case QtFatalMsg: out = out.arg("Magenta").arg("Fatal "); break;
|
||||
}
|
||||
|
||||
return out.arg(msg);
|
||||
}
|
||||
|
|
|
@ -12,9 +12,13 @@ class Console : public QPlainTextEdit
|
|||
|
||||
void setup();
|
||||
|
||||
void log(QString msg, QtMsgType type);
|
||||
|
||||
protected:
|
||||
DebugStream qcout;
|
||||
DebugStream qcerr;
|
||||
|
||||
QString format(QString msg, QtMsgType type);
|
||||
};
|
||||
|
||||
#endif // CONSOLE_H
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
#include <QtDebug>
|
||||
|
||||
#include "console.h"
|
||||
#include "debugstream.h"
|
||||
|
||||
QPlainTextEdit *DebugStream::console;
|
||||
Console *DebugStream::console;
|
||||
|
||||
DebugStream::DebugStream(std::ostream &s, QtMsgType t) :
|
||||
stream(s),
|
||||
|
@ -49,34 +50,24 @@ int DebugStream::overflow(int v)
|
|||
return v;
|
||||
}
|
||||
|
||||
QString DebugStream::format(QString msg, QtMsgType type)
|
||||
{
|
||||
QString out("<pre><div style='font-weight: bold; color: %1'>[%2]</div> %3</pre>");
|
||||
|
||||
switch (type) {
|
||||
case QtDebugMsg: out = out.arg("Gray").arg("Debug "); break;
|
||||
case QtWarningMsg: out = out.arg("Orange").arg("Warning "); break;
|
||||
case QtCriticalMsg: out = out.arg("DarkRed").arg("Critical"); break;
|
||||
case QtFatalMsg: out = out.arg("Magenta").arg("Fatal "); break;
|
||||
}
|
||||
|
||||
return out.arg(msg);
|
||||
}
|
||||
|
||||
void DebugStream::registerHandler()
|
||||
{
|
||||
qInstallMessageHandler(DebugStream::handler);
|
||||
}
|
||||
|
||||
void DebugStream::registerConsole(QPlainTextEdit *pte)
|
||||
void DebugStream::registerConsole(Console *con)
|
||||
{
|
||||
console = pte;
|
||||
console = con;
|
||||
}
|
||||
|
||||
void DebugStream::handler(QtMsgType type, const QString &msg)
|
||||
{
|
||||
static int dropped;
|
||||
static int next = 1;
|
||||
|
||||
// FIXME: dirty workaround
|
||||
if (msg == "Camera dropped frame!") {
|
||||
if (++dropped == next) {
|
||||
next = next + next*2;
|
||||
|
@ -87,9 +78,9 @@ void DebugStream::handler(QtMsgType type, const QString &msg)
|
|||
}
|
||||
|
||||
if (console)
|
||||
console->appendHtml(format(msg, type));
|
||||
else
|
||||
printf("%s\n", (const char *) QByteArray(msg.toLocal8Bit().constData()));
|
||||
console->log(msg, type);
|
||||
|
||||
printf("%s\n", (const char *) QByteArray(msg.toLocal8Bit().constData()));
|
||||
}
|
||||
|
||||
void DebugStream::handler(QtMsgType type, const QMessageLogContext &, const QString &msg)
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
|
||||
#include <iostream>
|
||||
|
||||
class Console;
|
||||
|
||||
class DebugStream : public std::basic_streambuf<char>
|
||||
{
|
||||
|
||||
|
@ -14,7 +16,7 @@ public:
|
|||
~DebugStream();
|
||||
|
||||
static QString format(QString msg, QtMsgType type = QtDebugMsg);
|
||||
static void registerConsole(QPlainTextEdit *);
|
||||
static void registerConsole(Console *);
|
||||
static void registerHandler();
|
||||
|
||||
protected:
|
||||
|
@ -30,7 +32,7 @@ private:
|
|||
|
||||
static void handler(QtMsgType type, const QString &msg);
|
||||
static void handler(QtMsgType type, const QMessageLogContext &, const QString &str);
|
||||
static QPlainTextEdit *console;
|
||||
static Console *console;
|
||||
};
|
||||
|
||||
#endif // DEBUGSTREAM_H
|
||||
|
|
Loading…
Add table
Reference in a new issue