| 1 | // SPDX-FileCopyrightText: 2024 - 2025 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #ifndef ORG_DEEPIN_LOG_VIEWER_H |
| 6 | #define ORG_DEEPIN_LOG_VIEWER_H |
| 7 | |
| 8 | #include <QThread> |
| 9 | #include <QVariant> |
| 10 | #include <QDebug> |
| 11 | #include <QAtomicPointer> |
| 12 | #include <QAtomicInteger> |
| 13 | #include <DConfig> |
| 14 | |
| 15 | class org_deepin_log_viewer : public QObject { |
| 16 | Q_OBJECT |
| 17 | |
| 18 | Q_PROPERTY(double coredumpReportMax READ coredumpReportMax WRITE setCoredumpReportMax NOTIFY coredumpReportMaxChanged) |
| 19 | Q_PROPERTY(QString coredumpReportTime READ coredumpReportTime WRITE setCoredumpReportTime NOTIFY coredumpReportTimeChanged) |
| 20 | Q_PROPERTY(QList<QVariant> customLogFiles READ customLogFiles WRITE setCustomLogFiles NOTIFY customLogFilesChanged) |
| 21 | Q_PROPERTY(QString log_rules READ log_rules WRITE setLog_rules NOTIFY log_rulesChanged) |
| 22 | Q_PROPERTY(double specialComType READ specialComType WRITE setSpecialComType NOTIFY specialComTypeChanged) |
| 23 | public: |
| 24 | explicit org_deepin_log_viewer(QThread *thread, const QString &appId, const QString &name, const QString &subpath, QObject *parent = nullptr) |
| 25 | : QObject(parent) { |
| 26 | |
| 27 | if (!thread->isRunning()) { |
| 28 | qWarning() << QStringLiteral("Warning: The provided thread is not running."); |
| 29 | } |
| 30 | Q_ASSERT(QThread::currentThread() != thread); |
| 31 | auto worker = new QObject(); |
| 32 | worker->moveToThread(thread); |
| 33 | QMetaObject::invokeMethod(worker, [=]() { |
| 34 | auto config = DTK_CORE_NAMESPACE::DConfig::create(appId, name, subpath, nullptr); |
| 35 | if (!config) { |
| 36 | qWarning() << QStringLiteral("Failed to create DConfig instance."); |
| 37 | worker->deleteLater(); |
| 38 | return; |
| 39 | } |
| 40 | config->moveToThread(QThread::currentThread()); |
| 41 | initialize(config); |
| 42 | worker->deleteLater(); |
| 43 | }); |
| 44 | } |
| 45 | explicit org_deepin_log_viewer(QThread *thread, DTK_CORE_NAMESPACE::DConfigBackend *backend, const QString &appId, const QString &name, const QString &subpath, QObject *parent = nullptr) |
| 46 | : QObject(parent) { |
| 47 | |
| 48 | if (!thread->isRunning()) { |
| 49 | qWarning() << QStringLiteral("Warning: The provided thread is not running."); |
| 50 | } |
| 51 | Q_ASSERT(QThread::currentThread() != thread); |
| 52 | auto worker = new QObject(); |
| 53 | worker->moveToThread(thread); |
| 54 | QMetaObject::invokeMethod(worker, [=]() { |
| 55 | auto config = DTK_CORE_NAMESPACE::DConfig::create(backend, appId, name, subpath, nullptr); |
| 56 | if (!config) { |
| 57 | qWarning() << QStringLiteral("Failed to create DConfig instance."); |
| 58 | worker->deleteLater(); |
| 59 | return; |
| 60 | } |
| 61 | config->moveToThread(QThread::currentThread()); |
| 62 | initialize(config); |
| 63 | worker->deleteLater(); |
| 64 | }); |
| 65 | } |
| 66 | explicit org_deepin_log_viewer(QThread *thread, const QString &name, const QString &subpath, QObject *parent = nullptr) |
| 67 | : QObject(parent) { |
| 68 | |
| 69 | if (!thread->isRunning()) { |
| 70 | qWarning() << QStringLiteral("Warning: The provided thread is not running."); |
| 71 | } |
| 72 | Q_ASSERT(QThread::currentThread() != thread); |
| 73 | auto worker = new QObject(); |
| 74 | worker->moveToThread(thread); |
| 75 | QMetaObject::invokeMethod(worker, [=]() { |
| 76 | auto config = DTK_CORE_NAMESPACE::DConfig::create(name, subpath, nullptr); |
| 77 | if (!config) { |
| 78 | qWarning() << QStringLiteral("Failed to create DConfig instance."); |
| 79 | worker->deleteLater(); |
| 80 | return; |
| 81 | } |
| 82 | config->moveToThread(QThread::currentThread()); |
| 83 | initialize(config); |
| 84 | worker->deleteLater(); |
| 85 | }); |
| 86 | } |
| 87 | explicit org_deepin_log_viewer(QThread *thread, DTK_CORE_NAMESPACE::DConfigBackend *backend, const QString &name, const QString &subpath, QObject *parent = nullptr) |
| 88 | : QObject(parent) { |
| 89 | |
| 90 | if (!thread->isRunning()) { |
| 91 | qWarning() << QStringLiteral("Warning: The provided thread is not running."); |
| 92 | } |
| 93 | Q_ASSERT(QThread::currentThread() != thread); |
| 94 | auto worker = new QObject(); |
| 95 | worker->moveToThread(thread); |
| 96 | QMetaObject::invokeMethod(worker, [=]() { |
| 97 | auto config = DTK_CORE_NAMESPACE::DConfig::create(backend, name, subpath, nullptr); |
| 98 | if (!config) { |
| 99 | qWarning() << QStringLiteral("Failed to create DConfig instance."); |
| 100 | worker->deleteLater(); |
| 101 | return; |
| 102 | } |
| 103 | config->moveToThread(QThread::currentThread()); |
| 104 | initialize(config); |
| 105 | worker->deleteLater(); |
| 106 | }); |
| 107 | } |
| 108 | ~org_deepin_log_viewer() { |
| 109 | if (m_config.loadRelaxed()) { |
| 110 | m_config.loadRelaxed()->deleteLater(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | double coredumpReportMax() const { |
| 115 | return p_coredumpReportMax; |
| 116 | } |
| 117 | void setCoredumpReportMax(const double &value) { |
| 118 | auto oldValue = p_coredumpReportMax; |
| 119 | p_coredumpReportMax = value; |
| 120 | markPropertySet(0); |
| 121 | if (auto config = m_config.loadRelaxed()) { |
| 122 | QMetaObject::invokeMethod(config, [this, value]() { |
| 123 | m_config.loadRelaxed()->setValue(QStringLiteral("coredumpReportMax"), value); |
| 124 | }); |
| 125 | } |
| 126 | if (p_coredumpReportMax != oldValue) { |
| 127 | Q_EMIT coredumpReportMaxChanged(); |
| 128 | } |
| 129 | } |
| 130 | QString coredumpReportTime() const { |
| 131 | return p_coredumpReportTime; |
| 132 | } |
| 133 | void setCoredumpReportTime(const QString &value) { |
| 134 | auto oldValue = p_coredumpReportTime; |
| 135 | p_coredumpReportTime = value; |
| 136 | markPropertySet(1); |
| 137 | if (auto config = m_config.loadRelaxed()) { |
| 138 | QMetaObject::invokeMethod(config, [this, value]() { |
| 139 | m_config.loadRelaxed()->setValue(QStringLiteral("coredumpReportTime"), value); |
| 140 | }); |
| 141 | } |
| 142 | if (p_coredumpReportTime != oldValue) { |
| 143 | Q_EMIT coredumpReportTimeChanged(); |
| 144 | } |
| 145 | } |
| 146 | QList<QVariant> customLogFiles() const { |
| 147 | return p_customLogFiles; |
| 148 | } |
| 149 | void setCustomLogFiles(const QList<QVariant> &value) { |
| 150 | auto oldValue = p_customLogFiles; |
| 151 | p_customLogFiles = value; |
| 152 | markPropertySet(2); |
| 153 | if (auto config = m_config.loadRelaxed()) { |
| 154 | QMetaObject::invokeMethod(config, [this, value]() { |
| 155 | m_config.loadRelaxed()->setValue(QStringLiteral("customLogFiles"), value); |
| 156 | }); |
| 157 | } |
| 158 | if (p_customLogFiles != oldValue) { |
| 159 | Q_EMIT customLogFilesChanged(); |
| 160 | } |
| 161 | } |
| 162 | QString log_rules() const { |
| 163 | return p_log_rules; |
| 164 | } |
| 165 | void setLog_rules(const QString &value) { |
| 166 | auto oldValue = p_log_rules; |
| 167 | p_log_rules = value; |
| 168 | markPropertySet(3); |
| 169 | if (auto config = m_config.loadRelaxed()) { |
| 170 | QMetaObject::invokeMethod(config, [this, value]() { |
| 171 | m_config.loadRelaxed()->setValue(QStringLiteral("log_rules"), value); |
| 172 | }); |
| 173 | } |
| 174 | if (p_log_rules != oldValue) { |
| 175 | Q_EMIT log_rulesChanged(); |
| 176 | } |
| 177 | } |
| 178 | double specialComType() const { |
| 179 | return p_specialComType; |
| 180 | } |
| 181 | void setSpecialComType(const double &value) { |
| 182 | auto oldValue = p_specialComType; |
| 183 | p_specialComType = value; |
| 184 | markPropertySet(4); |
| 185 | if (auto config = m_config.loadRelaxed()) { |
| 186 | QMetaObject::invokeMethod(config, [this, value]() { |
| 187 | m_config.loadRelaxed()->setValue(QStringLiteral("specialComType"), value); |
| 188 | }); |
| 189 | } |
| 190 | if (p_specialComType != oldValue) { |
| 191 | Q_EMIT specialComTypeChanged(); |
| 192 | } |
| 193 | } |
| 194 | Q_SIGNALS: |
| 195 | void coredumpReportMaxChanged(); |
| 196 | void coredumpReportTimeChanged(); |
| 197 | void customLogFilesChanged(); |
| 198 | void log_rulesChanged(); |
| 199 | void specialComTypeChanged(); |
| 200 | private: |
| 201 | void initialize(DTK_CORE_NAMESPACE::DConfig *config) { |
| 202 | Q_ASSERT(!m_config.loadRelaxed()); |
| 203 | m_config.storeRelaxed(config); |
| 204 | if (testPropertySet(0)) { |
| 205 | config->setValue(QStringLiteral("coredumpReportMax"), QVariant::fromValue(p_coredumpReportMax)); |
| 206 | } else { |
| 207 | updateValue(QStringLiteral("coredumpReportMax"), QVariant::fromValue(p_coredumpReportMax)); |
| 208 | } |
| 209 | if (testPropertySet(1)) { |
| 210 | config->setValue(QStringLiteral("coredumpReportTime"), QVariant::fromValue(p_coredumpReportTime)); |
| 211 | } else { |
| 212 | updateValue(QStringLiteral("coredumpReportTime"), QVariant::fromValue(p_coredumpReportTime)); |
| 213 | } |
| 214 | if (testPropertySet(2)) { |
| 215 | config->setValue(QStringLiteral("customLogFiles"), QVariant::fromValue(p_customLogFiles)); |
| 216 | } else { |
| 217 | updateValue(QStringLiteral("customLogFiles"), QVariant::fromValue(p_customLogFiles)); |
| 218 | } |
| 219 | if (testPropertySet(3)) { |
| 220 | config->setValue(QStringLiteral("log_rules"), QVariant::fromValue(p_log_rules)); |
| 221 | } else { |
| 222 | updateValue(QStringLiteral("log_rules"), QVariant::fromValue(p_log_rules)); |
| 223 | } |
| 224 | if (testPropertySet(4)) { |
| 225 | config->setValue(QStringLiteral("specialComType"), QVariant::fromValue(p_specialComType)); |
| 226 | } else { |
| 227 | updateValue(QStringLiteral("specialComType"), QVariant::fromValue(p_specialComType)); |
| 228 | } |
| 229 | |
| 230 | connect(config, &DTK_CORE_NAMESPACE::DConfig::valueChanged, this, [this](const QString &key) { |
| 231 | updateValue(key); |
| 232 | }, Qt::DirectConnection); |
| 233 | } |
| 234 | void updateValue(const QString &key, const QVariant &fallback = QVariant()) { |
| 235 | Q_ASSERT(QThread::currentThread() == m_config.loadRelaxed()->thread()); |
| 236 | const QVariant &value = m_config.loadRelaxed()->value(key, fallback); |
| 237 | if (key == QStringLiteral("coredumpReportMax")) { |
| 238 | auto newValue = qvariant_cast<double>(value); |
| 239 | QMetaObject::invokeMethod(this, [this, newValue]() { |
| 240 | if (p_coredumpReportMax != newValue) { |
| 241 | p_coredumpReportMax = newValue; |
| 242 | Q_EMIT coredumpReportMaxChanged(); |
| 243 | } |
| 244 | }); |
| 245 | return; |
| 246 | } |
| 247 | if (key == QStringLiteral("coredumpReportTime")) { |
| 248 | auto newValue = qvariant_cast<QString>(value); |
| 249 | QMetaObject::invokeMethod(this, [this, newValue]() { |
| 250 | if (p_coredumpReportTime != newValue) { |
| 251 | p_coredumpReportTime = newValue; |
| 252 | Q_EMIT coredumpReportTimeChanged(); |
| 253 | } |
| 254 | }); |
| 255 | return; |
| 256 | } |
| 257 | if (key == QStringLiteral("customLogFiles")) { |
| 258 | auto newValue = qvariant_cast<QList<QVariant>>(value); |
| 259 | QMetaObject::invokeMethod(this, [this, newValue]() { |
| 260 | if (p_customLogFiles != newValue) { |
| 261 | p_customLogFiles = newValue; |
| 262 | Q_EMIT customLogFilesChanged(); |
| 263 | } |
| 264 | }); |
| 265 | return; |
| 266 | } |
| 267 | if (key == QStringLiteral("log_rules")) { |
| 268 | auto newValue = qvariant_cast<QString>(value); |
| 269 | QMetaObject::invokeMethod(this, [this, newValue]() { |
| 270 | if (p_log_rules != newValue) { |
| 271 | p_log_rules = newValue; |
| 272 | Q_EMIT log_rulesChanged(); |
| 273 | } |
| 274 | }); |
| 275 | return; |
| 276 | } |
| 277 | if (key == QStringLiteral("specialComType")) { |
| 278 | auto newValue = qvariant_cast<double>(value); |
| 279 | QMetaObject::invokeMethod(this, [this, newValue]() { |
| 280 | if (p_specialComType != newValue) { |
| 281 | p_specialComType = newValue; |
| 282 | Q_EMIT specialComTypeChanged(); |
| 283 | } |
| 284 | }); |
| 285 | return; |
| 286 | } |
| 287 | } |
| 288 | inline void markPropertySet(const int index) { |
| 289 | if (index < 32) { |
| 290 | m_propertySetStatus0.fetchAndOrOrdered(1 << (index - 0)); |
| 291 | return; |
| 292 | } |
| 293 | Q_UNREACHABLE(); |
| 294 | } |
| 295 | inline bool testPropertySet(const int index) const { |
| 296 | if (index < 32) { |
| 297 | return (m_propertySetStatus0.loadRelaxed() & (1 << (index - 0))); |
| 298 | } |
| 299 | Q_UNREACHABLE(); |
| 300 | } |
| 301 | QAtomicPointer<DTK_CORE_NAMESPACE::DConfig> m_config = nullptr; |
| 302 | double p_coredumpReportMax { 50 }; |
| 303 | QString p_coredumpReportTime { QStringLiteral("") }; |
| 304 | QList<QVariant> p_customLogFiles { QList<QVariant>{} }; |
| 305 | QString p_log_rules { QStringLiteral("*.debug=false;*.info=false;*log.viewer*.warning=true") }; |
| 306 | double p_specialComType { -1 }; |
| 307 | QAtomicInteger<quint32> m_propertySetStatus0 = 0; |
| 308 | }; |
| 309 | |
| 310 | #endif // ORG_DEEPIN_LOG_VIEWER_H |