QGIS API Documentation  2.15.0-Master (02a0ebe)
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 
51 {
52  QString str;
53 
54 #ifdef QGISDEBUG
55  QString srcUrl;
56 #endif
57 
58 #if defined(QGISDEBUG) && defined(QGS_GIT_REMOTE_URL)
59  // TODO: verify if we are not ahead to origin (remote hash does not exist)
60  // and there are no local not commited changes
62  QString remote = QString( QGS_GIT_REMOTE_URL );
63  if ( !hash.isEmpty() && !remote.isEmpty() && remote.contains( "github.com" ) )
64  {
65  QString path = remote.remove( QRegExp( ".*github.com[:/]" ) ).remove( ".git" );
66  srcUrl = "https://github.com/" + path + "/blob/" + hash;
67  }
68 #endif
69 
70  Q_FOREACH ( const QgsErrorMessage& m, mMessageList )
71  {
72 #ifdef QGISDEBUG
73  QString file;
74 #ifndef _MSC_VER
75  int sPrefixLength = strlen( CMAKE_SOURCE_DIR ) + 1;
76  file = m.file().mid( sPrefixLength );
77 #else
78  file = m.file();
79 #endif
80 #endif
81 
82  if ( theFormat == QgsErrorMessage::Text )
83  {
84  if ( !str.isEmpty() )
85  {
86  str += '\n'; // new message
87  }
88  if ( !m.tag().isEmpty() )
89  {
90  str += m.tag() + ' ';
91  }
92  str += m.message();
93 #ifdef QGISDEBUG
94  QString where;
95  if ( !file.isEmpty() )
96  {
97  where += QString( "file: %1 row: %2" ).arg( file ).arg( m.line() );
98  }
99  if ( !m.function().isEmpty() )
100  {
101  where += QString( "function %1:" ).arg( m.function() );
102  }
103  if ( !where.isEmpty() )
104  {
105  str += QString( " (%1)" ).arg( where );
106  }
107 #endif
108  }
109  else // QgsErrorMessage::Html
110  {
111  str += "<p><b>" + m.tag() + ":</b> " + m.message();
112 #ifdef QGISDEBUG
113  QString location = QString( "%1 : %2 : %3" ).arg( file ).arg( m.line() ).arg( m.function() );
114  if ( !srcUrl.isEmpty() )
115  {
116  QString url = QString( "%1/%2#L%3" ).arg( srcUrl, file ).arg( m.line() );
117  str += QString( "<br>(<a href='%1'>%2</a>)" ).arg( url, location );
118  }
119  else
120  {
121  str += QString( "<br>(%1)" ).arg( location );
122  }
123 #endif
124  }
125  }
126  return str;
127 }
128 
130 {
131  // The first message in chain is usually the real error given by backend/server
132  return mMessageList.first().message();
133 }
QString & remove(int position, int n)
Format
Format.
Definition: qgserror.h:33
static const char * QGIS_DEV_VERSION
Definition: qgis.h:52
bool isEmpty() const
QgsErrorMessage represents single error message.
Definition: qgserror.h:29
int line() const
Definition: qgserror.h:57
QString message(QgsErrorMessage::Format theFormat=QgsErrorMessage::Html) const
Full error messages description.
Definition: qgserror.cpp:50
QString tag() const
Definition: qgserror.h:54
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QString function() const
Definition: qgserror.h:56
QString mid(int position, int n) const
void append(const QString &theMessage, const QString &theTag)
Append new error message.
Definition: qgserror.cpp:40
QgsError()
Definition: qgserror.h:84
QString file() const
Definition: qgserror.h:55
QString summary() const
Short error description, usually the first error in chain, the real error.
Definition: qgserror.cpp:129
QString message() const
Definition: qgserror.h:53
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const