debug.c
author Darkvater
Sat, 28 May 2005 16:59:51 +0000
changeset 1866 87ae212e7eda
parent 1847 d94ed71b61e7
child 1891 862800791170
permissions -rw-r--r--
(svn r2372) - Fix (console): update the example scripts in the scripts/ directory to reflect the new console functionality
- Fix (console): any line starting with a '#' is a comment so ignore it
- Fix (console): The special variables whose value can only be set by a custom process should, also print out their newly set value there, instead of relying on the default printout which is slightly confusing. Eg after you change the value it still printed out 'current value for...' instead of 'XXX changed to...'
1302
29f313f85ec5 (svn r1806) Add missing includes (see r1803)
tron
parents: 1299
diff changeset
     1
#include "stdafx.h"
29f313f85ec5 (svn r1806) Add missing includes (see r1803)
tron
parents: 1299
diff changeset
     2
#include <stdio.h>
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     3
#include <stdarg.h>
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     4
#include "ttd.h"
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     5
#include "console.h"
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     6
#include "debug.h"
1847
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
     7
#include "string.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     8
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     9
int _debug_ai_level;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    10
int _debug_grf_level;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    11
int _debug_map_level;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    12
int _debug_misc_level;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    13
int _debug_ms_level;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    14
int _debug_net_level;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    15
int _debug_spritecache_level;
1322
a7fef520f54c (svn r1826) -Feature: a brand new OldLoader so OpenTTD is TTD(Patch) compatible
truelight
parents: 1302
diff changeset
    16
int _debug_oldloader_level;
1678
187385f01cc9 (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
    17
int _debug_npf_level;
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    18
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    19
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    20
void CDECL debug(const char *s, ...)
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    21
{
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    22
	va_list va;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    23
	char buf[1024];
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    24
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    25
	va_start(va, s);
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    26
	vsnprintf(buf, lengthof(buf), s, va);
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    27
	va_end(va);
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    28
	fprintf(stderr, "dbg: %s\n", buf);
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    29
	IConsoleDebug(buf);
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    30
}
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    31
1847
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    32
typedef struct DebugLevel {
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    33
	const char *name;
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    34
	int *level;
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    35
} DebugLevel;
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    36
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    37
#define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    38
	static const DebugLevel debug_level[] = {
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    39
	DEBUG_LEVEL(ai),
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    40
	DEBUG_LEVEL(grf),
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    41
	DEBUG_LEVEL(map),
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    42
	DEBUG_LEVEL(misc),
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    43
	DEBUG_LEVEL(ms),
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    44
	DEBUG_LEVEL(net),
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    45
	DEBUG_LEVEL(spritecache),
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    46
	DEBUG_LEVEL(oldloader),
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    47
	DEBUG_LEVEL(npf)
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    48
	};
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    49
#undef DEBUG_LEVEL
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    50
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    51
void SetDebugString(const char *s)
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    52
{
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    53
	int v;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    54
	char *end;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    55
	const char *t;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    56
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    57
	// global debugging level?
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    58
	if (*s >= '0' && *s <= '9') {
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    59
		const DebugLevel *i;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    60
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    61
		v = strtoul(s, &end, 0);
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    62
		s = end;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    63
39c06aba09aa (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)
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    65
			*i->level = v;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    66
	}
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    67
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    68
	// individual levels
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    69
	for(;;) {
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    70
		const DebugLevel *i;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    71
		int *p;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    72
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    73
		// skip delimiters
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    74
		while (*s == ' ' || *s == ',' || *s == '\t') s++;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    75
		if (*s == '\0') break;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    76
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    77
		t = s;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    78
		while (*s >= 'a' && *s <= 'z') s++;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    79
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    80
		// check debugging levels
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    81
		p = NULL;
39c06aba09aa (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)
39c06aba09aa (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) {
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    84
				p = i->level;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    85
				break;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    86
			}
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    87
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    88
		if (*s == '=') s++;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    89
		v = strtoul(s, &end, 0);
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    90
		s = end;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    91
		if (p != NULL)
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    92
			*p = v;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    93
		else {
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    94
			ShowInfoF("Unknown debug level '%.*s'", s - t, t);
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    95
			return;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    96
		}
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    97
	}
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    98
}
1847
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    99
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   100
/** Print out the current debug-level
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   101
 * Just return a string with the values of all the debug categorites
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   102
 * @return string with debug-levels
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   103
 */
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   104
const char *GetDebugString(void)
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   105
{
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   106
	const DebugLevel *i;
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   107
	static char dbgstr[100];
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   108
	char dbgval[20];
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   109
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   110
	memset(dbgstr, 0, sizeof(dbgstr));
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   111
	i = debug_level;
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   112
	snprintf(dbgstr, sizeof(dbgstr), "%s=%d", i->name, *i->level);
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   113
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   114
	for (i++; i != endof(debug_level); i++) {
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   115
		snprintf(dbgval, sizeof(dbgval), ", %s=%d", i->name, *i->level);
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   116
		ttd_strlcat(dbgstr, dbgval, sizeof(dbgstr));
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   117
	}
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   118
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   119
	return dbgstr;
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   120
}