diff --git a/autotests/testlog.cpp b/autotests/testlog.cpp index 47cd1df..76654d6 100644 --- a/autotests/testlog.cpp +++ b/autotests/testlog.cpp @@ -1,131 +1,139 @@ /************************************************************************************* * Copyright 2016 by Sebastian Kügler * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or (at your option) any later version. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this library; if not, write to the Free Software * * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * *************************************************************************************/ #include #include #include #include "../src/log.h" Q_DECLARE_LOGGING_CATEGORY(KSCREEN_TESTLOG) Q_LOGGING_CATEGORY(KSCREEN_TESTLOG, "kscreen.testlog") using namespace KScreen; auto KSCREEN_LOGGING = "KSCREEN_LOGGING"; class TestLog : public QObject { Q_OBJECT private Q_SLOTS: void init(); void initTestCase(); void cleanupTestCase(); void testContext(); void testEnabled(); void testLog(); private: QString m_defaultLogFile; }; void TestLog::init() { QStandardPaths::setTestModeEnabled(true); m_defaultLogFile = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/kscreen/kscreen.log"; } void TestLog::initTestCase() { + qputenv(KSCREEN_LOGGING, QByteArray("true")); } void TestLog::cleanupTestCase() { qunsetenv(KSCREEN_LOGGING); } void TestLog::testContext() { auto log = Log::instance(); QString ctx("context text"); QVERIFY(log != nullptr); log->setContext(ctx); QCOMPARE(log->context(), ctx); delete log; } void TestLog::testEnabled() { qputenv(KSCREEN_LOGGING, QByteArray("faLSe")); auto log = Log::instance(); QCOMPARE(log->enabled(), false); QCOMPARE(log->logFile(), QString()); delete log; qunsetenv(KSCREEN_LOGGING); + log = Log::instance(); + QCOMPARE(log->enabled(), false); + QCOMPARE(log->logFile(), QString()); + + delete log; + qputenv(KSCREEN_LOGGING, QByteArray("truE")); + log = Log::instance(); QCOMPARE(log->enabled(), true); QCOMPARE(log->logFile(), m_defaultLogFile); delete log; } void TestLog::testLog() { auto log = Log::instance(); QFile lf(m_defaultLogFile); lf.remove(); QVERIFY(!lf.exists()); QString logmsg("This is a log message. ♥"); Log::log(logmsg); QVERIFY(lf.exists()); QVERIFY(lf.remove()); qCDebug(KSCREEN_TESTLOG) << "qCDebug message from testlog"; QVERIFY(lf.exists()); QVERIFY(lf.remove()); delete Log::instance(); // Make sure on log file gets written when disabled qputenv(KSCREEN_LOGGING, "false"); qCDebug(KSCREEN_TESTLOG) << logmsg; QCOMPARE(Log::instance()->enabled(), false); QVERIFY(!lf.exists()); Log::log(logmsg); QVERIFY(!lf.exists()); // Make sure we don't crash on cleanup delete Log::instance(); delete Log::instance(); } QTEST_MAIN(TestLog) #include "testlog.moc" diff --git a/src/log.cpp b/src/log.cpp index dbb48a3..51b385e 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -1,136 +1,138 @@ /************************************************************************************* * Copyright 2016 by Sebastian Kügler * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or (at your option) any later version. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * * Lesser General Public License for more details. * * * * You should have received a copy of the GNU Lesser General Public * * License along with this library; if not, write to the Free Software * * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * *************************************************************************************/ #include "log.h" #include #include #include #include #include namespace KScreen { Log* Log::sInstance = nullptr; QtMessageHandler sDefaultMessageHandler = nullptr; void kscreenLogOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg) { QByteArray localMsg = msg.toLocal8Bit(); if (QString::fromLocal8Bit(context.category).startsWith(QLatin1String("kscreen"))) { Log::log(localMsg.constData(), context.category); } sDefaultMessageHandler(type, context, msg); } void log(const QString& msg) { Log::log(msg); } Log* Log::instance() { if (!sInstance) { sInstance = new Log(); } return sInstance; } using namespace KScreen; class Log::Private { public: QString context; - bool enabled = true; + bool enabled = false; QString logFile; }; Log::Log() : d(new Private) { const char* logging_env = "KSCREEN_LOGGING"; if (qEnvironmentVariableIsSet(logging_env)) { const QString logging_env_value = qgetenv(logging_env).constData(); - if (logging_env_value == QStringLiteral("0") || logging_env_value.toLower() == QStringLiteral("false")) { - d->enabled = false; - return; + if (logging_env_value != QStringLiteral("0") && logging_env_value.toLower() != QStringLiteral("false")) { + d->enabled = true; } } + if (!d->enabled) { + return; + } d->logFile = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/kscreen/kscreen.log"; QLoggingCategory::setFilterRules("kscreen.*=true"); QFileInfo fi(d->logFile); if (!QDir().mkpath(fi.absolutePath())) { qWarning() << "Failed to create logging dir" << fi.absolutePath(); } if (!sDefaultMessageHandler) { sDefaultMessageHandler = qInstallMessageHandler(kscreenLogOutput); } } Log::Log(Log::Private *dd) : d(dd) { } Log::~Log() { delete d; sInstance = nullptr; } QString Log::context() const { return d->context; } void Log::setContext(const QString& context) { d->context = context; } bool Log::enabled() const { return d->enabled; } QString Log::logFile() const { return d->logFile; } void Log::log(const QString &msg, const QString &category) { if (!instance()->enabled()) { return; } auto _cat = category; _cat.remove("kscreen."); const QString timestamp = QDateTime::currentDateTime().toString("dd.MM.yyyy hh:mm:ss.zzz"); QString logMessage = QString("\n%1 ; %2 ; %3 : %4").arg(timestamp, _cat, instance()->context(), msg); QFile file(instance()->logFile()); if (!file.open(QIODevice::Append | QIODevice::Text)) { return; } file.write(logMessage.toUtf8()); } } // ns