QGIS API Documentation  2.0.1-Dufour
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgserror.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgserror.cpp - Error container
3  -------------------
4  begin : October 2012
5  copyright : (C) 2012 Radim Blazek
6  email : radim dot blazek at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 #include "qgis.h"
18 #include "qgsversion.h"
19 #include "qgsconfig.h"
20 #include "qgserror.h"
21 #include "qgslogger.h"
22 
23 #include <QRegExp>
24 
25 QgsErrorMessage::QgsErrorMessage( const QString & theMessage, const QString & theTag, const QString & theFile, const QString & theFunction, int theLine )
26  : mMessage( theMessage )
27  , mTag( theTag )
28  , mFile( theFile )
29  , mFunction( theFunction )
30  , mLine( theLine )
31  , mFormat( Text )
32 {
33 }
34 
35 QgsError::QgsError( const QString & theMessage, const QString & theTag )
36 {
37  append( theMessage, theTag );
38 }
39 
40 void QgsError::append( const QString & theMessage, const QString & theTag )
41 {
42  mMessageList.append( QgsErrorMessage( theMessage, theTag ) );
43 }
44 
45 void QgsError::append( const QgsErrorMessage & theMessage )
46 {
47  mMessageList.append( theMessage );
48 }
49 
50 QString QgsError::message( QgsErrorMessage::Format theFormat ) const
51 {
52  QString str;
53 
54  QString srcUrl;
55 #if defined(QGISDEBUG) && defined(QGS_GIT_REMOTE_URL)
56  // TODO: verify if we are not ahead to origin (remote hash does not exist)
57  // and there are no local not commited changes
58  QString hash = QString( QGis::QGIS_DEV_VERSION );
59  QString remote = QString( QGS_GIT_REMOTE_URL );
60  QgsDebugMsg( "remote = " + remote );
61  if ( !hash.isEmpty() && !remote.isEmpty() && remote.contains( "github.com" ) )
62  {
63  QString path = remote.remove( QRegExp( ".*github.com[:/]" ) ).remove( ".git" );
64  srcUrl = "https://github.com/" + path + "/blob/" + hash;
65  }
66 #endif
67 
68  foreach ( QgsErrorMessage m, mMessageList )
69  {
70 #ifdef QGISDEBUG
71  QString file;
72 #ifndef _MSC_VER
73  int sPrefixLength = strlen( CMAKE_SOURCE_DIR ) + 1;
74  file = m.file().mid( sPrefixLength );
75 #else
76  file = m.file();
77 #endif
78 #endif
79 
80  if ( theFormat == QgsErrorMessage::Text )
81  {
82  str += m.tag() + " " + m.message();
83 #ifdef QGISDEBUG
84  str += QString( "\nat %1 : %2 : %3" ).arg( file ).arg( m.line() ).arg( m.function() );
85 #endif
86  }
87  else // QgsErrorMessage::Html
88  {
89  str += "<p><b>" + m.tag() + ":</b> " + m.message();
90 #ifdef QGISDEBUG
91  QString location = QString( "%1 : %2 : %3" ).arg( file ).arg( m.line() ).arg( m.function() );
92  if ( !srcUrl.isEmpty() )
93  {
94  QString url = QString( "%1/%2#L%3" ).arg( srcUrl ).arg( file ).arg( m.line() );
95  str += QString( "<br>(<a href='%1'>%2</a>)" ).arg( url ).arg( location );
96  }
97  else
98  {
99  str += QString( "<br>(%1)" ).arg( location );
100  }
101 #endif
102  }
103  }
104  return str;
105 }
106 
107 QString QgsError::summary( ) const
108 {
109  // The first message in chain is usually the real error given by backend/server
110  return mMessageList.first().message();
111 }