From 1a6a941918968f037acd88c5f764eba43f64d393 Mon Sep 17 00:00:00 2001 From: Abs62 Date: Tue, 22 May 2018 17:37:21 +0300 Subject: [PATCH] Little refactoring for log messages to file --- gddebug.cc | 10 +++++----- gddebug.hh | 2 +- main.cc | 24 +++++++++++++----------- termination.cc | 4 ++-- termination.hh | 2 +- 5 files changed, 22 insertions(+), 20 deletions(-) diff --git a/gddebug.cc b/gddebug.cc index b4c2d918..57cd7ab1 100644 --- a/gddebug.cc +++ b/gddebug.cc @@ -5,7 +5,7 @@ #include #include "gddebug.hh" -QFile logFile; +QFile * logFilePtr; static QTextCodec * utf8Codec; void gdWarning(const char *msg, ...) @@ -14,7 +14,7 @@ va_list ap; va_start(ap, msg); QTextCodec *localeCodec = 0; - if( logFile.isOpen() ) + if( logFilePtr && logFilePtr->isOpen() ) { if( utf8Codec == 0 ) utf8Codec = QTextCodec::codecForName( "UTF8" ); @@ -25,7 +25,7 @@ QTextCodec *localeCodec = 0; qWarning( "%s", QString().vsprintf( msg, ap ).toUtf8().data() ); - if( logFile.isOpen() ) + if( logFilePtr && logFilePtr->isOpen() ) { QTextCodec::setCodecForLocale( localeCodec ); } @@ -39,7 +39,7 @@ va_list ap; va_start(ap, msg); QTextCodec *localeCodec = 0; - if( logFile.isOpen() ) + if( logFilePtr && logFilePtr->isOpen() ) { if( utf8Codec == 0 ) utf8Codec = QTextCodec::codecForName( "UTF8" ); @@ -50,7 +50,7 @@ QTextCodec *localeCodec = 0; qDebug( "%s", QString().vsprintf( msg, ap ).toUtf8().data() ); - if( logFile.isOpen() ) + if( logFilePtr && logFilePtr->isOpen() ) { QTextCodec::setCodecForLocale( localeCodec ); } diff --git a/gddebug.hh b/gddebug.hh index 682e5cc7..36d8af6f 100644 --- a/gddebug.hh +++ b/gddebug.hh @@ -23,6 +23,6 @@ void gdDebug(const char *, ...) #endif ; -extern QFile logFile; +extern QFile * logFilePtr; #endif // __GDDEBUG_HH_INCLUDED__ diff --git a/main.cc b/main.cc index 27119035..02d72c65 100644 --- a/main.cc +++ b/main.cc @@ -58,32 +58,32 @@ void gdMessageHandler( QtMsgType type, const char *msg_ ) switch (type) { case QtDebugMsg: - if( logFile.isOpen() ) + if( logFilePtr && logFilePtr->isOpen() ) message.insert( 0, "Debug: " ); else fprintf(stderr, "Debug: %s\n", msg.constData()); break; case QtWarningMsg: - if( logFile.isOpen() ) + if( logFilePtr && logFilePtr->isOpen() ) message.insert( 0, "Warning: " ); else fprintf(stderr, "Warning: %s\n", msg.constData()); break; case QtCriticalMsg: - if( logFile.isOpen() ) + if( logFilePtr && logFilePtr->isOpen() ) message.insert( 0, "Critical: " ); else fprintf(stderr, "Critical: %s\n", msg.constData()); break; case QtFatalMsg: - if( logFile.isOpen() ) + if( logFilePtr && logFilePtr->isOpen() ) { - logFile.write( "Fatal: " ); - logFile.write( msg ); - logFile.flush(); + logFilePtr->write( "Fatal: " ); + logFilePtr->write( msg ); + logFilePtr->flush(); } else fprintf(stderr, "Fatal: %s\n", msg.constData()); @@ -91,7 +91,7 @@ void gdMessageHandler( QtMsgType type, const char *msg_ ) #if QT_VERSION >= QT_VERSION_CHECK( 5, 5, 0 ) case QtInfoMsg: - if( logFile.isOpen() ) + if( logFilePtr && logFilePtr->isOpen() ) message.insert( 0, "Info: " ); else fprintf(stderr, "Info: %s\n", msg.constData()); @@ -99,11 +99,11 @@ void gdMessageHandler( QtMsgType type, const char *msg_ ) #endif } - if( logFile.isOpen() ) + if( logFilePtr && logFilePtr->isOpen() ) { message.append( "\n" ); - logFile.write( message.toUtf8() ); - logFile.flush(); + logFilePtr->write( message.toUtf8() ); + logFilePtr->flush(); } } @@ -276,6 +276,8 @@ int main( int argc, char ** argv ) #endif QHotkeyApplication app( "GoldenDict", argc, argv ); + QFile logFile; + logFilePtr = &logFile; if ( app.isRunning() ) { diff --git a/termination.cc b/termination.cc index 6aec0b12..d45cd538 100644 --- a/termination.cc +++ b/termination.cc @@ -26,8 +26,8 @@ using std::string; static void termHandler() { - if( logFile.isOpen() ) - logFile.close(); + if( logFilePtr && logFilePtr->isOpen() ) + logFilePtr->close(); std::string message( "GoldenDict has crashed with an unexpected exception\n\n" ); diff --git a/termination.hh b/termination.hh index 020a3ec7..3d63dbb6 100644 --- a/termination.hh +++ b/termination.hh @@ -6,7 +6,7 @@ #include -extern QFile logFile; +extern QFile * logFilePtr; // Installs the termination handler which attempts to pop Qt's dialog showing // the exception and backtrace, and then aborts.