From 3e31f78045086b11db96824b76bade2ab5c15ad5 Mon Sep 17 00:00:00 2001 From: John Janus Date: Fri, 3 Mar 2017 17:10:37 +0100 Subject: [PATCH] changed logging to categories --- UserSheets.pro | 13 ++++++++++--- controller.cpp | 4 ++-- database.cpp | 23 ++++++++++++----------- database.h | 3 ++- logging.cpp | 4 ++++ logging.h | 10 ++++++++++ logrules.ini | 3 +++ main.cpp | 6 ++---- 8 files changed, 45 insertions(+), 21 deletions(-) create mode 100644 logging.cpp create mode 100644 logging.h create mode 100644 logrules.ini diff --git a/UserSheets.pro b/UserSheets.pro index e3dd443..a5b4c0e 100644 --- a/UserSheets.pro +++ b/UserSheets.pro @@ -4,7 +4,8 @@ CONFIG += c++11 SOURCES += main.cpp \ database.cpp \ - controller.cpp + controller.cpp \ + logging.cpp RESOURCES += qml.qrc @@ -20,6 +21,9 @@ QML_DESIGNER_IMPORT_PATH = # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS +# Disable all qDebug output in release builds +#CONFIG(release):DEFINES += QT_NO_DEBUG_OUTPUT + # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. @@ -32,6 +36,9 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin HEADERS += \ database.h \ - controller.h + controller.h \ + logging.h -DISTFILES += style.astylrc +DISTFILES += style.astylrc \ + logrules.ini +#DEFINES += QT_LOGGING_CONF="logrules.ini" diff --git a/controller.cpp b/controller.cpp index 7fb9dc1..8455eb9 100644 --- a/controller.cpp +++ b/controller.cpp @@ -28,7 +28,7 @@ void Controller::openAddHuman() if (component.isReady() ) { QObject* dialog = component.create(); - QQmlEngine::setObjectOwnership (dialog, QQmlEngine::CppOwnership); + QQmlEngine::setObjectOwnership (dialog, QQmlEngine::JavaScriptOwnership); QObject::connect (dialog, SIGNAL (addHuman (QVariant, QVariant, QVariant) ), db, SLOT (addHuman (QVariant, QVariant, QVariant) ) ); @@ -45,7 +45,7 @@ void Controller::openAddService() if (component.isReady() ) { QObject* dialog = component.create(); - QQmlEngine::setObjectOwnership (dialog, QQmlEngine::CppOwnership); + QQmlEngine::setObjectOwnership (dialog, QQmlEngine::JavaScriptOwnership); QObject::connect (dialog, SIGNAL (addService (QVariant, QVariant, QVariant) ), db, SLOT (addService (QVariant, QVariant, QVariant) ) ); } else { diff --git a/database.cpp b/database.cpp index 51162c5..7128599 100644 --- a/database.cpp +++ b/database.cpp @@ -1,7 +1,7 @@ #include "database.h" #include -#include #include +#include "logging.h" DataBase::DataBase (QObject* parent) : QObject (parent) { @@ -10,7 +10,8 @@ DataBase::DataBase (QObject* parent) : QObject (parent) if (!db.open() ) { //do stuff } - qDebug() << initDB(); + + qCDebug (dbase) << initDB(); } @@ -22,26 +23,26 @@ DataBase::~DataBase() void DataBase::addHuman (const QVariant& name, const QVariant& firstname, const QVariant& birthday) { - qDebug() << "addHuman slot called with: " << name << ", " << firstname << ", " << birthday; + qCDebug (dbase) << "addHuman slot called with: " << name << ", " << firstname << ", " << birthday; QSqlQuery q; - if (!q.prepare (this->sqlAddHuman) ) qDebug() << "error while preparing: " << q.lastError(); + if (!q.prepare (this->sqlAddHuman) ) qCDebug (dbase) << "error while preparing: " << q.lastError(); q.addBindValue (name.toString() ); q.addBindValue (firstname.toString() ); q.addBindValue (birthday.toDate() ); - if (!q.exec() ) qDebug() << "error: " << q.lastError(); + if (!q.exec() ) qCDebug (dbase) << "error: " << q.lastError(); } void DataBase::addService (const QVariant& name, const QVariant& url, const QVariant& description) { - qDebug() << "addService slot called with: " << name << ", " << url << ", " << description; + qCDebug (dbase) << "addService slot called with: " << name << ", " << url << ", " << description; QSqlQuery q; - if (!q.prepare (this->sqlAddService) ) qDebug() << "error while preparing: " << q.lastError(); + if (!q.prepare (this->sqlAddService) ) qCDebug (dbase) << "error while preparing: " << q.lastError(); q.addBindValue (name.toString() ); q.addBindValue (url.toUrl() ); q.addBindValue (description.toString() ); - if (!q.exec() ) qDebug() << "error: " << q.lastError(); + if (!q.exec() ) qCDebug (dbase) << "error: " << q.lastError(); } void DataBase::addUser (const QVariant& humanID, const QVariant& serviceID, @@ -62,13 +63,13 @@ QSqlError DataBase::initDB() QSqlQuery q; if (!tables.contains ("human", Qt::CaseSensitivity::CaseInsensitive) ) { if (!q.exec (this->sqlCreateHumanTable) ) return q.lastError(); - } else qDebug() << "table human already created"; + } else qCDebug (dbase) << "table human already created"; if (!tables.contains ("service", Qt::CaseSensitivity::CaseInsensitive) ) { if (!q.exec (this->sqlCreateServiceTable) ) return q.lastError(); - } else qDebug() << "table service already created"; + } else qCDebug (dbase) << "table service already created"; if (!tables.contains ("user", Qt::CaseSensitivity::CaseInsensitive) ) { if (!q.exec (this->sqlCreateUserTable) ) return q.lastError(); - } else qDebug() << "table user already created"; + } else qCDebug (dbase) << "table user already created"; return QSqlError(); } diff --git a/database.h b/database.h index dc272d0..677aea0 100644 --- a/database.h +++ b/database.h @@ -7,6 +7,7 @@ #include #include + class DataBase : public QObject { Q_OBJECT @@ -29,8 +30,8 @@ public slots: private: QSqlError initDB(); - QSqlDatabase db; + const QLatin1String sqlCreateHumanTable = QLatin1String ( "create table human(" "id integer primary key autoincrement," diff --git a/logging.cpp b/logging.cpp new file mode 100644 index 0000000..408b1ec --- /dev/null +++ b/logging.cpp @@ -0,0 +1,4 @@ +#include "logging.h" + + +Q_LOGGING_CATEGORY (dbase, "database") diff --git a/logging.h b/logging.h new file mode 100644 index 0000000..25fd4bb --- /dev/null +++ b/logging.h @@ -0,0 +1,10 @@ +#ifndef LOGGING_H +#define LOGGING_H +#include + +const QtMsgType loglevel = QtMsgType::QtWarningMsg; +Q_DECLARE_LOGGING_CATEGORY (dbase) + + + +#endif // LOGGING_H diff --git a/logrules.ini b/logrules.ini new file mode 100644 index 0000000..e33cf2e --- /dev/null +++ b/logrules.ini @@ -0,0 +1,3 @@ +[Rules] +*.debug=false + diff --git a/main.cpp b/main.cpp index ef1cb52..4c99656 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,13 @@ #include -#include #include "database.h" #include "controller.h" -#include -#include +#include "logging.h" int main (int argc, char* argv[]) { // QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app (argc, argv); - + qSetMessagePattern ("[%{time yyyyMMdd h:mm:ss.zzz t}] %{if-category}%{category}: %{endif}%{message}"); DataBase db; Controller c (&db); -- 2.47.0