Quantum GIS API Documentation  master-ce49b66
src/core/qgserror.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                          qgserror.cpp  -  Error container
00003                              -------------------
00004     begin                : October 2012
00005     copyright            : (C) 2012 Radim Blazek
00006     email                : radim dot blazek at gmail dot com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 #include "qgis.h"
00018 #include "qgsversion.h"
00019 #include "qgsconfig.h"
00020 #include "qgserror.h"
00021 #include "qgslogger.h"
00022 
00023 #include <QRegExp>
00024 
00025 QgsErrorMessage::QgsErrorMessage( const QString & theMessage, const QString & theTag, const QString & theFile, const QString & theFunction, int theLine )
00026     : mMessage( theMessage )
00027     , mTag( theTag )
00028     , mFile( theFile )
00029     , mFunction( theFunction )
00030     , mLine( theLine )
00031     , mFormat( Text )
00032 {
00033 }
00034 
00035 QgsError::QgsError( const QString & theMessage, const QString & theTag )
00036 {
00037   append( theMessage, theTag );
00038 }
00039 
00040 void QgsError::append( const QString & theMessage, const QString & theTag )
00041 {
00042   mMessageList.append( QgsErrorMessage( theMessage, theTag ) );
00043 }
00044 
00045 void QgsError::append( const QgsErrorMessage & theMessage )
00046 {
00047   mMessageList.append( theMessage );
00048 }
00049 
00050 QString QgsError::message( QgsErrorMessage::Format theFormat ) const
00051 {
00052   QString str;
00053 
00054   QString srcUrl;
00055 #if defined(QGISDEBUG) && defined(QGS_GIT_REMOTE_URL)
00056   // TODO: verify if we are not ahead to origin (remote hash does not exist)
00057   //       and there are no local not commited changes
00058   QString hash = QString( QGis::QGIS_DEV_VERSION );
00059   QString remote = QString( QGS_GIT_REMOTE_URL );
00060   QgsDebugMsg( "remote = " + remote );
00061   if ( !hash.isEmpty() && !remote.isEmpty() && remote.contains( "github.com" ) )
00062   {
00063     QString path = remote.remove( QRegExp( ".*github.com[:/]" ) ).remove( ".git" );
00064     srcUrl = "https://github.com/" + path + "/blob/" + hash;
00065   }
00066 #endif
00067 
00068   foreach ( QgsErrorMessage m, mMessageList )
00069   {
00070 #ifdef QGISDEBUG
00071     QString file;
00072 #ifndef _MSC_VER
00073     int sPrefixLength = strlen( CMAKE_SOURCE_DIR ) + 1;
00074     file = m.file().mid( sPrefixLength );
00075 #else
00076     file = m.file();
00077 #endif
00078 #endif
00079 
00080     if ( theFormat == QgsErrorMessage::Text )
00081     {
00082       str += m.tag() + " " + m.message();
00083 #ifdef QGISDEBUG
00084       str += QString( "\nat %1 : %2 : %3" ).arg( file ).arg( m.line() ).arg( m.function() );
00085 #endif
00086     }
00087     else // QgsErrorMessage::Html
00088     {
00089       str += "<p><b>" + m.tag() + ":</b> " + m.message();
00090 #ifdef QGISDEBUG
00091       QString location = QString( "%1 : %2 : %3" ).arg( file ).arg( m.line() ).arg( m.function() );
00092       if ( !srcUrl.isEmpty() )
00093       {
00094         QString url = QString( "%1/%2#L%3" ).arg( srcUrl ).arg( file ).arg( m.line() );
00095         str += QString( "<br>(<a href='%1'>%2</a>)" ).arg( url ).arg( location );
00096       }
00097       else
00098       {
00099         str += QString( "<br>(%1)" ).arg( location );
00100       }
00101 #endif
00102     }
00103   }
00104   return str;
00105 }
00106 
00107 QString QgsError::summary( ) const
00108 {
00109   // The first message in chain is usually the real error given by backend/server
00110   return mMessageList.first().message();
00111 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines