From 5ef45298e1262cd5dbff8978be779917cc4daec2 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Thu, 24 Feb 2022 10:41:09 +0300 Subject: [PATCH] UI: Refactor QLoggingCategory usage --- src/ui/3rdparty/sqlite/sqlitedb.cpp | 25 +++++++++------------ src/ui/appinfo/appinfomanager.cpp | 21 ++++++++--------- src/ui/conf/confmanager.cpp | 16 +++++-------- src/ui/control/controlmanager.cpp | 25 ++++++++++----------- src/ui/control/controlworker.cpp | 22 +++++++++--------- src/ui/manager/logger.cpp | 6 ++++- src/ui/manager/logger.h | 1 - src/ui/rpc/rpcmanager.cpp | 10 ++++++--- src/ui/stat/statmanager.cpp | 35 +++++++++++++---------------- 9 files changed, 78 insertions(+), 83 deletions(-) diff --git a/src/ui/3rdparty/sqlite/sqlitedb.cpp b/src/ui/3rdparty/sqlite/sqlitedb.cpp index c694ae72..fbfd37f2 100644 --- a/src/ui/3rdparty/sqlite/sqlitedb.cpp +++ b/src/ui/3rdparty/sqlite/sqlitedb.cpp @@ -10,14 +10,10 @@ #include "sqlitestmt.h" -Q_DECLARE_LOGGING_CATEGORY(CLOG_SQLITEDB) -Q_LOGGING_CATEGORY(CLOG_SQLITEDB, "db") - -#define dbWarning() qCWarning(CLOG_SQLITEDB, ) -#define dbCritical() qCCritical(CLOG_SQLITEDB, ) - namespace { +const QLoggingCategory LC("db"); + const char *const defaultSqlPragmas = "PRAGMA journal_mode = WAL;" "PRAGMA locking_mode = NORMAL;" "PRAGMA synchronous = NORMAL;" @@ -29,7 +25,7 @@ bool removeDbFile(const QString &filePath) { if (!filePath.startsWith(QLatin1Char(':')) && QFile::exists(filePath) && !QFile::remove(filePath)) { - dbCritical() << "Cannot remove file:" << filePath; + qCCritical(LC) << "Cannot remove file:" << filePath; return false; } return true; @@ -40,7 +36,7 @@ bool renameDbFile(const QString &filePath, const QString &newFilePath) removeDbFile(newFilePath); if (!QFile::rename(filePath, newFilePath)) { - dbCritical() << "Cannot rename file" << filePath << "to" << newFilePath; + qCCritical(LC) << "Cannot rename file" << filePath << "to" << newFilePath; return false; } return true; @@ -304,7 +300,8 @@ bool SqliteDb::migrate(MigrateOptions &opt) return true; if (userVersion > opt.version) { - dbWarning() << "Cannot open new DB" << userVersion << "from old application" << opt.version; + qCWarning(LC) << "Cannot open new DB" << userVersion << "from old application" + << opt.version; return false; } @@ -350,14 +347,14 @@ bool SqliteDb::migrateSqlScripts(const MigrateOptions &opt, int userVersion, boo QFile file(filePath); if (file.exists()) { if (!file.open(QFile::ReadOnly | QFile::Text)) { - dbWarning() << "Cannot open migration file" << filePath << file.errorString(); + qCWarning(LC) << "Cannot open migration file" << filePath << file.errorString(); success = false; break; } const QByteArray data = file.readAll(); if (data.isEmpty()) { - dbWarning() << "Migration file is empty" << filePath; + qCWarning(LC) << "Migration file is empty" << filePath; success = false; break; } @@ -372,7 +369,7 @@ bool SqliteDb::migrateSqlScripts(const MigrateOptions &opt, int userVersion, boo if (success) { releaseSavepoint(); } else { - dbCritical() << "Migration error:" << filePath << errorMessage(); + qCCritical(LC) << "Migration error:" << filePath << errorMessage(); rollbackSavepoint(); break; } @@ -394,7 +391,7 @@ bool SqliteDb::clearWithBackup(const char *sqlPragmas) const QString tempFilePath = backupFilePath(); if (!(renameDbFile(m_filePath, tempFilePath) && open())) { - dbWarning() << "Cannot re-create the DB" << m_filePath; + qCWarning(LC) << "Cannot re-create the DB" << m_filePath; renameDbFile(tempFilePath, m_filePath); return false; } @@ -437,7 +434,7 @@ bool SqliteDb::importDb( const QString dstSchema = migrationNewSchemaName(); if (!attach(srcSchema, sourceFilePath)) { - dbWarning() << "Cannot attach the DB" << sourceFilePath << "Error:" << errorMessage(); + qCWarning(LC) << "Cannot attach the DB" << sourceFilePath << "Error:" << errorMessage(); return false; } diff --git a/src/ui/appinfo/appinfomanager.cpp b/src/ui/appinfo/appinfomanager.cpp index 507e76e9..59f36750 100644 --- a/src/ui/appinfo/appinfomanager.cpp +++ b/src/ui/appinfo/appinfomanager.cpp @@ -10,18 +10,14 @@ #include "appinfoutil.h" #include "appinfoworker.h" -Q_DECLARE_LOGGING_CATEGORY(CLOG_APPINFO_MANAGER) -Q_LOGGING_CATEGORY(CLOG_APPINFO_MANAGER, "appInfo") - -#define logWarning() qCWarning(CLOG_APPINFO_MANAGER, ) -#define logCritical() qCCritical(CLOG_APPINFO_MANAGER, ) - -#define DATABASE_USER_VERSION 4 - -#define APP_CACHE_MAX_COUNT 2000 - namespace { +const QLoggingCategory LC("appInfo"); + +constexpr int DATABASE_USER_VERSION = 4; + +constexpr int APP_CACHE_MAX_COUNT = 2000; + const char *const sqlSelectAppInfo = "SELECT alt_path, file_descr, company_name," " product_name, product_ver, file_mod_time, icon_id" " FROM app WHERE path = ?1;"; @@ -74,7 +70,8 @@ AppInfoManager::~AppInfoManager() void AppInfoManager::setUp() { if (!sqliteDb()->open()) { - logCritical() << "File open error:" << sqliteDb()->filePath() << sqliteDb()->errorMessage(); + qCCritical(LC) << "File open error:" << sqliteDb()->filePath() + << sqliteDb()->errorMessage(); return; } @@ -84,7 +81,7 @@ void AppInfoManager::setUp() .importOldData = false }; if (!sqliteDb()->migrate(opt)) { - logCritical() << "Migration error" << sqliteDb()->filePath(); + qCCritical(LC) << "Migration error" << sqliteDb()->filePath(); return; } } diff --git a/src/ui/conf/confmanager.cpp b/src/ui/conf/confmanager.cpp index b1582202..01aa8987 100644 --- a/src/ui/conf/confmanager.cpp +++ b/src/ui/conf/confmanager.cpp @@ -28,16 +28,12 @@ #include "appgroup.h" #include "firewallconf.h" -Q_DECLARE_LOGGING_CATEGORY(CLOG_CONF_MANAGER) -Q_LOGGING_CATEGORY(CLOG_CONF_MANAGER, "conf") - -#define logWarning() qCWarning(CLOG_CONF_MANAGER, ) -#define logCritical() qCCritical(CLOG_CONF_MANAGER, ) - -#define DATABASE_USER_VERSION 11 - namespace { +const QLoggingCategory LC("conf"); + +constexpr int DATABASE_USER_VERSION = 11; + const char *const sqlSelectAddressGroups = "SELECT addr_group_id, include_all, exclude_all," " include_zones, exclude_zones," " include_text, exclude_text" @@ -398,7 +394,7 @@ void ConfManager::showErrorMessage(const QString &errorMessage) void ConfManager::setUp() { if (!sqliteDb()->open()) { - logCritical() << "File open error:" << sqliteDb()->filePath() << sqliteDb()->errorMessage(); + qCCritical(LC) << "File open error:" << sqliteDb()->filePath() << sqliteDb()->errorMessage(); return; } @@ -408,7 +404,7 @@ void ConfManager::setUp() .migrateFunc = &migrateFunc }; if (!sqliteDb()->migrate(opt)) { - logCritical() << "Migration error" << sqliteDb()->filePath(); + qCCritical(LC) << "Migration error" << sqliteDb()->filePath(); return; } diff --git a/src/ui/control/controlmanager.cpp b/src/ui/control/controlmanager.cpp index 4e35de86..a8ec0b16 100644 --- a/src/ui/control/controlmanager.cpp +++ b/src/ui/control/controlmanager.cpp @@ -19,12 +19,11 @@ #include "controlworker.h" -Q_DECLARE_LOGGING_CATEGORY(CLOG_CONTROL_MANAGER) -Q_LOGGING_CATEGORY(CLOG_CONTROL_MANAGER, "control") +namespace { -#define logDebug() qCDebug(CLOG_CONTROL_MANAGER, ) -#define logWarning() qCWarning(CLOG_CONTROL_MANAGER, ) -#define logCritical() qCCritical(CLOG_CONTROL_MANAGER, ) +const QLoggingCategory LC("control"); + +} ControlManager::ControlManager(QObject *parent) : QObject(parent) { } @@ -54,7 +53,7 @@ ControlWorker *ControlManager::newServiceClient(QObject *parent) const connect(w, &ControlWorker::requestReady, this, &ControlManager::processRequest); if (!w->connectToServer(getServerName(true))) { - logWarning() << "Server connect error:" << socket->state() << socket->errorString(); + qCWarning(LC) << "Server connect error:" << socket->state() << socket->errorString(); } return w; } @@ -70,7 +69,7 @@ bool ControlManager::listen() settings->isService() ? QLocalServer::WorldAccessOption : QLocalServer::NoOptions); if (!m_server->listen(getServerName(settings->isService()))) { - logWarning() << "Server listen error:" << m_server->errorString(); + qCWarning(LC) << "Server listen error:" << m_server->errorString(); return false; } @@ -87,7 +86,7 @@ bool ControlManager::postCommand() if (settings->controlCommand() == "prog") { command = Control::Prog; } else { - logWarning() << "Unknown control command:" << settings->controlCommand(); + qCWarning(LC) << "Unknown control command:" << settings->controlCommand(); return false; } @@ -96,7 +95,7 @@ bool ControlManager::postCommand() // Connect to server if (!w.connectToServer(getServerName())) { - logWarning() << "Connect to server error:" << socket.errorString(); + qCWarning(LC) << "Connect to server error:" << socket.errorString(); return false; } @@ -113,7 +112,7 @@ void ControlManager::onNewConnection() while (QLocalSocket *socket = m_server->nextPendingConnection()) { constexpr int maxClientsCount = 9; if (m_clients.size() > maxClientsCount) { - logDebug() << "Client dropped"; + qCDebug(LC) << "Client dropped"; delete socket; continue; } @@ -126,7 +125,7 @@ void ControlManager::onNewConnection() m_clients.append(w); - logDebug() << "Client connected:" << w->id(); + qCDebug(LC) << "Client connected:" << w->id(); } } @@ -139,7 +138,7 @@ void ControlManager::onDisconnected() w->deleteLater(); m_clients.removeOne(w); - logDebug() << "Client disconnected:" << w->id(); + qCDebug(LC) << "Client disconnected:" << w->id(); } bool ControlManager::processRequest(Control::Command command, const QVariantList &args) @@ -150,7 +149,7 @@ bool ControlManager::processRequest(Control::Command command, const QVariantList QString errorMessage; if (!processCommand({ w, command, args, errorMessage })) { - logWarning() << "Bad command" << errorMessage << ':' << command << args; + qCWarning(LC) << "Bad command" << errorMessage << ':' << command << args; return false; } return true; diff --git a/src/ui/control/controlworker.cpp b/src/ui/control/controlworker.cpp index 0221cc8b..7b68cbfb 100644 --- a/src/ui/control/controlworker.cpp +++ b/src/ui/control/controlworker.cpp @@ -6,6 +6,8 @@ namespace { +const QLoggingCategory LC("control"); + constexpr int commandMaxArgs = 16; constexpr int commandArgMaxSize = 4 * 1024; constexpr quint32 dataMaxSize = 1 * 1024 * 1024; @@ -96,7 +98,7 @@ void ControlWorker::setupForAsync() connect(socket(), &QLocalSocket::disconnected, this, &ControlWorker::disconnected); connect(socket(), &QLocalSocket::errorOccurred, this, [&](QLocalSocket::LocalSocketError socketError) { - qWarning() << "Client error:" << id() << socketError << errorString(); + qCWarning(LC) << "Client error:" << id() << socketError << errorString(); close(); }); connect(socket(), &QLocalSocket::readyRead, this, &ControlWorker::processRequest); @@ -137,9 +139,9 @@ bool ControlWorker::sendCommandData(const QByteArray &commandData) const int bytesSent = socket()->write(commandData); if (bytesSent != commandData.size()) { if (bytesSent < 0) { - qWarning() << "Send error:" << id() << errorString(); + qCWarning(LC) << "Send error:" << id() << errorString(); } else { - qWarning() << "Sent partial:" << id() << bytesSent << commandData.size(); + qCWarning(LC) << "Sent partial:" << id() << bytesSent << commandData.size(); } return false; } @@ -153,7 +155,7 @@ bool ControlWorker::sendCommand(Control::Command command, const QVariantList &ar { const QByteArray buffer = buildCommandData(command, args); if (buffer.isEmpty()) { - qWarning() << "Bad RPC command to send:" << command << args; + qCWarning(LC) << "Bad RPC command to send:" << command << args; return false; } @@ -215,7 +217,7 @@ bool ControlWorker::readRequest() clearRequest(); - // qDebug() << "requestReady>" << id() << command << args; + // qCDebug(LC) << "requestReady>" << id() << command << args; emit requestReady(command, args); @@ -226,16 +228,16 @@ bool ControlWorker::readRequestHeader() { const int headerSize = socket()->read((char *) &m_requestHeader, sizeof(RequestHeader)); if (headerSize != sizeof(RequestHeader)) { - qWarning() << "Bad request header:" - << "size=" << headerSize; + qCWarning(LC) << "Bad request header:" + << "size=" << headerSize; return false; } if (m_requestHeader.command() == Control::CommandNone || m_requestHeader.dataSize() > dataMaxSize) { - qWarning() << "Bad request:" - << "command=" << m_requestHeader.command() - << "size=" << m_requestHeader.dataSize(); + qCWarning(LC) << "Bad request:" + << "command=" << m_requestHeader.command() + << "size=" << m_requestHeader.dataSize(); return false; } diff --git a/src/ui/manager/logger.cpp b/src/ui/manager/logger.cpp index abbca1aa..24dd06e6 100644 --- a/src/ui/manager/logger.cpp +++ b/src/ui/manager/logger.cpp @@ -1,5 +1,7 @@ #include "logger.h" +#include + #include #include @@ -8,6 +10,8 @@ namespace { +const QLoggingCategory LC("logger"); + constexpr int LOGGER_FILE_MAX_SIZE = 1024 * 1024; constexpr int LOGGER_KEEP_FILES = 7; @@ -124,7 +128,7 @@ bool Logger::openLogFile() if (tryOpenLogFile(m_dir, fileName)) return true; - qDebug() << "Cannot open log file:" << m_file.fileName() << m_file.errorString(); + qCDebug(LC) << "Cannot open log file:" << m_file.fileName() << m_file.errorString(); return false; } diff --git a/src/ui/manager/logger.h b/src/ui/manager/logger.h index 143523f4..157e6638 100644 --- a/src/ui/manager/logger.h +++ b/src/ui/manager/logger.h @@ -3,7 +3,6 @@ #include #include -#include class Logger : public QObject { diff --git a/src/ui/rpc/rpcmanager.cpp b/src/ui/rpc/rpcmanager.cpp index 6b00ae1d..ae696239 100644 --- a/src/ui/rpc/rpcmanager.cpp +++ b/src/ui/rpc/rpcmanager.cpp @@ -1,5 +1,7 @@ #include "rpcmanager.h" +#include + #include #include #include @@ -16,6 +18,8 @@ namespace { +const QLoggingCategory LC("appInfo"); + inline bool sendCommandDataToClients( const QByteArray &commandData, const QList &clients) { @@ -26,7 +30,7 @@ inline bool sendCommandDataToClients( continue; if (!w->sendCommandData(commandData)) { - qWarning() << "Send command error:" << w->id() << w->errorString(); + qCWarning(LC) << "Send command error:" << w->id() << w->errorString(); ok = false; } } @@ -419,12 +423,12 @@ void RpcManager::invokeOnClients(Control::Command cmd, const QVariantList &args) const QByteArray buffer = ControlWorker::buildCommandData(cmd, args); if (buffer.isEmpty()) { - qWarning() << "Bad RPC command to invoke:" << cmd << args; + qCWarning(LC) << "Bad RPC command to invoke:" << cmd << args; return; } if (!sendCommandDataToClients(buffer, clients)) { - qWarning() << "Invoke on clients error:" << cmd << args; + qCWarning(LC) << "Invoke on clients error:" << cmd << args; } } diff --git a/src/ui/stat/statmanager.cpp b/src/ui/stat/statmanager.cpp index 59d461b8..86250a06 100644 --- a/src/ui/stat/statmanager.cpp +++ b/src/ui/stat/statmanager.cpp @@ -18,20 +18,16 @@ #include "quotamanager.h" #include "statsql.h" -Q_DECLARE_LOGGING_CATEGORY(CLOG_STAT_MANAGER) -Q_LOGGING_CATEGORY(CLOG_STAT_MANAGER, "stat") - -#define logWarning() qCWarning(CLOG_STAT_MANAGER, ) -#define logCritical() qCCritical(CLOG_STAT_MANAGER, ) - -#define DATABASE_USER_VERSION 5 - -#define ACTIVE_PERIOD_CHECK_SECS (60 * OS_TICKS_PER_SECOND) - -#define INVALID_APP_ID qint64(-1) - namespace { +const QLoggingCategory LC("stat"); + +constexpr int DATABASE_USER_VERSION = 5; + +constexpr qint32 ACTIVE_PERIOD_CHECK_SECS = 60 * OS_TICKS_PER_SECOND; + +constexpr qint64 INVALID_APP_ID = Q_INT64_C(-1); + bool migrateFunc(SqliteDb *db, int version, bool isNewDb, void *ctx) { Q_UNUSED(ctx); @@ -93,7 +89,8 @@ void StatManager::emitConnChanged() void StatManager::setUp() { if (!sqliteDb()->open()) { - logCritical() << "File open error:" << sqliteDb()->filePath() << sqliteDb()->errorMessage(); + qCCritical(LC) << "File open error:" << sqliteDb()->filePath() + << sqliteDb()->errorMessage(); return; } @@ -103,7 +100,7 @@ void StatManager::setUp() .migrateFunc = &migrateFunc }; if (!sqliteDb()->migrate(opt)) { - logCritical() << "Migration error" << sqliteDb()->filePath(); + qCCritical(LC) << "Migration error" << sqliteDb()->filePath(); return; } @@ -595,8 +592,8 @@ void StatManager::logTrafBytes(const QStmtList &insertStmtList, const QStmtList const QString appPath = m_appPidPathMap.value(pid); if (Q_UNLIKELY(appPath.isEmpty())) { - logCritical() << "UI & Driver's states mismatch! Expected processes:" - << m_appPidPathMap.keys() << "Got:" << pid << inactive; + qCCritical(LC) << "UI & Driver's states mismatch! Expected processes:" + << m_appPidPathMap.keys() << "Got:" << pid << inactive; return; } @@ -632,9 +629,9 @@ void StatManager::updateTrafficList(const QStmtList &insertStmtList, if (!updateTraffic(stmtUpdate, inBytes, outBytes, appId)) { SqliteStmt *stmtInsert = insertStmtList.at(i); if (!updateTraffic(stmtInsert, inBytes, outBytes, appId)) { - logCritical() << "Update traffic error:" << sqliteDb()->errorMessage() - << "inBytes:" << inBytes << "outBytes:" << outBytes - << "appId:" << appId << "index:" << i; + qCCritical(LC) << "Update traffic error:" << sqliteDb()->errorMessage() + << "inBytes:" << inBytes << "outBytes:" << outBytes + << "appId:" << appId << "index:" << i; } } ++i;