-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: partially revert "Enable application log" to "Log to file"
- Loading branch information
1 parent
057380b
commit 0aedff4
Showing
9 changed files
with
97 additions
and
104 deletions.
There are no files selected for viewing
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 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,78 @@ | ||
#include "config.hh" | ||
#include "logger.hh" | ||
#include <QDateTime> | ||
#include <QFile> | ||
#include <QGlobalStatic> | ||
#include <QMutexLocker> | ||
|
||
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 |
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,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 |
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 "logger.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
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