Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 45135a14

Von Christian Ehringfeld vor mehr als 9 Jahren hinzugefügt

  • ID 45135a146dc995359644296ea96b021054316ddf
  • Vorgänger 100034b7
  • Nachfolger f682a8cb

integration of logger component

Unterschiede anzeigen:

src/database.cpp
*/
#include "database.h"
#include "logger.h"
#include <QDir>
using namespace CuteEntityManager;
Database::Database(QSqlDatabase database) {
Database::Database(QSqlDatabase database, bool loggerActivated, bool logQueries, bool logErrors) {
this->database = database;
this->init();
this->connectionName = this->database.connectionName();
this->initLogger(loggerActivated,logQueries,logErrors);
}
Database::Database(QString databaseType, QString connectionName,
QString hostname,
QString databasename,
QString username, QString password, qint64 port) {
QString username, QString password, qint64 port, bool loggerActivated, bool logQueries, bool logErrors) {
this->database = QSqlDatabase::addDatabase(databaseType, connectionName);
this->connectionName = connectionName;
if (hostname != QString("")) {
......
this->database.setPort(port);
}
this->init();
this->initLogger(loggerActivated,logQueries,logErrors);
}
void Database::init() {
this->database.open();
this->supportTransactions = this->database.driver()->hasFeature(
QSqlDriver::Transactions);
QSqlDriver::Transactions);
}
void Database::initLogger(bool activated, bool logQueries, bool logErrors) {
this->logQueries = logQueries;
this->logErrors = logErrors;
if(activated) {
this->logger = new Logger(QDir::currentPath() + "/db" + this->connectionName + "db.log");
}
}
Database::~Database() {
if(this->logger) {
delete this->logger;
this->logger = nullptr;
}
if (this->database.isOpen()) {
this->database.close();
}
......
QSqlQuery sqlquery = QSqlQuery(this->database);
for (int var = 0; var < queries.size(); ++var) {
sqlquery.exec(queries.at(var));
this->debugQuery(sqlquery);
}
ok = this->commitTransaction();
} else {
......
}
QSharedPointer<Schema> Database::getSchema(DatabaseType db,
QSharedPointer<Database> database) {
QSharedPointer<Database> database) {
switch (db) {
case DatabaseType::SQLITE:
return QSharedPointer<Schema>(new SqliteSchema(database));;
break;
// case PGSQL:
// return QSharedPointer<Schema>(new PgSqlSchema());
// break;
// case MYSQL:
// return QSharedPointer<Schema>(new MysqlSchema());
// break;
// case PGSQL:
// return QSharedPointer<Schema>(new PgSqlSchema());
// break;
// case MYSQL:
// return QSharedPointer<Schema>(new MysqlSchema());
// break;
default:
return QSharedPointer<Schema>(new SqliteSchema(database));
break;
......
}
void Database::debugQuery(const QSqlQuery &query) const {
qDebug() << query.executedQuery();
if(this->logger) {
if(this->logErrors) {
this->logger->lastError(query,this->logQueries);
}
} else {
qDebug() << query.executedQuery();
}
}
bool Database::select(QSqlQuery &query) {
src/database.h
#include <QString>
#include <QDebug>
#include "enums/databasetype.h"
#include "logger.h"
namespace CuteEntityManager {
class Database {
private:
QSqlDatabase database;
QString connectionName;
bool supportTransactions;
Logger *logger = nullptr;
void init();
void initLogger(bool activated, bool logQueries, bool logErrors);
bool logQueries;
bool logErrors;
public:
Database(QSqlDatabase database);
Database(QSqlDatabase database, bool loggerActivated = true, bool logQueries=false, bool logErrors=true);
~Database();
Database(QString databaseType, QString connectionName = QString(""),
QString hostname = QString(""),
QString databasename = QString("") ,
QString username = QString(""), QString password = QString(""),
qint64 port = 0);
qint64 port = 0, bool loggerActivated = true, bool logQueries=false, bool logErrors=true);
QSqlDatabase getDatabase();
QString getConnectionName();
QSqlQuery getQuery();
src/entitymanager.cpp
QString hostname,
QString username,
QString password, QString port) : QObject() {
bool logQueries = false;
#ifdef QT_DEBUG
logQueries = true;
#endif
auto db = new Database(databaseType, this->createConnection(), hostname,
databasename, username,
password,
port.toInt());
port.toInt(),true,logQueries);
this->db = QSharedPointer<Database>(db);
this->init();
}
src/entitymanager.h
namespace CuteEntityManager {
class Logger;
class EntityManager : public QObject {
Q_OBJECT
signals:
src/logger.cpp
return QDir::currentPath() + "/errors.log";
}
void Logger::lastError(const QSqlQuery &q) {
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);
qDebug() << this->generateLogMsg(q.lastError());
qDebug() << generateLogMsg(q);
stream << this->generateLogMsg(q.lastError());
stream << generateLogMsg(q);
const QString errorMsg = this->generateLogMsg(q.lastError());
if (!errorMsg.isEmpty()) {
qDebug() << errorMsg;
stream << errorMsg;
}
if(logQuery) {
const QString query = this->generateLogMsg(q);
if(!query.isEmpty()) {
qDebug() << query;
stream << query;
}
}
stream.flush();
log.close();
}
......
}
QString Logger::generateLogMsg(const QSqlQuery &q) const {
QString r = "Query:<" + q.executedQuery() + ">";
QString r = "<" + 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() + ">";
if(!m.isEmpty()) {
r += "Values: ";
for (i = m.begin(); i != m.end(); ++i) {
r += "{" + i.key() + ":" + i.value().toString() + "}";
}
}
return r;
}
......
}
QString Logger::generateLogMsg(const QSqlError &e) const {
return "UTC:" + QDateTime::currentDateTime().toString("yyyy-MM-dd|hh:MM:ss") + "|" + e.driverText() + "|" + e.databaseText().toLatin1() + "\n";
if(e.isValid()) {
return "UTC:" + QDateTime::currentDateTime().toString("yyyy-MM-dd|hh:MM:ss") + "|" + e.driverText() + "|" + e.databaseText().toLatin1() + "\n";
} else {
return "";
}
}
src/logger.h
{
public:
Logger(QString path="");
virtual ~Logger();
~Logger();
QString defaultPath() const;
void lastError(const QSqlError &e);
void lastError(const QSqlQuery &q);
void lastError(const QSqlQuery &q, bool logQuery=false);
QString getPath();
void setPath(const QString &value);

Auch abrufbar als: Unified diff