2 ******************************************************************************
4 * @file texteditloggerengine.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * Parts by Qxt Foundation http://www.libqxt.org Copyright (C)
7 * @addtogroup GCSPlugins GCS Plugins
9 * @addtogroup ConsolePlugin Console Plugin
11 * @brief The Console Gadget impliments a console view
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "texteditloggerengine.h"
31 #include <QtGui/QTextEdit>
32 #include <QtGui/QScrollBar>
35 #define QXT_REQUIRED_LEVELS (QxtLogger::WarningLevel | QxtLogger::ErrorLevel | QxtLogger::CriticalLevel | QxtLogger::FatalLevel)
37 TextEditLoggerEngine::TextEditLoggerEngine(QTextEdit
*textEdit
) : m_textEdit(textEdit
)
40 setLogLevelsEnabled(QXT_REQUIRED_LEVELS
);
42 setLogLevelsEnabled(QXT_REQUIRED_LEVELS
| QxtLogger::DebugLevel
);
47 TextEditLoggerEngine::~TextEditLoggerEngine()
50 void TextEditLoggerEngine::initLoggerEngine()
52 // Should work out of the box!
55 void TextEditLoggerEngine::killLoggerEngine()
60 bool TextEditLoggerEngine::isInitialized() const
65 void TextEditLoggerEngine::setLogLevelEnabled(QxtLogger::LogLevels level
, bool enable
)
67 QxtLoggerEngine::setLogLevelsEnabled(level
| QXT_REQUIRED_LEVELS
, enable
);
70 QxtLoggerEngine::setLogLevelsEnabled(QXT_REQUIRED_LEVELS
);
74 void TextEditLoggerEngine::writeFormatted(QxtLogger::LogLevel level
, const QList
<QVariant
> &msgs
)
77 case QxtLogger::ErrorLevel
:
78 writeToTextEdit("Error", msgs
, Qt::red
);
80 case QxtLogger::WarningLevel
:
81 writeToTextEdit("Warning", msgs
, Qt::red
);
83 case QxtLogger::CriticalLevel
:
84 writeToTextEdit("Critical", msgs
, Qt::red
);
86 case QxtLogger::FatalLevel
:
87 writeToTextEdit("!!FATAL!!", msgs
, Qt::red
);
89 case QxtLogger::TraceLevel
:
90 writeToTextEdit("Trace", msgs
, Qt::blue
);
92 case QxtLogger::DebugLevel
:
93 writeToTextEdit("DEBUG", msgs
, Qt::blue
);
95 case QxtLogger::InfoLevel
:
96 writeToTextEdit("INFO", msgs
);
99 writeToTextEdit("", msgs
);
104 void TextEditLoggerEngine::writeToTextEdit(const QString
& level
, const QList
<QVariant
> &msgs
, QColor color
)
107 [time] [error level] First message.....
111 if (msgs
.isEmpty()) {
114 QScrollBar
*sb
= m_textEdit
->verticalScrollBar();
115 bool scroll
= sb
->value() == sb
->maximum();
116 QString header
= '[' + QTime::currentTime().toString("hh:mm:ss.zzz") + "] [" + level
+ "] ";
119 appendText
.append(header
);
120 for (int i
= 0; i
< header
.size(); i
++) {
124 Q_FOREACH(const QVariant
&out
, msgs
) {
127 appendText
.append(padding
);
129 appendText
.append(out
.toString());
133 Q_ASSERT(m_textEdit
);
134 appendText
= QString("<font color=%1>%2</font>").arg(color
.name()).arg(appendText
);
135 m_textEdit
->append(appendText
);
137 sb
->setValue(sb
->maximum());