debug.c
author tron
Mon, 14 Nov 2005 19:48:04 +0000
changeset 2639 8a7342eb3a78
parent 2210 58a293892a66
child 2767 3282c77ffc27
permissions -rw-r--r--
(svn r3181) -Bracing
-Indentation
-Whitespace
-DeMorgan's Law
-Test with NULL or 0 for non-booleans
-'\0' instead of 0 for chars
-Remove redundantly redundant comments (like DoFoo(); // Do foo)
-Join multiple short lines with a single statement
-Split single lines with multiple statements
-Avoid assignments in if
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
1302
1e4e3e7e23aa (svn r1806) Add missing includes (see r1803)
tron
parents: 1299
diff changeset
     3
#include "stdafx.h"
1e4e3e7e23aa (svn r1806) Add missing includes (see r1803)
tron
parents: 1299
diff changeset
     4
#include <stdio.h>
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     5
#include <stdarg.h>
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1847
diff changeset
     6
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     7
#include "console.h"
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
     8
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2125
diff changeset
     9
#include "functions.h"
1847
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    10
#include "string.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    11
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    12
int _debug_ai_level;
2210
58a293892a66 (svn r2728) -Fix/Feature: Change the driver probing algorithm
tron
parents: 2186
diff changeset
    13
int _debug_driver_level;
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    14
int _debug_grf_level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    15
int _debug_map_level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    16
int _debug_misc_level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    17
int _debug_ms_level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    18
int _debug_net_level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    19
int _debug_spritecache_level;
1322
8697b73baa64 (svn r1826) -Feature: a brand new OldLoader so OpenTTD is TTD(Patch) compatible
truelight
parents: 1302
diff changeset
    20
int _debug_oldloader_level;
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 1891
diff changeset
    21
int _debug_pbs_level;
2125
3098398bf7ff (svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents: 2066
diff changeset
    22
int _debug_ntp_level;
2033
1eb15b79b6f3 (svn r2542) -Add: [GPMI] Added GPMI (http://sourceforge.net/projects/libgpmi/)
truelight
parents: 2008
diff changeset
    23
#ifdef GPMI
1eb15b79b6f3 (svn r2542) -Add: [GPMI] Added GPMI (http://sourceforge.net/projects/libgpmi/)
truelight
parents: 2008
diff changeset
    24
int _debug_gpmi_level;
1eb15b79b6f3 (svn r2542) -Add: [GPMI] Added GPMI (http://sourceforge.net/projects/libgpmi/)
truelight
parents: 2008
diff changeset
    25
#endif /* GPMI */
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
    26
int _debug_npf_level;
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    27
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    28
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    29
void CDECL debug(const char *s, ...)
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
	va_list va;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    32
	char buf[1024];
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
	va_start(va, s);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    35
	vsnprintf(buf, lengthof(buf), s, va);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    36
	va_end(va);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    37
	fprintf(stderr, "dbg: %s\n", buf);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    38
	IConsoleDebug(buf);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    39
}
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    40
1847
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    41
typedef struct DebugLevel {
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    42
	const char *name;
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    43
	int *level;
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    44
} DebugLevel;
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    45
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    46
#define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    47
	static const DebugLevel debug_level[] = {
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    48
	DEBUG_LEVEL(ai),
2210
58a293892a66 (svn r2728) -Fix/Feature: Change the driver probing algorithm
tron
parents: 2186
diff changeset
    49
	DEBUG_LEVEL(driver),
1847
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    50
	DEBUG_LEVEL(grf),
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    51
	DEBUG_LEVEL(map),
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    52
	DEBUG_LEVEL(misc),
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    53
	DEBUG_LEVEL(ms),
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    54
	DEBUG_LEVEL(net),
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    55
	DEBUG_LEVEL(spritecache),
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    56
	DEBUG_LEVEL(oldloader),
2008
c9d6585c96c8 (svn r2516) - Feature: [pbs] Implement path-based-signalling. This allows multiple trains within the same signal block, provided their paths dont intersect. For this the block must have all exit and entry signals be pbs signals. Place these by ctrl-clicking 4 times on a normal signal.
hackykid
parents: 1891
diff changeset
    57
	DEBUG_LEVEL(pbs),
2125
3098398bf7ff (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),
2033
1eb15b79b6f3 (svn r2542) -Add: [GPMI] Added GPMI (http://sourceforge.net/projects/libgpmi/)
truelight
parents: 2008
diff changeset
    59
#ifdef GPMI
1eb15b79b6f3 (svn r2542) -Add: [GPMI] Added GPMI (http://sourceforge.net/projects/libgpmi/)
truelight
parents: 2008
diff changeset
    60
	DEBUG_LEVEL(gpmi),
2066
9ee1aea9701f (svn r2576) -Fix: Whitespace. Somehow a commit bypassed the pre-commit hook
celestar
parents: 2033
diff changeset
    61
#endif
1847
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    62
	DEBUG_LEVEL(npf)
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    63
	};
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
    64
#undef DEBUG_LEVEL
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    65
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    66
void SetDebugString(const char *s)
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
	int v;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    69
	char *end;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    70
	const char *t;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    71
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    72
	// global debugging level?
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    73
	if (*s >= '0' && *s <= '9') {
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    74
		const DebugLevel *i;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    75
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    76
		v = strtoul(s, &end, 0);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    77
		s = end;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    78
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2210
diff changeset
    79
		for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    80
	}
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    81
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    82
	// individual levels
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2210
diff changeset
    83
	for (;;) {
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    84
		const DebugLevel *i;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    85
		int *p;
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
		// skip delimiters
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    88
		while (*s == ' ' || *s == ',' || *s == '\t') s++;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    89
		if (*s == '\0') break;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    90
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    91
		t = s;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    92
		while (*s >= 'a' && *s <= 'z') s++;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    93
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    94
		// check debugging levels
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    95
		p = NULL;
0a6510cc889b (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)
0a6510cc889b (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) {
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    98
				p = i->level;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
    99
				break;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   100
			}
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   101
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   102
		if (*s == '=') s++;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   103
		v = strtoul(s, &end, 0);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   104
		s = end;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2210
diff changeset
   105
		if (p != NULL) {
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   106
			*p = v;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2210
diff changeset
   107
		} else {
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   108
			ShowInfoF("Unknown debug level '%.*s'", s - t, t);
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   109
			return;
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   110
		}
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   111
	}
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents:
diff changeset
   112
}
1847
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   113
47703af63895 (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
47703af63895 (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
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   116
 * @return string with debug-levels
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   117
 */
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   118
const char *GetDebugString(void)
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   119
{
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   120
	const DebugLevel *i;
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   121
	static char dbgstr[100];
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   122
	char dbgval[20];
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   123
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   124
	memset(dbgstr, 0, sizeof(dbgstr));
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   125
	i = debug_level;
47703af63895 (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);
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   127
47703af63895 (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++) {
47703af63895 (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);
47703af63895 (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));
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   131
	}
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   132
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   133
	return dbgstr;
47703af63895 (svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents: 1678
diff changeset
   134
}
2033
1eb15b79b6f3 (svn r2542) -Add: [GPMI] Added GPMI (http://sourceforge.net/projects/libgpmi/)
truelight
parents: 2008
diff changeset
   135
1eb15b79b6f3 (svn r2542) -Add: [GPMI] Added GPMI (http://sourceforge.net/projects/libgpmi/)
truelight
parents: 2008
diff changeset
   136
#ifdef GPMI
1eb15b79b6f3 (svn r2542) -Add: [GPMI] Added GPMI (http://sourceforge.net/projects/libgpmi/)
truelight
parents: 2008
diff changeset
   137
void gpmi_debug_openttd(int level, char *s)
1eb15b79b6f3 (svn r2542) -Add: [GPMI] Added GPMI (http://sourceforge.net/projects/libgpmi/)
truelight
parents: 2008
diff changeset
   138
{
1eb15b79b6f3 (svn r2542) -Add: [GPMI] Added GPMI (http://sourceforge.net/projects/libgpmi/)
truelight
parents: 2008
diff changeset
   139
	DEBUG(gpmi, level)("[GPMI] %s", s);
1eb15b79b6f3 (svn r2542) -Add: [GPMI] Added GPMI (http://sourceforge.net/projects/libgpmi/)
truelight
parents: 2008
diff changeset
   140
}
1eb15b79b6f3 (svn r2542) -Add: [GPMI] Added GPMI (http://sourceforge.net/projects/libgpmi/)
truelight
parents: 2008
diff changeset
   141
#endif /* GPMI */