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 "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 );
}

View file

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

24
main.cc
View file

@ -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() )
{

View file

@ -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" );

View file

@ -6,7 +6,7 @@
#include <QFile>
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.