Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 100034b7

Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt

  • ID 100034b76429677f919eea98bc74eb17c392f979
  • Vorgänger 26fb3804
  • Nachfolger 45135a14

logger

Unterschiede anzeigen:

EntityManager.pro
src/entityinstancefactory.h \
src/condition.h \
src/cache.h \
src/entityhelper.h
src/entityhelper.h \
src/logger.h
SOURCES += \
src/entity.cpp \
......
src/entityinstancefactory.cpp \
src/condition.cpp \
src/cache.cpp \
src/entityhelper.cpp
src/entityhelper.cpp \
src/logger.cpp
unix {
target.path = /usr/lib
......
QMAKE_CXXFLAGS += -Wall -Wextra -Wmaybe-uninitialized -Wsuggest-final-types -Wsuggest-final-methods -Wsuggest-override -Wunsafe-loop-optimizations -pedantic -Wfloat-equal -Wundef -Wpointer-arith -Wcast-align -Wunreachable-code -O
}
CONFIG += c++14
QMAKE_CXXFLAGS += -std=c++14
#QMAKE_CXXFLAGS += -Winit-self
CONFIG(release, debug|release):DEFINES += QT_NO_DEBUG_OUTPUT
samples/example/Example.pro
unix:INCLUDEPATH += $$PWD/../../src
unix:DEPENDPATH += $$PWD/../../src
CONFIG += c++14
QMAKE_CXXFLAGS += -std=c++14
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../build-EntityManager-Desktop-Release/release/ -lCuteEntityManager
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../build-EntityManager-Desktop-Debug/debug/ -lCuteEntityManager
......
win32:INCLUDEPATH += $$PWD/../../build-EntityManager-Desktop-Debug/debug
win32:DEPENDPATH += $$PWD/../../build-EntityManager-Desktop-Debug/debug
INCLUDEPATH += $$PWD/../../src
DEPENDPATH += $$PWD/../../src
unix {
QMAKE_CXXFLAGS += -Wall -Wextra -Wmaybe-uninitialized -Wsuggest-final-types -Wsuggest-final-methods -Wsuggest-override -Wunsafe-loop-optimizations -pedantic -Wfloat-equal -Wundef -Wpointer-arith -Wcast-align -Wunreachable-code -O -Winit-self
}
src/logger.cpp
#include "logger.h"
#include <QDir>
#include <QDebug>
#include <QTextStream>
#include <QDateTime>
Logger::Logger(QString path) {
this->path = path;
}
Logger::~Logger() {
}
QString Logger::defaultPath() const {
return QDir::currentPath() + "/errors.log";
}
void Logger::lastError(const QSqlQuery &q) {
QFile log(this->getPath());
log.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
log.seek(log.size());
QTextStream stream(&log);
qDebug() << this->generateLogMsg(q.lastError());
qDebug() << generateLogMsg(q);
stream << this->generateLogMsg(q.lastError());
stream << generateLogMsg(q);
log.close();
}
void Logger::lastError(const QSqlError &e) {
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);
log.close();
}
}
QString Logger::generateLogMsg(const QSqlQuery &q) const {
QString r = "Query:<" + q.executedQuery() + ">";
QMap<QString, QVariant> m = q.boundValues();
QMap<QString,QVariant>::iterator i;
for (i = m.begin(); i != m.end(); ++i) {
r += "\n<" + i.key() + "|" + i.value().toString() + ">";
}
return r;
}
QString Logger::getPath() {
if(this->path.isEmpty()) {
this->path = this->defaultPath();
}
return path;
}
void Logger::setPath(const QString &value) {
path = value;
}
QString Logger::generateLogMsg(const QSqlError &e) const {
return "UTC:" + QDateTime::currentDateTime().toString("yyyy-MM-dd|hh:MM:ss") + "|" + e.driverText() + "|" + e.databaseText().toLatin1() + "\n";
}
src/logger.h
#ifndef LOGGER_H
#define LOGGER_H
#include <QString>
#include <QFile>
#include <QSqlError>
#include <QSqlQuery>
class Logger
{
public:
Logger(QString path="");
virtual ~Logger();
QString defaultPath() const;
void lastError(const QSqlError &e);
void lastError(const QSqlQuery &q);
QString getPath();
void setPath(const QString &value);
protected:
QString generateLogMsg(const QSqlError &e) const;
QString generateLogMsg(const QSqlQuery &q) const;
private:
QString path;
};
#endif // LOGGER_H

Auch abrufbar als: Unified diff