File indexing completed on 2022-09-27 16:31:46

0001 /*
0002     SPDX-FileCopyrightText: 2019-2022 Laurent Montel <montel@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 
0006 */
0007 
0008 #include "kdebugsettingsloadingcategories.h"
0009 #include "kdebugsettingsutil.h"
0010 #include "loadcategoriesjob.h"
0011 #include <QDir>
0012 #include <QLibraryInfo>
0013 #include <QStandardPaths>
0014 
0015 KDebugSettingsLoadingCategories::KDebugSettingsLoadingCategories() = default;
0016 
0017 KDebugSettingsLoadingCategories::~KDebugSettingsLoadingCategories() = default;
0018 
0019 void KDebugSettingsLoadingCategories::readQtLoggingFile()
0020 {
0021     mCategoriesList.clear();
0022     const QString envPath = QStandardPaths::locate(QStandardPaths::GenericConfigLocation, QStringLiteral("QtProject/qtlogging.ini"));
0023     if (!envPath.isEmpty()) {
0024         readCategoriesFiles(envPath);
0025     } else {
0026 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0027         const QString dataPath = QDir(QLibraryInfo::path(QLibraryInfo::DataPath)).absoluteFilePath(QStringLiteral("qtlogging.ini"));
0028 #else
0029         const QString dataPath = QDir(QLibraryInfo::location(QLibraryInfo::DataPath)).absoluteFilePath(QStringLiteral("qtlogging.ini"));
0030 #endif
0031         readCategoriesFiles(dataPath);
0032     }
0033 }
0034 
0035 void KDebugSettingsLoadingCategories::readCategoriesFiles(const QString &path)
0036 {
0037     // KDE debug categories area
0038 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0039     const QString confAreasFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("qlogging-categories6/kde.categories"));
0040 #else
0041     const QString confAreasFile = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("qlogging-categories5/kde.categories"));
0042 #endif
0043     if (!confAreasFile.isEmpty()) {
0044         KDebugSettingsUtil::readLoggingCategories(confAreasFile, mCategoriesList, false);
0045     }
0046 
0047     mRenameCategoriesList.clear();
0048     // Load *.renamecategories file in QStandardPaths::ConfigLocation for kde apps.
0049 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
0050     const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::ConfigLocation, QString(), QStandardPaths::LocateDirectory)
0051         + QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("qlogging-categories6/"), QStandardPaths::LocateDirectory);
0052 #else
0053     const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::ConfigLocation, QString(), QStandardPaths::LocateDirectory)
0054         + QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("qlogging-categories5/"), QStandardPaths::LocateDirectory);
0055 #endif
0056     for (const QString &dir : dirs) {
0057         const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.renamecategories"));
0058         for (const QString &file : fileNames) {
0059             mRenameCategoriesList.append(KDebugSettingsUtil::readRenameCategories(dir + file));
0060         }
0061     }
0062     // TODO add load rename file from external kde apps.
0063     const QStringList dirs2 =
0064         QStandardPaths::locateAll(QStandardPaths::GenericConfigLocation, QStringLiteral("qdebug.categories/"), QStandardPaths::LocateDirectory);
0065     // qDebug() << " dirs 2 " << dirs2;
0066     for (const QString &dir : dirs2) {
0067         const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.renamecategories"));
0068         for (const QString &file : fileNames) {
0069             mRenameCategoriesList.append(KDebugSettingsUtil::readRenameCategories(dir + file));
0070         }
0071     }
0072 
0073     // Load *.categories file in QStandardPaths::ConfigLocation for kde apps.
0074     for (const QString &dir : dirs) {
0075         const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.categories"));
0076         for (const QString &file : fileNames) {
0077             if (file != QLatin1String("kde.categories")) {
0078                 KDebugSettingsUtil::readLoggingCategories(dir + file, mCategoriesList, true);
0079             }
0080         }
0081     }
0082 
0083     // Load *.categories files. in qdebug.categories for external kde apps.
0084     for (const QString &dir : dirs2) {
0085         const QStringList fileNames = QDir(dir).entryList(QStringList() << QStringLiteral("*.categories"));
0086         for (const QString &file : fileNames) {
0087             KDebugSettingsUtil::readLoggingCategories(dir + QLatin1Char('/') + file, mCategoriesList, true);
0088         }
0089     }
0090 
0091     const QByteArray rulesFilePath = qgetenv("QT_LOGGING_CONF");
0092     if (!rulesFilePath.isEmpty()) {
0093         // const QList<KDebugSettingsUtil::LoadLoggingCategory> envCategories = KDebugSettingsUtil::readLoggingQtCategories(QString::fromLatin1(rulesFilePath));
0094         // TODO
0095     }
0096 
0097     mEnvironmentrules = QString::fromLatin1(qgetenv("QT_LOGGING_RULES"));
0098     // qt logging.ini
0099     LoadCategoriesJob job;
0100     job.setFileName(path);
0101     job.setCategories(mCategoriesList);
0102     job.setRenamedCategories(mRenameCategoriesList);
0103     job.start();
0104 
0105     mCustomCategories = job.customCategories();
0106     mQtKdeCategories = job.qtKdeCategories();
0107     mFoundOverrideRule = job.foundOverrideRule();
0108 }
0109 
0110 bool KDebugSettingsLoadingCategories::foundOverrideRule() const
0111 {
0112     return mFoundOverrideRule;
0113 }
0114 
0115 LoggingCategory::List KDebugSettingsLoadingCategories::qtKdeCategories() const
0116 {
0117     return mQtKdeCategories;
0118 }
0119 
0120 LoggingCategory::List KDebugSettingsLoadingCategories::customCategories() const
0121 {
0122     return mCustomCategories;
0123 }
0124 
0125 KdeLoggingCategory::List KDebugSettingsLoadingCategories::categoriesList() const
0126 {
0127     return mCategoriesList;
0128 }
0129 
0130 RenameCategory::List KDebugSettingsLoadingCategories::renameCategoriesList() const
0131 {
0132     return mRenameCategoriesList;
0133 }
0134 
0135 QString KDebugSettingsLoadingCategories::environmentrules() const
0136 {
0137     return mEnvironmentrules;
0138 }