diff --git a/src/engine/OCREngine.cpp b/src/engine/OCREngine.cpp index 07c8dd8b..8cf17369 100644 --- a/src/engine/OCREngine.cpp +++ b/src/engine/OCREngine.cpp @@ -52,7 +52,7 @@ OCREngine::OCREngine() ocrDriver->setUseMaxThreadsCount(2); if (!isGpuEnable()) { - qWarning() << "GPU is not enabled"; + qCWarning(dmOcr) << "GPU is not enabled"; return; } diff --git a/src/main.cpp b/src/main.cpp index 6f1346fb..e1148a3e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,14 +20,19 @@ #include "engine/OcrResult.h" #include +#include "util/logger.h" DWIDGET_USE_NAMESPACE int main(int argc, char *argv[]) { + // 动态日志监听必须在任何日志打印之前创建(置空环境变量才能生效), + // 且实例需存活整个进程以持续监听 DConfig 变更; + // 必须在 Application 实例化之前,否则 QApplication 内部日志打印会抢先应用规则 + MLogger logger; if (argc < 2) { - qDebug() << "Cant open a null file"; + qCDebug(dmOcr) << "Cant open a null file"; return 0; } @@ -87,7 +92,7 @@ int main(int argc, char *argv[]) // 调用DBus接口,处理交给第一次调用的进程 // 本进程退退出 OcrInterface *pOcr = new OcrInterface("com.deepin.Ocr", "/com/deepin/Ocr", QDBusConnection::sessionBus(), &instance); - qDebug() << __FUNCTION__ << __LINE__; + qCDebug(dmOcr) << __FUNCTION__ << __LINE__; pOcr->openFile(QString(argv[1])); delete pOcr; return 0; diff --git a/src/mainwidget.cpp b/src/mainwidget.cpp index 8fe37016..483c900a 100644 --- a/src/mainwidget.cpp +++ b/src/mainwidget.cpp @@ -6,6 +6,7 @@ #include "view/imageview.h" #include "loadingwidget.h" #include "frame.h" +#include "util/log.h" #include #include @@ -57,7 +58,7 @@ MainWidget::~MainWidget() if (m_loadImagethread) { m_loadImagethread->requestInterruption(); // 请求线程中断 if (!m_loadImagethread->wait(3000)) { // 等待线程结束,超时时间为3秒 - qWarning() << "OCR thread TIMEOUT"; + qCWarning(dmOcr) << "OCR thread TIMEOUT"; m_loadImagethread->terminate(); // 如果线程没有结束,强制终止线程 m_loadImagethread->deleteLater(); // 删除线程对象 m_loadImagethread = nullptr; @@ -404,10 +405,10 @@ void MainWidget::loadingUi() if (m_loadingWidget && m_loadingTip && m_frameStack) { int x = this->width() - m_frameStack->width() / 2; int y = this->height() / 2 - 50; - qDebug() << m_loadingWidget->width(); + qCDebug(dmOcr) << m_loadingWidget->width(); m_loadingWidget->setFixedSize(QSize(24, 24)); m_loadingWidget->move(x - m_loadingWidget->width() / 2, y); - qDebug() << m_loadingTip->width(); + qCDebug(dmOcr) << m_loadingTip->width(); // m_loadingTip->setAlignment(Qt::AlignCenter); QFontMetrics fm(DFontSizeManager::instance()->get(DFontSizeManager::T8)); int mPoint = fm.boundingRect(tr("Recognizing")).width(); @@ -721,7 +722,7 @@ void MainWidget::slotExport() } QString file_path = QFileDialog::getSaveFileName(this, "save as", download + "/" + fileName, "*.txt"); - qDebug() << file_path; + qCDebug(dmOcr) << file_path; //这里不应该增加一个.txt,会导致有两个后缀 QString path = file_path; diff --git a/src/resulttextview.cpp b/src/resulttextview.cpp index a8760e97..adf8184c 100644 --- a/src/resulttextview.cpp +++ b/src/resulttextview.cpp @@ -189,7 +189,7 @@ void ResultTextView::mouseReleaseEvent(QMouseEvent *e) { //修复一指滑动导致了ocr无法选择任何文本 if (e->type() == QEvent::MouseButtonRelease && e->source() == Qt::MouseEventSynthesizedByQt) { - qDebug() << "action is over" << m_gestureAction; + qCDebug(dmOcr) << "action is over" << m_gestureAction; m_gestureAction = GA_null; } diff --git a/src/service/ocrinterface.h b/src/service/ocrinterface.h index 01d41198..a4e59dfd 100644 --- a/src/service/ocrinterface.h +++ b/src/service/ocrinterface.h @@ -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 @@ -15,6 +15,7 @@ #include #include #include +#include "util/log.h" #include class OcrInterface: public QDBusAbstractInterface @@ -55,7 +56,7 @@ public Q_SLOTS: // METHODS */ inline QDBusPendingReply<> openImage(const QImage &image) { - qDebug() << __FUNCTION__; + qCDebug(dmOcr) << __FUNCTION__; QByteArray data; QBuffer buf(&data); if (image.save(&buf, "PNG")) { @@ -74,7 +75,7 @@ public Q_SLOTS: // METHODS */ inline QDBusPendingReply<> openImageAndName(const QImage &image, const QString &imageName) { - qDebug() << __FUNCTION__; + qCDebug(dmOcr) << __FUNCTION__; QByteArray data; QBuffer buf(&data); if (image.save(&buf, "PNG")) { diff --git a/src/util/log.cpp b/src/util/log.cpp index 761439bd..e6ebdebd 100644 --- a/src/util/log.cpp +++ b/src/util/log.cpp @@ -1,3 +1,3 @@ #include "log.h" -Q_LOGGING_CATEGORY(dmOcr, "deepin.ocr") \ No newline at end of file +Q_LOGGING_CATEGORY(dmOcr, "org.deepin.ocr") \ No newline at end of file diff --git a/src/util/logger.cpp b/src/util/logger.cpp new file mode 100644 index 00000000..3543d18c --- /dev/null +++ b/src/util/logger.cpp @@ -0,0 +1,84 @@ +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "logger.h" + +#include +#include + +#include + +#include "log.h" + +DCORE_USE_NAMESPACE + +MLogger::MLogger(QObject *parent) + : QObject(parent), m_rules("") +#ifdef DTKCORE_CLASS_DConfig + , m_config(nullptr) +#endif +{ + QByteArray logRules = qgetenv("QT_LOGGING_RULES"); + // 在 Qt 日志系统初始化前置空 QT_LOGGING_RULES,避免环境变量规则 + // 与 DConfig 规则叠加;此时一定不能有任何日志打印,否则置空不会生效 + qputenv("QT_LOGGING_RULES", ""); + + // set env + m_rules = logRules; + +#ifdef DTKCORE_CLASS_DConfig + // set dconfig + m_config = DConfig::create("org.deepin.ocr", "org.deepin.ocr"); + if (!m_config) { + // meta 缺失时兑底:仅沿用环境变量规则,避免空指针崩溃 + setRules(m_rules); + return; + } + 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_DConfig + if (m_config) { + m_config->deleteLater(); + } +#endif +} + +void MLogger::setRules(const QString &rules) +{ + auto tmpRules = rules; + // Qt 限制:setFilterRules 的规则分隔符必须为 \n,不能是 ; + m_rules = tmpRules.replace(";", "\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/util/logger.h b/src/util/logger.h new file mode 100644 index 00000000..c5950c9d --- /dev/null +++ b/src/util/logger.h @@ -0,0 +1,40 @@ +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. +// +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef DEEPIN_OCR_LOGGER_H +#define DEEPIN_OCR_LOGGER_H + +#include +#include + +#ifdef DTKCORE_CLASS_DConfig +DCORE_BEGIN_NAMESPACE +class DConfig; +DCORE_END_NAMESPACE +#endif + +// 动态日志规则监听器:从 DConfig(org.deepin.ocr) 读取 log_rules 并应用, +// 监听运行中的配置变更,实现调试模式动态调整日志级别(参考 deepin-reader 的 MLogger)。 +// 必须在 Application 实例化之前创建,且实例需存活整个进程,否则 valueChanged 监听失效。 +class MLogger : public QObject +{ + Q_OBJECT +public: + explicit MLogger(QObject *parent = nullptr); + ~MLogger() override; + + 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_DConfig + Dtk::Core::DConfig *m_config; +#endif +}; + +#endif // LOGGER_H diff --git a/src/utils/dconfigmanager.cpp b/src/utils/dconfigmanager.cpp index d5a913fc..dd48e2b0 100644 --- a/src/utils/dconfigmanager.cpp +++ b/src/utils/dconfigmanager.cpp @@ -1,9 +1,10 @@ -// SPDX-FileCopyrightText: 2022 - 2023 UnionTech Software Technology Co., Ltd. +// SPDX-FileCopyrightText: 2022-2026 UnionTech Software Technology Co., Ltd. // // SPDX-License-Identifier: GPL-3.0-or-later #include "dconfigmanager.h" #include "private/dconfigmanager_p.h" +#include "util/log.h" #include @@ -108,7 +109,7 @@ QVariant DConfigManager::value(const QString &config, const QString &key, const if (d->configs.contains(config)) { // 如果DConfig没刷新,存在有config但没key的情况 if (!this->contains(config, key)) { - qWarning() << "DConfig has no key:" << key << "in config:" << config << "REBOOT may fix it"; + qCWarning(dmOcr) << "DConfig has no key:" << key << "in config:" << config << "REBOOT may fix it"; return fallback; } @@ -116,7 +117,7 @@ QVariant DConfigManager::value(const QString &config, const QString &key, const return d->configs.value(config)->value(key, fallback); } else - qWarning() << "Config:" << config << "is not registered!!!"; + qCWarning(dmOcr) << "Config:" << config << "is not registered!!!"; return fallback; #else return fallback; @@ -131,14 +132,14 @@ void DConfigManager::setValue(const QString &config, const QString &key, const Q if (d->configs.contains(config)) { // 如果DConfig没刷新,存在有config但没key的情况 if (!this->contains(config, key)) { - qWarning() << "DConfig has no key:" << key << "in config:" << config << "REBOOT may fix it"; + qCWarning(dmOcr) << "DConfig has no key:" << key << "in config:" << config << "REBOOT may fix it"; return; } - qInfo() << "dconfig:" << config << "key:" << key << "value:" << value; + qCInfo(dmOcr) << "dconfig:" << config << "key:" << key << "value:" << value; d->configs.value(config)->setValue(key, value); } else { - qWarning() << "Config:" << config << "is not registered!!!"; + qCWarning(dmOcr) << "Config:" << config << "is not registered!!!"; } #endif } diff --git a/src/view/imageview.cpp b/src/view/imageview.cpp index 11df99e7..d0afd2a9 100644 --- a/src/view/imageview.cpp +++ b/src/view/imageview.cpp @@ -380,20 +380,20 @@ void ImageView::wheelEvent(QWheelEvent *event) void ImageView::handleGestureEvent(QGestureEvent *gesture) { - qDebug() << "------" << __FUNCTION__ << ""; + qCDebug(dmOcr) << "------" << __FUNCTION__ << ""; if (QGesture *pinch = gesture->gesture(Qt::PinchGesture)) pinchTriggered(static_cast(pinch)); } void ImageView::pinchTriggered(QPinchGesture *gesture) { - qDebug() << "------" << __FUNCTION__ << ""; + qCDebug(dmOcr) << "------" << __FUNCTION__ << ""; QPinchGesture::ChangeFlags changeFlags = gesture->changeFlags(); //缩放手势 if (changeFlags & QPinchGesture::ScaleFactorChanged) { QPoint pos = mapFromGlobal(gesture->centerPoint().toPoint()); if (abs(gesture->scaleFactor() - 1) > 0.006) { - qDebug() << "scaleFactor" << gesture->scaleFactor(); + qCDebug(dmOcr) << "scaleFactor" << gesture->scaleFactor(); scaleAtPoint(pos, gesture->scaleFactor()); } } @@ -420,7 +420,7 @@ void ImageView::setScaleValue(qreal v) { //由于矩阵被旋转,通过矩阵获取缩放因子,计算缩放比例错误,因此记录过程中的缩放因子来判断缩放比例 m_scal *= v; - qDebug() << m_scal; + qCDebug(dmOcr) << m_scal; scale(v, v); //const qreal irs = imageRelativeScale() * devicePixelRatioF(); // Rollback