author | celestar |
Sat, 30 Dec 2006 17:16:07 +0000 | |
branch | custombridgeheads |
changeset 5607 | 5169e3e15ce9 |
parent 5592 | fd60d4ecc921 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
1302 | 3 |
#include "stdafx.h" |
4 |
#include <stdio.h> |
|
1299 | 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 | 7 |
#include "console.h" |
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 | 11 |
|
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 | 14 |
int _debug_grf_level; |
15 |
int _debug_map_level; |
|
16 |
int _debug_misc_level; |
|
17 |
int _debug_ms_level; |
|
18 |
int _debug_net_level; |
|
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5108
diff
changeset
|
19 |
int _debug_sprite_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; |
2125
3098398bf7ff
(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
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
|
22 |
int _debug_npf_level; |
3900
4984308f9125
(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
dc67d70b5a45
(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; |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5108
diff
changeset
|
25 |
int _debug_sl_level; |
1299 | 26 |
|
27 |
||
1847
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
28 |
typedef struct DebugLevel { |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
29 |
const char *name; |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
30 |
int *level; |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
31 |
} DebugLevel; |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
32 |
|
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
33 |
#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
|
34 |
static const DebugLevel debug_level[] = { |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
35 |
DEBUG_LEVEL(ai), |
2210
58a293892a66
(svn r2728) -Fix/Feature: Change the driver probing algorithm
tron
parents:
2186
diff
changeset
|
36 |
DEBUG_LEVEL(driver), |
1847
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
37 |
DEBUG_LEVEL(grf), |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
38 |
DEBUG_LEVEL(map), |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
39 |
DEBUG_LEVEL(misc), |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
40 |
DEBUG_LEVEL(ms), |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
41 |
DEBUG_LEVEL(net), |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5108
diff
changeset
|
42 |
DEBUG_LEVEL(sprite), |
1847
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
43 |
DEBUG_LEVEL(oldloader), |
2125
3098398bf7ff
(svn r2635) Fix: [ntp/misc] Improve the old pathfinder. Changed it to A* instead of Dijkstra.
ludde
parents:
2066
diff
changeset
|
44 |
DEBUG_LEVEL(ntp), |
3900
4984308f9125
(svn r4987) Feature: Merged YAPF into trunk. Thanks to devs for continuous support and users for testing.
KUDr
parents:
2916
diff
changeset
|
45 |
DEBUG_LEVEL(npf), |
5108
dc67d70b5a45
(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
|
46 |
DEBUG_LEVEL(yapf), |
5568
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5108
diff
changeset
|
47 |
DEBUG_LEVEL(freetype), |
75f13d7bfaed
(svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents:
5108
diff
changeset
|
48 |
DEBUG_LEVEL(sl), |
1847
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
49 |
}; |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
50 |
#undef DEBUG_LEVEL |
1299 | 51 |
|
5592
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
52 |
#if !defined(NO_DEBUG_MESSAGES) |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
53 |
|
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
54 |
/** Functionized DEBUG macro for compilers that don't support |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
55 |
* variadic macros (__VA_ARGS__) such as...yes MSVC2003 and lower */ |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
56 |
#if defined(NO_VARARG_MACRO) |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
57 |
void CDECL DEBUG(int name, int level, ...) |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
58 |
{ |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
59 |
va_list va; |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
60 |
const char *dbg; |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
61 |
const DebugLevel *dl = &debug_level[name]; |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
62 |
|
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
63 |
if (level != 0 && *dl->level < level) return; |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
64 |
dbg = dl->name; |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
65 |
va_start(va, level); |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
66 |
#else |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
67 |
void CDECL debug(const char *dbg, ...) |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
68 |
{ |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
69 |
va_list va; |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
70 |
va_start(va, dbg); |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
71 |
#endif /* NO_VARARG_MACRO */ |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
72 |
{ |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
73 |
const char *s; |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
74 |
char buf[1024]; |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
75 |
|
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
76 |
s = va_arg(va, const char*); |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
77 |
vsnprintf(buf, lengthof(buf), s, va); |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
78 |
va_end(va); |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
79 |
fprintf(stderr, "dbg: [%s] %s\n", dbg, buf); |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
80 |
IConsoleDebug(dbg, buf); |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
81 |
} |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
82 |
} |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
83 |
#endif /* NO_DEBUG_MESSAGES */ |
fd60d4ecc921
(svn r7608) [cbh] - Merge with trunk r7593:7607 because I need 7607 here
celestar
parents:
5568
diff
changeset
|
84 |
|
1299 | 85 |
void SetDebugString(const char *s) |
86 |
{ |
|
87 |
int v; |
|
88 |
char *end; |
|
89 |
const char *t; |
|
90 |
||
91 |
// global debugging level? |
|
92 |
if (*s >= '0' && *s <= '9') { |
|
93 |
const DebugLevel *i; |
|
94 |
||
95 |
v = strtoul(s, &end, 0); |
|
96 |
s = end; |
|
97 |
||
2639 | 98 |
for (i = debug_level; i != endof(debug_level); ++i) *i->level = v; |
1299 | 99 |
} |
100 |
||
101 |
// individual levels |
|
2639 | 102 |
for (;;) { |
1299 | 103 |
const DebugLevel *i; |
104 |
int *p; |
|
105 |
||
106 |
// skip delimiters |
|
107 |
while (*s == ' ' || *s == ',' || *s == '\t') s++; |
|
108 |
if (*s == '\0') break; |
|
109 |
||
110 |
t = s; |
|
111 |
while (*s >= 'a' && *s <= 'z') s++; |
|
112 |
||
113 |
// check debugging levels |
|
114 |
p = NULL; |
|
115 |
for (i = debug_level; i != endof(debug_level); ++i) |
|
116 |
if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) { |
|
117 |
p = i->level; |
|
118 |
break; |
|
119 |
} |
|
120 |
||
121 |
if (*s == '=') s++; |
|
122 |
v = strtoul(s, &end, 0); |
|
123 |
s = end; |
|
2639 | 124 |
if (p != NULL) { |
1299 | 125 |
*p = v; |
2639 | 126 |
} else { |
1299 | 127 |
ShowInfoF("Unknown debug level '%.*s'", s - t, t); |
128 |
return; |
|
129 |
} |
|
130 |
} |
|
131 |
} |
|
1847
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 |
/** 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
|
134 |
* 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
|
135 |
* @return string with debug-levels |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
136 |
*/ |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
137 |
const char *GetDebugString(void) |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
138 |
{ |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
139 |
const DebugLevel *i; |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
140 |
static char dbgstr[100]; |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
141 |
char dbgval[20]; |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
142 |
|
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
143 |
memset(dbgstr, 0, sizeof(dbgstr)); |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
144 |
i = debug_level; |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
145 |
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
|
146 |
|
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
147 |
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
|
148 |
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
|
149 |
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
|
150 |
} |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
151 |
|
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
152 |
return dbgstr; |
47703af63895
(svn r2352) - Feature: add the possibility to print out the current debug-level
Darkvater
parents:
1678
diff
changeset
|
153 |
} |