From 0aedff4d34390550d27278c38933ed633c1f0d6f Mon Sep 17 00:00:00 2001 From: shenleban tongying Date: Mon, 3 Feb 2025 02:35:56 -0500 Subject: [PATCH] fix: partially revert "Enable application log" to "Log to file" --- .clang-format | 1 + src/common/logfileptr.cc | 2 - src/common/logfileptr.hh | 3 -- src/logger.cc | 78 ++++++++++++++++++++++++++++++++ src/logger.hh | 7 +++ src/main.cc | 97 +++------------------------------------- src/termination.cc | 8 +--- src/ui/mainwindow.cc | 3 ++ src/ui/preferences.ui | 2 +- 9 files changed, 97 insertions(+), 104 deletions(-) delete mode 100644 src/common/logfileptr.cc delete mode 100644 src/common/logfileptr.hh create mode 100644 src/logger.cc create mode 100644 src/logger.hh diff --git a/.clang-format b/.clang-format index c87855373..87244e8e7 100644 --- a/.clang-format +++ b/.clang-format @@ -81,6 +81,7 @@ IndentExternBlock: AfterExternBlock IndentWidth: 2 IndentWrappedFunctionNames: false # InsertBraces: false +InsertNewlineAtEOF: true InsertTrailingCommas: None KeepEmptyLinesAtTheStartOfBlocks: true LambdaBodyIndentation: Signature diff --git a/src/common/logfileptr.cc b/src/common/logfileptr.cc deleted file mode 100644 index dae054267..000000000 --- a/src/common/logfileptr.cc +++ /dev/null @@ -1,2 +0,0 @@ -#include "logfileptr.hh" -QFile * logFilePtr; diff --git a/src/common/logfileptr.hh b/src/common/logfileptr.hh deleted file mode 100644 index 32b7038c8..000000000 --- a/src/common/logfileptr.hh +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once -#include -extern QFile * logFilePtr; diff --git a/src/logger.cc b/src/logger.cc new file mode 100644 index 000000000..4b8575e1b --- /dev/null +++ b/src/logger.cc @@ -0,0 +1,78 @@ +#include "config.hh" +#include "logger.hh" +#include +#include +#include +#include + +QFile logFile; +QMutex logFileMutex; // Logging could happen in any threads! + +void logToFileMessageHander( QtMsgType type, const QMessageLogContext & context, const QString & mess ) +{ + QString strTime = QDateTime::currentDateTime().toString( "MM-dd hh:mm:ss" ); + QString message = QString( "%1 %2\r\n" ).arg( strTime, mess ); + QMutexLocker _( &logFileMutex ); + + if ( logFile.isOpen() ) { + switch ( type ) { + case QtDebugMsg: + message.prepend( "Debug: " ); + break; + case QtWarningMsg: + message.prepend( "Warning: " ); + break; + case QtCriticalMsg: + message.prepend( "Critical: " ); + break; + case QtFatalMsg: + message.prepend( "Fatal: " ); + logFile.write( message.toUtf8() ); + logFile.flush(); + abort(); + case QtInfoMsg: + message.insert( 0, "Info: " ); + break; + } + + logFile.write( message.toUtf8() ); + logFile.flush(); + + return; + } + else { + fprintf( stderr, "log file failed to open\n!" ); + fprintf( stderr, "%s\n", message.toUtf8().constData() ); + } +} + +namespace Logger { +void switchLoggingMethod( bool logToFile ) +{ + QMutexLocker _( &logFileMutex ); + if ( logToFile ) { + if ( !logFile.isOpen() ) { + logFile.setFileName( Config::getConfigDir() + "gd_log.txt" ); + if ( !logFile.open( QFile::WriteOnly ) ) { + qDebug() << "Failed to open log file!"; + return; + }; + } + qInstallMessageHandler( logToFileMessageHander ); + } + else { + if ( logFile.isOpen() ) { + logFile.flush(); + } + qInstallMessageHandler( nullptr ); // restore the default one + } +} + +void closeLogFile() +{ + if ( logFile.isOpen() ) { + logFile.flush(); + logFile.close(); + } +} +} // namespace Logger diff --git a/src/logger.hh b/src/logger.hh new file mode 100644 index 000000000..22aa6fc72 --- /dev/null +++ b/src/logger.hh @@ -0,0 +1,7 @@ +#pragma once + +/// Manage Logging, mainly for switching to log-to-file because of Windows +namespace Logger { +void switchLoggingMethod( bool logToFile ); +void closeLogFile(); +}; // namespace Logger diff --git a/src/main.cc b/src/main.cc index 4d8884f17..2fc7dc556 100644 --- a/src/main.cc +++ b/src/main.cc @@ -2,7 +2,7 @@ * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ #include "config.hh" -#include "logfileptr.hh" +#include "logger.hh" #include "mainwindow.hh" #include "termination.hh" #include "version.hh" @@ -14,11 +14,9 @@ #include #include #include -#include #include #include #include - #if defined( Q_OS_UNIX ) #include #include "unix/ksignalhandler.hh" @@ -82,66 +80,6 @@ bool callback( const char * dump_dir, const char * minidump_id, void * context, #endif #endif -QMutex logMutex; - -void gdMessageHandler( QtMsgType type, const QMessageLogContext & context, const QString & mess ) -{ - if ( GlobalBroadcaster::instance()->getPreference() == nullptr - || !GlobalBroadcaster::instance()->getPreference()->enableApplicationLog ) { - return; - } - QString strTime = QDateTime::currentDateTime().toString( "MM-dd hh:mm:ss" ); - QString message = QString( "%1 %2\r\n" ).arg( strTime, mess ); - - if ( ( logFilePtr != nullptr ) && logFilePtr->isOpen() ) { - //without the lock ,on multithread,there would be assert error. - QMutexLocker _( &logMutex ); - switch ( type ) { - case QtDebugMsg: - message.insert( 0, "Debug: " ); - break; - case QtWarningMsg: - message.insert( 0, "Warning: " ); - break; - case QtCriticalMsg: - message.insert( 0, "Critical: " ); - break; - case QtFatalMsg: - message.insert( 0, "Fatal: " ); - logFilePtr->write( message.toUtf8() ); - logFilePtr->flush(); - abort(); - case QtInfoMsg: - message.insert( 0, "Info: " ); - break; - } - - logFilePtr->write( message.toUtf8() ); - logFilePtr->flush(); - - return; - } - - //the following code lines actually will have no chance to run, schedule to remove in the future. - QByteArray msg = mess.toUtf8().constData(); - switch ( type ) { - case QtDebugMsg: - fprintf( stderr, "Debug: %s\n", msg.constData() ); - break; - case QtWarningMsg: - fprintf( stderr, "Warning: %s\n", msg.constData() ); - break; - case QtCriticalMsg: - fprintf( stderr, "Critical: %s\n", msg.constData() ); - break; - case QtFatalMsg: - fprintf( stderr, "Fatal: %s\n", msg.constData() ); - abort(); - case QtInfoMsg: - fprintf( stderr, "Info: %s\n", msg.constData() ); - break; - } -} struct GDOptions { @@ -170,11 +108,6 @@ struct GDOptions return popupGroupName; } - inline bool needLogFile() const - { - return logFile; - } - inline bool needTranslateWord() const { return !word.isEmpty(); @@ -204,7 +137,7 @@ void processCommandLine( QCoreApplication * app, GDOptions * result ) QCommandLineOption logFileOption( QStringList() << "l" << "log-to-file", - QObject::tr( "Save debug messages to gd_log.txt in the config folder." ) ); + QObject::tr( "Save debug messages to gd_log.txt in the config folder" ) + '.' ); QCommandLineOption resetState( QStringList() << "r" << "reset-window-state", @@ -431,15 +364,6 @@ int main( int argc, char ** argv ) QWebEngineUrlScheme::registerScheme( webUiScheme ); } - QFile file; - logFilePtr = &file; - auto guard = qScopeGuard( [ &file ]() { - logFilePtr = nullptr; - file.close(); - } ); - - Q_UNUSED( guard ) - QFont f = QApplication::font(); f.setStyleStrategy( QFont::PreferAntialias ); QApplication::setFont( f ); @@ -492,9 +416,6 @@ int main( int argc, char ** argv ) for ( ;; ) { try { cfg = Config::load(); - - //enabled through command line or preference - gdcl.logFile = gdcl.logFile || cfg.preferences.enableApplicationLog; } catch ( Config::exError & ) { QMessageBox mb( @@ -526,13 +447,8 @@ int main( int argc, char ** argv ) cfg.resetState = gdcl.resetState; - // Open log file - logFilePtr->setFileName( Config::getConfigDir() + "gd_log.txt" ); - logFilePtr->open( QFile::WriteOnly ); - - - // Install message handler - qInstallMessageHandler( gdMessageHandler ); + // Log to file enabled through command line or preference + Logger::switchLoggingMethod( gdcl.logFile || cfg.preferences.enableApplicationLog ); // Reload translations for user selected locale is nesessary QTranslator qtTranslator; @@ -630,10 +546,7 @@ int main( int argc, char ** argv ) QObject::connect( KSignalHandler::self(), &KSignalHandler::signalReceived, &m, &MainWindow::quitApp ); #endif int r = app.exec(); - - if ( logFilePtr->isOpen() ) { - logFilePtr->close(); - } + Logger::closeLogFile(); return r; } diff --git a/src/termination.cc b/src/termination.cc index 66f660205..63947bbcb 100644 --- a/src/termination.cc +++ b/src/termination.cc @@ -2,7 +2,7 @@ /* This file is (c) 2008-2012 Konstantin Isakov * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ -#include "logfileptr.hh" +#include "logger.hh" #include "termination.hh" #include #include @@ -10,11 +10,7 @@ static void termHandler() { qDebug() << "GoldenDict has crashed unexpectedly.\n\n"; - - if ( logFilePtr && logFilePtr->isOpen() ) { - logFilePtr->close(); - } - + Logger::closeLogFile(); abort(); } diff --git a/src/ui/mainwindow.cc b/src/ui/mainwindow.cc index bef9af319..a37115234 100644 --- a/src/ui/mainwindow.cc +++ b/src/ui/mainwindow.cc @@ -8,6 +8,7 @@ #endif #include "mainwindow.hh" +#include "logger.hh" #include #include "edit_dictionaries.hh" #include "dict/loaddictionaries.hh" @@ -2366,6 +2367,8 @@ void MainWindow::editPreferences() ui.fullTextSearchAction->setEnabled( cfg.preferences.fts.enabled ); + Logger::switchLoggingMethod( cfg.preferences.enableApplicationLog ); + Config::save( cfg ); } diff --git a/src/ui/preferences.ui b/src/ui/preferences.ui index ae2899e12..e894c2ff1 100644 --- a/src/ui/preferences.ui +++ b/src/ui/preferences.ui @@ -1938,7 +1938,7 @@ from Stardict, Babylon and GLS dictionaries - Enable application log + Save debug messages to gd_log.txt in the config folder