Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 #include <QtDebug>
21 
22 int QgsLogger::mDebugLevel = -999; // undefined value
23 
24 void QgsLogger::debug( const QString& msg, int debuglevel, const char* file, const char* function, int line )
25 {
26  const char* dfile = debugFile();
27  if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
28  {
29  if ( !file || strcmp( dfile, file ) != 0 )
30  {
31  return;
32  }
33  }
34 
35  int dlevel = debugLevel();
36  if ( dlevel >= debuglevel && debuglevel > 0 )
37  {
38  if ( file == NULL )
39  {
40  qDebug( "%s", msg.toLocal8Bit().constData() );
41  }
42  else if ( function == NULL )
43  {
44  qDebug( "%s: %s", file, msg.toLocal8Bit().constData() );
45  }
46  else if ( line == -1 )
47  {
48  qDebug( "%s: (%s) %s", file, function, msg.toLocal8Bit().constData() );
49  }
50  else
51  {
52 #ifndef _MSC_VER
53  qDebug( "%s: %d: (%s) %s", file, line, function, msg.toLocal8Bit().constData() );
54 #else
55  qDebug( "%s(%d) : (%s) %s", file, line, function, msg.toLocal8Bit().constData() );
56 #endif
57  }
58  }
59 }
60 
61 void QgsLogger::debug( const QString& var, int val, int debuglevel, const char* file, const char* function, int line )
62 {
63  const char* dfile = debugFile();
64  if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
65  {
66  if ( !file || strcmp( dfile, file ) != 0 )
67  {
68  return;
69  }
70  }
71 
72  int dlevel = debugLevel();
73  if ( dlevel >= debuglevel && debuglevel > 0 )
74  {
75  if ( file == NULL )
76  {
77  qDebug( "%s: %d", var.toLocal8Bit().constData(), val );
78  }
79  else if ( function == NULL )
80  {
81  qDebug( "%s: %s: %d", file, var.toLocal8Bit().constData(), val );
82  }
83  else if ( line == -1 )
84  {
85  qDebug( "%s: (%s): %s: %d", file, function, var.toLocal8Bit().constData(), val );
86  }
87  else
88  {
89 #ifdef _MSC_VER
90  qDebug( "%s(%d): (%s), %s: %d", file, line, function, var.toLocal8Bit().constData(), val );
91 #else
92  qDebug( "%s: %d: (%s), %s: %d", file, line, function, var.toLocal8Bit().constData(), val );
93 #endif
94  }
95  }
96 }
97 
98 void QgsLogger::debug( const QString& var, double val, int debuglevel, const char* file, const char* function, int line )
99 {
100  const char* dfile = debugFile();
101  if ( dfile ) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file
102  {
103  if ( !file || strcmp( dfile, file ) != 0 )
104  {
105  return;
106  }
107  }
108 
109  int dlevel = debugLevel();
110  if ( dlevel >= debuglevel && debuglevel > 0 )
111  {
112  if ( file == NULL )
113  {
114  qDebug( "%s: %f", var.toLocal8Bit().constData(), val );
115  }
116  else if ( function == NULL )
117  {
118  qDebug( "%s: %s: %f", file, var.toLocal8Bit().constData(), val );
119  }
120  else if ( line == -1 )
121  {
122  qDebug( "%s: (%s): %s: %f", file, function, var.toLocal8Bit().constData(), val );
123  }
124  else
125  {
126 #ifdef _MSC_VER
127  qDebug( "%s(%d): (%s), %s: %f", file, line, function, var.toLocal8Bit().constData(), val );
128 #else
129  qDebug( "%s: %d: (%s), %s: %f", file, line, function, var.toLocal8Bit().constData(), val );
130 #endif
131  }
132  }
133 }
134 
135 void QgsLogger::warning( const QString& msg )
136 {
137  qWarning( "%s", msg.toLocal8Bit().constData() );
138 }
139 
140 void QgsLogger::critical( const QString& msg )
141 {
142  qCritical( "%s", msg.toLocal8Bit().constData() );
143 }
144 
145 void QgsLogger::fatal( const QString& msg )
146 {
147  qFatal( "%s", msg.toLocal8Bit().constData() );
148 }
149 
151 {
152  if ( mDebugLevel == -999 )
153  {
154  // read the environment variable QGIS_DEBUG just once,
155  // then reuse the value
156 
157  const char* dlevel = getenv( "QGIS_DEBUG" );
158  if ( dlevel == NULL ) //environment variable not set
159  {
160 #ifdef QGISDEBUG
161  mDebugLevel = 1; //1 is default value in debug mode
162 #else
163  mDebugLevel = 0;
164 #endif
165  }
166  else
167  {
168  mDebugLevel = atoi( dlevel );
169 #ifdef QGISDEBUG
170  if ( mDebugLevel == 0 )
171  {
172  mDebugLevel = 1;
173  }
174 #endif
175  }
176  }
177 
178  return mDebugLevel;
179 }
180 
181 const char* QgsLogger::debugFile()
182 {
183  const char* dfile = getenv( "QGIS_DEBUG_FILE" );
184  return dfile;
185 }