(svn r1800) Make adding new debug categories to the command line parser easier

This commit is contained in:
tron 2005-02-05 14:18:25 +00:00
parent 422ebcf8e0
commit 6932461e63

58
ttd.c
View File

@ -436,23 +436,40 @@ void SetDebugString(const char *s)
int v; int v;
char *end; char *end;
const char *t; const char *t;
int *p;
typedef struct DebugLevel {
const char* name;
int* level;
} DebugLevel;
#define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
static const DebugLevel debug_level[] = {
DEBUG_LEVEL(ai),
DEBUG_LEVEL(grf),
DEBUG_LEVEL(map),
DEBUG_LEVEL(misc),
DEBUG_LEVEL(ms),
DEBUG_LEVEL(net),
DEBUG_LEVEL(spritecache)
};
#undef DEBUG_LEVEL
// global debugging level? // global debugging level?
if (*s >= '0' && *s <= '9') { if (*s >= '0' && *s <= '9') {
const DebugLevel *i;
v = strtoul(s, &end, 0); v = strtoul(s, &end, 0);
s = end; s = end;
_debug_spritecache_level = v; for (i = debug_level; i != endof(debug_level); ++i)
_debug_misc_level = v; *i->level = v;
_debug_grf_level = v;
_debug_ai_level = v;
_debug_net_level = v;
_debug_map_level = v;
} }
// individual levels // individual levels
for(;;) { for(;;) {
const DebugLevel *i;
int *p;
// skip delimiters // skip delimiters
while (*s == ' ' || *s == ',' || *s == '\t') s++; while (*s == ' ' || *s == ',' || *s == '\t') s++;
if (*s == 0) break; if (*s == 0) break;
@ -460,24 +477,23 @@ void SetDebugString(const char *s)
t = s; t = s;
while (*s >= 'a' && *s <= 'z') s++; while (*s >= 'a' && *s <= 'z') s++;
#define IS_LVL(x) (s - t == sizeof(x)-1 && !memcmp(t, x, sizeof(x)-1))
// check debugging levels // check debugging levels
if IS_LVL("misc") p = &_debug_misc_level; p = NULL;
else if IS_LVL("spritecache") p = &_debug_spritecache_level; for (i = debug_level; i != endof(debug_level); ++i)
else if IS_LVL("grf") p = &_debug_grf_level; if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
else if IS_LVL("ai") p = &_debug_ai_level; p = i->level;
else if IS_LVL("net") p = &_debug_net_level; break;
else if IS_LVL("map") p = &_debug_map_level; }
else if IS_LVL("ms") p = &_debug_ms_level;
else {
ShowInfoF("Unknown debug level '%.*s'", s-t, t);
return;
}
#undef IS_LVL
if (*s == '=') s++; if (*s == '=') s++;
v = strtoul(s, &end, 0); v = strtoul(s, &end, 0);
s = end; s = end;
if (p) *p = v; if (p != NULL)
*p = v;
else {
ShowInfoF("Unknown debug level '%.*s'", s - t, t);
return;
}
} }
} }