2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file debug.cpp Handling of printing debug messages. */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "stdafx.h"
|
2005-02-05 21:57:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2008-05-24 10:15:06 +00:00
|
|
|
#include "console_func.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2007-02-08 12:27:53 +00:00
|
|
|
#include "network/core/core.h"
|
|
|
|
|
|
|
|
#if defined(ENABLE_NETWORK)
|
|
|
|
SOCKET _debug_socket = INVALID_SOCKET;
|
|
|
|
#endif /* ENABLE_NETWORK */
|
2005-02-05 15:58:59 +00:00
|
|
|
|
|
|
|
int _debug_ai_level;
|
2005-07-27 19:57:12 +00:00
|
|
|
int _debug_driver_level;
|
2005-02-05 15:58:59 +00:00
|
|
|
int _debug_grf_level;
|
|
|
|
int _debug_map_level;
|
|
|
|
int _debug_misc_level;
|
|
|
|
int _debug_ms_level;
|
|
|
|
int _debug_net_level;
|
2006-12-26 17:36:18 +00:00
|
|
|
int _debug_sprite_level;
|
2005-02-06 18:28:35 +00:00
|
|
|
int _debug_oldloader_level;
|
2005-07-19 11:42:40 +00:00
|
|
|
int _debug_ntp_level;
|
2005-04-11 19:53:44 +00:00
|
|
|
int _debug_npf_level;
|
2006-05-27 16:12:16 +00:00
|
|
|
int _debug_yapf_level;
|
2006-11-16 22:05:33 +00:00
|
|
|
int _debug_freetype_level;
|
2006-12-26 17:36:18 +00:00
|
|
|
int _debug_sl_level;
|
2007-01-14 19:13:36 +00:00
|
|
|
int _debug_station_level;
|
2008-06-03 18:35:58 +00:00
|
|
|
int _debug_gamelog_level;
|
2005-02-05 15:58:59 +00:00
|
|
|
|
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct DebugLevel {
|
2005-05-20 17:59:24 +00:00
|
|
|
const char *name;
|
|
|
|
int *level;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2005-05-20 17:59:24 +00:00
|
|
|
|
|
|
|
#define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
|
|
|
|
static const DebugLevel debug_level[] = {
|
|
|
|
DEBUG_LEVEL(ai),
|
2005-07-27 19:57:12 +00:00
|
|
|
DEBUG_LEVEL(driver),
|
2005-05-20 17:59:24 +00:00
|
|
|
DEBUG_LEVEL(grf),
|
|
|
|
DEBUG_LEVEL(map),
|
|
|
|
DEBUG_LEVEL(misc),
|
|
|
|
DEBUG_LEVEL(ms),
|
|
|
|
DEBUG_LEVEL(net),
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG_LEVEL(sprite),
|
2005-05-20 17:59:24 +00:00
|
|
|
DEBUG_LEVEL(oldloader),
|
2005-07-19 11:42:40 +00:00
|
|
|
DEBUG_LEVEL(ntp),
|
2006-05-27 16:12:16 +00:00
|
|
|
DEBUG_LEVEL(npf),
|
2006-11-16 22:05:33 +00:00
|
|
|
DEBUG_LEVEL(yapf),
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG_LEVEL(freetype),
|
|
|
|
DEBUG_LEVEL(sl),
|
2007-01-14 19:13:36 +00:00
|
|
|
DEBUG_LEVEL(station),
|
2008-06-03 18:35:58 +00:00
|
|
|
DEBUG_LEVEL(gamelog),
|
2005-05-20 17:59:24 +00:00
|
|
|
};
|
|
|
|
#undef DEBUG_LEVEL
|
2005-02-05 15:58:59 +00:00
|
|
|
|
2006-12-28 19:38:09 +00:00
|
|
|
#if !defined(NO_DEBUG_MESSAGES)
|
|
|
|
|
2008-06-03 18:35:58 +00:00
|
|
|
void CDECL debug_print(const char *dbg, const char *buf)
|
2006-12-28 19:38:09 +00:00
|
|
|
{
|
2007-02-08 12:27:53 +00:00
|
|
|
#if defined(ENABLE_NETWORK)
|
2008-01-24 18:47:05 +00:00
|
|
|
if (_debug_socket != INVALID_SOCKET) {
|
2008-06-03 18:35:58 +00:00
|
|
|
char buf2[1024 + 32];
|
2007-02-08 12:27:53 +00:00
|
|
|
|
2008-01-24 18:47:05 +00:00
|
|
|
snprintf(buf2, lengthof(buf2), "dbg: [%s] %s\n", dbg, buf);
|
2008-05-27 21:41:00 +00:00
|
|
|
send(_debug_socket, buf2, (int)strlen(buf2), 0);
|
2008-01-24 18:47:05 +00:00
|
|
|
} else
|
2007-02-08 12:27:53 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2008-01-24 18:47:05 +00:00
|
|
|
{
|
2007-08-03 23:26:12 +00:00
|
|
|
#if defined(WINCE)
|
2008-01-24 18:47:05 +00:00
|
|
|
/* We need to do OTTD2FS twice, but as it uses a static buffer, we need to store one temporary */
|
|
|
|
TCHAR tbuf[512];
|
|
|
|
_sntprintf(tbuf, sizeof(tbuf), _T("%s"), OTTD2FS(dbg));
|
|
|
|
NKDbgPrintfW(_T("dbg: [%s] %s\n"), tbuf, OTTD2FS(buf));
|
2007-08-03 23:26:12 +00:00
|
|
|
#else
|
2008-01-24 18:47:05 +00:00
|
|
|
fprintf(stderr, "dbg: [%s] %s\n", dbg, buf);
|
2007-08-03 23:26:12 +00:00
|
|
|
#endif
|
2008-01-24 18:47:05 +00:00
|
|
|
IConsoleDebug(dbg, buf);
|
2006-12-28 19:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-03 18:35:58 +00:00
|
|
|
|
|
|
|
void CDECL debug(const char *dbg, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
va_start(va, dbg);
|
|
|
|
const char *s;
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
s = va_arg(va, const char*);
|
|
|
|
vsnprintf(buf, lengthof(buf), s, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
debug_print(dbg, buf);
|
|
|
|
}
|
2006-12-28 19:38:09 +00:00
|
|
|
#endif /* NO_DEBUG_MESSAGES */
|
|
|
|
|
2005-02-05 15:58:59 +00:00
|
|
|
void SetDebugString(const char *s)
|
|
|
|
{
|
|
|
|
int v;
|
|
|
|
char *end;
|
|
|
|
const char *t;
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* global debugging level? */
|
2005-02-05 15:58:59 +00:00
|
|
|
if (*s >= '0' && *s <= '9') {
|
|
|
|
const DebugLevel *i;
|
|
|
|
|
|
|
|
v = strtoul(s, &end, 0);
|
|
|
|
s = end;
|
|
|
|
|
2005-11-14 19:48:04 +00:00
|
|
|
for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
|
2005-02-05 15:58:59 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* individual levels */
|
2005-11-14 19:48:04 +00:00
|
|
|
for (;;) {
|
2005-02-05 15:58:59 +00:00
|
|
|
const DebugLevel *i;
|
|
|
|
int *p;
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* skip delimiters */
|
2005-02-05 15:58:59 +00:00
|
|
|
while (*s == ' ' || *s == ',' || *s == '\t') s++;
|
|
|
|
if (*s == '\0') break;
|
|
|
|
|
|
|
|
t = s;
|
|
|
|
while (*s >= 'a' && *s <= 'z') s++;
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* check debugging levels */
|
2005-02-05 15:58:59 +00:00
|
|
|
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;
|
2005-11-14 19:48:04 +00:00
|
|
|
if (p != NULL) {
|
2005-02-05 15:58:59 +00:00
|
|
|
*p = v;
|
2005-11-14 19:48:04 +00:00
|
|
|
} else {
|
2005-02-05 15:58:59 +00:00
|
|
|
ShowInfoF("Unknown debug level '%.*s'", s - t, t);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-05-20 17:59:24 +00:00
|
|
|
|
|
|
|
/** Print out the current debug-level
|
|
|
|
* Just return a string with the values of all the debug categorites
|
|
|
|
* @return string with debug-levels
|
|
|
|
*/
|
2007-03-07 11:47:46 +00:00
|
|
|
const char *GetDebugString()
|
2005-05-20 17:59:24 +00:00
|
|
|
{
|
|
|
|
const DebugLevel *i;
|
2008-01-04 10:09:57 +00:00
|
|
|
static char dbgstr[150];
|
2005-05-20 17:59:24 +00:00
|
|
|
char dbgval[20];
|
|
|
|
|
|
|
|
memset(dbgstr, 0, sizeof(dbgstr));
|
|
|
|
i = debug_level;
|
|
|
|
snprintf(dbgstr, sizeof(dbgstr), "%s=%d", i->name, *i->level);
|
|
|
|
|
|
|
|
for (i++; i != endof(debug_level); i++) {
|
|
|
|
snprintf(dbgval, sizeof(dbgval), ", %s=%d", i->name, *i->level);
|
|
|
|
ttd_strlcat(dbgstr, dbgval, sizeof(dbgstr));
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbgstr;
|
|
|
|
}
|
2008-01-11 00:30:32 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_DUMP_COMMANDS
|
|
|
|
#include "fileio.h"
|
|
|
|
|
|
|
|
void CDECL DebugDumpCommands(const char *s, ...)
|
|
|
|
{
|
|
|
|
static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
|
|
|
|
if (f == NULL) return;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, s);
|
|
|
|
vfprintf(f, s, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
fflush(f);
|
|
|
|
}
|
|
|
|
#endif /* DEBUG_DUMP_COMMANDS */
|