Revision 66704054
Von Christian Ehringfeld vor etwa 10 Jahren hinzugefügt
| src/logger.cpp | ||
|---|---|---|
|
}
|
||
|
|
||
|
void Logger::lastError(const QSqlQuery &q, bool logQuery) {
|
||
|
QFile log(this->getPath());
|
||
|
log.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
|
||
|
log.seek(log.size());
|
||
|
QTextStream stream(&log);
|
||
|
const QString errorMsg = this->generateLogMsg(q.lastError());
|
||
|
if (!errorMsg.isEmpty()) {
|
||
|
qDebug() << errorMsg;
|
||
|
stream << errorMsg;
|
||
|
}
|
||
|
if(logQuery || !errorMsg.isEmpty()) {
|
||
|
const QString query = this->generateLogMsg(q);
|
||
|
if(!query.isEmpty()) {
|
||
|
qDebug() << query;
|
||
|
stream << query;
|
||
|
if (logQuery || q.lastError().isValid()) {
|
||
|
QFile log(this->getPath());
|
||
|
log.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
|
||
|
log.seek(log.size());
|
||
|
QTextStream stream(&log);
|
||
|
const QString errorMsg = this->generateLogMsg(q.lastError());
|
||
|
if (!errorMsg.isEmpty()) {
|
||
|
qDebug() << errorMsg;
|
||
|
stream << errorMsg;
|
||
|
}
|
||
|
if (logQuery || !errorMsg.isEmpty()) {
|
||
|
const QString query = this->generateLogMsg(q);
|
||
|
if (!query.isEmpty()) {
|
||
|
qDebug() << query;
|
||
|
stream << query;
|
||
|
}
|
||
|
}
|
||
|
stream << "\n";
|
||
|
stream.flush();
|
||
|
log.close();
|
||
|
}
|
||
|
stream << "\n";
|
||
|
stream.flush();
|
||
|
log.close();
|
||
|
}
|
||
|
|
||
|
void Logger::lastError(const QSqlError &e) {
|
||
|
if(e.isValid()) {
|
||
|
if (e.isValid()) {
|
||
|
QFile log(this->getPath());
|
||
|
log.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
|
||
|
log.seek(log.size());
|
||
|
QTextStream stream(&log);
|
||
|
qDebug() << this->generateLogMsg(e);
|
||
|
stream << this->generateLogMsg(e);
|
||
|
stream << "\n";
|
||
|
stream.flush();
|
||
|
log.close();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
QString Logger::generateLogMsg(const QSqlQuery &q, bool withValues) const {
|
||
|
QString r = "<" + q.executedQuery() + ">";
|
||
|
if(withValues) {
|
||
|
QMap<QString, QVariant> m = q.boundValues();
|
||
|
QMap<QString,QVariant>::iterator i;
|
||
|
if(!m.isEmpty()) {
|
||
|
r += "Values: ";
|
||
|
for (i = m.begin(); i != m.end(); ++i) {
|
||
|
r += "{" + i.key() + ":" + i.value().toString() + "}";
|
||
|
if (withValues) {
|
||
|
QMap<QString, QVariant> m = q.boundValues();
|
||
|
QMap<QString, QVariant>::iterator i;
|
||
|
if (!m.isEmpty()) {
|
||
|
r += "Values: ";
|
||
|
for (i = m.begin(); i != m.end(); ++i) {
|
||
|
r += "{" + i.key() + "=" + i.value().toString() + "}";
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return r;
|
||
|
}
|
||
|
|
||
|
QString Logger::getPath() {
|
||
|
if(this->path.isEmpty()) {
|
||
|
if (this->path.isEmpty()) {
|
||
|
this->path = this->defaultPath();
|
||
|
}
|
||
|
return path;
|
||
| ... | ... | |
|
}
|
||
|
|
||
|
QString Logger::generateLogMsg(const QSqlError &e) const {
|
||
|
if(e.isValid()) {
|
||
|
return "ErrorUTC:" + QDateTime::currentDateTime().toString("yyyy-MM-dd|hh:MM:ss") + "|" + e.driverText() + "|" + e.databaseText().toLatin1();
|
||
|
} else {
|
||
|
return "";
|
||
|
QString r = "";
|
||
|
if (e.isValid()) {
|
||
|
r = "ErrorUTC:" +
|
||
|
QDateTime::currentDateTime().toString("yyyy-MM-dd|hh:MM:ss") + "|" +
|
||
|
e.driverText() + "|" + e.databaseText().toLatin1();
|
||
|
}
|
||
|
return r;
|
||
|
}
|
||
Auch abrufbar als: Unified diff
improvements