kmobiletools
errorhandler.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "errorhandler.h"
00021 #include "errorlog.h"
00022
00023 #include <KMessageBox>
00024 #include <KPassivePopup>
00025 #include <KGlobal>
00026 #include <QMutex>
00027
00028 namespace KMobileTools {
00029
00030 class ErrorHandlerInstance {
00031 public:
00032 ErrorHandler m_uniqueInstance;
00033 };
00034
00035 class ErrorHandlerPrivate {
00036 public:
00037 QMutex m_mutex;
00038 QStack<const BaseError*> m_errorStack;
00039
00040 ~ErrorHandlerPrivate() {
00041 qDeleteAll( m_errorStack.begin(), m_errorStack.end() );
00042 }
00043 };
00044
00045 K_GLOBAL_STATIC(ErrorHandlerInstance, errorHandlerInstance)
00046
00047 ErrorHandler::ErrorHandler()
00048 : QObject( 0 ), d( new ErrorHandlerPrivate )
00049 {
00050 }
00051
00052
00053 ErrorHandler::~ErrorHandler()
00054 {
00055 }
00056
00057 ErrorHandler* ErrorHandler::instance() {
00058
00059 return &errorHandlerInstance->m_uniqueInstance;
00060 }
00061
00062 void ErrorHandler::addError( const BaseError* error ) {
00064 d->m_mutex.lock();
00065
00066
00067 if( !d->m_errorStack.isEmpty() ) {
00068 if( *(d->m_errorStack.top()) != *error )
00069 d->m_errorStack.push( error );
00070 } else
00071 d->m_errorStack.push( error );
00072
00073 displayError( error );
00074 writeToLog( error );
00075
00076 d->m_mutex.unlock();
00077 }
00078
00079 int ErrorHandler::errorCount() const {
00080 return d->m_errorStack.count();
00081 }
00082
00083 QStack<const BaseError*> ErrorHandler::errorStack() {
00084 return d->m_errorStack;
00085 }
00086
00087 void ErrorHandler::displayError( const BaseError* error ) {
00088 if( receivers( SIGNAL(errorOccurred(QString,BaseError::Priority)) ) > 0 )
00089 emit errorOccurred( error->description(), error->priority() );
00090 else
00091 KMessageBox::error( 0, error->description() );
00092 }
00093
00094 void ErrorHandler::writeToLog( const BaseError* error ) {
00095 ErrorLog* errorLog = ErrorLog::instance();
00096
00097 errorLog->write( QString( "===============================" ) );
00098 errorLog->write( QString( "Occurred on %1").arg( error->dateTime().toString( Qt::ISODate ) ) );
00099 errorLog->write( QString( "Location: %1:%2" ).arg( error->fileName() )
00100 .arg( QString::number( error->lineNumber() ) ) );
00101 errorLog->write( QString( "Method: %1()" ).arg( error->methodName() ) );
00102 errorLog->write( QString( "Priority: %1" ).arg( error->priority() ) );
00103 errorLog->write( QString( "Description: %1" ).arg( error->description() ) );
00104 }
00105
00106 }
00107
00108 #include "errorhandler.moc"