| 1 | /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]> |
| 2 | |
| 3 | This file is part of Dekko email client for Ubuntu devices |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU General Public License as |
| 7 | published by the Free Software Foundation; either version 2 of |
| 8 | the License or (at your option) version 3 |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | #include "LogRecorder.h" |
| 19 | |
| 20 | LogRecorder::LogRecorder(QObject *parent) : QObject(parent), m_model(0) |
| 21 | { |
| 22 | m_model = new QQmlObjectListModel<LogMessage>(this); |
| 23 | emit modelChanged(); |
| 24 | } |
| 25 | |
| 26 | QObject *LogRecorder::factory(QQmlEngine *engine, QJSEngine *scriptEngine) |
| 27 | { |
| 28 | Q_UNUSED(scriptEngine); |
| 29 | LogRecorder *lr = new LogRecorder(reinterpret_cast<QObject*>(engine)); |
| 30 | return lr; |
| 31 | } |
| 32 | |
| 33 | void LogRecorder::logMessage(const QString &location, const int &type, const QString &message) |
| 34 | { |
| 35 | if (m_model->count() > 250) { |
| 36 | m_model->remove(m_model->first()); |
| 37 | } |
| 38 | if (message.isEmpty()) { |
| 39 | return; |
| 40 | } |
| 41 | LogMessage *msg = new LogMessage(0); |
| 42 | msg->setMessage(formatMessage(location, type, message)); |
| 43 | m_model->append(msg); |
| 44 | } |
| 45 | // error #ee4035 |
| 46 | // info #7bc043 |
| 47 | // warning #9AD3EB |
| 48 | // status #ffd633 |
| 49 | // text #00aba9 |
| 50 | // text err #f37736 |
| 51 | QString LogRecorder::formatMessage(const QString &location, const int &type, const QString &message) |
| 52 | { |
| 53 | if (message.isEmpty()) { |
| 54 | return QString(); |
| 55 | } |
| 56 | |
| 57 | QString text; |
| 58 | switch ((Type)type) { |
| 59 | case INFO: |
| 60 | text = QStringLiteral("<font color=\"#f09609\">[%1] </font><font color=\"#7bc043\">[INFO] </font><font color=\"#00aba9\">%2</font>").arg(location, message); |
| 61 | break; |
| 62 | case STATUS: |
| 63 | text = QStringLiteral("<font color=\"#f09609\">[%1] </font><font color=\"#ffd633\">[STATUS] </font><font color=\"#00aba9\">%2</font>").arg(location, message); |
| 64 | break; |
| 65 | case WARNING: |
| 66 | text = QStringLiteral("<font color=\"#f09609\">[%1] </font><font color=\"#9AD3EB\">[WARNING] </font><font color=\"#00aba9\">%2</font>").arg(location, message); |
| 67 | break; |
| 68 | case ERROR: |
| 69 | text = QStringLiteral("<font color=\"#f09609\">[%1] </font><font color=\"#ee4035\">[ERROR] </font><font color=\"#f37736\">%2</font>").arg(location, message); |
| 70 | break; |
| 71 | } |
| 72 | |
| 73 | if (text.isEmpty()) { |
| 74 | text = message; |
| 75 | } |
| 76 | return text; |
| 77 | } |
| 78 |