Little refactoring for log messages to file

This commit is contained in:
Abs62 2018-05-22 17:37:21 +03:00
parent fd00e9d156
commit 1a6a941918
5 changed files with 22 additions and 20 deletions

View file

@ -5,7 +5,7 @@
#include <QString> #include <QString>
#include "gddebug.hh" #include "gddebug.hh"
QFile logFile; QFile * logFilePtr;
static QTextCodec * utf8Codec; static QTextCodec * utf8Codec;
void gdWarning(const char *msg, ...) void gdWarning(const char *msg, ...)
@ -14,7 +14,7 @@ va_list ap;
va_start(ap, msg); va_start(ap, msg);
QTextCodec *localeCodec = 0; QTextCodec *localeCodec = 0;
if( logFile.isOpen() ) if( logFilePtr && logFilePtr->isOpen() )
{ {
if( utf8Codec == 0 ) if( utf8Codec == 0 )
utf8Codec = QTextCodec::codecForName( "UTF8" ); utf8Codec = QTextCodec::codecForName( "UTF8" );
@ -25,7 +25,7 @@ QTextCodec *localeCodec = 0;
qWarning( "%s", QString().vsprintf( msg, ap ).toUtf8().data() ); qWarning( "%s", QString().vsprintf( msg, ap ).toUtf8().data() );
if( logFile.isOpen() ) if( logFilePtr && logFilePtr->isOpen() )
{ {
QTextCodec::setCodecForLocale( localeCodec ); QTextCodec::setCodecForLocale( localeCodec );
} }
@ -39,7 +39,7 @@ va_list ap;
va_start(ap, msg); va_start(ap, msg);
QTextCodec *localeCodec = 0; QTextCodec *localeCodec = 0;
if( logFile.isOpen() ) if( logFilePtr && logFilePtr->isOpen() )
{ {
if( utf8Codec == 0 ) if( utf8Codec == 0 )
utf8Codec = QTextCodec::codecForName( "UTF8" ); utf8Codec = QTextCodec::codecForName( "UTF8" );
@ -50,7 +50,7 @@ QTextCodec *localeCodec = 0;
qDebug( "%s", QString().vsprintf( msg, ap ).toUtf8().data() ); qDebug( "%s", QString().vsprintf( msg, ap ).toUtf8().data() );
if( logFile.isOpen() ) if( logFilePtr && logFilePtr->isOpen() )
{ {
QTextCodec::setCodecForLocale( localeCodec ); QTextCodec::setCodecForLocale( localeCodec );
} }

View file

@ -23,6 +23,6 @@ void gdDebug(const char *, ...)
#endif #endif
; ;
extern QFile logFile; extern QFile * logFilePtr;
#endif // __GDDEBUG_HH_INCLUDED__ #endif // __GDDEBUG_HH_INCLUDED__

24
main.cc
View file

@ -58,32 +58,32 @@ void gdMessageHandler( QtMsgType type, const char *msg_ )
switch (type) { switch (type) {
case QtDebugMsg: case QtDebugMsg:
if( logFile.isOpen() ) if( logFilePtr && logFilePtr->isOpen() )
message.insert( 0, "Debug: " ); message.insert( 0, "Debug: " );
else else
fprintf(stderr, "Debug: %s\n", msg.constData()); fprintf(stderr, "Debug: %s\n", msg.constData());
break; break;
case QtWarningMsg: case QtWarningMsg:
if( logFile.isOpen() ) if( logFilePtr && logFilePtr->isOpen() )
message.insert( 0, "Warning: " ); message.insert( 0, "Warning: " );
else else
fprintf(stderr, "Warning: %s\n", msg.constData()); fprintf(stderr, "Warning: %s\n", msg.constData());
break; break;
case QtCriticalMsg: case QtCriticalMsg:
if( logFile.isOpen() ) if( logFilePtr && logFilePtr->isOpen() )
message.insert( 0, "Critical: " ); message.insert( 0, "Critical: " );
else else
fprintf(stderr, "Critical: %s\n", msg.constData()); fprintf(stderr, "Critical: %s\n", msg.constData());
break; break;
case QtFatalMsg: case QtFatalMsg:
if( logFile.isOpen() ) if( logFilePtr && logFilePtr->isOpen() )
{ {
logFile.write( "Fatal: " ); logFilePtr->write( "Fatal: " );
logFile.write( msg ); logFilePtr->write( msg );
logFile.flush(); logFilePtr->flush();
} }
else else
fprintf(stderr, "Fatal: %s\n", msg.constData()); 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 ) #if QT_VERSION >= QT_VERSION_CHECK( 5, 5, 0 )
case QtInfoMsg: case QtInfoMsg:
if( logFile.isOpen() ) if( logFilePtr && logFilePtr->isOpen() )
message.insert( 0, "Info: " ); message.insert( 0, "Info: " );
else else
fprintf(stderr, "Info: %s\n", msg.constData()); fprintf(stderr, "Info: %s\n", msg.constData());
@ -99,11 +99,11 @@ void gdMessageHandler( QtMsgType type, const char *msg_ )
#endif #endif
} }
if( logFile.isOpen() ) if( logFilePtr && logFilePtr->isOpen() )
{ {
message.append( "\n" ); message.append( "\n" );
logFile.write( message.toUtf8() ); logFilePtr->write( message.toUtf8() );
logFile.flush(); logFilePtr->flush();
} }
} }
@ -276,6 +276,8 @@ int main( int argc, char ** argv )
#endif #endif
QHotkeyApplication app( "GoldenDict", argc, argv ); QHotkeyApplication app( "GoldenDict", argc, argv );
QFile logFile;
logFilePtr = &logFile;
if ( app.isRunning() ) if ( app.isRunning() )
{ {

View file

@ -26,8 +26,8 @@ using std::string;
static void termHandler() static void termHandler()
{ {
if( logFile.isOpen() ) if( logFilePtr && logFilePtr->isOpen() )
logFile.close(); logFilePtr->close();
std::string message( "GoldenDict has crashed with an unexpected exception\n\n" ); std::string message( "GoldenDict has crashed with an unexpected exception\n\n" );

View file

@ -6,7 +6,7 @@
#include <QFile> #include <QFile>
extern QFile logFile; extern QFile * logFilePtr;
// Installs the termination handler which attempts to pop Qt's dialog showing // Installs the termination handler which attempts to pop Qt's dialog showing
// the exception and backtrace, and then aborts. // the exception and backtrace, and then aborts.