dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
debuggadgetwidget.cpp
Go to the documentation of this file.
1 
13 /*
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.
18  *
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.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 #include "debuggadgetwidget.h"
28 
29 #include <QDebug>
30 #include <QStringList>
31 #include <QWidget>
32 #include <QTextEdit>
33 #include <QVBoxLayout>
34 #include <QPushButton>
35 #include "debugengine.h"
36 #include <QFile>
37 #include <QFileDialog>
38 #include <QMessageBox>
39 #include <QScrollBar>
40 #include <QTime>
41 
43  : QLabel(parent)
44 {
45  m_config = new Ui_Form();
46  m_config->setupUi(this);
48  connect(de, &DebugEngine::message, this, &DebugGadgetWidget::message, Qt::QueuedConnection);
49  connect(m_config->saveToFile, &QAbstractButton::clicked, this, &DebugGadgetWidget::saveLog);
50  connect(m_config->clearLog, &QAbstractButton::clicked, this, &DebugGadgetWidget::clearLog);
51 }
52 
54 {
55  // Do nothing
56 }
57 
58 void DebugGadgetWidget::saveLog()
59 {
60  QString fileName = QFileDialog::getSaveFileName(
61  this, tr("Save log File As"),
62  QString("gcs-debug-log-%0.html")
63  .arg(QDateTime::currentDateTime().toString("yyyyMMdd-hhmmss")),
64  tr("HTML (*.html)"));
65  if (fileName.isEmpty())
66  return;
67 
68  QFile file(fileName);
69  if (file.open(QIODevice::WriteOnly)
70  && (file.write(m_config->plainTextEdit->toHtml().toLatin1()) != -1)) {
71  file.close();
72  } else {
73  QMessageBox::critical(this, tr("Log Save"), tr("Unable to save log: ") + fileName,
74  QMessageBox::Ok);
75  return;
76  }
77 }
78 
79 void DebugGadgetWidget::clearLog()
80 {
81  m_config->plainTextEdit->clear();
82 }
83 
84 void DebugGadgetWidget::message(DebugEngine::Level level, const QString &msg, const QString &file,
85  const int line, const QString &function)
86 {
87  QColor color;
88  QString type;
89  switch (level) {
90  case DebugEngine::DEBUG:
91  color = Qt::blue;
92  type = "debug";
93  break;
94  case DebugEngine::INFO:
95  color = Qt::black;
96  type = "info";
97  break;
99  color = Qt::red;
100  type = "WARNING";
101  break;
103  color = Qt::red;
104  type = "CRITICAL";
105  break;
106  case DebugEngine::FATAL:
107  color = Qt::red;
108  type = "FATAL";
109  break;
110  }
111 
112  QString source;
113 #ifdef QT_DEBUG // only display this extended info to devs
114  source = QString("[%0:%1 %2]").arg(file).arg(line).arg(function);
115 #else
116  Q_UNUSED(file);
117  Q_UNUSED(line);
118  Q_UNUSED(function);
119 #endif
120 
121  m_config->plainTextEdit->setTextColor(color);
122  m_config->plainTextEdit->append(
123  QString("%0[%1]%2 %3").arg(QTime::currentTime().toString()).arg(type).arg(source).arg(msg));
124 
125  QScrollBar *sb = m_config->plainTextEdit->verticalScrollBar();
126  sb->setValue(sb->maximum());
127 }
128 
void message(DebugEngine::Level level, const QString &msg, const QString &file="", const int line=0, const QString &function="")
Parse log file
DebugGadgetWidget(QWidget *parent=nullptr)
static DebugEngine * getInstance()
Definition: debugengine.cpp:35