QGIS API Documentation  2.2.0-Valmiera
 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  if ( !hash.isEmpty() && !remote.isEmpty() && remote.contains( "github.com" ) )
61  {
62  QString path = remote.remove( QRegExp( ".*github.com[:/]" ) ).remove( ".git" );
63  srcUrl = "https://github.com/" + path + "/blob/" + hash;
64  }
65 #endif
66 
67  foreach ( QgsErrorMessage m, mMessageList )
68  {
69 #ifdef QGISDEBUG
70  QString file;
71 #ifndef _MSC_VER
72  int sPrefixLength = strlen( CMAKE_SOURCE_DIR ) + 1;
73  file = m.file().mid( sPrefixLength );
74 #else
75  file = m.file();
76 #endif
77 #endif
78 
79  if ( theFormat == QgsErrorMessage::Text )
80  {
81  if ( !str.isEmpty() )
82  {
83  str += "\n"; // new message
84  }
85  if ( !m.tag().isEmpty() )
86  {
87  str += m.tag() + " ";
88  }
89  str += m.message();
90 #ifdef QGISDEBUG
91  QString where;
92  if ( !file.isEmpty() )
93  {
94  where += QString( "file: %1 row: %2" ).arg( file ).arg( m.line() );
95  }
96  if ( !m.function().isEmpty() )
97  {
98  where += QString( "function %1:" ).arg( m.function() );
99  }
100  if ( !where.isEmpty() )
101  {
102  str += QString( " (%1)" ).arg( where );
103  }
104 #endif
105  }
106  else // QgsErrorMessage::Html
107  {
108  str += "<p><b>" + m.tag() + ":</b> " + m.message();
109 #ifdef QGISDEBUG
110  QString location = QString( "%1 : %2 : %3" ).arg( file ).arg( m.line() ).arg( m.function() );
111  if ( !srcUrl.isEmpty() )
112  {
113  QString url = QString( "%1/%2#L%3" ).arg( srcUrl ).arg( file ).arg( m.line() );
114  str += QString( "<br>(<a href='%1'>%2</a>)" ).arg( url ).arg( location );
115  }
116  else
117  {
118  str += QString( "<br>(%1)" ).arg( location );
119  }
120 #endif
121  }
122  }
123  return str;
124 }
125 
126 QString QgsError::summary( ) const
127 {
128  // The first message in chain is usually the real error given by backend/server
129  return mMessageList.first().message();
130 }