QGIS API Documentation  2.8.2-Wien
 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 <QApplication>
22 #include <QtDebug>
23 #include <QFile>
24 #include <QThread>
25 
26 #include "qgsconfig.h"
27 
28 #ifndef CMAKE_SOURCE_DIR
29 #error CMAKE_SOURCE_DIR undefined
30 #endif // CMAKE_SOURCE_DIR
31 
32 int QgsLogger::sDebugLevel = -999; // undefined value
33 int QgsLogger::sPrefixLength = -1;
34 QString QgsLogger::sFileFilter;
35 QString QgsLogger::sLogFile;
36 QTime QgsLogger::sTime;
37 
38 void QgsLogger::init()
39 {
40  if ( sDebugLevel != -999 )
41  return;
42 
43  sTime.start();
44 
45  sLogFile = getenv( "QGIS_LOG_FILE" ) ? getenv( "QGIS_LOG_FILE" ) : "";
46  sFileFilter = getenv( "QGIS_DEBUG_FILE" ) ? getenv( "QGIS_DEBUG_FILE" ) : "";
47  sDebugLevel = getenv( "QGIS_DEBUG" ) ? atoi( getenv( "QGIS_DEBUG" ) ) :
48 #ifdef QGISDEBUG
49  1
50 #else
51  0
52 #endif
53  ;
54 
55  sPrefixLength = sizeof( CMAKE_SOURCE_DIR );
56  if ( CMAKE_SOURCE_DIR[sPrefixLength-1] == '/' )
57  sPrefixLength++;
58 }
59 
60 void QgsLogger::debug( const QString& msg, int debuglevel, const char* file, const char* function, int line )
61 {
62  init();
63 
64  if ( !file && !sFileFilter.isEmpty() && !sFileFilter.endsWith( file ) )
65  return;
66 
67  if ( sDebugLevel == 0 || debuglevel > sDebugLevel )
68  return;
69 
70 
71  QString m = msg;
72 
73  if ( file )
74  {
75  if ( qApp && qApp->thread() != QThread::currentThread() )
76  {
77  m.prepend( QString( "[thread:0x%1] " ).arg(( qint64 ) QThread::currentThread(), 0, 16 ) );
78  }
79 
80  m.prepend( QString( "[%1ms] " ).arg( sTime.elapsed() ) );
81  sTime.restart();
82 
83  if ( function )
84  {
85  m.prepend( QString( " (%1) " ).arg( function ) );
86  }
87 
88  if ( line != -1 )
89  {
90 #ifndef _MSC_VER
91  m.prepend( QString( ": %1:" ).arg( line ) );
92 #else
93  m.prepend( QString( "(%1) :" ).arg( line ) );
94 #endif
95  }
96 
97 #ifndef _MSC_VER
98  m.prepend( file + sPrefixLength );
99 #else
100  m.prepend( file );
101 #endif
102  }
103 
104  if ( sLogFile.isEmpty() )
105  {
106  qDebug( "%s", m.toLocal8Bit().constData() );
107  }
108  else
109  {
110  logMessageToFile( m );
111  }
112 }
113 
114 void QgsLogger::debug( const QString& var, int val, int debuglevel, const char* file, const char* function, int line )
115 {
116  debug( QString( "%1: %2" ).arg( var ).arg( val ), debuglevel, file, function, line );
117 }
118 
119 void QgsLogger::debug( const QString& var, double val, int debuglevel, const char* file, const char* function, int line )
120 {
121  debug( QString( "%1: %2" ).arg( var ).arg( val ), debuglevel, file, function, line );
122 }
123 
124 void QgsLogger::warning( const QString& msg )
125 {
126  logMessageToFile( msg );
127  qWarning( "%s", msg.toLocal8Bit().constData() );
128 }
129 
130 void QgsLogger::critical( const QString& msg )
131 {
132  logMessageToFile( msg );
133  qCritical( "%s", msg.toLocal8Bit().constData() );
134 }
135 
136 void QgsLogger::fatal( const QString& msg )
137 {
138  logMessageToFile( msg );
139  qFatal( "%s", msg.toLocal8Bit().constData() );
140 }
141 
142 void QgsLogger::logMessageToFile( QString theMessage )
143 {
144  if ( sLogFile.isEmpty() )
145  return;
146 
147  //Maybe more efficient to keep the file open for the life of qgis...
148  QFile file( sLogFile );
149  if ( !file.open( QIODevice::Append ) )
150  return;
151  file.write( theMessage.toLocal8Bit().constData() );
152  file.write( "\n" );
153  file.close();
154 }