debug.c
author Darkvater
Sat, 07 May 2005 10:26:12 +0000
changeset 1775 08ff0f12ccdc
parent 1678 838dd6f46081
child 1847 47703af63895
permissions -rw-r--r--
(svn r2279) - Fix: Check the parameters of the first 10 Commands. While there also add proper comments for the functions and fix up CmdFailed()
1302
1e4e3e7e23aa (svn r1806) Add missing includes (see r1803)
tron
parents: 1299
diff changeset
     1
#include "stdafx.h"
1e4e3e7e23aa (svn r1806) Add missing includes (see r1803)
tron
parents: 1299
diff changeset
     2
#include <stdio.h>
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     3
#include <stdarg.h>
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     4
#include "ttd.h"
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     5
#include "console.h"
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     6
#include "debug.h"
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     7
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     8
int _debug_ai_level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     9
int _debug_grf_level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    10
int _debug_map_level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    11
int _debug_misc_level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    12
int _debug_ms_level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    13
int _debug_net_level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    14
int _debug_spritecache_level;
1322
8697b73baa64 (svn r1826) -Feature: a brand new OldLoader so OpenTTD is TTD(Patch) compatible
truelight
parents: 1302
diff changeset
    15
int _debug_oldloader_level;
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1322
diff changeset
    16
int _debug_npf_level;
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    17
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    18
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    19
void CDECL debug(const char *s, ...)
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    20
{
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    21
	va_list va;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    22
	char buf[1024];
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    23
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    24
	va_start(va, s);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    25
	vsnprintf(buf, lengthof(buf), s, va);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    26
	va_end(va);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    27
	fprintf(stderr, "dbg: %s\n", buf);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    28
	IConsoleDebug(buf);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    29
}
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    30
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    31
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    32
void SetDebugString(const char *s)
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    33
{
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    34
	int v;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    35
	char *end;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    36
	const char *t;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    37
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    38
	typedef struct DebugLevel {
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    39
		const char* name;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    40
		int* level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    41
	} DebugLevel;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    42
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    43
	#define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    44
	static const DebugLevel debug_level[] = {
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    45
		DEBUG_LEVEL(ai),
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    46
		DEBUG_LEVEL(grf),
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    47
		DEBUG_LEVEL(map),
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    48
		DEBUG_LEVEL(misc),
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    49
		DEBUG_LEVEL(ms),
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    50
		DEBUG_LEVEL(net),
1322
8697b73baa64 (svn r1826) -Feature: a brand new OldLoader so OpenTTD is TTD(Patch) compatible
truelight
parents: 1302
diff changeset
    51
		DEBUG_LEVEL(spritecache),
1678
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1322
diff changeset
    52
		DEBUG_LEVEL(oldloader),
838dd6f46081 (svn r2182) - Add: [NPF] There is now a debug class for NPF. Use -d npf<level> to enable debugging printouts from npf.
matthijs
parents: 1322
diff changeset
    53
		DEBUG_LEVEL(npf)
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    54
	};
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    55
	#undef DEBUG_LEVEL
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    56
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    57
	// global debugging level?
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    58
	if (*s >= '0' && *s <= '9') {
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    59
		const DebugLevel *i;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    60
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    61
		v = strtoul(s, &end, 0);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    62
		s = end;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    63
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    64
		for (i = debug_level; i != endof(debug_level); ++i)
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    65
			*i->level = v;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    66
	}
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    67
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    68
	// individual levels
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    69
	for(;;) {
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    70
		const DebugLevel *i;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    71
		int *p;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    72
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    73
		// skip delimiters
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    74
		while (*s == ' ' || *s == ',' || *s == '\t') s++;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    75
		if (*s == '\0') break;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    76
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    77
		t = s;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    78
		while (*s >= 'a' && *s <= 'z') s++;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    79
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    80
		// check debugging levels
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    81
		p = NULL;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    82
		for (i = debug_level; i != endof(debug_level); ++i)
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    83
			if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    84
				p = i->level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    85
				break;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    86
			}
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    87
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    88
		if (*s == '=') s++;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    89
		v = strtoul(s, &end, 0);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    90
		s = end;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    91
		if (p != NULL)
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    92
			*p = v;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    93
		else {
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    94
			ShowInfoF("Unknown debug level '%.*s'", s - t, t);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    95
			return;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    96
		}
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    97
	}
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    98
}