From 6ba1147d1172e295ad5467382f45cec206267464 Mon Sep 17 00:00:00 2001 From: Albert S Date: Wed, 10 Apr 2019 18:57:27 +0200 Subject: [PATCH] Added Utils::error() and Utils::info() to print to stderr/stdout instead of qDebug() --- cli/command.cpp | 2 +- cli/command.h | 1 + cli/commandadd.cpp | 21 +++++++++++---------- cli/commanddelete.cpp | 14 +++++++------- cli/main.cpp | 5 +++-- cli/utils.cpp | 22 ++++++++++++++++++++++ cli/utils.h | 3 +++ 7 files changed, 48 insertions(+), 20 deletions(-) diff --git a/cli/command.cpp b/cli/command.cpp index 733bb21..5c0b20b 100644 --- a/cli/command.cpp +++ b/cli/command.cpp @@ -46,7 +46,7 @@ QSqlDatabase Command::dbConnection() db.setDatabaseName(this->dbConnectionString); if(!db.open()) { - qDebug() << "Failed to open the database: " << this->dbConnectionString; + Utils::error() << "Failed to open the database: " << this->dbConnectionString; } dbStore.setLocalData(db); return db; diff --git a/cli/command.h b/cli/command.h index e2f547e..946ca42 100644 --- a/cli/command.h +++ b/cli/command.h @@ -5,6 +5,7 @@ #include #include #include +#include "utils.h" class Command { protected: diff --git a/cli/commandadd.cpp b/cli/commandadd.cpp index ea1f722..8290d21 100644 --- a/cli/commandadd.cpp +++ b/cli/commandadd.cpp @@ -58,14 +58,14 @@ AddFileResult CommandAdd::addFile(QString path) if(pageData.isEmpty()) { - qDebug() << "Could not get any content for " << absPath; + Utils::error() << "Could not get any content for " << absPath << endl; } // Workaround to "database is locked" error. Perhaps try WAL mode etc. // QMutexLocker locker(&writeMutex); if(!db.transaction()) { - qDebug() << "Failed to open transaction for " << absPath << " : " << db.lastError(); + Utils::error() << "Failed to open transaction for " << absPath << " : " << db.lastError() << endl; return DBFAIL; } @@ -73,7 +73,7 @@ AddFileResult CommandAdd::addFile(QString path) delQuery.addBindValue(absPath); if(!delQuery.exec()) { - qDebug() << "Failed DELETE query" << delQuery.lastError(); + Utils::error() << "Failed DELETE query" << delQuery.lastError() << endl; db.rollback(); return DBFAIL; } @@ -85,7 +85,7 @@ AddFileResult CommandAdd::addFile(QString path) inserterQuery.addBindValue(fileType); if(!inserterQuery.exec()) { - qDebug() << "Failed INSERT query" << inserterQuery.lastError(); + Utils::error() << "Failed INSERT query" << inserterQuery.lastError() << endl; db.rollback(); return DBFAIL; } @@ -99,7 +99,7 @@ AddFileResult CommandAdd::addFile(QString path) if(!contentQuery.exec()) { db.rollback(); - qDebug() << "Failed content insertion " << contentQuery.lastError(); + Utils::error() << "Failed content insertion " << contentQuery.lastError() << endl; return DBFAIL; } } @@ -107,7 +107,7 @@ AddFileResult CommandAdd::addFile(QString path) if(!db.commit()) { db.rollback(); - qDebug() << "Failed to commit transaction for " << absPath << " : " << db.lastError(); + Utils::error() << "Failed to commit transaction for " << absPath << " : " << db.lastError() << endl; return DBFAIL; } return OK; @@ -168,12 +168,12 @@ int CommandAdd::handle(QStringList arguments) } if(verbose) { - qDebug() << "Processing " << path; + Utils::info() << "Processing " << path << endl; } auto result = addFile(path); if(result == DBFAIL) { - qDebug() << "Failed to add " << path; + Utils::error() << "Failed to add " << path << endl; if(!keepGoing) { terminate = true; @@ -183,11 +183,12 @@ int CommandAdd::handle(QStringList arguments) { if(result == SKIPPED) { - qDebug() << "Skipped" << path << "as it already exists in the database"; + Utils::info() + << "Skipped" << path << "as it already exists in the database" << endl; } else { - qDebug() << "Added" << path; + Utils::info() << "Added" << path << endl; } } }); diff --git a/cli/commanddelete.cpp b/cli/commanddelete.cpp index 28c0a7f..514ec4e 100644 --- a/cli/commanddelete.cpp +++ b/cli/commanddelete.cpp @@ -21,7 +21,7 @@ int CommandDelete::removeNonExistent(QSqlDatabase &db, bool verbose, bool dryRun QSqlQuery pathsQuery("SELECT path FROM file", db); if(!pathsQuery.exec()) { - qDebug() << "Failed to query current paths"; + Utils::error() << "Failed to query current paths" << endl; return 1; } @@ -44,17 +44,17 @@ int CommandDelete::removeNonExistent(QSqlDatabase &db, bool verbose, bool dryRun query.addBindValue(path); if(!query.exec()) { - qDebug() << "Failed to delete " << path << query.lastError(); + Utils::error() << "Failed to delete " << path << query.lastError() << endl; return 1; } if(verbose) { - qInfo() << "Deleted " << path; + Utils::info() << "Deleted " << path << endl; } } else { - qInfo() << "Would delete " << path; + Utils::info() << "Would delete " << path << endl; } } } @@ -78,19 +78,19 @@ int CommandDelete::removePaths(const QStringList &paths, QSqlDatabase &db, bool { if(verbose) { - qInfo() << "Deleted" << absPath; + Utils::info() << "Deleted" << absPath << endl; } } else { - qDebug() << "Failed to delete:" << absPath << deletionQuery.lastError(); + Utils::error() << "Failed to delete:" << absPath << deletionQuery.lastError() << endl; result = 1; } } } else { - qInfo() << "No such file in database:" << absPath; + Utils::error() << "No such file in database:" << absPath << endl; result = 1; } } diff --git a/cli/main.cpp b/cli/main.cpp index cddc38f..34fd200 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -15,6 +15,7 @@ #include "encodingdetector.h" #include "pdfprocessor.h" #include "defaulttextprocessor.h" +#include "utils.h" #include "command.h" #include "commandadd.h" #include "commanddelete.h" @@ -64,12 +65,12 @@ int main(int argc, char *argv[]) } catch(const QSSGeneralException &e) { - qDebug() << "Exception caught, message: " << e.message; + Utils::error() << "Exception caught, message: " << e.message << endl; } } else { - qDebug() << "Unknown command " << commandName; + Utils::error() << "Unknown command " << commandName << endl; } return 1; } diff --git a/cli/utils.cpp b/cli/utils.cpp index 802af30..cbe1737 100644 --- a/cli/utils.cpp +++ b/cli/utils.cpp @@ -18,3 +18,25 @@ QByteArray Utils::readFile(QString path) } return data; } + +QDebug &Utils::info() +{ + static QDebug result = [] + { + QFile *file = new QFile(); + file->open(stderr, QIODevice::WriteOnly); + return QDebug(file); + }(); + return result; +} + +QDebug &Utils::error() +{ + static QDebug result = [] + { + QFile *file = new QFile(); + file->open(stdout, QIODevice::WriteOnly); + return QDebug(file); + }(); + return result; +} diff --git a/cli/utils.h b/cli/utils.h index ee5143a..fc64ea5 100644 --- a/cli/utils.h +++ b/cli/utils.h @@ -3,6 +3,7 @@ #include #include #include +#include #include "qssgeneralexception.h" class Utils @@ -10,6 +11,8 @@ class Utils public: Utils(); static QByteArray readFile(QString path); + static QDebug &info(); + static QDebug &error(); }; #endif // UTILS_H