|
| 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 | +void QgsLogger::debug(const QString& msg, int debuglevel, const char* file, const char* function, int line) |
| 22 | +{ |
| 23 | + const char* dfile = debugFile(); |
| 24 | + if(dfile) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file |
| 25 | + { |
| 26 | + if(!file || strcmp(dfile, file) != 0) |
| 27 | + { |
| 28 | + return; |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + int dlevel = debugLevel(); |
| 33 | + if(dlevel >= debuglevel && debuglevel > 0) |
| 34 | + { |
| 35 | + if(file == NULL) |
| 36 | + { |
| 37 | + qDebug(msg.toLocal8Bit().data()); |
| 38 | + } |
| 39 | + else if(function == NULL) |
| 40 | + { |
| 41 | + qDebug("File: %s, Message: %s", file, msg.toLocal8Bit().data()); |
| 42 | + } |
| 43 | + else if(line == -1) |
| 44 | + { |
| 45 | + qDebug("File: %s, Function: %s, Message: %s", file, function, msg.toLocal8Bit().data()); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + qDebug("File: %s, Function: %s, Line: %d, Message: %s", file, function, line, msg.toLocal8Bit().data()); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void QgsLogger::debug(const QString& var, int val, int debuglevel, const char* file, const char* function, int line) |
| 55 | +{ |
| 56 | + const char* dfile = debugFile(); |
| 57 | + if(dfile) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file |
| 58 | + { |
| 59 | + if(!file || strcmp(dfile, file) != 0) |
| 60 | + { |
| 61 | + return; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + int dlevel = debugLevel(); |
| 66 | + if(dlevel >= debuglevel && debuglevel > 0) |
| 67 | + { |
| 68 | + if(file == NULL) |
| 69 | + { |
| 70 | + qDebug("Variable: %s, Value: %d", var.toLocal8Bit().data(), val); |
| 71 | + } |
| 72 | + else if(function == NULL) |
| 73 | + { |
| 74 | + qDebug("File: %s, Variable: %s, Value: %d", file, var.toLocal8Bit().data(), val); |
| 75 | + } |
| 76 | + else if(line == -1) |
| 77 | + { |
| 78 | + qDebug("File: %s, Function: %s, Variable: %s, Value: %d", file, function, var.toLocal8Bit().data(), val); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + qDebug("File: %s, Function: %s, Line: %d, Variable: %s, Value: %d", file, function, line, var.toLocal8Bit().data(), val); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +void QgsLogger::debug(const QString& var, double val, int debuglevel, const char* file, const char* function, int line) |
| 88 | +{ |
| 89 | + const char* dfile = debugFile(); |
| 90 | + if(dfile) //exit if QGIS_DEBUG_FILE is set and the message comes from the wrong file |
| 91 | + { |
| 92 | + if(!file || strcmp(dfile, file) != 0) |
| 93 | + { |
| 94 | + return; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + int dlevel = debugLevel(); |
| 99 | + if(dlevel >= debuglevel && debuglevel > 0) |
| 100 | + { |
| 101 | + if(file == NULL) |
| 102 | + { |
| 103 | + qDebug("Variable: %s, Value: %f", var.toLocal8Bit().data(), val); |
| 104 | + } |
| 105 | + else if(function == NULL) |
| 106 | + { |
| 107 | + qDebug("File: %s, Variable: %s, Value: %f", file, var.toLocal8Bit().data(), val); |
| 108 | + } |
| 109 | + else if(line == -1) |
| 110 | + { |
| 111 | + qDebug("File: %s, Function: %s, Variable: %s, Value: %f", file, function, var.toLocal8Bit().data(), val); |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + qDebug("File: %s, Function: %s, Line: %d, Variable: %s, Value: %f", file, function, line, var.toLocal8Bit().data(), val); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +void QgsLogger::warning(const QString& msg) |
| 121 | +{ |
| 122 | + qWarning(msg.toLocal8Bit().data()); |
| 123 | +} |
| 124 | + |
| 125 | +void QgsLogger::critical(const QString& msg) |
| 126 | +{ |
| 127 | + qCritical(msg.toLocal8Bit().data()); |
| 128 | +} |
| 129 | + |
| 130 | +void QgsLogger::fatal(const QString& msg) |
| 131 | +{ |
| 132 | + qFatal(msg.toLocal8Bit().data()); |
| 133 | +} |
| 134 | + |
| 135 | +int QgsLogger::debugLevel() |
| 136 | +{ |
| 137 | + const char* dlevel = getenv("QGIS_DEBUG"); |
| 138 | + if(dlevel == NULL) //environment variable not set |
| 139 | + { |
| 140 | +#ifdef QGISDEBUG |
| 141 | + return 1; //1 is default value in debug mode |
| 142 | +#else |
| 143 | + return 0; |
| 144 | +#endif |
| 145 | + } |
| 146 | + int level = atoi(dlevel); |
| 147 | +#ifdef QGISDEBUG |
| 148 | + if(level == 0) |
| 149 | + { |
| 150 | + level = 1; |
| 151 | + } |
| 152 | +#endif |
| 153 | + return level; |
| 154 | +} |
| 155 | + |
| 156 | +const char* QgsLogger::debugFile() |
| 157 | +{ |
| 158 | + const char* dfile = getenv("QGIS_DEBUG_FILE"); |
| 159 | + return dfile; |
| 160 | +} |
0 commit comments