File indexing completed on 2024-04-14 15:37:37

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Sebastian Kügler <sebas@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-or-later
0005  */
0006 
0007 #include "log.h"
0008 
0009 #include <QDateTime>
0010 #include <QDir>
0011 #include <QFile>
0012 #include <QFileInfo>
0013 #include <QStandardPaths>
0014 
0015 namespace KScreen
0016 {
0017 Log *Log::sInstance = nullptr;
0018 QtMessageHandler sDefaultMessageHandler = nullptr;
0019 
0020 void kscreenLogOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
0021 {
0022     auto category = QString::fromLocal8Bit(context.category);
0023     if (category.startsWith(QLatin1String("kscreen"))) {
0024         Log::log(msg, category);
0025     }
0026     sDefaultMessageHandler(type, context, msg);
0027 }
0028 
0029 void log(const QString &msg)
0030 {
0031     Log::log(msg);
0032 }
0033 
0034 Log *Log::instance()
0035 {
0036     if (!sInstance) {
0037         sInstance = new Log();
0038     }
0039 
0040     return sInstance;
0041 }
0042 
0043 using namespace KScreen;
0044 class Q_DECL_HIDDEN Log::Private
0045 {
0046 public:
0047     QString context;
0048     bool enabled = false;
0049     QString logFile;
0050 };
0051 
0052 Log::Log()
0053     : d(new Private)
0054 {
0055     const char *logging_env = "KSCREEN_LOGGING";
0056 
0057     if (qEnvironmentVariableIsSet(logging_env)) {
0058         const QString logging_env_value = QString::fromUtf8(qgetenv(logging_env));
0059         if (logging_env_value != QLatin1Char('0') && logging_env_value.toLower() != QLatin1String("false")) {
0060             d->enabled = true;
0061         }
0062     }
0063     if (!d->enabled) {
0064         return;
0065     }
0066     d->logFile = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kscreen/kscreen.log");
0067 
0068     QLoggingCategory::setFilterRules(QStringLiteral("kscreen.*=true"));
0069     QFileInfo fi(d->logFile);
0070     if (!QDir().mkpath(fi.absolutePath())) {
0071         qWarning() << "Failed to create logging dir" << fi.absolutePath();
0072     }
0073 
0074     if (!sDefaultMessageHandler) {
0075         sDefaultMessageHandler = qInstallMessageHandler(kscreenLogOutput);
0076     }
0077 }
0078 
0079 Log::Log(Log::Private *dd)
0080     : d(dd)
0081 {
0082 }
0083 
0084 Log::~Log()
0085 {
0086     delete d;
0087     sInstance = nullptr;
0088 }
0089 
0090 QString Log::context() const
0091 {
0092     return d->context;
0093 }
0094 
0095 void Log::setContext(const QString &context)
0096 {
0097     d->context = context;
0098 }
0099 
0100 bool Log::enabled() const
0101 {
0102     return d->enabled;
0103 }
0104 
0105 QString Log::logFile() const
0106 {
0107     return d->logFile;
0108 }
0109 
0110 void Log::log(const QString &msg, const QString &category)
0111 {
0112     if (!instance()->enabled()) {
0113         return;
0114     }
0115     auto _cat = category;
0116     _cat.remove(QStringLiteral("kscreen."));
0117     const QString timestamp = QDateTime::currentDateTime().toString(QStringLiteral("dd.MM.yyyy hh:mm:ss.zzz"));
0118     QString logMessage = QStringLiteral("\n%1 ; %2 ; %3 : %4").arg(timestamp, _cat, instance()->context(), msg);
0119     QFile file(instance()->logFile());
0120     if (!file.open(QIODevice::Append | QIODevice::Text)) {
0121         return;
0122     }
0123     file.write(logMessage.toUtf8());
0124 }
0125 
0126 } // ns