Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/engine/OCREngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ OCREngine::OCREngine()

ocrDriver->setUseMaxThreadsCount(2);
if (!isGpuEnable()) {
qWarning() << "GPU is not enabled";
qCWarning(dmOcr) << "GPU is not enabled";
return;
}

Expand Down
9 changes: 7 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@
#include "util/log.h"
#include "engine/OcrResult.h"

#include <QMetaType>

Check warning on line 22 in src/main.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QMetaType> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#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;
}

Expand Down Expand Up @@ -87,7 +92,7 @@
// 调用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;
Expand Down
9 changes: 5 additions & 4 deletions src/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
#include "view/imageview.h"
#include "loadingwidget.h"
#include "frame.h"
#include "util/log.h"

#include <QtCore/QVariant>

Check warning on line 11 in src/mainwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QtCore/QVariant> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QtWidgets/QApplication>

Check warning on line 12 in src/mainwidget.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QtWidgets/QApplication> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QHBoxLayout>
#include <QTextBlock>
Expand Down Expand Up @@ -57,7 +58,7 @@
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;
Expand Down Expand Up @@ -404,10 +405,10 @@
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();
Expand Down Expand Up @@ -721,7 +722,7 @@
}

QString file_path = QFileDialog::getSaveFileName(this, "save as", download + "/" + fileName, "*.txt");
qDebug() << file_path;
qCDebug(dmOcr) << file_path;

//这里不应该增加一个.txt,会导致有两个后缀
QString path = file_path;
Expand Down
2 changes: 1 addition & 1 deletion src/resulttextview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions src/service/ocrinterface.h
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -12,10 +12,11 @@
#include <QtCore/QString>
#include <QtCore/QStringList>
#include <QtCore/QVariant>
#include <QtDBus/QtDBus>

Check warning on line 15 in src/service/ocrinterface.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QtDBus/QtDBus> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QImage>

Check warning on line 16 in src/service/ocrinterface.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QImage> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QBuffer>

Check warning on line 17 in src/service/ocrinterface.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QBuffer> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include "util/log.h"

Check warning on line 18 in src/service/ocrinterface.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "util/log.h" not found.
#include <QDebug>

Check warning on line 19 in src/service/ocrinterface.h

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.

class OcrInterface: public QDBusAbstractInterface
{
Expand Down Expand Up @@ -55,7 +56,7 @@
*/
inline QDBusPendingReply<> openImage(const QImage &image)
{
qDebug() << __FUNCTION__;
qCDebug(dmOcr) << __FUNCTION__;
QByteArray data;
QBuffer buf(&data);
if (image.save(&buf, "PNG")) {
Expand All @@ -74,7 +75,7 @@
*/
inline QDBusPendingReply<> openImageAndName(const QImage &image, const QString &imageName)
{
qDebug() << __FUNCTION__;
qCDebug(dmOcr) << __FUNCTION__;
QByteArray data;
QBuffer buf(&data);
if (image.save(&buf, "PNG")) {
Expand Down
2 changes: 1 addition & 1 deletion src/util/log.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include "log.h"

Q_LOGGING_CATEGORY(dmOcr, "deepin.ocr")
Q_LOGGING_CATEGORY(dmOcr, "org.deepin.ocr")
84 changes: 84 additions & 0 deletions src/util/logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "logger.h"

#include <QLoggingCategory>

Check warning on line 7 in src/util/logger.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QLoggingCategory> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDebug>

Check warning on line 8 in src/util/logger.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDebug> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <DConfig>

#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();
Comment on lines +32 to +38

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 High · Application exits during startup

When DConfig::create returns null, MLogger immediately dereferences m_config to read log_rules, so construction terminates the process before the application starts.

Check m_config before reading log_rules and continue with the captured environment rules when creation fails.

Prompt for AI agents
In `src/util/logger.cpp` at lines 31-32:

**Application exits during startup**

When DConfig::create returns null, `MLogger` immediately dereferences `m_config` to read `log_rules`, so construction terminates the process before the application starts.

Check `m_config` before reading `log_rules` and continue with the captured environment rules when creation fails.

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());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Environment filters stop applying

When environment rules are set and log_rules changes at runtime, setRules() replaces the initially merged filter set with only the DConfig value, so environment-configured filters stop applying and logging behavior changes.

Rebuild the active rules from both the environment rules and the updated DConfig value before calling setRules().

Prompt for AI agents
In `src/util/logger.cpp` at line 39:

**Environment filters stop applying**

When environment rules are set and `log_rules` changes at runtime, `setRules()` replaces the initially merged filter set with only the DConfig value, so environment-configured filters stop applying and logging behavior changes.

Rebuild the active rules from both the environment rules and the updated DConfig value before calling `setRules()`.

}
});
#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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Framework debug logs remain enabled

When A Qt/framework category emits debug or info messages while the app’s logging rules are active, setFilterRules applies only the app-category rules from DConfig; Qt categories that do not match them retain Qt’s default filter, so their debug and info messages appear alongside app logs instead of remaining excluded.

Add explicit wildcard rules to disable debug/info for unmatched categories, then enable the requested levels for org.deepin.ocr*.

Prompt for AI agents
In `src/util/logger.cpp` at line 61:

**Framework debug logs remain enabled**

When A Qt/framework category emits debug or info messages while the app’s logging rules are active, `setFilterRules` applies only the app-category rules from DConfig; Qt categories that do not match them retain Qt’s default filter, so their debug and info messages appear alongside app logs instead of remaining excluded.

Add explicit wildcard rules to disable debug/info for unmatched categories, then enable the requested levels for `org.deepin.ocr*`.

}

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))) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Substring match suppresses DConfig rules

When an environment rule contains a DConfig rule as a substring but is not the same rule, m_rules.contains treats any substring match as a duplicate, so an environment rule such as xorg.deepin.ocr*.debug=false suppresses the distinct DConfig rule org.deepin.ocr*.debug=false. The DConfig policy is then not applied, and the application category follows other matching rules or Qt defaults instead.

Compare normalized individual rules for exact equality rather than using substring containment.

Prompt for AI agents
In `src/util/logger.cpp` at line 70:

**Substring match suppresses DConfig rules**

When an environment rule contains a DConfig rule as a substring but is not the same rule, `m_rules.contains` treats any substring match as a duplicate, so an environment rule such as `xorg.deepin.ocr*.debug=false` suppresses the distinct DConfig rule `org.deepin.ocr*.debug=false`. The DConfig policy is then not applied, and the application category follows other matching rules or Qt defaults instead.

Compare normalized individual rules for exact equality rather than using substring containment.

tmplist.removeAt(i);
i--;
}
if (tmplist.isEmpty())
return;
m_rules.isEmpty() ? m_rules = tmplist.join("\n")
: m_rules += "\n" + tmplist.join("\n");
}
40 changes: 40 additions & 0 deletions src/util/logger.h
Original file line number Diff line number Diff line change
@@ -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 <QObject>
#include <dtkcore_global.h>

#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
13 changes: 7 additions & 6 deletions src/utils/dconfigmanager.cpp
Original file line number Diff line number Diff line change
@@ -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 <DConfig>

Expand Down Expand Up @@ -108,15 +109,15 @@ 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;
}

//qDebug() << "dconfig:" << config << "key:" << key << "value:" << d->configs.value(config)->value(key, fallback);
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;
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions src/view/imageview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<QPinchGesture *>(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());
}
}
Expand All @@ -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
Expand Down
Loading