From d30fdc67c65339857321d10b5f9e279e4eac4a36 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Thu, 12 Jun 2025 15:05:06 +0200 Subject: [PATCH] Remove file logging This hasn't proven to be as useful as i had hoped: - My arcane logic for determining logging categories is apparently broken - It won't work with the logging by the new crypto sdk - I never actually ended up looking at my own logs, or anyone else's - It seems to cause crashes --- src/app/CMakeLists.txt | 2 - src/app/logger.cpp | 223 ----------------------------------------- src/app/logger.h | 9 -- src/app/main.cpp | 3 - 4 files changed, 237 deletions(-) delete mode 100644 src/app/logger.cpp delete mode 100644 src/app/logger.h diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 82e9f7f87..627af322c 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -20,8 +20,6 @@ add_library(neochat STATIC windowcontroller.h models/serverlistmodel.cpp models/serverlistmodel.h - logger.cpp - logger.h models/notificationsmodel.cpp models/notificationsmodel.h proxycontroller.cpp diff --git a/src/app/logger.cpp b/src/app/logger.cpp deleted file mode 100644 index 22489df1f..000000000 --- a/src/app/logger.cpp +++ /dev/null @@ -1,223 +0,0 @@ -// SPDX-FileCopyrightText: 1997 Matthias Kalle Dalheimer -// SPDX-FileCopyrightText: 2002 Holger Freyther -// SPDX-FileCopyrightText: 2008 Volker Krause -// SPDX-FileCopyrightText: 2023 Tobias Fella -// SPDX-License-Identifier: LGPL-2.0-or-later - -#include "logger.h" - -#include -#include -#include -#include -#include -#include - -using namespace Qt::StringLiterals; - -static QLoggingCategory::CategoryFilter oldCategoryFilter = nullptr; -static QtMessageHandler oldHandler = nullptr; -static bool e2eeDebugEnabled = false; - -class FileDebugStream : public QIODevice -{ - Q_OBJECT -public: - FileDebugStream() - : mType(QtCriticalMsg) - { - open(WriteOnly); - } - - bool isSequential() const override - { - return true; - } - qint64 readData(char *, qint64) override - { - return 0; - } - qint64 readLineData(char *, qint64) override - { - return 0; - } - - qint64 writeData(const char *data, qint64 len) override - { - if (!mFileName.isEmpty()) { - QFile outputFile(mFileName); - outputFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Unbuffered); - outputFile.write(data, len); - outputFile.putChar('\n'); - outputFile.close(); - } - - return len; - } - - void setFileName(const QString &fileName) - { - mFileName = fileName; - } - - void setType(QtMsgType type) - { - mType = type; - } - -private: - QString mFileName; - QtMsgType mType; -}; - -class DebugPrivate -{ -public: - DebugPrivate() - : origHandler(nullptr) - { - } - - ~DebugPrivate() - { - qInstallMessageHandler(origHandler); - file.close(); - } - - void log(QtMsgType type, const QMessageLogContext &context, const QString &message) - { - QMutexLocker locker(&mutex); - QByteArray buf; - QTextStream str(&buf); - str << QDateTime::currentDateTime().toString(Qt::ISODate) << u" ["_s; - switch (type) { - case QtDebugMsg: - str << u"DEBUG"_s; - break; - case QtInfoMsg: - str << u"INFO "_s; - break; - case QtWarningMsg: - str << u"WARN "_s; - break; - case QtFatalMsg: - str << u"FATAL"_s; - break; - case QtCriticalMsg: - str << u"CRITICAL"_s; - break; - } - str << u"] "_s << context.category << u": "_s; - if (context.file && *context.file && context.line) { - str << context.file << u":"_s << context.line << u": "_s; - } - if (context.function && *context.function) { - str << context.function << u": "_s; - } - str << message << u"\n"_s; - str.flush(); - file.write(buf.constData(), buf.size()); - file.flush(); - - if (oldHandler && (!context.category || (strcmp(context.category, "quotient.e2ee") != 0 || e2eeDebugEnabled))) { - oldHandler(type, context, message); - } - } - - void setName(const QString &appName) - { - name = appName; - - if (file.isOpen()) { - file.close(); - } - const auto &filePath = u"%1%2%3"_s.arg(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation), QDir::separator(), appName); - - QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator()); - auto entryList = dir.entryList({appName + u".*"_s}); - std::sort(entryList.begin(), entryList.end(), [](const auto &left, const auto &right) { - auto leftIndex = left.split(u"."_s).last().toInt(); - auto rightIndex = right.split(u"."_s).last().toInt(); - return leftIndex > rightIndex; - }); - for (const auto &entry : entryList) { - bool ok = false; - const auto index = entry.split(u"."_s).last().toInt(&ok); - if (!ok) { - continue; - } - QFileInfo info(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + QDir::separator() + entry); - if (info.exists()) { - QFile file(info.absoluteFilePath()); - if (index > 50) { - file.remove(); - continue; - } - const auto &newName = u"%1.%2"_s.arg(filePath, QString::number(index + 1)); - const auto success = file.copy(newName); - if (success) { - file.remove(); - } else { - qFatal("Cannot rename log file '%s' to '%s': %s", - qUtf8Printable(file.fileName()), - qUtf8Printable(newName), - qUtf8Printable(file.errorString())); - } - } - } - - QFileInfo finfo(filePath); - if (!finfo.absoluteDir().exists()) { - QDir().mkpath(finfo.absolutePath()); - } - file.setFileName(filePath + u".0"_s); - file.open(QIODevice::WriteOnly | QIODevice::Unbuffered); - } - - void setOrigHandler(QtMessageHandler origHandler_) - { - origHandler = origHandler_; - } - - QMutex mutex; - QFile file; - QString name; - QtMessageHandler origHandler; - QByteArray loggingCategory; -}; - -Q_GLOBAL_STATIC(DebugPrivate, sInstance) - -void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message) -{ - switch (type) { - case QtDebugMsg: - case QtInfoMsg: - case QtWarningMsg: - case QtCriticalMsg: - sInstance()->log(type, context, message); - break; - case QtFatalMsg: - sInstance()->log(QtInfoMsg, context, message); - } -} - -void filter(QLoggingCategory *category) -{ - if (qstrcmp(category->categoryName(), "quotient.e2ee") == 0) { - category->setEnabled(QtDebugMsg, true); - } else if (oldCategoryFilter) { - oldCategoryFilter(category); - } -} - -void initLogging() -{ - e2eeDebugEnabled = QLoggingCategory("quotient.e2ee", QtInfoMsg).isEnabled(QtDebugMsg); - oldCategoryFilter = QLoggingCategory::installFilter(filter); - oldHandler = qInstallMessageHandler(messageHandler); - sInstance->setOrigHandler(oldHandler); - sInstance->setName(u"neochat.log"_s); -} - -#include "logger.moc" diff --git a/src/app/logger.h b/src/app/logger.h deleted file mode 100644 index 7e2567641..000000000 --- a/src/app/logger.h +++ /dev/null @@ -1,9 +0,0 @@ -// SPDX-FileCopyrightText: 2023 Tobias Fella -// SPDX-License-Identifier: GPL-2.0-or-later - -#pragma once - -/** - * Initlalize logging to file and enables some additional categories, which will only be logged to the file - */ -void initLogging(); diff --git a/src/app/main.cpp b/src/app/main.cpp index 34ac458a6..cd7158eee 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -49,7 +49,6 @@ #include "blurhashimageprovider.h" #include "colorschemer.h" #include "controller.h" -#include "logger.h" #include "login.h" #include "registration.h" #include "roommanager.h" @@ -182,8 +181,6 @@ int main(int argc, char *argv[]) KCrash::initialize(); #endif - initLogging(); - Connection::setEncryptionDefault(true); Connection::setDirectChatEncryptionDefault(true);