From f0f85a7ef3dd7502657138bea7575dd54f6f8bdb Mon Sep 17 00:00:00 2001 From: tron Date: Sat, 5 Feb 2005 15:58:59 +0000 Subject: [PATCH] (svn r1803) Move debugging stuff into files of it's own --- Makefile | 1 + ai_build.c | 1 + ai_new.c | 1 + ai_pathfinder.c | 1 + ai_shared.c | 1 + aircraft_cmd.c | 1 + aircraft_gui.c | 1 + airport.c | 1 + console.h | 4 -- console_cmds.c | 1 + debug.c | 93 ++++++++++++++++++++++++++++++++++++++++++++++ debug.h | 22 +++++++++++ dedicated.c | 1 + engine.c | 1 + industry_gui.c | 1 + map.c | 1 + misc_gui.c | 1 + namegen.c | 2 +- network.c | 1 + network_client.c | 1 + network_data.c | 1 + network_gamelist.c | 1 + network_server.c | 1 + network_udp.c | 1 + newgrf.c | 1 + npf.c | 1 + pool.c | 1 + roadveh_cmd.c | 1 + roadveh_gui.c | 1 + saveload.c | 1 + screenshot.c | 1 + sdl.c | 1 + ship_gui.c | 1 + spritecache.c | 1 + station_cmd.c | 1 + station_gui.c | 1 + town_gui.c | 1 + train_gui.c | 1 + ttd.c | 77 +------------------------------------- variables.h | 16 -------- vehicle_gui.c | 1 + viewport.c | 1 + window.c | 1 + 43 files changed, 154 insertions(+), 97 deletions(-) create mode 100644 debug.c create mode 100644 debug.h diff --git a/Makefile b/Makefile index b2ed18ac21..d6b359bd63 100644 --- a/Makefile +++ b/Makefile @@ -561,6 +561,7 @@ C_SOURCES += clear_cmd.c C_SOURCES += command.c C_SOURCES += console.c C_SOURCES += console_cmds.c +C_SOURCES += debug.c C_SOURCES += dedicated.c C_SOURCES += disaster_cmd.c C_SOURCES += dock_gui.c diff --git a/ai_build.c b/ai_build.c index e4dcf42cd8..55fab3fbc7 100644 --- a/ai_build.c +++ b/ai_build.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "map.h" #include "tile.h" #include "command.h" diff --git a/ai_new.c b/ai_new.c index 885c04e850..cabd0ee9a7 100644 --- a/ai_new.c +++ b/ai_new.c @@ -15,6 +15,7 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "map.h" #include "tile.h" diff --git a/ai_pathfinder.c b/ai_pathfinder.c index 6266352dd5..1a4dcd6846 100644 --- a/ai_pathfinder.c +++ b/ai_pathfinder.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "map.h" #include "tile.h" #include "command.h" diff --git a/ai_shared.c b/ai_shared.c index 30dc3adcb7..9a82f744a6 100644 --- a/ai_shared.c +++ b/ai_shared.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "map.h" #include "ai.h" #include "vehicle.h" diff --git a/aircraft_cmd.c b/aircraft_cmd.c index fbde61536e..7656c689ea 100644 --- a/aircraft_cmd.c +++ b/aircraft_cmd.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "map.h" #include "tile.h" diff --git a/aircraft_gui.c b/aircraft_gui.c index 000415e735..1d3302f071 100644 --- a/aircraft_gui.c +++ b/aircraft_gui.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "map.h" #include "tile.h" diff --git a/airport.c b/airport.c index 4cb84fb5bb..1acf083514 100644 --- a/airport.c +++ b/airport.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "map.h" #include "airport.h" diff --git a/console.h b/console.h index 55c1340467..6e9bb81747 100644 --- a/console.h +++ b/console.h @@ -104,10 +104,6 @@ VARDEF byte _iconsole_color_debug; VARDEF byte _iconsole_color_commands; VARDEF _iconsole_modes _iconsole_mode; -// ** ttd.c functions ** // - -void SetDebugString(const char* s); - // ** console functions ** // void IConsoleInit(void); diff --git a/console_cmds.c b/console_cmds.c index b8ea17826f..e73896b210 100644 --- a/console_cmds.c +++ b/console_cmds.c @@ -2,6 +2,7 @@ #include "stdafx.h" #include "ttd.h" #include "console.h" +#include "debug.h" #include "engine.h" #include "functions.h" #include "variables.h" diff --git a/debug.c b/debug.c new file mode 100644 index 0000000000..6639f04265 --- /dev/null +++ b/debug.c @@ -0,0 +1,93 @@ +#include +#include "stdafx.h" +#include "ttd.h" +#include "console.h" +#include "debug.h" + +int _debug_ai_level; +int _debug_grf_level; +int _debug_map_level; +int _debug_misc_level; +int _debug_ms_level; +int _debug_net_level; +int _debug_spritecache_level; + + +void CDECL debug(const char *s, ...) +{ + va_list va; + char buf[1024]; + + va_start(va, s); + vsnprintf(buf, lengthof(buf), s, va); + va_end(va); + fprintf(stderr, "dbg: %s\n", buf); + IConsoleDebug(buf); +} + + +void SetDebugString(const char *s) +{ + int v; + char *end; + const char *t; + + typedef struct DebugLevel { + const char* name; + int* level; + } DebugLevel; + + #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level } + static const DebugLevel debug_level[] = { + DEBUG_LEVEL(ai), + DEBUG_LEVEL(grf), + DEBUG_LEVEL(map), + DEBUG_LEVEL(misc), + DEBUG_LEVEL(ms), + DEBUG_LEVEL(net), + DEBUG_LEVEL(spritecache) + }; + #undef DEBUG_LEVEL + + // global debugging level? + if (*s >= '0' && *s <= '9') { + const DebugLevel *i; + + v = strtoul(s, &end, 0); + s = end; + + for (i = debug_level; i != endof(debug_level); ++i) + *i->level = v; + } + + // individual levels + for(;;) { + const DebugLevel *i; + int *p; + + // skip delimiters + while (*s == ' ' || *s == ',' || *s == '\t') s++; + if (*s == '\0') break; + + t = s; + while (*s >= 'a' && *s <= 'z') s++; + + // check debugging levels + p = NULL; + for (i = debug_level; i != endof(debug_level); ++i) + if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) { + p = i->level; + break; + } + + if (*s == '=') s++; + v = strtoul(s, &end, 0); + s = end; + if (p != NULL) + *p = v; + else { + ShowInfoF("Unknown debug level '%.*s'", s - t, t); + return; + } + } +} diff --git a/debug.h b/debug.h new file mode 100644 index 0000000000..f48cb741a9 --- /dev/null +++ b/debug.h @@ -0,0 +1,22 @@ +#ifndef DEBUG_H +#define DEBUG_H + +#ifdef NO_DEBUG_MESSAGES + #define DEBUG(name, level) +#else + #define DEBUG(name, level) if (level == 0 || _debug_ ## name ## _level >= level) debug + + extern int _debug_ai_level; + extern int _debug_grf_level; + extern int _debug_map_level; + extern int _debug_misc_level; + extern int _debug_ms_level; + extern int _debug_net_level; + extern int _debug_spritecache_level; +#endif + +void CDECL debug(const char *s, ...); + +void SetDebugString(const char *s); + +#endif diff --git a/dedicated.c b/dedicated.c index 17944b3938..9ee6f4da17 100644 --- a/dedicated.c +++ b/dedicated.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "network.h" #include "hal.h" diff --git a/engine.c b/engine.c index 0656f88f9c..2e8d3130ea 100644 --- a/engine.c +++ b/engine.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "engine.h" #include "table/engines.h" diff --git a/industry_gui.c b/industry_gui.c index 1d5e738423..b503f1f573 100644 --- a/industry_gui.c +++ b/industry_gui.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "map.h" //#include "gui.h" diff --git a/map.c b/map.c index 98d2c4e39b..64525ef1c9 100644 --- a/map.c +++ b/map.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "functions.h" #include "map.h" diff --git a/misc_gui.c b/misc_gui.c index 53bdc0f7e5..8a5ef640ee 100644 --- a/misc_gui.c +++ b/misc_gui.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "map.h" #include "window.h" diff --git a/namegen.c b/namegen.c index 17f6369537..81dce6fae2 100644 --- a/namegen.c +++ b/namegen.c @@ -1,6 +1,6 @@ #include "stdafx.h" #include "ttd.h" - +#include "debug.h" #include "table/namegen.h" static inline uint32 SeedChance(int shift_by, int max, uint32 seed) diff --git a/network.c b/network.c index 83833d7dbe..5b6a46427c 100644 --- a/network.c +++ b/network.c @@ -1,4 +1,5 @@ #include "stdafx.h" +#include "debug.h" #include "map.h" #include "network_data.h" diff --git a/network_client.c b/network_client.c index 29821a5a5a..8deec2fd3c 100644 --- a/network_client.c +++ b/network_client.c @@ -1,4 +1,5 @@ #include "stdafx.h" +#include "debug.h" #include "network_data.h" #ifdef ENABLE_NETWORK diff --git a/network_data.c b/network_data.c index f2d370749b..9de3d40051 100644 --- a/network_data.c +++ b/network_data.c @@ -1,4 +1,5 @@ #include "stdafx.h" +#include "debug.h" #include "network_data.h" // Is the network enabled? diff --git a/network_gamelist.c b/network_gamelist.c index b10b0cb64c..f135c48519 100644 --- a/network_gamelist.c +++ b/network_gamelist.c @@ -1,4 +1,5 @@ #include "stdafx.h" +#include "debug.h" #include "network_data.h" #ifdef ENABLE_NETWORK diff --git a/network_server.c b/network_server.c index 219ba98446..0fee527797 100644 --- a/network_server.c +++ b/network_server.c @@ -1,4 +1,5 @@ #include "stdafx.h" +#include "debug.h" #include "network_data.h" #ifdef ENABLE_NETWORK diff --git a/network_udp.c b/network_udp.c index 4f2b942d30..aa0af45211 100644 --- a/network_udp.c +++ b/network_udp.c @@ -1,4 +1,5 @@ #include "stdafx.h" +#include "debug.h" #include "network_data.h" #ifdef ENABLE_NETWORK diff --git a/newgrf.c b/newgrf.c index 8a687220b8..03f622a2e0 100644 --- a/newgrf.c +++ b/newgrf.c @@ -3,6 +3,7 @@ #include #include "ttd.h" +#include "debug.h" #include "gfx.h" #include "fileio.h" #include "engine.h" diff --git a/npf.c b/npf.c index c8b456c203..9b81174f88 100644 --- a/npf.c +++ b/npf.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "npf.h" #include "aystar.h" #include "macros.h" diff --git a/pool.c b/pool.c index 12e4b731df..4130c0723f 100644 --- a/pool.c +++ b/pool.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "pool.h" /** diff --git a/roadveh_cmd.c b/roadveh_cmd.c index bdeac3757e..3a71c58b29 100644 --- a/roadveh_cmd.c +++ b/roadveh_cmd.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "map.h" #include "tile.h" diff --git a/roadveh_gui.c b/roadveh_gui.c index 0b0c4704ef..2bbdceab45 100644 --- a/roadveh_gui.c +++ b/roadveh_gui.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "map.h" #include "window.h" diff --git a/saveload.c b/saveload.c index ef621afcdc..33e14e965e 100644 --- a/saveload.c +++ b/saveload.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "vehicle.h" #include "station.h" #include "town.h" diff --git a/screenshot.c b/screenshot.c index 26bf99fc9a..d0e0107d33 100644 --- a/screenshot.c +++ b/screenshot.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "gfx.h" #include "viewport.h" diff --git a/sdl.c b/sdl.c index 87d8ec6c94..c5a7f7cf0f 100644 --- a/sdl.c +++ b/sdl.c @@ -2,6 +2,7 @@ #if defined(WITH_SDL) #include "ttd.h" +#include "debug.h" #include "gfx.h" #include "sound.h" #include "window.h" diff --git a/ship_gui.c b/ship_gui.c index e5de0fdfe8..9c953e9f11 100644 --- a/ship_gui.c +++ b/ship_gui.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "map.h" #include "window.h" diff --git a/spritecache.c b/spritecache.c index 2ab89f4ff4..c580813f8b 100644 --- a/spritecache.c +++ b/spritecache.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "gfx.h" #include "fileio.h" #include "newgrf.h" diff --git a/station_cmd.c b/station_cmd.c index 587c6acf4b..d592dc3eba 100644 --- a/station_cmd.c +++ b/station_cmd.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "map.h" #include "tile.h" diff --git a/station_gui.c b/station_gui.c index fe57f4dbe9..8453c555f0 100644 --- a/station_gui.c +++ b/station_gui.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "window.h" #include "gui.h" diff --git a/town_gui.c b/town_gui.c index cd08386b13..84b5a319c6 100644 --- a/town_gui.c +++ b/town_gui.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "town.h" #include "window.h" diff --git a/train_gui.c b/train_gui.c index cc98d34711..f6656cb425 100644 --- a/train_gui.c +++ b/train_gui.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "map.h" #include "window.h" diff --git a/ttd.c b/ttd.c index db522fdf63..f26e4d8965 100644 --- a/ttd.c +++ b/ttd.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "table/strings.h" +#include "debug.h" #include "map.h" #include "tile.h" @@ -71,17 +72,6 @@ void CDECL error(const char *s, ...) { exit(1); } -void CDECL debug(const char *s, ...) -{ - va_list va; - char buf[1024]; - va_start(va, s); - vsprintf(buf, s, va); - va_end(va); - fprintf(stderr, "dbg: %s\n", buf); - IConsoleDebug(buf); -} - void CDECL ShowInfoF(const char *str, ...) { va_list va; @@ -431,71 +421,6 @@ md_continue_here:; } } -void SetDebugString(const char *s) -{ - int v; - char *end; - const char *t; - - typedef struct DebugLevel { - const char* name; - int* level; - } DebugLevel; - - #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level } - static const DebugLevel debug_level[] = { - DEBUG_LEVEL(ai), - DEBUG_LEVEL(grf), - DEBUG_LEVEL(map), - DEBUG_LEVEL(misc), - DEBUG_LEVEL(ms), - DEBUG_LEVEL(net), - DEBUG_LEVEL(spritecache) - }; - #undef DEBUG_LEVEL - - // global debugging level? - if (*s >= '0' && *s <= '9') { - const DebugLevel *i; - - v = strtoul(s, &end, 0); - s = end; - - for (i = debug_level; i != endof(debug_level); ++i) - *i->level = v; - } - - // individual levels - for(;;) { - const DebugLevel *i; - int *p; - - // skip delimiters - while (*s == ' ' || *s == ',' || *s == '\t') s++; - if (*s == 0) break; - - t = s; - while (*s >= 'a' && *s <= 'z') s++; - - // check debugging levels - p = NULL; - for (i = debug_level; i != endof(debug_level); ++i) - if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) { - p = i->level; - break; - } - - if (*s == '=') s++; - v = strtoul(s, &end, 0); - s = end; - if (p != NULL) - *p = v; - else { - ShowInfoF("Unknown debug level '%.*s'", s - t, t); - return; - } - } -} static void ParseResolution(int res[2], char *s) { diff --git a/variables.h b/variables.h index c28105e5fe..b9cee1f0e6 100644 --- a/variables.h +++ b/variables.h @@ -452,15 +452,6 @@ VARDEF byte _autoreplace_array[256]; VARDEF uint16 _player_num_engines[256]; VARDEF byte _railtype_selected_in_replace_gui; -/* Debugging levels */ -VARDEF int _debug_spritecache_level; -VARDEF int _debug_misc_level; -VARDEF int _debug_grf_level; -VARDEF int _debug_ai_level; -VARDEF int _debug_net_level; -VARDEF int _debug_map_level; -VARDEF int _debug_ms_level; - /* Forking stuff */ VARDEF bool _dedicated_forks; VARDEF bool _dedicated_enabled; @@ -468,11 +459,4 @@ VARDEF bool _dedicated_enabled; VARDEF pid_t _dedicated_pid; #endif -void CDECL debug(const char *s, ...); -#ifdef NO_DEBUG_MESSAGES - #define DEBUG(name, level) -#else - #define DEBUG(name, level) if (level == 0 || _debug_ ## name ## _level >= level) debug -#endif - #endif /* VARIABLES_H */ diff --git a/vehicle_gui.c b/vehicle_gui.c index e88d3a8d42..9344fc2b93 100644 --- a/vehicle_gui.c +++ b/vehicle_gui.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "vehicle.h" #include "window.h" diff --git a/viewport.c b/viewport.c index 2bc10ad0d9..737a5c3505 100644 --- a/viewport.c +++ b/viewport.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "table/strings.h" #include "map.h" #include "viewport.h" diff --git a/window.c b/window.c index 5b0db975b1..3dbb0fbe9b 100644 --- a/window.c +++ b/window.c @@ -1,5 +1,6 @@ #include "stdafx.h" #include "ttd.h" +#include "debug.h" #include "map.h" #include "window.h" #include "gfx.h"