LP-56 - Better txpid option namings, fix tabs-spaces, tooltips. headers, variable...
[librepilot.git] / ground / openpilotgcs / src / plugins / consolegadget / texteditloggerengine.cpp
blobdf9acadeb31629a9e31b3892c774d429ba7ff86c
1 /**
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
8 * @{
9 * @addtogroup ConsolePlugin Console Plugin
10 * @{
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
22 * for more details.
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"
30 #include <QTime>
31 #include <QtGui/QTextEdit>
32 #include <QtGui/QScrollBar>
33 #include <QObject>
35 #define QXT_REQUIRED_LEVELS (QxtLogger::WarningLevel | QxtLogger::ErrorLevel | QxtLogger::CriticalLevel | QxtLogger::FatalLevel)
37 TextEditLoggerEngine::TextEditLoggerEngine(QTextEdit *textEdit) : m_textEdit(textEdit)
39 #ifndef QT_NO_DEBUG
40 setLogLevelsEnabled(QXT_REQUIRED_LEVELS);
41 #else
42 setLogLevelsEnabled(QXT_REQUIRED_LEVELS | QxtLogger::DebugLevel);
43 #endif
44 enableLogging();
47 TextEditLoggerEngine::~TextEditLoggerEngine()
50 void TextEditLoggerEngine::initLoggerEngine()
52 // Should work out of the box!
55 void TextEditLoggerEngine::killLoggerEngine()
57 // I do nothing.
60 bool TextEditLoggerEngine::isInitialized() const
62 return true;
65 void TextEditLoggerEngine::setLogLevelEnabled(QxtLogger::LogLevels level, bool enable)
67 QxtLoggerEngine::setLogLevelsEnabled(level | QXT_REQUIRED_LEVELS, enable);
69 if (!enable) {
70 QxtLoggerEngine::setLogLevelsEnabled(QXT_REQUIRED_LEVELS);
74 void TextEditLoggerEngine::writeFormatted(QxtLogger::LogLevel level, const QList<QVariant> &msgs)
76 switch (level) {
77 case QxtLogger::ErrorLevel:
78 writeToTextEdit("Error", msgs, Qt::red);
79 break;
80 case QxtLogger::WarningLevel:
81 writeToTextEdit("Warning", msgs, Qt::red);
82 break;
83 case QxtLogger::CriticalLevel:
84 writeToTextEdit("Critical", msgs, Qt::red);
85 break;
86 case QxtLogger::FatalLevel:
87 writeToTextEdit("!!FATAL!!", msgs, Qt::red);
88 break;
89 case QxtLogger::TraceLevel:
90 writeToTextEdit("Trace", msgs, Qt::blue);
91 break;
92 case QxtLogger::DebugLevel:
93 writeToTextEdit("DEBUG", msgs, Qt::blue);
94 break;
95 case QxtLogger::InfoLevel:
96 writeToTextEdit("INFO", msgs);
97 break;
98 default:
99 writeToTextEdit("", msgs);
100 break;
104 void TextEditLoggerEngine::writeToTextEdit(const QString & level, const QList<QVariant> &msgs, QColor color)
106 /* Message format...
107 [time] [error level] First message.....
108 second message
109 third message
111 if (msgs.isEmpty()) {
112 return;
114 QScrollBar *sb = m_textEdit->verticalScrollBar();
115 bool scroll = sb->value() == sb->maximum();
116 QString header = '[' + QTime::currentTime().toString("hh:mm:ss.zzz") + "] [" + level + "] ";
117 QString padding;
118 QString appendText;
119 appendText.append(header);
120 for (int i = 0; i < header.size(); i++) {
121 padding.append(' ');
123 int count = 0;
124 Q_FOREACH(const QVariant &out, msgs) {
125 if (!out.isNull()) {
126 if (count != 0) {
127 appendText.append(padding);
129 appendText.append(out.toString());
131 count++;
133 Q_ASSERT(m_textEdit);
134 appendText = QString("<font color=%1>%2</font>").arg(color.name()).arg(appendText);
135 m_textEdit->append(appendText);
136 if (scroll) {
137 sb->setValue(sb->maximum());