From 162e1ca996df64e828428c1e43ea8dea19dba5ec Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Sun, 22 Nov 2020 05:58:47 +0200 Subject: [PATCH] Take logging categories into use Logging can be enabled in release build like this: QT_LOGGING_RULES="fernschreiber.*=true" harbour-fernschreiber See https://doc.qt.io/qt-5/qloggingcategory.html for more details. --- harbour-fernschreiber.pro | 1 + src/appsettings.cpp | 4 ++-- src/chatlistmodel.cpp | 4 ++-- src/chatmodel.cpp | 4 ++-- src/dbusadaptor.cpp | 8 +++---- src/dbusinterface.cpp | 12 ++++++---- src/debuglog.h | 45 +++++++++++++++++++++++++++++++++++ src/emojisearchworker.cpp | 3 ++- src/emojisearchworker.h | 1 - src/harbour-fernschreiber.cpp | 11 +++++++++ src/notificationmanager.cpp | 4 ++-- src/processlauncher.cpp | 19 +++++++++------ src/processlauncher.h | 3 --- src/stickermanager.cpp | 4 ++-- src/tdlibfile.cpp | 4 ++-- src/tdlibreceiver.cpp | 9 ++----- src/tdlibreceiver.h | 3 ++- src/tdlibwrapper.cpp | 38 +++++++++++++---------------- src/tgsplugin.cpp | 18 +++++++------- 19 files changed, 123 insertions(+), 72 deletions(-) create mode 100644 src/debuglog.h diff --git a/harbour-fernschreiber.pro b/harbour-fernschreiber.pro index 1276f8d..cc70636 100644 --- a/harbour-fernschreiber.pro +++ b/harbour-fernschreiber.pro @@ -145,6 +145,7 @@ HEADERS += \ src/chatmodel.h \ src/dbusadaptor.h \ src/dbusinterface.h \ + src/debuglog.h \ src/emojisearchworker.h \ src/fernschreiberutils.h \ src/notificationmanager.h \ diff --git a/src/appsettings.cpp b/src/appsettings.cpp index 6dd3bcc..5f402ff 100644 --- a/src/appsettings.cpp +++ b/src/appsettings.cpp @@ -16,9 +16,9 @@ */ #include "appsettings.h" -#include -#define LOG(x) qDebug() << "[AppSettings]" << x +#define DEBUG_MODULE AppSettings +#include "debuglog.h" namespace { const QString KEY_SEND_BY_ENTER("sendByEnter"); diff --git a/src/chatlistmodel.cpp b/src/chatlistmodel.cpp index 2610179..4a48045 100644 --- a/src/chatlistmodel.cpp +++ b/src/chatlistmodel.cpp @@ -19,9 +19,9 @@ #include "chatlistmodel.h" #include "fernschreiberutils.h" -#include -#define LOG(x) qDebug() << "[ChatListModel]" << x +#define DEBUG_MODULE ChatListModel +#include "debuglog.h" namespace { const QString ID("id"); diff --git a/src/chatmodel.cpp b/src/chatmodel.cpp index 084a737..0b02943 100644 --- a/src/chatmodel.cpp +++ b/src/chatmodel.cpp @@ -22,9 +22,9 @@ #include #include #include -#include -#define LOG(x) qDebug() << "[ChatModel]" << x +#define DEBUG_MODULE ChatModel +#include "debuglog.h" namespace { const QString ID("id"); diff --git a/src/dbusadaptor.cpp b/src/dbusadaptor.cpp index f2b91a1..16e17ed 100644 --- a/src/dbusadaptor.cpp +++ b/src/dbusadaptor.cpp @@ -19,22 +19,22 @@ #include "dbusadaptor.h" -#include +#define DEBUG_MODULE DBusAdaptor +#include "debuglog.h" DBusAdaptor::DBusAdaptor(QObject *parent): QDBusAbstractAdaptor(parent) { - } void DBusAdaptor::openMessage(const QString &chatId, const QString &messageId) { - qDebug() << "[DBusAdaptor] Open Message " << chatId << messageId; + LOG("Open Message" << chatId << messageId); emit pleaseOpenMessage(chatId, messageId); } void DBusAdaptor::openUrl(const QStringList &arguments) { - qDebug() << "[DBusAdaptor] Open Url" << arguments; + LOG("Open Url" << arguments); if (arguments.length() >= 1) { emit pleaseOpenUrl(arguments.first()); } diff --git a/src/dbusinterface.cpp b/src/dbusinterface.cpp index ab38826..38753d0 100644 --- a/src/dbusinterface.cpp +++ b/src/dbusinterface.cpp @@ -19,27 +19,29 @@ #include "dbusinterface.h" +#define DEBUG_MODULE DBusInterface +#include "debuglog.h" + DBusInterface::DBusInterface(QObject *parent) : QObject(parent) { - qDebug() << "[DBusInterface] Initializing D-BUS connectivity"; + LOG("Initializing D-BUS connectivity"); this->dbusAdaptor = new DBusAdaptor(this); QDBusConnection sessionBusConnection = QDBusConnection::sessionBus(); if (!sessionBusConnection.isConnected()) { - qDebug() << "[DBusInterface] Error connecting to D-BUS"; + WARN("Error connecting to D-BUS"); return; } if (!sessionBusConnection.registerObject(PATH_NAME, this)) { - qDebug() << "[DBusInterface] Error registering root object to D-BUS" << sessionBusConnection.lastError().message(); + WARN("Error registering root object to D-BUS" << sessionBusConnection.lastError().message()); return; } if (!sessionBusConnection.registerService(INTERFACE_NAME)) { - qDebug() << "[DBusInterface] Error registering interface to D-BUS" << sessionBusConnection.lastError().message(); + WARN("Error registering interface to D-BUS" << sessionBusConnection.lastError().message()); return; } - } DBusAdaptor *DBusInterface::getDBusAdaptor() diff --git a/src/debuglog.h b/src/debuglog.h new file mode 100644 index 0000000..d198781 --- /dev/null +++ b/src/debuglog.h @@ -0,0 +1,45 @@ +/* + Copyright (C) 2020 Slava Monich et al. + + This file is part of Fernschreiber. + + Fernschreiber is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Fernschreiber is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Fernschreiber. If not, see . +*/ + +#ifndef FERNSCHREIBER_DEBUG_LOG_H +#define FERNSCHREIBER_DEBUG_LOG_H + +#include + +#ifndef DEBUG_MODULE +# define DEBUG_MODULE Debug +#endif + +#define LOG_CATEGORY__(x) x##Log +#define LOG_CATEGORY_(x) LOG_CATEGORY__(x) +#define LOG_CATEGORY LOG_CATEGORY_(DEBUG_MODULE) +static const QLoggingCategory LOG_CATEGORY("fernschreiber." QT_STRINGIFY(DEBUG_MODULE)); +#define LOG(x) qCDebug(LOG_CATEGORY) << "[" QT_STRINGIFY(DEBUG_MODULE) "]" << x +#define WARN(x) qCWarning(LOG_CATEGORY) << "[" QT_STRINGIFY(DEBUG_MODULE) "]" << x + +// No VERBOSE in release build +#ifndef VERBOSE +# if defined (QT_DEBUG) || defined (DEBUG) +# define VERBOSE(x) LOG(x) +# else +# define VERBOSE(x) +# endif +#endif + +#endif // FERNSCHREIBER_DEBUG_LOG_H diff --git a/src/emojisearchworker.cpp b/src/emojisearchworker.cpp index c239157..c01bfd5 100644 --- a/src/emojisearchworker.cpp +++ b/src/emojisearchworker.cpp @@ -18,7 +18,8 @@ */ #include "emojisearchworker.h" -#define LOG(x) qDebug() << "[EmojiSearchWorker]" << x +#define DEBUG_MODULE EmojiSearchWorker +#include "debuglog.h" EmojiSearchWorker::~EmojiSearchWorker() { diff --git a/src/emojisearchworker.h b/src/emojisearchworker.h index 25a785d..7401225 100644 --- a/src/emojisearchworker.h +++ b/src/emojisearchworker.h @@ -24,7 +24,6 @@ #include #include #include -#include class EmojiSearchWorker : public QThread { diff --git a/src/harbour-fernschreiber.cpp b/src/harbour-fernschreiber.cpp index 5d413a1..77bca68 100644 --- a/src/harbour-fernschreiber.cpp +++ b/src/harbour-fernschreiber.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include "appsettings.h" #include "tdlibfile.h" @@ -41,10 +42,20 @@ #include "tgsplugin.h" #include "fernschreiberutils.h" +// The default filter can be overridden by QT_LOGGING_RULES envinronment variable, e.g. +// QT_LOGGING_RULES="fernschreiber.*=true" harbour-fernschreiber +#if defined (QT_DEBUG) || defined(DEBUG) +# define DEFAULT_LOG_FILTER "fernschreiber.*=true" +#else +# define DEFAULT_LOG_FILTER "fernschreiber.*=false" +#endif + Q_IMPORT_PLUGIN(TgsIOPlugin) int main(int argc, char *argv[]) { + QLoggingCategory::setFilterRules(DEFAULT_LOG_FILTER); + QScopedPointer app(SailfishApp::application(argc, argv)); QScopedPointer view(SailfishApp::createView()); diff --git a/src/notificationmanager.cpp b/src/notificationmanager.cpp index f1c6914..d87bc91 100644 --- a/src/notificationmanager.cpp +++ b/src/notificationmanager.cpp @@ -21,14 +21,14 @@ #include "fernschreiberutils.h" #include "chatmodel.h" #include -#include #include #include #include #include #include -#define LOG(x) qDebug() << "[NotificationManager]" << x +#define DEBUG_MODULE NotificationManager +#include "debuglog.h" namespace { const QString _TYPE("@type"); diff --git a/src/processlauncher.cpp b/src/processlauncher.cpp index b2f9b5f..323822b 100644 --- a/src/processlauncher.cpp +++ b/src/processlauncher.cpp @@ -1,18 +1,23 @@ #include "processlauncher.h" +#include +#include -#define LOG(x) qDebug() << "[ProcessLauncher]" << x +#define DEBUG_MODULE ProcessLauncher +#include "debuglog.h" ProcessLauncher::ProcessLauncher(QObject *parent) : QObject(parent) { - } + bool ProcessLauncher::launchProgram(const QString &program, const QStringList &arguments) { - QString executablePath = QStandardPaths::findExecutable(program); - if(executablePath == "") { - LOG("[ProcessLauncher] Program " + program + "not found"); + const QString executablePath(QStandardPaths::findExecutable(program)); + if (executablePath.isEmpty()) { + LOG("Program" << program << "not found"); return false; } - QProcess *externalProcess = new QProcess(this); - return externalProcess->startDetached(program, arguments); + + QProcess *process = new QProcess(this); + connect(process, SIGNAL(finished(int)), process, SLOT(deleteLater())); + return process->startDetached(program, arguments); } diff --git a/src/processlauncher.h b/src/processlauncher.h index 5583637..84ae84a 100644 --- a/src/processlauncher.h +++ b/src/processlauncher.h @@ -2,9 +2,6 @@ #define PROCESSLAUNCHER_H #include -#include -#include -#include class ProcessLauncher : public QObject { diff --git a/src/stickermanager.cpp b/src/stickermanager.cpp index fc6b593..f09ca3b 100644 --- a/src/stickermanager.cpp +++ b/src/stickermanager.cpp @@ -18,10 +18,10 @@ */ #include "stickermanager.h" -#include #include -#define LOG(x) qDebug() << "[StickerManager]" << x +#define DEBUG_MODULE StickerManager +#include "debuglog.h" StickerManager::StickerManager(TDLibWrapper *tdLibWrapper, QObject *parent) : QObject(parent) { diff --git a/src/tdlibfile.cpp b/src/tdlibfile.cpp index 7e98979..232fa9a 100644 --- a/src/tdlibfile.cpp +++ b/src/tdlibfile.cpp @@ -18,9 +18,9 @@ */ #include "tdlibfile.h" -#include -#define LOG(x) qDebug() << "[TDLibFile]" << x +#define DEBUG_MODULE TDLibFile +#include "debuglog.h" namespace { const QString ID("id"); diff --git a/src/tdlibreceiver.cpp b/src/tdlibreceiver.cpp index a37f1a9..b15230c 100644 --- a/src/tdlibreceiver.cpp +++ b/src/tdlibreceiver.cpp @@ -18,13 +18,8 @@ */ #include "tdlibreceiver.h" -#define LOG(x) qDebug() << "[TDLibReceiver]" << x - -#if defined (QT_DEBUG) || defined (DEBUG) -# define VERBOSE(x) LOG(x) -#else -# define VERBOSE(x) -#endif +#define DEBUG_MODULE TDLibReceiver +#include "debuglog.h" namespace { const QString ID("id"); diff --git a/src/tdlibreceiver.h b/src/tdlibreceiver.h index 3d041c7..48b04af 100644 --- a/src/tdlibreceiver.h +++ b/src/tdlibreceiver.h @@ -19,7 +19,8 @@ #ifndef TDLIBRECEIVER_H #define TDLIBRECEIVER_H -#include +#include +#include #include #include #include diff --git a/src/tdlibwrapper.cpp b/src/tdlibwrapper.cpp index 293dbfa..df6f71c 100644 --- a/src/tdlibwrapper.cpp +++ b/src/tdlibwrapper.cpp @@ -25,19 +25,13 @@ #include #include #include -#include #include #include #include #include -#define LOG(x) qDebug() << "[TDLibWrapper]" << x - -#if defined (QT_DEBUG) || defined (DEBUG) -# define VERBOSE(x) LOG(x) -#else -# define VERBOSE(x) -#endif +#define DEBUG_MODULE TDLibWrapper +#include "debuglog.h" namespace { const QString STATUS("status"); @@ -199,7 +193,7 @@ void TDLibWrapper::setAuthenticationPassword(const QString &authenticationPasswo void TDLibWrapper::registerUser(const QString &firstName, const QString &lastName) { - qDebug() << "[TDLibWrapper] Register User " << firstName << lastName; + LOG("Register User " << firstName << lastName); QVariantMap requestObject; requestObject.insert("@type", "registerUser"); requestObject.insert("first_name", firstName); @@ -890,9 +884,9 @@ void TDLibWrapper::openFileOnDevice(const QString &filePath) argumentsList.append(filePath); bool successfullyStarted = QProcess::startDetached("xdg-open", argumentsList); if (successfullyStarted) { - qDebug() << "Successfully opened file " << filePath; + LOG("Successfully opened file " << filePath); } else { - qDebug() << "Error opening file " << filePath; + LOG("Error opening file " << filePath); } } @@ -903,10 +897,10 @@ void TDLibWrapper::controlScreenSaver(bool enabled) QDBusInterface dbusInterface("com.nokia.mce", "/com/nokia/mce/request", "com.nokia.mce.request", dbusConnection); if (enabled) { - qDebug() << "Enabling screensaver"; + LOG("Enabling screensaver"); dbusInterface.call("req_display_cancel_blanking_pause"); } else { - qDebug() << "Disabling screensaver"; + LOG("Disabling screensaver"); dbusInterface.call("req_display_blanking_pause"); } } @@ -1178,21 +1172,21 @@ void TDLibWrapper::setLogVerbosityLevel() void TDLibWrapper::initializeOpenWith() { LOG("Initialize open-with"); - - qDebug() << "Checking standard open URL file..."; - QString openUrlFilePath = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation) + "/open-url.desktop"; + LOG("Checking standard open URL file..."); + const QString applicationsLocation(QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)); + const QString openUrlFilePath(applicationsLocation + "/open-url.desktop"); if (QFile::exists(openUrlFilePath)) { - qDebug() << "Standard open URL file exists, good!"; + LOG("Standard open URL file exists, good!"); } else { - qDebug() << "Copying standard open URL file to " << openUrlFilePath; + LOG("Copying standard open URL file to " << openUrlFilePath); QFile::copy("/usr/share/applications/open-url.desktop", openUrlFilePath); - QProcess::startDetached("update-desktop-database " + QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)); + QProcess::startDetached("update-desktop-database " + applicationsLocation); } - QString desktopFilePath = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation) + "/harbour-fernschreiber-open-url.desktop"; + const QString desktopFilePath(applicationsLocation + "/harbour-fernschreiber-open-url.desktop"); QFile desktopFile(desktopFilePath); if (!desktopFile.exists()) { - qDebug() << "Creating Open-With file at " << desktopFile.fileName(); + LOG("Creating Open-With file at " << desktopFile.fileName()); if (desktopFile.open(QIODevice::WriteOnly | QIODevice::Text)) { QTextStream fileOut(&desktopFile); fileOut.setCodec("UTF-8"); @@ -1208,7 +1202,7 @@ void TDLibWrapper::initializeOpenWith() fileOut << QString("Hidden=true;").toUtf8() << "\n"; fileOut.flush(); desktopFile.close(); - QProcess::startDetached("update-desktop-database " + QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation)); + QProcess::startDetached("update-desktop-database " + applicationsLocation); } } diff --git a/src/tgsplugin.cpp b/src/tgsplugin.cpp index 23a7193..e95b46e 100644 --- a/src/tgsplugin.cpp +++ b/src/tgsplugin.cpp @@ -18,7 +18,6 @@ #include "tgsplugin.h" #include "rlottie.h" -#include #include #include #include @@ -27,8 +26,9 @@ #include -#define LOG(x) qDebug() << "[TgsIOHandler]" << qPrintable(fileName) << x -#define WARN(x) qWarning() << "[TgsIOHandler]" << qPrintable(fileName) << x +#define DEBUG_MODULE TgsIOHandler +#include "debuglog.h" +#define LOG_(x) LOG(qPrintable(fileName) << x) class TgsIOHandler : public QImageIOHandler { @@ -93,7 +93,7 @@ TgsIOHandler::~TgsIOHandler() if (currentRender.valid()) { currentRender.get(); } - LOG("Done"); + LOG_("Done"); } TgsIOHandler::ByteArray TgsIOHandler::uncompress() @@ -112,7 +112,7 @@ TgsIOHandler::ByteArray TgsIOHandler::uncompress() unzip.next_out = (Bytef*)unzipped.data(); unzip.avail_in = zipped.size(); unzip.avail_out = chunk; - LOG("Compressed size" << zipped.size()); + LOG_("Compressed size" << zipped.size()); while (unzip.avail_out > 0 && zerr == Z_OK) { zerr = inflate(&unzip, Z_NO_FLUSH); if (zerr == Z_OK && unzip.avail_out < chunk) { @@ -124,7 +124,7 @@ TgsIOHandler::ByteArray TgsIOHandler::uncompress() } if (zerr == Z_STREAM_END) { unzipped.resize(unzip.next_out - (Bytef*)unzipped.data()); - LOG("Uncompressed size" << unzipped.size()); + LOG_("Uncompressed size" << unzipped.size()); } else { unzipped.clear(); } @@ -146,7 +146,7 @@ bool TgsIOHandler::load() frameRate = animation->frameRate(); frameCount = (int) animation->totalFrame(); size = QSize(width, height); - LOG(size << frameCount << "frames," << frameRate << "fps"); + LOG_(size << frameCount << "frames," << frameRate << "fps"); render(0); // Pre-render first frame } } @@ -160,7 +160,7 @@ void TgsIOHandler::finishRendering() currentRender.get(); prevImage = currentImage; if (!currentFrame && !firstImage.isNull()) { - LOG("Rendered first frame"); + LOG_("Rendered first frame"); firstImage = currentImage; } } else { @@ -192,7 +192,7 @@ bool TgsIOHandler::read(QImage* out) if (currentFrame && currentRender.valid()) { std::future_status status = currentRender.wait_for(std::chrono::milliseconds(0)); if (status != std::future_status::ready) { - LOG("Skipping frame" << currentFrame); + LOG_("Skipping frame" << currentFrame); currentFrame = (currentFrame + 1) % frameCount; *out = prevImage; return true;