Merged in f5soh/librepilot/LP-575_fedora_package (pull request #491)
[librepilot.git] / ground / gcs / src / libs / utils / filelogger.h
blobeddd05f38a43f08382be19165838556fa3d0210a
1 /**
2 ******************************************************************************
4 * @file filelogger.h
5 * @author The LibrePilot Project, http://www.openpilot.org Copyright (C) 2016.
6 * Parts by Nokia Corporation (qt-info@nokia.com) Copyright (C) 2009.
7 * @brief
8 * @see The GNU Public License (GPL) Version 3
9 * @defgroup
10 * @{
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 #pragma once
31 #include "utils_global.h"
33 #include <QObject>
34 #include <QFile>
35 #include <QTextStream>
36 #include <QThread>
37 #include <QDebug>
39 class QTCREATOR_UTILS_EXPORT FileLogger : public QObject {
40 Q_OBJECT
42 public:
43 FileLogger(QString &fileName) : QObject(), file(fileName), logStream(NULL), loggerThread(NULL), started(false)
46 virtual ~FileLogger()
48 qDebug() << "~FileLogger";
49 stop();
50 delete logStream;
51 logStream = NULL;
54 bool start()
56 if (started) {
57 return false;
60 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
61 return false;
64 logStream = new QTextStream(&file);
66 // loggerThread = new QThread(this);
67 moveToThread(&loggerThread);
68 // connect(&loggerThread, &QThread::finished, this, &QObject::deleteLater);
69 loggerThread.start();
71 started = true;
73 return true;
76 bool stop()
78 if (!started) {
79 return false;
81 // stop accepting messages
82 started = false;
84 // make sure all messages are flushed by sending a blocking message
85 QtMsgType type = QtDebugMsg;
86 const QString msg = "stopping file logger";
87 QMetaObject::invokeMethod(this, "doLog", Qt::BlockingQueuedConnection,
88 Q_ARG(QtMsgType, type), Q_ARG(const QString &, msg));
90 loggerThread.quit();
91 loggerThread.wait();
93 return true;
96 void log(QtMsgType type, const QMessageLogContext &context, const QString &msg)
98 Q_UNUSED(context);
100 if (!started) {
101 return;
104 QMetaObject::invokeMethod(this, "doLog", Qt::QueuedConnection,
105 Q_ARG(QtMsgType, type), Q_ARG(const QString &, msg));
108 private slots:
109 void doLog(QtMsgType type, const QString &msg)
111 QTextStream &out = *logStream;
113 // logStream << QTime::currentTime().toString("hh:mm:ss.zzz ");
115 switch (type) {
116 case QtDebugMsg:
117 out << "DBG: ";
118 break;
119 #if (QT_VERSION >= QT_VERSION_CHECK(5, 5, 0))
120 case QtInfoMsg:
121 out << "INF: ";
122 break;
123 #endif
124 case QtWarningMsg:
125 out << "WRN: ";
126 break;
127 case QtCriticalMsg:
128 out << "CRT: ";
129 break;
130 case QtFatalMsg:
131 out << "FTL: ";
132 break;
135 out << msg << '\n';
136 out.flush();
139 private:
140 QFile file;
141 QTextStream *logStream;
142 QThread loggerThread;
143 bool started;