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

common/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::disableDebugChannel(const String &name) {
00082     DebugChannelMap::iterator i = gDebugChannels.find(name);
00083 
00084     if (i != gDebugChannels.end()) {
00085         gDebugChannelsEnabled &= ~i->_value.channel;
00086         i->_value.enabled = false;
00087 
00088         return true;
00089     } else {
00090         return false;
00091     }
00092 }
00093 
00094 DebugManager::DebugChannelList DebugManager::listDebugChannels() {
00095     DebugChannelList tmp;
00096     for (DebugChannelMap::iterator i = gDebugChannels.begin(); i != gDebugChannels.end(); ++i)
00097         tmp.push_back(i->_value);
00098     sort(tmp.begin(), tmp.end(), DebugLevelComperator());
00099 
00100     return tmp;
00101 }
00102 
00103 void DebugManager::enableAllDebugChannels() {
00104     for (DebugChannelMap::iterator i = gDebugChannels.begin(); i != gDebugChannels.end(); ++i)
00105         enableDebugChannel(i->_value.name);
00106 }
00107 
00108 void DebugManager::disableAllDebugChannels() {
00109     for (DebugChannelMap::iterator i = gDebugChannels.begin(); i != gDebugChannels.end(); ++i)
00110         disableDebugChannel(i->_value.name);
00111 }
00112 
00113 bool DebugManager::isDebugChannelEnabled(uint32 channel, bool enforce) {
00114     // Debug level 11 turns on all special debug level messages
00115     if (gDebugLevel == 11 && enforce == false)
00116         return true;
00117     else
00118         return (gDebugChannelsEnabled & channel) != 0;
00119 }
00120 
00121 } // End of namespace Common
00122 
00123 bool debugLevelSet(int level) {
00124     return level <= gDebugLevel;
00125 }
00126 
00127 bool debugChannelSet(int level, uint32 debugChannels) {
00128     if (gDebugLevel != 11 || level == -1)
00129         if ((level != -1 && level > gDebugLevel) || !(DebugMan.isDebugChannelEnabled(debugChannels, level == -1)))
00130             return false;
00131 
00132     return true;
00133 }
00134 
00135 
00136 #ifndef DISABLE_TEXT_CONSOLE
00137 
00138 static void debugHelper(const char *s, va_list va, bool caret = true) {
00139     Common::String buf = Common::String::vformat(s, va);
00140 
00141     if (caret)
00142         buf += '\n';
00143 
00144     if (g_system)
00145         g_system->logMessage(LogMessageType::kDebug, buf.c_str());
00146     // TODO: Think of a good fallback in case we do not have
00147     // any OSystem yet.
00148 }
00149 
00150 void debug(const char *s, ...) {
00151     va_list va;
00152 
00153     if (gDebugChannelsOnly)
00154         return;
00155 
00156     va_start(va, s);
00157     debugHelper(s, va);
00158     va_end(va);
00159 }
00160 
00161 void debug(int level, const char *s, ...) {
00162     va_list va;
00163 
00164     if (level > gDebugLevel || gDebugChannelsOnly)
00165         return;
00166 
00167     va_start(va, s);
00168     debugHelper(s, va);
00169     va_end(va);
00170 
00171 }
00172 
00173 void debugN(const char *s, ...) {
00174     va_list va;
00175 
00176     if (gDebugChannelsOnly)
00177         return;
00178 
00179     va_start(va, s);
00180     debugHelper(s, va, false);
00181     va_end(va);
00182 }
00183 
00184 void debugN(int level, const char *s, ...) {
00185     va_list va;
00186 
00187     if (level > gDebugLevel || gDebugChannelsOnly)
00188         return;
00189 
00190     va_start(va, s);
00191     debugHelper(s, va, false);
00192     va_end(va);
00193 }
00194 
00195 void debugC(int level, uint32 debugChannels, const char *s, ...) {
00196     va_list va;
00197 
00198     // Debug level 11 turns on all special debug level messages
00199     if (gDebugLevel != 11)
00200         if (level > gDebugLevel || !(DebugMan.isDebugChannelEnabled(debugChannels)))
00201             return;
00202 
00203     va_start(va, s);
00204     debugHelper(s, va);
00205     va_end(va);
00206 }
00207 
00208 void debugCN(int level, uint32 debugChannels, const char *s, ...) {
00209     va_list va;
00210 
00211     // Debug level 11 turns on all special debug level messages
00212     if (gDebugLevel != 11)
00213         if (level > gDebugLevel || !(DebugMan.isDebugChannelEnabled(debugChannels)))
00214             return;
00215 
00216     va_start(va, s);
00217     debugHelper(s, va, false);
00218     va_end(va);
00219 }
00220 
00221 void debugC(uint32 debugChannels, const char *s, ...) {
00222     va_list va;
00223 
00224     // Debug level 11 turns on all special debug level messages
00225     if (gDebugLevel != 11)
00226         if (!(DebugMan.isDebugChannelEnabled(debugChannels)))
00227             return;
00228 
00229     va_start(va, s);
00230     debugHelper(s, va);
00231     va_end(va);
00232 }
00233 
00234 void debugCN(uint32 debugChannels, const char *s, ...) {
00235     va_list va;
00236 
00237     // Debug level 11 turns on all special debug level messages
00238     if (gDebugLevel != 11)
00239         if (!(DebugMan.isDebugChannelEnabled(debugChannels)))
00240             return;
00241 
00242     va_start(va, s);
00243     debugHelper(s, va, false);
00244     va_end(va);
00245 }
00246 
00247 #endif


Generated on Sat Mar 16 2019 05:01:24 for ResidualVM by doxygen 1.7.1
curved edge   curved edge