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.
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)
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)
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
);
83 void clearAllDebugChannels() {
84 gDebugLevelsEnabled
= 0;
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;
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;
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());
124 bool isDebugChannelEnabled(uint32 level
) {
125 // Debug level 11 turns on all special debug level messages
126 if (gDebugLevel
== 11)
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)
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
;
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
);
162 strncpy(buf
, in_buf
, STRINGBUFLEN
);
164 buf
[STRINGBUFLEN
-1] = '\0';
167 buf
[STRINGBUFLEN
-2] = '\0';
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
);
179 OutputDebugString(buf
);
186 void debug(const char *s
, ...) {
194 void debug(int level
, const char *s
, ...) {
197 if (level
> gDebugLevel
)
206 void debugN(int level
, const char *s
, ...) {
209 if (level
> gDebugLevel
)
213 debugHelper(s
, va
, false);
217 void debugC(int level
, uint32 debugChannels
, const char *s
, ...) {
220 // Debug level 11 turns on all special debug level messages
221 if (gDebugLevel
!= 11)
222 if (level
> gDebugLevel
|| !(Common::gDebugLevelsEnabled
& debugChannels
))
230 void debugCN(int level
, uint32 debugChannels
, const char *s
, ...) {
233 // Debug level 11 turns on all special debug level messages
234 if (gDebugLevel
!= 11)
235 if (level
> gDebugLevel
|| !(Common::gDebugLevelsEnabled
& debugChannels
))
239 debugHelper(s
, va
, false);
243 void debugC(uint32 debugChannels
, const char *s
, ...) {
246 // Debug level 11 turns on all special debug level messages
247 if (gDebugLevel
!= 11)
248 if (!(Common::gDebugLevelsEnabled
& debugChannels
))
256 void debugCN(uint32 debugChannels
, const char *s
, ...) {
259 // Debug level 11 turns on all special debug level messages
260 if (gDebugLevel
!= 11)
261 if (!(Common::gDebugLevelsEnabled
& debugChannels
))
265 debugHelper(s
, va
, false);