-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modularize logging code and redo runtime logging method switching
- Loading branch information
1 parent
284a559
commit 755c041
Showing
7 changed files
with
96 additions
and
92 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "config.hh" | ||
#include "logging.hh" | ||
#include <QGlobalStatic> | ||
#include <QMutexLocker> | ||
#include <QDateTime> | ||
|
||
Q_GLOBAL_STATIC(Logger, gd_logger) | ||
|
||
QMutex logMutex; | ||
|
||
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 ); | ||
|
||
if ( gd_logger -> logFile.isOpen() ) { | ||
//without the lock ,on multithread,there would be assert error. | ||
QMutexLocker _( &logMutex ); | ||
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: " ); | ||
gd_logger -> logFile.write( message.toUtf8() ); | ||
gd_logger -> logFile.flush(); | ||
abort(); | ||
case QtInfoMsg: | ||
message.insert( 0, "Info: " ); | ||
break; | ||
} | ||
|
||
gd_logger -> logFile.write( message.toUtf8() ); | ||
gd_logger -> logFile.flush(); | ||
|
||
return; | ||
} else { | ||
throw std::runtime_error( "logToFileMessageHandler fatal error!" ); | ||
} | ||
} | ||
|
||
void Logger::retainDefaultMessageHandler(QtMessageHandler handler){ | ||
gd_logger->defaultMessageHandler=handler; | ||
} | ||
|
||
void Logger::switchLoggingMethod(bool logToFile){ | ||
if(logToFile){ | ||
if(!gd_logger -> logFile.isOpen()){ | ||
gd_logger -> logFile.setFileName( Config::getConfigDir() + "gd_log.txt" ); | ||
if(!gd_logger -> logFile.open( QFile::WriteOnly )){ | ||
qDebug()<<"Failed to open log file!"; | ||
return; | ||
}; | ||
} | ||
qInstallMessageHandler(logToFileMessageHander); | ||
} else { | ||
if(gd_logger -> logFile.isOpen()){ | ||
gd_logger -> logFile.flush(); | ||
} | ||
qInstallMessageHandler(gd_logger->defaultMessageHandler); | ||
} | ||
} | ||
|
||
void Logger::closeLogFile(){ | ||
if(gd_logger -> logFile.isOpen()){ | ||
gd_logger -> logFile.flush(); | ||
gd_logger -> logFile.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
#include <QFile> | ||
#include <QMutex> | ||
|
||
/// Manage Logging, mainly for switching to log-to-file because of Windows | ||
struct Logging { | ||
static void retainDefaultMessageHandler(QtMessageHandler); | ||
static void switchLoggingMethod( bool logToFile ); | ||
static void closeLogFile(); | ||
QFile logFile; | ||
QtMessageHandler defaultMessageHandler; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,19 +2,15 @@ | |
/* This file is (c) 2008-2012 Konstantin Isakov <[email protected]> | ||
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ | ||
|
||
#include "logfileptr.hh" | ||
#include "log_to_file.hh" | ||
#include "termination.hh" | ||
#include <QDebug> | ||
#include <exception> | ||
|
||
static void termHandler() | ||
{ | ||
qDebug() << "GoldenDict has crashed unexpectedly.\n\n"; | ||
|
||
if ( logFilePtr && logFilePtr->isOpen() ) { | ||
logFilePtr->close(); | ||
} | ||
|
||
Logger::closeLogFile(); | ||
abort(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters