QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgslogger.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgslogger.cpp - description
3  -------------------
4  begin : April 2006
5  copyright : (C) 2006 by Marco Hugentobler
6  email : marco.hugentobler at karto dot baug dot ethz dot ch
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 
18 
19 #include "qgslogger.h"
20 
21 #include <QtDebug>
22 #include <QFile>
23 
24 #include "qgsconfig.h"
25 
26 #ifndef CMAKE_SOURCE_DIR
27 #error CMAKE_SOURCE_DIR undefinied
28 #endif // CMAKE_SOURCE_DIR
29 
30 int QgsLogger::sDebugLevel = -999; // undefined value
32 
33 void QgsLogger::debug( const QString& msg, int debuglevel, const char* file, const char* function, int line )
34 {
35  const char* dfile = debugFile();
36  if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
37  {
38  if ( !file || strncmp( dfile, file, strlen( dfile ) ) != 0 )
39  {
40  return;
41  }
42  }
43 
44  int dlevel = debugLevel();
45  if ( dlevel >= debuglevel && debuglevel > 0 )
46  {
47  QString m;
48 
49  if ( !file )
50  {
51  m = msg;
52  }
53  else if ( !function )
54  {
55  m = QString( "%1: %2" ).arg( file + sPrefixLength ).arg( msg );
56  }
57  else if ( line == -1 )
58  {
59  m = QString( "%1: (%2) %3" ).arg( file + sPrefixLength ).arg( function ).arg( msg );
60  }
61  else
62  {
63 #ifndef _MSC_VER
64  m = QString( "%1: %2: (%3) %4" ).arg( file + sPrefixLength ).arg( line ).arg( function ).arg( msg );
65 #else
66  m = QString( "%1(%2) : (%3) %4" ).arg( file ).arg( line ).arg( function ).arg( msg );
67 #endif
68  }
69  if ( logFile().isEmpty() )
70  {
71  qDebug( "%s", m.toLocal8Bit().constData() );
72  }
73  else
74  {
75  logMessageToFile( m );
76  }
77  }
78 }
79 
80 void QgsLogger::debug( const QString& var, int val, int debuglevel, const char* file, const char* function, int line )
81 {
82  const char* dfile = debugFile();
83  if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
84  {
85  if ( !file || strncmp( dfile, file, strlen( dfile ) ) != 0 )
86  {
87  return;
88  }
89  }
90 
91  int dlevel = debugLevel();
92  if ( dlevel >= debuglevel && debuglevel > 0 )
93  {
94  if ( !file )
95  {
96  qDebug( "%s: %d", var.toLocal8Bit().constData(), val );
97  logMessageToFile( QString( "%s: %d" ).arg( var.toLocal8Bit().constData() ).arg( val ) );
98  }
99  else if ( !function )
100  {
101  qDebug( "%s: %s: %d", file + sPrefixLength, var.toLocal8Bit().constData(), val );
102  logMessageToFile( QString( "%s: %s: %d" ).arg( file + sPrefixLength ).arg( var.toLocal8Bit().constData() ).arg( val ) );
103  }
104  else if ( line == -1 )
105  {
106  qDebug( "%s: (%s): %s: %d", file + sPrefixLength, function, var.toLocal8Bit().constData(), val );
107  logMessageToFile( QString( "%s: (%s): %s: %d" ).arg( file + sPrefixLength ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) );
108  }
109  else
110  {
111 #ifdef _MSC_VER
112  qDebug( "%s(%d): (%s), %s: %d", file + sPrefixLength, line, function, var.toLocal8Bit().constData(), val );
113 #else
114  qDebug( "%s: %d: (%s), %s: %d", file + sPrefixLength, line, function, var.toLocal8Bit().constData(), val );
115 #endif
116  logMessageToFile( QString( "%s: %d: (%s), %s: %d" ).arg( file + sPrefixLength ).arg( line ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) );
117  }
118  }
119 }
120 
121 void QgsLogger::debug( const QString& var, double val, int debuglevel, const char* file, const char* function, int line )
122 {
123  const char* dfile = debugFile();
124  if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
125  {
126  if ( !file || strncmp( dfile, file, strlen( dfile ) ) != 0 )
127  {
128  return;
129  }
130  }
131 
132  int dlevel = debugLevel();
133  if ( dlevel >= debuglevel && debuglevel > 0 )
134  {
135  if ( !file )
136  {
137  qDebug( "%s: %f", var.toLocal8Bit().constData(), val );
138  logMessageToFile( QString( "%s: %f" ).arg( var.toLocal8Bit().constData() ).arg( val ) );
139  }
140  else if ( !function )
141  {
142  qDebug( "%s: %s: %f", file + sPrefixLength, var.toLocal8Bit().constData(), val );
143  logMessageToFile( QString( "%s: %s: %f" ).arg( file + sPrefixLength ).arg( var.toLocal8Bit().constData() ).arg( val ) );
144  }
145  else if ( line == -1 )
146  {
147  qDebug( "%s: (%s): %s: %f", file + sPrefixLength, function, var.toLocal8Bit().constData(), val );
148  logMessageToFile( QString( "%s: (%s): %s: %f" ).arg( file + sPrefixLength ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) );
149  }
150  else
151  {
152 #ifdef _MSC_VER
153  qDebug( "%s(%d): (%s), %s: %f", file + sPrefixLength, line, function, var.toLocal8Bit().constData(), val );
154 #else
155  qDebug( "%s: %d: (%s), %s: %f", file + sPrefixLength, line, function, var.toLocal8Bit().constData(), val );
156 #endif
157  logMessageToFile( QString( "%s: %d: (%s), %s: %f" ).arg( file + sPrefixLength ).arg( line ).arg( function ).arg( var.toLocal8Bit().constData() ).arg( val ) );
158  }
159  }
160 }
161 
162 void QgsLogger::warning( const QString& msg )
163 {
164  logMessageToFile( msg );
165  qWarning( "%s", msg.toLocal8Bit().constData() );
166 }
167 
168 void QgsLogger::critical( const QString& msg )
169 {
170  logMessageToFile( msg );
171  qCritical( "%s", msg.toLocal8Bit().constData() );
172 }
173 
174 void QgsLogger::fatal( const QString& msg )
175 {
176  logMessageToFile( msg );
177  qFatal( "%s", msg.toLocal8Bit().constData() );
178 }
179 
181 {
182  if ( sPrefixLength == -1 )
183  {
184  sPrefixLength = sizeof( CMAKE_SOURCE_DIR );
185  if ( CMAKE_SOURCE_DIR[sPrefixLength-1] == '/' )
186  sPrefixLength++;
187  }
188 
189  if ( sDebugLevel == -999 )
190  {
191  // read the environment variable QGIS_DEBUG just once,
192  // then reuse the value
193 
194  const char* dlevel = getenv( "QGIS_DEBUG" );
195  if ( dlevel == NULL ) //environment variable not set
196  {
197 #ifdef QGISDEBUG
198  sDebugLevel = 1; //1 is default value in debug mode
199 #else
200  sDebugLevel = 0;
201 #endif
202  }
203  else
204  {
205  sDebugLevel = atoi( dlevel );
206 #ifdef QGISDEBUG
207  if ( sDebugLevel == 0 )
208  {
209  sDebugLevel = 1;
210  }
211 #endif
212  }
213  }
214 
215  return sDebugLevel;
216 }
217 
218 const QString QgsLogger::logFile()
219 {
220  const QString logFile = getenv( "QGIS_LOG_FILE" );
221  return logFile;
222 }
223 
224 void QgsLogger::logMessageToFile( QString theMessage )
225 {
226  if ( ! logFile().isEmpty() )
227  {
228  //Maybe more efficient to keep the file open for the life of qgis...
229  QFile file( logFile() );
230  file.open( QIODevice::Append );
231  file.write( theMessage.toStdString().c_str() );
232  file.write( "\n" );
233  file.close();
234  }
235  return;
236 }
237 
238 const char* QgsLogger::debugFile()
239 {
240  const char* dfile = getenv( "QGIS_DEBUG_FILE" );
241  return dfile;
242 }