2009-01-30 01:17:12 +00:00
|
|
|
/* ScummVM - Graphic Adventure Engine
|
|
|
|
*
|
|
|
|
* ScummVM is the legal property of its developers, whose names
|
|
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
|
|
* file distributed with this source distribution.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common/debug.h"
|
|
|
|
#include "common/util.h"
|
2009-01-30 05:03:04 +00:00
|
|
|
#include "common/hashmap.h"
|
2009-11-24 22:11:07 +00:00
|
|
|
#include "common/hash-str.h"
|
2009-01-30 01:17:12 +00:00
|
|
|
|
|
|
|
#include <stdarg.h> // For va_list etc.
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __PLAYSTATION2__
|
|
|
|
// for those replaced fopen/fread/etc functions
|
|
|
|
#include "backends/platform/ps2/fileio.h"
|
|
|
|
|
|
|
|
#define fputs(str, file) ps2_fputs(str, file)
|
|
|
|
#define fflush(a) ps2_fflush(a)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __DS__
|
|
|
|
#include "backends/fs/ds/ds-fs.h"
|
|
|
|
|
2009-11-23 22:29:39 +00:00
|
|
|
#define fputs(str, file) DS::std_fwrite(str, strlen(str), 1, file)
|
|
|
|
#define fflush(file) DS::std_fflush(file)
|
2009-01-30 01:17:12 +00:00
|
|
|
#endif
|
|
|
|
|
2009-11-24 22:11:07 +00:00
|
|
|
// TODO: Move gDebugLevel into namespace Common.
|
|
|
|
int gDebugLevel = -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-01-30 01:17:12 +00:00
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2009-01-30 05:10:24 +00:00
|
|
|
typedef HashMap<String, DebugChannel, IgnoreCase_Hash, IgnoreCase_EqualTo> DebugLevelMap;
|
2009-01-30 05:03:04 +00:00
|
|
|
|
|
|
|
static DebugLevelMap gDebugLevels;
|
2009-01-30 04:52:53 +00:00
|
|
|
static uint32 gDebugLevelsEnabled = 0;
|
2009-01-30 01:17:12 +00:00
|
|
|
|
2009-01-30 05:03:04 +00:00
|
|
|
struct DebugLevelComperator {
|
2009-01-30 05:10:24 +00:00
|
|
|
bool operator()(const DebugChannel &l, const DebugChannel &r) {
|
2009-01-30 04:42:30 +00:00
|
|
|
return (l.name.compareToIgnoreCase(r.name) < 0);
|
2009-01-30 01:17:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-01-30 05:10:24 +00:00
|
|
|
bool addDebugChannel(uint32 level, const String &name, const String &description) {
|
2009-01-30 05:03:04 +00:00
|
|
|
if (gDebugLevels.contains(name)) {
|
|
|
|
warning("Duplicate declaration of engine debug level '%s'", name.c_str());
|
2009-01-30 01:17:12 +00:00
|
|
|
}
|
2009-01-30 05:10:24 +00:00
|
|
|
gDebugLevels[name] = DebugChannel(level, name, description);
|
2009-01-30 01:17:12 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-01-30 05:10:24 +00:00
|
|
|
void clearAllDebugChannels() {
|
2009-01-30 01:17:12 +00:00
|
|
|
gDebugLevelsEnabled = 0;
|
|
|
|
gDebugLevels.clear();
|
|
|
|
}
|
|
|
|
|
2009-01-30 05:10:24 +00:00
|
|
|
bool enableDebugChannel(const String &name) {
|
2009-01-30 05:03:04 +00:00
|
|
|
DebugLevelMap::iterator i = gDebugLevels.find(name);
|
2009-01-30 01:17:12 +00:00
|
|
|
|
|
|
|
if (i != gDebugLevels.end()) {
|
2009-01-30 05:03:04 +00:00
|
|
|
gDebugLevelsEnabled |= i->_value.level;
|
|
|
|
i->_value.enabled = true;
|
2009-01-30 01:17:12 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-30 05:10:24 +00:00
|
|
|
bool disableDebugChannel(const String &name) {
|
2009-01-30 05:03:04 +00:00
|
|
|
DebugLevelMap::iterator i = gDebugLevels.find(name);
|
2009-01-30 01:17:12 +00:00
|
|
|
|
|
|
|
if (i != gDebugLevels.end()) {
|
2009-01-30 05:03:04 +00:00
|
|
|
gDebugLevelsEnabled &= ~i->_value.level;
|
|
|
|
i->_value.enabled = false;
|
2009-01-30 01:17:12 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-30 05:03:04 +00:00
|
|
|
|
2009-01-30 05:10:24 +00:00
|
|
|
DebugChannelList listDebugChannels() {
|
|
|
|
DebugChannelList tmp;
|
2009-01-30 05:03:04 +00:00
|
|
|
for (DebugLevelMap::iterator i = gDebugLevels.begin(); i != gDebugLevels.end(); ++i)
|
|
|
|
tmp.push_back(i->_value);
|
|
|
|
sort(tmp.begin(), tmp.end(), DebugLevelComperator());
|
|
|
|
|
|
|
|
return tmp;
|
2009-01-30 01:17:12 +00:00
|
|
|
}
|
|
|
|
|
2009-01-30 05:10:24 +00:00
|
|
|
bool isDebugChannelEnabled(uint32 level) {
|
2009-05-31 16:02:10 +00:00
|
|
|
// Debug level 11 turns on all special debug level messages
|
2009-01-30 04:42:30 +00:00
|
|
|
if (gDebugLevel == 11)
|
|
|
|
return true;
|
|
|
|
// return gDebugLevelsEnabled & (1 << level);
|
|
|
|
return gDebugLevelsEnabled & level;
|
|
|
|
}
|
|
|
|
|
2009-01-30 05:10:24 +00:00
|
|
|
bool isDebugChannelEnabled(const String &name) {
|
2009-05-31 16:02:10 +00:00
|
|
|
// Debug level 11 turns on all special debug level messages
|
2009-01-30 04:42:30 +00:00
|
|
|
if (gDebugLevel == 11)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Search for the debug level with the given name and check if it is enabled
|
2009-01-30 05:03:04 +00:00
|
|
|
DebugLevelMap::iterator i = gDebugLevels.find(name);
|
2009-01-30 04:42:30 +00:00
|
|
|
if (i != gDebugLevels.end())
|
2009-01-30 05:03:04 +00:00
|
|
|
return i->_value.enabled;
|
2009-01-30 04:42:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-30 01:17:12 +00:00
|
|
|
|
2009-11-24 22:11:07 +00:00
|
|
|
static OutputFormatter s_debugOutputFormatter = 0;
|
|
|
|
|
|
|
|
void setDebugOutputFormatter(OutputFormatter f) {
|
|
|
|
s_debugOutputFormatter = f;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Common
|
2009-01-30 01:17:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef DISABLE_TEXT_CONSOLE
|
|
|
|
|
|
|
|
static void debugHelper(const char *s, va_list va, bool caret = true) {
|
|
|
|
char in_buf[STRINGBUFLEN];
|
|
|
|
char buf[STRINGBUFLEN];
|
|
|
|
vsnprintf(in_buf, STRINGBUFLEN, s, va);
|
|
|
|
|
|
|
|
// Next, give the active engine (if any) a chance to augment the message,
|
|
|
|
// but only if not used from debugN.
|
2009-11-24 22:11:07 +00:00
|
|
|
if (Common::s_debugOutputFormatter) {
|
|
|
|
(*Common::s_debugOutputFormatter)(buf, in_buf, STRINGBUFLEN);
|
2009-01-30 01:17:12 +00:00
|
|
|
} else {
|
|
|
|
strncpy(buf, in_buf, STRINGBUFLEN);
|
|
|
|
}
|
|
|
|
buf[STRINGBUFLEN-1] = '\0';
|
|
|
|
|
|
|
|
if (caret) {
|
|
|
|
buf[STRINGBUFLEN-2] = '\0';
|
|
|
|
strcat(buf, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
fputs(buf, stdout);
|
|
|
|
|
|
|
|
#if defined( USE_WINDBG )
|
|
|
|
#if defined( _WIN32_WCE )
|
|
|
|
TCHAR buf_unicode[1024];
|
|
|
|
MultiByteToWideChar(CP_ACP, 0, buf, strlen(buf) + 1, buf_unicode, sizeof(buf_unicode));
|
|
|
|
OutputDebugString(buf_unicode);
|
|
|
|
#else
|
|
|
|
OutputDebugString(buf);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void debug(const char *s, ...) {
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, s);
|
|
|
|
debugHelper(s, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
|
|
|
void debug(int level, const char *s, ...) {
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
if (level > gDebugLevel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(va, s);
|
|
|
|
debugHelper(s, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void debugN(int level, const char *s, ...) {
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
if (level > gDebugLevel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(va, s);
|
|
|
|
debugHelper(s, va, false);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2009-03-01 21:47:57 +00:00
|
|
|
void debugC(int level, uint32 debugChannels, const char *s, ...) {
|
2009-01-30 01:17:12 +00:00
|
|
|
va_list va;
|
|
|
|
|
2009-05-31 16:02:10 +00:00
|
|
|
// Debug level 11 turns on all special debug level messages
|
2009-01-30 01:17:12 +00:00
|
|
|
if (gDebugLevel != 11)
|
2009-03-01 21:47:57 +00:00
|
|
|
if (level > gDebugLevel || !(Common::gDebugLevelsEnabled & debugChannels))
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(va, s);
|
|
|
|
debugHelper(s, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2009-06-10 10:11:24 +00:00
|
|
|
void debugCN(int level, uint32 debugChannels, const char *s, ...) {
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
// Debug level 11 turns on all special debug level messages
|
|
|
|
if (gDebugLevel != 11)
|
|
|
|
if (level > gDebugLevel || !(Common::gDebugLevelsEnabled & debugChannels))
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(va, s);
|
|
|
|
debugHelper(s, va, false);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2009-03-01 21:47:57 +00:00
|
|
|
void debugC(uint32 debugChannels, const char *s, ...) {
|
|
|
|
va_list va;
|
|
|
|
|
2009-05-31 16:02:10 +00:00
|
|
|
// Debug level 11 turns on all special debug level messages
|
2009-03-01 21:47:57 +00:00
|
|
|
if (gDebugLevel != 11)
|
|
|
|
if (!(Common::gDebugLevelsEnabled & debugChannels))
|
2009-01-30 01:17:12 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(va, s);
|
|
|
|
debugHelper(s, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2009-06-10 10:11:24 +00:00
|
|
|
void debugCN(uint32 debugChannels, const char *s, ...) {
|
|
|
|
va_list va;
|
|
|
|
|
|
|
|
// Debug level 11 turns on all special debug level messages
|
|
|
|
if (gDebugLevel != 11)
|
|
|
|
if (!(Common::gDebugLevelsEnabled & debugChannels))
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start(va, s);
|
|
|
|
debugHelper(s, va, false);
|
|
|
|
va_end(va);
|
|
|
|
}
|
|
|
|
|
2009-01-30 01:17:12 +00:00
|
|
|
#endif
|