It's alive!! (The second one.)
[scummvm-innocent.git] / common / debug.cpp
blobff17959cbf7631843286a5ccd8fd6eeb9bfc393e
1 /* ScummVM - Graphic Adventure Engine
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * $URL$
22 * $Id$
25 #include "common/debug.h"
26 #include "common/util.h"
27 #include "common/hashmap.h"
29 #include "engines/engine.h"
31 #include <stdarg.h> // For va_list etc.
34 #ifdef __PLAYSTATION2__
35 // for those replaced fopen/fread/etc functions
36 typedef unsigned long uint64;
37 typedef signed long int64;
38 #include "backends/platform/ps2/fileio.h"
40 #define fprintf ps2_fprintf
41 #define fputs(str, file) ps2_fputs(str, file)
42 #define fflush(a) ps2_fflush(a)
43 #endif
45 #ifdef __DS__
46 #include "backends/fs/ds/ds-fs.h"
48 void std_fprintf(FILE* handle, const char* fmt, ...);
49 void std_fflush(FILE* handle);
51 #define fprintf(file, fmt, ...) do { char str[128]; sprintf(str, fmt, ##__VA_ARGS__); DS::std_fwrite(str, strlen(str), 1, file); } while(0)
52 #define fputs(str, file) DS::std_fwrite(str, strlen(str), 1, file)
53 #define fflush(file) DS::std_fflush(file)
54 #endif
57 namespace Common {
59 namespace {
61 typedef HashMap<String, DebugChannel, IgnoreCase_Hash, IgnoreCase_EqualTo> DebugLevelMap;
63 static DebugLevelMap gDebugLevels;
64 static uint32 gDebugLevelsEnabled = 0;
66 struct DebugLevelComperator {
67 bool operator()(const DebugChannel &l, const DebugChannel &r) {
68 return (l.name.compareToIgnoreCase(r.name) < 0);
74 bool addDebugChannel(uint32 level, const String &name, const String &description) {
75 if (gDebugLevels.contains(name)) {
76 warning("Duplicate declaration of engine debug level '%s'", name.c_str());
78 gDebugLevels[name] = DebugChannel(level, name, description);
80 return true;
83 void clearAllDebugChannels() {
84 gDebugLevelsEnabled = 0;
85 gDebugLevels.clear();
88 bool enableDebugChannel(const String &name) {
89 DebugLevelMap::iterator i = gDebugLevels.find(name);
91 if (i != gDebugLevels.end()) {
92 gDebugLevelsEnabled |= i->_value.level;
93 i->_value.enabled = true;
95 return true;
96 } else {
97 return false;
101 bool disableDebugChannel(const String &name) {
102 DebugLevelMap::iterator i = gDebugLevels.find(name);
104 if (i != gDebugLevels.end()) {
105 gDebugLevelsEnabled &= ~i->_value.level;
106 i->_value.enabled = false;
108 return true;
109 } else {
110 return false;
115 DebugChannelList listDebugChannels() {
116 DebugChannelList tmp;
117 for (DebugLevelMap::iterator i = gDebugLevels.begin(); i != gDebugLevels.end(); ++i)
118 tmp.push_back(i->_value);
119 sort(tmp.begin(), tmp.end(), DebugLevelComperator());
121 return tmp;
124 bool isDebugChannelEnabled(uint32 level) {
125 // Debug level 11 turns on all special debug level messages
126 if (gDebugLevel == 11)
127 return true;
128 // return gDebugLevelsEnabled & (1 << level);
129 return gDebugLevelsEnabled & level;
132 bool isDebugChannelEnabled(const String &name) {
133 // Debug level 11 turns on all special debug level messages
134 if (gDebugLevel == 11)
135 return true;
137 // Search for the debug level with the given name and check if it is enabled
138 DebugLevelMap::iterator i = gDebugLevels.find(name);
139 if (i != gDebugLevels.end())
140 return i->_value.enabled;
141 return false;
145 } // End of namespace Common
148 int gDebugLevel = -1;
150 #ifndef DISABLE_TEXT_CONSOLE
152 static void debugHelper(const char *s, va_list va, bool caret = true) {
153 char in_buf[STRINGBUFLEN];
154 char buf[STRINGBUFLEN];
155 vsnprintf(in_buf, STRINGBUFLEN, s, va);
157 // Next, give the active engine (if any) a chance to augment the message,
158 // but only if not used from debugN.
159 if (g_engine && caret) {
160 g_engine->errorString(in_buf, buf, STRINGBUFLEN);
161 } else {
162 strncpy(buf, in_buf, STRINGBUFLEN);
164 buf[STRINGBUFLEN-1] = '\0';
166 if (caret) {
167 buf[STRINGBUFLEN-2] = '\0';
168 strcat(buf, "\n");
171 fputs(buf, stdout);
173 #if defined( USE_WINDBG )
174 #if defined( _WIN32_WCE )
175 TCHAR buf_unicode[1024];
176 MultiByteToWideChar(CP_ACP, 0, buf, strlen(buf) + 1, buf_unicode, sizeof(buf_unicode));
177 OutputDebugString(buf_unicode);
178 #else
179 OutputDebugString(buf);
180 #endif
181 #endif
183 fflush(stdout);
186 void debug(const char *s, ...) {
187 va_list va;
189 va_start(va, s);
190 debugHelper(s, va);
191 va_end(va);
194 void debug(int level, const char *s, ...) {
195 va_list va;
197 if (level > gDebugLevel)
198 return;
200 va_start(va, s);
201 debugHelper(s, va);
202 va_end(va);
206 void debugN(int level, const char *s, ...) {
207 va_list va;
209 if (level > gDebugLevel)
210 return;
212 va_start(va, s);
213 debugHelper(s, va, false);
214 va_end(va);
217 void debugC(int level, uint32 debugChannels, const char *s, ...) {
218 va_list va;
220 // Debug level 11 turns on all special debug level messages
221 if (gDebugLevel != 11)
222 if (level > gDebugLevel || !(Common::gDebugLevelsEnabled & debugChannels))
223 return;
225 va_start(va, s);
226 debugHelper(s, va);
227 va_end(va);
230 void debugCN(int level, uint32 debugChannels, const char *s, ...) {
231 va_list va;
233 // Debug level 11 turns on all special debug level messages
234 if (gDebugLevel != 11)
235 if (level > gDebugLevel || !(Common::gDebugLevelsEnabled & debugChannels))
236 return;
238 va_start(va, s);
239 debugHelper(s, va, false);
240 va_end(va);
243 void debugC(uint32 debugChannels, const char *s, ...) {
244 va_list va;
246 // Debug level 11 turns on all special debug level messages
247 if (gDebugLevel != 11)
248 if (!(Common::gDebugLevelsEnabled & debugChannels))
249 return;
251 va_start(va, s);
252 debugHelper(s, va);
253 va_end(va);
256 void debugCN(uint32 debugChannels, const char *s, ...) {
257 va_list va;
259 // Debug level 11 turns on all special debug level messages
260 if (gDebugLevel != 11)
261 if (!(Common::gDebugLevelsEnabled & debugChannels))
262 return;
264 va_start(va, s);
265 debugHelper(s, va, false);
266 va_end(va);
269 #endif