debug.c
author Darkvater
Tue, 26 Dec 2006 17:36:18 +0000
changeset 5380 8ea58542b6e0
parent 5108 aeaef6fe53b7
child 5406 2df85542fee4
permissions -rw-r--r--
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
use in debug.h. grfmsg() is now used as a specific debug-function for grf.
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
1302
29f313f85ec5 (svn r1806) Add missing includes (see r1803)
tron
parents: 1299
diff changeset
     3
#include "stdafx.h"
29f313f85ec5 (svn r1806) Add missing includes (see r1803)
tron
parents: 1299
diff changeset
     4
#include <stdio.h>
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     5
#include <stdarg.h>
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1847
diff changeset
     6
#include "openttd.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     7
#include "console.h"
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     8
#include "debug.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2125
diff changeset
     9
#include "functions.h"
1847
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    10
#include "string.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    11
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    12
int _debug_ai_level;
2210
b76184e0929c (svn r2728) -Fix/Feature: Change the driver probing algorithm
tron
parents: 2186
diff changeset
    13
int _debug_driver_level;
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    14
int _debug_grf_level;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    15
int _debug_map_level;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    16
int _debug_misc_level;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    17
int _debug_ms_level;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    18
int _debug_net_level;
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
    19
int _debug_sprite_level;
1322
a7fef520f54c (svn r1826) -Feature: a brand new OldLoader so OpenTTD is TTD(Patch) compatible
truelight
parents: 1302
diff changeset
    20
int _debug_oldloader_level;
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2066
diff changeset
    21
int _debug_ntp_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
    22
int _debug_npf_level;
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 2916
diff changeset
    23
int _debug_yapf_level;
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 3900
diff changeset
    24
int _debug_freetype_level;
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
    25
int _debug_sl_level;
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    26
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    27
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
    28
void CDECL debug(const char *dbg, ...)
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    29
{
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    30
	va_list va;
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
    31
	const char *s;
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    32
	char buf[1024];
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    33
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
    34
	va_start(va, dbg);
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
    35
	s = va_arg(va, const char*);
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    36
	vsnprintf(buf, lengthof(buf), s, va);
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    37
	va_end(va);
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
    38
	fprintf(stderr, "dbg: [%s] %s\n", dbg, buf);
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
    39
	IConsoleDebug(dbg, buf);
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    40
}
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    41
1847
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    42
typedef struct DebugLevel {
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    43
	const char *name;
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    44
	int *level;
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    45
} DebugLevel;
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    46
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    47
#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
    48
	static const DebugLevel debug_level[] = {
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    49
	DEBUG_LEVEL(ai),
2210
b76184e0929c (svn r2728) -Fix/Feature: Change the driver probing algorithm
tron
parents: 2186
diff changeset
    50
	DEBUG_LEVEL(driver),
1847
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    51
	DEBUG_LEVEL(grf),
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    52
	DEBUG_LEVEL(map),
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    53
	DEBUG_LEVEL(misc),
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    54
	DEBUG_LEVEL(ms),
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    55
	DEBUG_LEVEL(net),
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
    56
	DEBUG_LEVEL(sprite),
1847
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    57
	DEBUG_LEVEL(oldloader),
2125
edc17858f9f6 (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2066
diff changeset
    58
	DEBUG_LEVEL(ntp),
3900
2c84ed52709d (svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents: 2916
diff changeset
    59
	DEBUG_LEVEL(npf),
5108
aeaef6fe53b7 (svn r7182) -Feature: Merge utf8 branch. This brings us support for Unicode/UTF-8 and the option for fonts rendered by FreeType. Language changes to come.
peter1138
parents: 3900
diff changeset
    60
	DEBUG_LEVEL(yapf),
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
    61
	DEBUG_LEVEL(freetype),
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5108
diff changeset
    62
	DEBUG_LEVEL(sl),
1847
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    63
	};
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    64
#undef DEBUG_LEVEL
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    65
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    66
void SetDebugString(const char *s)
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
	int v;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    69
	char *end;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    70
	const char *t;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    71
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    72
	// global debugging level?
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    73
	if (*s >= '0' && *s <= '9') {
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    74
		const DebugLevel *i;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    75
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    76
		v = strtoul(s, &end, 0);
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    77
		s = end;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    78
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2210
diff changeset
    79
		for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    80
	}
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    81
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    82
	// individual levels
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2210
diff changeset
    83
	for (;;) {
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    84
		const DebugLevel *i;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    85
		int *p;
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
		// skip delimiters
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    88
		while (*s == ' ' || *s == ',' || *s == '\t') s++;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    89
		if (*s == '\0') break;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    90
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    91
		t = s;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    92
		while (*s >= 'a' && *s <= 'z') s++;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    93
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    94
		// check debugging levels
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    95
		p = NULL;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    96
		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
    97
			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
    98
				p = i->level;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    99
				break;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   100
			}
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   101
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   102
		if (*s == '=') s++;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   103
		v = strtoul(s, &end, 0);
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   104
		s = end;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2210
diff changeset
   105
		if (p != NULL) {
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   106
			*p = v;
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2210
diff changeset
   107
		} else {
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   108
			ShowInfoF("Unknown debug level '%.*s'", s - t, t);
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   109
			return;
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   110
		}
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   111
	}
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   112
}
1847
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
/** 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
   115
 * 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
   116
 * @return string with debug-levels
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
const char *GetDebugString(void)
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   119
{
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   120
	const DebugLevel *i;
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   121
	static char dbgstr[100];
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   122
	char dbgval[20];
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   123
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   124
	memset(dbgstr, 0, sizeof(dbgstr));
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   125
	i = debug_level;
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   126
	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
   127
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   128
	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
   129
		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
   130
		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
   131
	}
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   132
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   133
	return dbgstr;
d94ed71b61e7 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   134
}