Herunterladen als
root/src/logger.cpp @ 92f2130d
4b90a7ee | Christian Ehringfeld | /*
|
|
* Copyright (C) 2015 Christian Ehringfeld <c.ehringfeld@t-online.de>
|
|||
*
|
|||
* This program is free software; you can redistribute it and/or modify it
|
|||
* under the terms of the GNU Lesser General Public License as published by
|
|||
* the Free Software Foundation.
|
|||
*
|
|||
* This program is distributed in the hope that it will be useful, but
|
|||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|||
* for more details.
|
|||
*
|
|||
* You should have received a copy of the GNU Lesser General Public License
|
|||
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|||
*/
|
|||
100034b7 | Christian Ehringfeld | #include "logger.h"
|
|
#include <QDir>
|
|||
#include <QDebug>
|
|||
#include <QTextStream>
|
|||
#include <QDateTime>
|
|||
ce7a4eda | Christian Ehringfeld | #include <QtGlobal>
|
|
100034b7 | Christian Ehringfeld | ||
2b455197 | Christian Ehringfeld | using namespace CuteEntityManager;
|
|
f12670e9 | Christian Ehringfeld | ||
Logger::Logger(QString path, MsgType min) {
|
|||
100034b7 | Christian Ehringfeld | this->path = path;
|
|
f12670e9 | Christian Ehringfeld | this->minimum = min;
|
|
100034b7 | Christian Ehringfeld | }
|
|
Logger::~Logger() {
|
|||
}
|
|||
QString Logger::defaultPath() const {
|
|||
return QDir::currentPath() + "/errors.log";
|
|||
}
|
|||
45135a14 | Christian Ehringfeld | void Logger::lastError(const QSqlQuery &q, bool logQuery) {
|
|
66704054 | Christian Ehringfeld | if (logQuery || q.lastError().isValid()) {
|
|
const QString errorMsg = this->generateLogMsg(q.lastError());
|
|||
if (logQuery || !errorMsg.isEmpty()) {
|
|||
93285804 | Christian Ehringfeld | QString msg = "{" + QString("\"time\":\"") +
|
|
QDateTime::currentDateTime().toString(Qt::ISODate) + QString("\"") + errorMsg;
|
|||
msg += this->generateLogMsg(q) + "}";
|
|||
f12670e9 | Christian Ehringfeld | this->logMsg(msg, errorMsg.isEmpty() ? MsgType::DEBUG : MsgType::WARNING);
|
|
66704054 | Christian Ehringfeld | }
|
|
45135a14 | Christian Ehringfeld | }
|
|
100034b7 | Christian Ehringfeld | }
|
|
void Logger::lastError(const QSqlError &e) {
|
|||
66704054 | Christian Ehringfeld | if (e.isValid()) {
|
|
f12670e9 | Christian Ehringfeld | this->logMsg(this->generateLogMsg(e), MsgType::WARNING);
|
|
100034b7 | Christian Ehringfeld | }
|
|
}
|
|||
f12670e9 | Christian Ehringfeld | void Logger::logMsg(const QString &value, const MsgType type) {
|
|
if (!value.isEmpty() && this->shouldBeLogged(type)) {
|
|||
3938a37e | Christian Ehringfeld | QString msg = value;
|
|
f12670e9 | Christian Ehringfeld | QFile log(this->getPath());
|
|
log.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
|
|||
log.seek(log.size());
|
|||
QTextStream stream(&log);
|
|||
stream.setCodec("UTF-8");
|
|||
3938a37e | Christian Ehringfeld | if (value.size() - 1 != value.lastIndexOf("\n")) {
|
|
msg.append("\n");
|
|||
}
|
|||
stream << msg;
|
|||
2f40d160 | Christian Ehringfeld | this->outputToConsole(type, msg.replace("\"", "'").replace("\n", ""));
|
|
f12670e9 | Christian Ehringfeld | stream.flush();
|
|
log.close();
|
|||
}
|
|||
e332a521 | Christian Ehringfeld | }
|
|
f682a8cb | Christian Ehringfeld | QString Logger::generateLogMsg(const QSqlQuery &q, bool withValues) const {
|
|
93285804 | Christian Ehringfeld | QString r = ", \"query\":\"" + q.executedQuery() + "\"";
|
|
66704054 | Christian Ehringfeld | if (withValues) {
|
|
QMap<QString, QVariant> m = q.boundValues();
|
|||
if (!m.isEmpty()) {
|
|||
93285804 | Christian Ehringfeld | r += ", \"values\": {";
|
|
bool first = true;
|
|||
for (auto i = m.begin(); i != m.end(); ++i) {
|
|||
if (first) {
|
|||
first = false;
|
|||
} else {
|
|||
r += ", ";
|
|||
}
|
|||
r += "\"" + i.key().mid(1) + "\":\"" + i.value().toString() + "\"";
|
|||
66704054 | Christian Ehringfeld | }
|
|
45135a14 | Christian Ehringfeld | }
|
|
93285804 | Christian Ehringfeld | r += "}";
|
|
100034b7 | Christian Ehringfeld | }
|
|
return r;
|
|||
}
|
|||
f12670e9 | Christian Ehringfeld | bool Logger::shouldBeLogged(const MsgType &type) const {
|
|
switch (type) {
|
|||
case MsgType::DEBUG:
|
|||
return this->minimum == MsgType::DEBUG;
|
|||
case MsgType::INFO:
|
|||
return this->minimum == MsgType::DEBUG || this->minimum == MsgType::INFO;
|
|||
case MsgType::WARNING:
|
|||
return this->minimum == MsgType::DEBUG || this->minimum == MsgType::INFO
|
|||
|| this->minimum == MsgType::WARNING;
|
|||
case MsgType::CRITICAL:
|
|||
return this->minimum == MsgType::DEBUG || this->minimum == MsgType::INFO
|
|||
|| this->minimum == MsgType::WARNING || this->minimum == MsgType::CRITICAL;
|
|||
default:
|
|||
return true;
|
|||
break;
|
|||
}
|
|||
}
|
|||
void Logger::outputToConsole(const MsgType &type, const QString &msg) const {
|
|||
switch (type) {
|
|||
case MsgType::DEBUG:
|
|||
qDebug() << msg;
|
|||
break;
|
|||
ce7a4eda | Christian Ehringfeld | #if QT_VERSION >= 0x050500
|
|
f12670e9 | Christian Ehringfeld | case MsgType::INFO:
|
|
qInfo() << msg;
|
|||
break;
|
|||
case MsgType::WARNING:
|
|||
qWarning() << msg;
|
|||
break;
|
|||
ce7a4eda | Christian Ehringfeld | #else
|
|
case MsgType::INFO:
|
|||
qDebug() << msg;
|
|||
break;
|
|||
case MsgType::WARNING:
|
|||
qDebug() << msg;
|
|||
break;
|
|||
#endif
|
|||
f12670e9 | Christian Ehringfeld | case MsgType::CRITICAL:
|
|
qCritical() << msg;
|
|||
break;
|
|||
case MsgType::FATAL:
|
|||
qFatal(msg.toUtf8().constData());
|
|||
break;
|
|||
}
|
|||
}
|
|||
MsgType Logger::getMinimum() const {
|
|||
return minimum;
|
|||
}
|
|||
void Logger::setMinimum(const MsgType &value) {
|
|||
minimum = value;
|
|||
}
|
|||
100034b7 | Christian Ehringfeld | QString Logger::getPath() {
|
|
66704054 | Christian Ehringfeld | if (this->path.isEmpty()) {
|
|
100034b7 | Christian Ehringfeld | this->path = this->defaultPath();
|
|
}
|
|||
return path;
|
|||
}
|
|||
void Logger::setPath(const QString &value) {
|
|||
path = value;
|
|||
}
|
|||
QString Logger::generateLogMsg(const QSqlError &e) const {
|
|||
66704054 | Christian Ehringfeld | QString r = "";
|
|
if (e.isValid()) {
|
|||
93285804 | Christian Ehringfeld | r = ",\"error\":\"" + e.text() + "\",\"code\":\"" + e.number() + "\"";
|
|
45135a14 | Christian Ehringfeld | }
|
|
66704054 | Christian Ehringfeld | return r;
|
|
100034b7 | Christian Ehringfeld | }
|