From a345e105ce29a5c76a56a2554903e42d4f573d5d Mon Sep 17 00:00:00 2001 From: zhanghongyuan Date: Thu, 22 Oct 2026 22:49:00 +0800 Subject: [PATCH] feat: add debug mode support with dynamic log level control Categorized logging (org.deepin.picker) + MLogger self-managed DConfig log_rules dynamic watching + dconfig meta default rules, for deepin-debug-config adaptation. Log: Add debug mode adaptation for deepin-picker Influence: Change to categorized logging, only warning level is output by default --- assets/configs/org.deepin.picker.json | 19 +++++++ deepin-picker.pro | 11 +++- src/animation.cpp | 14 ++--- src/colormenu.cpp | 12 ++--- src/cpickermanager.cpp | 42 +++++++-------- src/ddlog.h | 15 ++++++ src/logger.cpp | 77 +++++++++++++++++++++++++++ src/logger.h | 37 +++++++++++++ src/main.cpp | 33 +++++++----- 9 files changed, 210 insertions(+), 50 deletions(-) create mode 100644 assets/configs/org.deepin.picker.json create mode 100644 src/ddlog.h create mode 100644 src/logger.cpp create mode 100644 src/logger.h diff --git a/assets/configs/org.deepin.picker.json b/assets/configs/org.deepin.picker.json new file mode 100644 index 0000000..2a15107 --- /dev/null +++ b/assets/configs/org.deepin.picker.json @@ -0,0 +1,19 @@ +{ + "magic": "dsg.config.meta", + "version": "1.0", + "contents": { + "log_rules": { + "value": "org.deepin.picker*.debug=false;org.deepin.picker*.info=false;org.deepin.picker*.warning=true", + "serial": 0, + "flags": [ + "global" + ], + "name": "log rules", + "name[zh_CN]": "日志规则", + "description": "log rules", + "description[zh_CN]": "日志规则", + "permissions": "readwrite", + "visibility": "public" + } + } +} diff --git a/deepin-picker.pro b/deepin-picker.pro index 0c42c42..4b2bbdd 100644 --- a/deepin-picker.pro +++ b/deepin-picker.pro @@ -41,7 +41,9 @@ HEADERS += \ src/animation.h \ src/dbusnotify.h \ src/settings.h \ - src/utils.h + src/utils.h \ + src/logger.h \ + src/ddlog.h SOURCES += \ src/cpickermanager.cpp \ src/desktopinfo.cpp \ @@ -51,6 +53,7 @@ SOURCES += \ src/dbusnotify.cpp \ src/settings.cpp \ src/utils.cpp \ + src/logger.cpp \ src/main.cpp # 移除不必要的注释 @@ -82,7 +85,11 @@ manual.files = manual/* dbus_service.files = $$PWD/com.deepin.Picker.service dbus_service.path = $$PREFIX/share/dbus-1/services -INSTALLS += target icon desktop manual dbus_service +# DConfig 元数据(调试模式动态日志级别读取的默认规则) +dconfig_meta.files = $$PWD/assets/configs/org.deepin.picker.json +dconfig_meta.path = $$PREFIX/share/dsg/configs/org.deepin.picker + +INSTALLS += target icon desktop manual dbus_service dconfig_meta isEmpty(TRANSLATIONS) { include(translations.pri) diff --git a/src/animation.cpp b/src/animation.cpp index 85ce56d..faf7a77 100644 --- a/src/animation.cpp +++ b/src/animation.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2011-2022 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2011-2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -7,12 +7,12 @@ #include #include #include -#include +#include "ddlog.h" #include Animation::Animation(int x, int y, QPixmap pixmap, QColor color, QWidget *parent) : QWidget(parent) { - qDebug() << "Initializing animation at position:" << x << y << "with color:" << color.name(); + qCDebug(appLog) << "Initializing animation at position:" << x << y << "with color:" << color.name(); // Init window flags to make window transparent and get correctly behavior. setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint); @@ -42,12 +42,12 @@ Animation::Animation(int x, int y, QPixmap pixmap, QColor color, QWidget *parent renderTimer = new QTimer(); connect(renderTimer, &QTimer::timeout, this, &Animation::renderAnimation); renderTimer->start(animationDuration); - qDebug() << "Animation timer started with duration:" << animationDuration << "ms"; + qCDebug(appLog) << "Animation timer started with duration:" << animationDuration << "ms"; } Animation::~Animation() { - qDebug() << "Destroying animation instance"; + qCDebug(appLog) << "Destroying animation instance"; delete renderTimer; } @@ -79,11 +79,11 @@ void Animation::renderAnimation() { if (renderTicker < animationFrames) { renderTicker++; - qDebug() << "Animation frame:" << renderTicker << "/" << animationFrames; + qCDebug(appLog) << "Animation frame:" << renderTicker << "/" << animationFrames; repaint(); } else { renderTimer->stop(); - qInfo() << "Animation completed, hiding window"; + qCInfo(appLog) << "Animation completed, hiding window"; hide(); // hide window when animation finish emit finish(); diff --git a/src/colormenu.cpp b/src/colormenu.cpp index 776ab95..34a2d40 100644 --- a/src/colormenu.cpp +++ b/src/colormenu.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2011-2022 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2011-2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -12,11 +12,11 @@ #include #include #include -#include +#include "ddlog.h" ColorMenu::ColorMenu(int x, int y, int size, QColor color, QWidget *parent) : QWidget(parent) { - qDebug() << "Initializing color menu at position:" << x << y << "with size:" << size << "and color:" << color.name(); + qCDebug(appLog) << "Initializing color menu at position:" << x << y << "with size:" << size << "and color:" << color.name(); // Init window flags. setWindowFlags(Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint); @@ -49,7 +49,7 @@ ColorMenu::ColorMenu(int x, int y, int size, QColor color, QWidget *parent) : QW connect(colorMenu, &QMenu::aboutToHide, this, [&]() { QTimer::singleShot(200, this, [&]() { if (!clickMenuItem) { - qDebug() << "Menu closed without selection, exiting"; + qCDebug(appLog) << "Menu closed without selection, exiting"; exit(); } }); @@ -94,7 +94,7 @@ ColorMenu::ColorMenu(int x, int y, int size, QColor color, QWidget *parent) : QW // Set menu action check status with color type. Settings *settings = new Settings(); QString colorType = settings->getOption("color_type", "HEX").toString(); - qDebug() << "Loading saved color type preference:" << colorType; + qCDebug(appLog) << "Loading saved color type preference:" << colorType; if (colorType == "HEX") { hexAction->setChecked(true); @@ -119,7 +119,7 @@ ColorMenu::ColorMenu(int x, int y, int size, QColor color, QWidget *parent) : QW ColorMenu::~ColorMenu() { - qDebug() << "Destroying color menu instance"; + qCDebug(appLog) << "Destroying color menu instance"; delete hexAction; delete rgbAction; delete rgbFloatAction; diff --git a/src/cpickermanager.cpp b/src/cpickermanager.cpp index c931478..11b27ea 100644 --- a/src/cpickermanager.cpp +++ b/src/cpickermanager.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2022-2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later @@ -9,7 +9,7 @@ #include #include #include -#include +#include "ddlog.h" #include #include #include @@ -25,7 +25,7 @@ DGUI_USE_NAMESPACE CScreenshotWidget::CScreenshotWidget(CPickerManager *parent): QWidget(nullptr), _parentManager(parent) { - qDebug() << "Initializing screenshot widget"; + qCDebug(appLog) << "Initializing screenshot widget"; //为顶层窗口 this->setWindowFlags(this->windowFlags() | Qt::Window | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint); @@ -59,7 +59,7 @@ void CScreenshotWidget::keyPressEvent(QKeyEvent *event) { //wayland下 DRegionMonitor::keyPress会失效,这里会响应 if (event->matches(QKeySequence::Cancel)) { - qDebug() << "Escape key pressed, exiting application"; + qCDebug(appLog) << "Escape key pressed, exiting application"; QApplication::quit(); } QWidget::keyPressEvent(event); @@ -83,13 +83,13 @@ void CScreenshotWidget::wheelEvent(QWheelEvent *event) CPickerManager::CPickerManager(): QObject(nullptr) { - qDebug() << "Initializing color picker manager"; + qCDebug(appLog) << "Initializing color picker manager"; const int windowHeight = 236; const int windowWidth = 236; qreal radio = qApp->devicePixelRatio(); _shadowPixmap = QPixmap(Utils::getQrcPath("shadow.png")); if (isWaylandPlatform()) { - qDebug() << "Running on Wayland platform"; + qCDebug(appLog) << "Running on Wayland platform"; _shadowPixmap = _shadowPixmap.scaled(windowWidth, windowHeight); } else { _shadowPixmap = _shadowPixmap.scaled(windowWidth / radio, windowHeight / radio, Qt::KeepAspectRatio, Qt::SmoothTransformation); @@ -103,7 +103,7 @@ CPickerManager::CPickerManager(): QObject(nullptr) QObject::connect(eventMonitor, &DRegionMonitor::keyPress, this, [ = ](const QString & name) { if (name == "Escape") { - qDebug() << "Escape key detected from region monitor, exiting application"; + qCDebug(appLog) << "Escape key detected from region monitor, exiting application"; QApplication::quit(); } }); @@ -120,7 +120,7 @@ CPickerManager::CPickerManager(): QObject(nullptr) eventMonitor->registerRegion(); if (!eventMonitor->registered()) { - qWarning() << "Failed to register region monitor, exiting application"; + qCWarning(appLog) << "Failed to register region monitor, exiting application"; QApplication::quit(); } ensureDeskTopPixmap(); @@ -136,12 +136,12 @@ CPickerManager::CPickerManager(): QObject(nullptr) CPickerManager::~CPickerManager() { - qDebug() << "Destroying color picker manager"; + qCDebug(appLog) << "Destroying color picker manager"; } void CPickerManager::setLanchFlag(CPickerManager::ELanchType tp, const QString &appName) { - qDebug() << "Setting launch flag:" << tp << "for application:" << appName; + qCDebug(appLog) << "Setting launch flag:" << tp << "for application:" << appName; _isLaunchByDBus = tp; if (_isLaunchByDBus == ELanchedByOtherApp) _appid = appName; @@ -159,7 +159,7 @@ void CPickerManager::setLanchFlag(CPickerManager::ELanchType tp, const QString & void CPickerManager::StartPick(const QString &id) { - qDebug() << "Starting color pick with ID:" << id; + qCDebug(appLog) << "Starting color pick with ID:" << id; _appid = id; } @@ -181,12 +181,12 @@ void CPickerManager::onMousePress(const QPoint &p, const int flag) //wayland触摸屏下的点击是DRegionMonitor::Button_Middle,鼠标左键点击DRegionMonitor::Button_Left。无法区分是鼠标左键还是触摸屏,故支持中键点击 //最佳方案是TDK将触摸屏点击修改成左键点击。。。。。。。 if (button != DRegionMonitor::Button_Left && button != DRegionMonitor::Button_Middle) { - qDebug() << "Ignoring non-left/middle button press on Wayland"; + qCDebug(appLog) << "Ignoring non-left/middle button press on Wayland"; return; } } else { if (button != DRegionMonitor::Button_Left) { - qDebug() << "Ignoring non-left button press"; + qCDebug(appLog) << "Ignoring non-left button press"; return; } } @@ -205,14 +205,14 @@ void CPickerManager::onMousePress(const QPoint &p, const int flag) // Rest color type to hex if config file not exist. Settings settings; QString colorType = settings.getOption("color_type", "HEX").toString(); - qDebug() << "Color picked:" << _curColor.name() << "in format:" << colorType; + qCDebug(appLog) << "Color picked:" << _curColor.name() << "in format:" << colorType; // Emit copyColor signal to copy color to system clipboard. copyColor(_curColor, colorType); // Send colorPicked signal when call by DBus and no empty appid. if (_appid != "") { - qDebug() << "Sending color picked signal to application:" << _appid; + qCDebug(appLog) << "Sending color picked signal to application:" << _appid; colorPicked(_appid, Utils::colorToHex(_curColor)); } } @@ -226,7 +226,7 @@ void CPickerManager::handleMouseMove() void CPickerManager::initShotScreenWidgets() { - qDebug() << "Initializing screen shot widgets"; + qCDebug(appLog) << "Initializing screen shot widgets"; ensureDeskTopPixmap(); auto screens = QApplication::screens(); //去出复制屏 @@ -234,7 +234,7 @@ void CPickerManager::initShotScreenWidgets() while (i < screens.size()) { for (int j = screens.size() - 1; j > i; j--) { if (screens.at(i)->geometry().topLeft() == screens.at(j)->geometry().topLeft()) { - qDebug() << "Removing duplicate screen at index:" << j; + qCDebug(appLog) << "Removing duplicate screen at index:" << j; screens.removeAt(j); } } @@ -259,7 +259,7 @@ void CPickerManager::initShotScreenWidgets() // 解决 wayland 下调出取色器后点击 Esc 无法退出取色器 if (isWaylandPlatform()) { - qDebug() << "Setting up Wayland-specific window activation"; + qCDebug(appLog) << "Setting up Wayland-specific window activation"; // 延迟 200ms 后取得当前背景窗口的焦点,用以捕获 Esc 按键退出 QTimer::singleShot(200, this, [ = ]() { auto currentWidget = _widgets.value(qApp->screenAt(QCursor::pos())); @@ -348,7 +348,7 @@ void CPickerManager::updateCursor(const QPixmap &pixMap, const QPoint &posInPixm void CPickerManager::ensureDeskTopPixmap() { if (_desktopPixmapDirty) { - qDebug() << "Updating desktop pixmap"; + qCDebug(appLog) << "Updating desktop pixmap"; _desktopPixmap = getDesktopPixmap(); _desktopPixmapDirty = false; } @@ -384,7 +384,7 @@ QPixmap CPickerManager::getDesktopPixmap() QPixmap result; bool iswayLand = isWaylandPlatform(); if (iswayLand) { - qDebug() << "Getting desktop pixmap via KWin DBus interface"; + qCDebug(appLog) << "Getting desktop pixmap via KWin DBus interface"; QPixmap res; QDBusInterface kwinInterface(QStringLiteral("org.kde.KWin"), QStringLiteral("/Screenshot"), @@ -397,7 +397,7 @@ QPixmap CPickerManager::getDesktopPixmap() } result = res; } else { - qDebug() << "Getting desktop pixmap via screen grab"; + qCDebug(appLog) << "Getting desktop pixmap via screen grab"; QPixmap pixs(getDeskTopRect().size()); pixs.fill(Qt::transparent); QPainter painter(&pixs); diff --git a/src/ddlog.h b/src/ddlog.h new file mode 100644 index 0000000..d96b0d6 --- /dev/null +++ b/src/ddlog.h @@ -0,0 +1,15 @@ +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef DDLOG_H +#define DDLOG_H + +#include +#include + +// 全项目统一的日志分类,便于 DConfig 调试模式(deepin-debug-config)按应用统一过滤。 +// deepin-picker 为单进程应用,仅一个分类;如需子分类,按 org.deepin.picker.<子模块> 命名。 +inline Q_LOGGING_CATEGORY(appLog, "org.deepin.picker") + +#endif // DDLOG_H diff --git a/src/logger.cpp b/src/logger.cpp new file mode 100644 index 0000000..4b7b636 --- /dev/null +++ b/src/logger.cpp @@ -0,0 +1,77 @@ +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "logger.h" + +#include +#include + +#ifdef DTKCORE_CLASS_DConfigFile +#include +#endif + +DCORE_USE_NAMESPACE + +MLogger::MLogger(QObject *parent) + : QObject(parent), m_rules("") +#ifdef DTKCORE_CLASS_DConfigFile + , m_config(nullptr) +#endif +{ + QByteArray logRules = qgetenv("QT_LOGGING_RULES"); + // qunsetenv 之前一定不要有任何日志打印,否则取消环境变量设置不会生效 + qunsetenv("QT_LOGGING_RULES"); + + // set env + m_rules = logRules; + +#ifdef DTKCORE_CLASS_DConfigFile + // set dconfig:读取本应用 DConfig 的 log_rules 默认规则(默认仅 warning 级开启) + m_config = DConfig::create("org.deepin.picker", "org.deepin.picker"); + logRules = m_config->value("log_rules").toByteArray(); + appendRules(logRules); + setRules(m_rules); + + // watch dconfig:监听变更,支持运行中动态调整日志级别(调试模式逐级切换) + connect(m_config, &DConfig::valueChanged, this, [this](const QString &key) { + if (key == "log_rules") { + setRules(m_config->value(key).toByteArray()); + } + }); +#else + setRules(m_rules); +#endif +} + +MLogger::~MLogger() +{ +#ifdef DTKCORE_CLASS_DConfigFile + if (m_config) { + m_config->deleteLater(); + } +#endif +} + +void MLogger::setRules(const QString &rules) +{ + auto tmpRules = rules; + m_rules = tmpRules.replace(";", "\n"); // Qt 限制:setFilterRules 分隔符必须 \n 不能 ; + QLoggingCategory::setFilterRules(m_rules); +} + +void MLogger::appendRules(const QString &rules) +{ + QString tmpRules = rules; + tmpRules = tmpRules.replace(";", "\n"); + auto tmplist = tmpRules.split('\n'); + for (int i = 0; i < tmplist.count(); i++) + if (m_rules.contains(tmplist.at(i))) { + tmplist.removeAt(i); + i--; + } + if (tmplist.isEmpty()) + return; + m_rules.isEmpty() ? m_rules = tmplist.join("\n") + : m_rules += "\n" + tmplist.join("\n"); +} diff --git a/src/logger.h b/src/logger.h new file mode 100644 index 0000000..3465358 --- /dev/null +++ b/src/logger.h @@ -0,0 +1,37 @@ +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef LOGGER_H +#define LOGGER_H + +#include +#include + +#ifdef DTKCORE_CLASS_DConfigFile +DCORE_BEGIN_NAMESPACE +class DConfig; +DCORE_END_NAMESPACE +#endif + +class MLogger : public QObject +{ + Q_OBJECT +public: + explicit MLogger(QObject *parent = nullptr); + ~MLogger(); + + inline QString rules() const { return m_rules; } + void setRules(const QString &rules); + +private: + void appendRules(const QString &rules); + +private: + QString m_rules; +#ifdef DTKCORE_CLASS_DConfigFile + Dtk::Core::DConfig *m_config; +#endif +}; + +#endif // LOGGER_H diff --git a/src/main.cpp b/src/main.cpp index 658b0b9..df6a320 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2011-2022 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2011-2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later #include @@ -12,7 +12,8 @@ #include #include #include -#include +#include "ddlog.h" +#include "logger.h" #include #include #include "utils.h" @@ -24,10 +25,14 @@ DWIDGET_USE_NAMESPACE int main(int argc, char *argv[]) { - qDebug() << "Starting Deepin Picker application"; + // 日志处理必须放在最前(Application 实例化与任何日志打印之前), + // 否则 QT_LOGGING_RULES 环境变量清理与 DConfig 动态日志级别均不生效 + MLogger logger; + + qCDebug(appLog) << "Starting Deepin Picker application"; if (!QString(qgetenv("XDG_CURRENT_DESKTOP")).toLower().startsWith("deepin")) { - qDebug() << "Setting XDG_CURRENT_DESKTOP to Deepin"; + qCDebug(appLog) << "Setting XDG_CURRENT_DESKTOP to Deepin"; setenv("XDG_CURRENT_DESKTOP", "Deepin", 1); } @@ -45,7 +50,7 @@ int main(int argc, char *argv[]) // Init dtk application's attrubites. DApplication app(argc, argv); app.setAttribute(Qt::AA_UseHighDpiPixmaps); - qDebug() << "Initialized DApplication with high DPI pixmaps support"; + qCDebug(appLog) << "Initialized DApplication with high DPI pixmaps support"; // 判断窗口特效是否开启 // if (!DWindowManagerHelper::instance()->hasComposite()) { @@ -54,12 +59,12 @@ int main(int argc, char *argv[]) // } app.loadTranslator(); - qDebug() << "Loaded application translations"; + qCDebug(appLog) << "Loaded application translations"; app.setOrganizationName("deepin"); app.setApplicationName("deepin-picker"); app.setApplicationVersion("1.2"); - qDebug() << "Set application info - name: deepin-picker, version: 1.2"; + qCDebug(appLog) << "Set application info - name: deepin-picker, version: 1.2"; app.setProductIcon(QIcon(Utils::getQrcPath("logo_96.svg"))); app.setProductName(DApplication::translate("MainWindow", "Deepin Picker")); @@ -76,30 +81,30 @@ int main(int argc, char *argv[]) parser.process(app); bool isLaunchByDBus = parser.isSet(appidOption); - qDebug() << "Application launch mode:" << (isLaunchByDBus ? "DBus" : "Direct"); + qCDebug(appLog) << "Application launch mode:" << (isLaunchByDBus ? "DBus" : "Direct"); // Init modules. - qDebug() << "Initializing application modules"; + qCDebug(appLog) << "Initializing application modules"; Clipboard clipboard; QPointer picker = new CPickerManager; picker->setLanchFlag(isLaunchByDBus ? CPickerManager::ELanchedByOtherApp : CPickerManager::ELanchedBySelf); if (!isLaunchByDBus) { - qDebug() << "Starting color picker in direct mode"; + qCDebug(appLog) << "Starting color picker in direct mode"; picker->StartPick(""); } QObject::connect(picker.data(), &CPickerManager::copyColor, &clipboard, &Clipboard::copyToClipboard, Qt::QueuedConnection); if (isLaunchByDBus) { - qDebug() << "Registering DBus service"; + qCDebug(appLog) << "Registering DBus service"; QDBusConnection dbus = QDBusConnection::sessionBus(); if (dbus.registerService("com.deepin.Picker")) { - qDebug() << "Successfully registered DBus service: com.deepin.Picker"; + qCDebug(appLog) << "Successfully registered DBus service: com.deepin.Picker"; dbus.registerObject("/com/deepin/Picker", picker.data(), QDBusConnection::ExportScriptableSlots | QDBusConnection::ExportScriptableSignals); } else { - qWarning() << "Failed to register DBus service: com.deepin.Picker"; + qCWarning(appLog) << "Failed to register DBus service: com.deepin.Picker"; } } - qDebug() << "Entering application event loop"; + qCDebug(appLog) << "Entering application event loop"; return app.exec(); }