ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

debug.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/debug.h"
00024 #include "common/debug-channels.h"
00025 #include "common/system.h"
00026 #include "common/textconsole.h"
00027 #include "common/algorithm.h"
00028 
00029 #include <stdarg.h> // For va_list etc.
00030 
00031 // TODO: Move gDebugLevel into namespace Common.
00032 int gDebugLevel = -1;
00033 bool gDebugChannelsOnly = false;
00034 
00035 namespace Common {
00036 
00037 DECLARE_SINGLETON(DebugManager);
00038 
00039 namespace {
00040 
00041 struct DebugLevelComperator {
00042     bool operator()(const DebugManager::DebugChannel &l, const DebugManager::DebugChannel &r) {
00043         return (l.name.compareToIgnoreCase(r.name) < 0);
00044     }
00045 };
00046 
00047 } // end of anonymous namespace
00048 
00049 bool DebugManager::addDebugChannel(uint32 channel, const String &name, const String &description) {
00050     if (name.equalsIgnoreCase("all")) {
00051         warning("Debug channel 'all' is reserved for internal use");
00052         return false;
00053     }
00054 
00055     if (gDebugChannels.contains(name))
00056         warning("Duplicate declaration of engine debug channel '%s'", name.c_str());
00057 
00058     gDebugChannels[name] = DebugChannel(channel, name, description);
00059 
00060     return true;
00061 }
00062 
00063 void DebugManager::clearAllDebugChannels() {
00064     gDebugChannelsEnabled = 0;
00065     gDebugChannels.clear();
00066 }
00067 
00068 bool DebugManager::enableDebugChannel(const String &name) {
00069     DebugChannelMap::iterator i = gDebugChannels.find(name);
00070 
00071     if (i != gDebugChannels.end()) {
00072         gDebugChannelsEnabled |= i->_value.channel;
00073         i->_value.enabled = true;
00074 
00075         return true;
00076     } else {
00077         return false;
00078     }
00079 }
00080 
00081 bool DebugManager::enableDebugChannel(uint32 channel) {
00082     gDebugChannelsEnabled |= channel;
00083     return true;
00084 }
00085 
00086 bool DebugManager::disableDebugChannel(const String &name) {
00087     DebugChannelMap::iterator i = gDebugChannels.find(name);
00088 
00089     if (i != gDebugChannels.end()) {
00090         gDebugChannelsEnabled &= ~i->_value.channel;
00091         i->_value.enabled = false;
00092 
00093         return true;
00094     } else {
00095         return false;
00096     }
00097 }
00098 
00099 bool DebugManager::disableDebugChannel(uint32 channel) {
00100     gDebugChannelsEnabled &= ~channel;
00101     return true;
00102 }
00103 
00104 DebugManager::DebugChannelList DebugManager::listDebugChannels() {
00105     DebugChannelList tmp;
00106     for (DebugChannelMap::iterator i = gDebugChannels.begin(); i != gDebugChannels.end(); ++i)
00107         tmp.push_back(i->_value);
00108     sort(tmp.begin(), tmp.end(), DebugLevelComperator());
00109 
00110     return tmp;
00111 }
00112 
00113 void DebugManager::enableAllDebugChannels() {
00114     for (DebugChannelMap::iterator i = gDebugChannels.begin(); i != gDebugChannels.end(); ++i)
00115         enableDebugChannel(i->_value.name);
00116 }
00117 
00118 void DebugManager::disableAllDebugChannels() {
00119     for (DebugChannelMap::iterator i = gDebugChannels.begin(); i != gDebugChannels.end(); ++i)
00120         disableDebugChannel(i->_value.name);
00121 }
00122 
00123 bool DebugManager::isDebugChannelEnabled(uint32 channel, bool enforce) {
00124     // Debug level 11 turns on all special debug level messages
00125     if (gDebugLevel == 11 && enforce == false)
00126         return true;
00127     else
00128         return (gDebugChannelsEnabled & channel) != 0;
00129 }
00130 
00131 } // End of namespace Common
00132 
00133 bool debugLevelSet(int level) {
00134     return level <= gDebugLevel;
00135 }
00136 
00137 bool debugChannelSet(int level, uint32 debugChannels) {
00138     if (gDebugLevel != 11 || level == -1)
00139         if ((level != -1 && level > gDebugLevel) || !(DebugMan.isDebugChannelEnabled(debugChannels, level == -1)))
00140             return false;
00141 
00142     return true;
00143 }
00144 
00145 
00146 #ifndef DISABLE_TEXT_CONSOLE
00147 
00148 static void debugHelper(const char *s, va_list va, bool caret = true) {
00149     Common::String buf = Common::String::vformat(s, va);
00150 
00151     if (caret)
00152         buf += '\n';
00153 
00154     if (g_system)
00155         g_system->logMessage(LogMessageType::kDebug, buf.c_str());
00156     // TODO: Think of a good fallback in case we do not have
00157     // any OSystem yet.
00158 }
00159 
00160 void debug(const char *s, ...) {
00161     va_list va;
00162 
00163     if (gDebugChannelsOnly)
00164         return;
00165 
00166     va_start(va, s);
00167     debugHelper(s, va);
00168     va_end(va);
00169 }
00170 
00171 void debug(int level, const char *s, ...) {
00172     va_list va;
00173 
00174     if (level > gDebugLevel || gDebugChannelsOnly)
00175         return;
00176 
00177     va_start(va, s);
00178     debugHelper(s, va);
00179     va_end(va);
00180 
00181 }
00182 
00183 void debugN(const char *s, ...) {
00184     va_list va;
00185 
00186     if (gDebugChannelsOnly)
00187         return;
00188 
00189     va_start(va, s);
00190     debugHelper(s, va, false);
00191     va_end(va);
00192 }
00193 
00194 void debugN(int level, const char *s, ...) {
00195     va_list va;
00196 
00197     if (level > gDebugLevel || gDebugChannelsOnly)
00198         return;
00199 
00200     va_start(va, s);
00201     debugHelper(s, va, false);
00202     va_end(va);
00203 }
00204 
00205 void debugC(int level, uint32 debugChannels, const char *s, ...) {
00206     va_list va;
00207 
00208     // Debug level 11 turns on all special debug level messages
00209     if (gDebugLevel != 11)
00210         if (level > gDebugLevel || !(DebugMan.isDebugChannelEnabled(debugChannels)))
00211             return;
00212 
00213     va_start(va, s);
00214     debugHelper(s, va);
00215     va_end(va);
00216 }
00217 
00218 void debugCN(int level, uint32 debugChannels, const char *s, ...) {
00219     va_list va;
00220 
00221     // Debug level 11 turns on all special debug level messages
00222     if (gDebugLevel != 11)
00223         if (level > gDebugLevel || !(DebugMan.isDebugChannelEnabled(debugChannels)))
00224             return;
00225 
00226     va_start(va, s);
00227     debugHelper(s, va, false);
00228     va_end(va);
00229 }
00230 
00231 void debugC(uint32 debugChannels, const char *s, ...) {
00232     va_list va;
00233 
00234     // Debug level 11 turns on all special debug level messages
00235     if (gDebugLevel != 11)
00236         if (!(DebugMan.isDebugChannelEnabled(debugChannels)))
00237             return;
00238 
00239     va_start(va, s);
00240     debugHelper(s, va);
00241     va_end(va);
00242 }
00243 
00244 void debugCN(uint32 debugChannels, const char *s, ...) {
00245     va_list va;
00246 
00247     // Debug level 11 turns on all special debug level messages
00248     if (gDebugLevel != 11)
00249         if (!(DebugMan.isDebugChannelEnabled(debugChannels)))
00250             return;
00251 
00252     va_start(va, s);
00253     debugHelper(s, va, false);
00254     va_end(va);
00255 }
00256 
00257 #endif


Generated on Sat Apr 4 2020 05:00:27 for ResidualVM by doxygen 1.7.1
curved edge   curved edge