2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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 <stdarg.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"
|
2008-12-29 21:50:25 +00:00
|
|
|
#include "fileio_func.h"
|
2009-09-09 15:11:46 +00:00
|
|
|
#include "settings_type.h"
|
|
|
|
|
|
|
|
#include <time.h>
|
2007-02-08 12:27:53 +00:00
|
|
|
|
|
|
|
#if defined(ENABLE_NETWORK)
|
2010-10-17 17:43:01 +00:00
|
|
|
#include "network/network_admin.h"
|
2007-02-08 12:27:53 +00:00
|
|
|
SOCKET _debug_socket = INVALID_SOCKET;
|
|
|
|
#endif /* ENABLE_NETWORK */
|
2005-02-05 15:58:59 +00:00
|
|
|
|
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_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-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;
|
2011-12-01 12:04:10 +00:00
|
|
|
int _debug_script_level;
|
2006-12-26 17:36:18 +00:00
|
|
|
int _debug_sl_level;
|
2008-06-03 18:35:58 +00:00
|
|
|
int _debug_gamelog_level;
|
2008-12-29 21:50:25 +00:00
|
|
|
int _debug_desync_level;
|
2010-02-10 16:10:05 +00:00
|
|
|
int _debug_console_level;
|
2005-02-05 15:58:59 +00:00
|
|
|
|
2010-07-19 16:21:49 +00:00
|
|
|
uint32 _realtime_tick = 0;
|
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[] = {
|
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(net),
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG_LEVEL(sprite),
|
2005-05-20 17:59:24 +00:00
|
|
|
DEBUG_LEVEL(oldloader),
|
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),
|
2011-12-01 12:04:10 +00:00
|
|
|
DEBUG_LEVEL(script),
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG_LEVEL(sl),
|
2008-06-03 18:35:58 +00:00
|
|
|
DEBUG_LEVEL(gamelog),
|
2008-12-29 21:50:25 +00:00
|
|
|
DEBUG_LEVEL(desync),
|
2010-02-10 16:12:54 +00:00
|
|
|
DEBUG_LEVEL(console),
|
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)
|
|
|
|
|
2010-09-11 11:48:37 +00:00
|
|
|
/**
|
|
|
|
* Internal function for outputting the debug line.
|
|
|
|
* @param dbg Debug category.
|
|
|
|
* @param buf Text line to output.
|
|
|
|
*/
|
2008-07-18 12:11:46 +00:00
|
|
|
static void 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
|
|
|
|
2009-09-09 15:11:46 +00:00
|
|
|
snprintf(buf2, lengthof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
|
2008-05-27 21:41:00 +00:00
|
|
|
send(_debug_socket, buf2, (int)strlen(buf2), 0);
|
2009-08-20 10:23:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2007-02-08 12:27:53 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2008-12-29 21:50:25 +00:00
|
|
|
if (strcmp(dbg, "desync") != 0) {
|
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
|
2009-09-09 15:11:46 +00:00
|
|
|
fprintf(stderr, "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
|
2007-08-03 23:26:12 +00:00
|
|
|
#endif
|
2010-10-17 17:50:40 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
2010-10-17 17:43:01 +00:00
|
|
|
NetworkAdminConsole(dbg, buf);
|
2010-10-17 17:50:40 +00:00
|
|
|
#endif /* ENABLE_NETWORK */
|
2008-01-24 18:47:05 +00:00
|
|
|
IConsoleDebug(dbg, buf);
|
2008-12-29 21:50:25 +00:00
|
|
|
} else {
|
|
|
|
static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
|
|
|
|
if (f == NULL) return;
|
|
|
|
|
2010-04-08 21:14:49 +00:00
|
|
|
fprintf(f, "%s%s\n", GetLogPrefix(), buf);
|
2008-12-29 21:50:25 +00:00
|
|
|
fflush(f);
|
2006-12-28 19:38:09 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-03 18:35:58 +00:00
|
|
|
|
2010-09-11 11:48:37 +00:00
|
|
|
/**
|
|
|
|
* Output a debug line.
|
|
|
|
* @note Do not call directly, use the #DEBUG macro instead.
|
|
|
|
* @param dbg Debug category.
|
|
|
|
* @param format Text string a la printf, with optional arguments.
|
|
|
|
*/
|
2009-05-10 17:27:25 +00:00
|
|
|
void CDECL debug(const char *dbg, const char *format, ...)
|
2008-06-03 18:35:58 +00:00
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
|
2009-05-10 17:27:25 +00:00
|
|
|
va_list va;
|
|
|
|
va_start(va, format);
|
|
|
|
vsnprintf(buf, lengthof(buf), format, va);
|
2008-06-03 18:35:58 +00:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
debug_print(dbg, buf);
|
|
|
|
}
|
2006-12-28 19:38:09 +00:00
|
|
|
#endif /* NO_DEBUG_MESSAGES */
|
|
|
|
|
2010-09-11 11:48:37 +00:00
|
|
|
/**
|
|
|
|
* Set debugging levels by parsing the text in \a s.
|
|
|
|
* For setting individual levels a string like \c "net=3,grf=6" should be used.
|
|
|
|
* If the string starts with a number, the number is used as global debugging level.
|
|
|
|
* @param s Text describing the wanted debugging levels.
|
|
|
|
*/
|
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;
|
2010-07-24 10:14:39 +00:00
|
|
|
for (i = debug_level; i != endof(debug_level); ++i) {
|
2005-02-05 15:58:59 +00:00
|
|
|
if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
|
|
|
|
p = i->level;
|
|
|
|
break;
|
|
|
|
}
|
2010-07-24 10:14:39 +00:00
|
|
|
}
|
2005-02-05 15:58:59 +00:00
|
|
|
|
|
|
|
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 {
|
2009-05-10 17:27:25 +00:00
|
|
|
ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
|
2005-02-05 15:58:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-05-20 17:59:24 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
2010-09-11 11:48:37 +00:00
|
|
|
* Print out the current debug-level.
|
|
|
|
* Just return a string with the values of all the debug categories.
|
2005-05-20 17:59:24 +00:00
|
|
|
* @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);
|
2008-11-02 11:20:15 +00:00
|
|
|
strecat(dbgstr, dbgval, lastof(dbgstr));
|
2005-05-20 17:59:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dbgstr;
|
|
|
|
}
|
2009-09-09 15:11:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the prefix for logs; if show_date_in_logs is enabled it returns
|
|
|
|
* the date, otherwise it returns nothing.
|
|
|
|
* @return the prefix for logs (do not free), never NULL
|
|
|
|
*/
|
|
|
|
const char *GetLogPrefix()
|
|
|
|
{
|
|
|
|
static char _log_prefix[24];
|
|
|
|
if (_settings_client.gui.show_date_in_logs) {
|
|
|
|
time_t cur_time = time(NULL);
|
|
|
|
strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
|
|
|
|
} else {
|
|
|
|
*_log_prefix = '\0';
|
|
|
|
}
|
|
|
|
return _log_prefix;
|
|
|
|
}
|
|
|
|
|