Files
@ r20758:f233b5e2cdd2
Branch filter:
Location: cpp/openttd-patchpack/source/src/debug.cpp
r20758:f233b5e2cdd2
6.3 KiB
text/x-c
(svn r25801) -Update from WebTranslator v3.0:
estonian - 4 changes by notAbot
japanese - 5 changes by guppy
slovenian - 1 changes by matej1245
estonian - 4 changes by notAbot
japanese - 5 changes by guppy
slovenian - 1 changes by matej1245
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | /* $Id$ */
/*
* 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/>.
*/
/** @file debug.cpp Handling of printing debug messages. */
#include "stdafx.h"
#include <stdarg.h>
#include "console_func.h"
#include "debug.h"
#include "string_func.h"
#include "fileio_func.h"
#include "settings_type.h"
#include <time.h>
#if defined(ENABLE_NETWORK)
#include "network/network_admin.h"
SOCKET _debug_socket = INVALID_SOCKET;
#endif /* ENABLE_NETWORK */
int _debug_driver_level;
int _debug_grf_level;
int _debug_map_level;
int _debug_misc_level;
int _debug_net_level;
int _debug_sprite_level;
int _debug_oldloader_level;
int _debug_npf_level;
int _debug_yapf_level;
int _debug_freetype_level;
int _debug_script_level;
int _debug_sl_level;
int _debug_gamelog_level;
int _debug_desync_level;
int _debug_console_level;
#ifdef RANDOM_DEBUG
int _debug_random_level;
#endif
uint32 _realtime_tick = 0;
struct DebugLevel {
const char *name;
int *level;
};
#define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
static const DebugLevel debug_level[] = {
DEBUG_LEVEL(driver),
DEBUG_LEVEL(grf),
DEBUG_LEVEL(map),
DEBUG_LEVEL(misc),
DEBUG_LEVEL(net),
DEBUG_LEVEL(sprite),
DEBUG_LEVEL(oldloader),
DEBUG_LEVEL(npf),
DEBUG_LEVEL(yapf),
DEBUG_LEVEL(freetype),
DEBUG_LEVEL(script),
DEBUG_LEVEL(sl),
DEBUG_LEVEL(gamelog),
DEBUG_LEVEL(desync),
DEBUG_LEVEL(console),
#ifdef RANDOM_DEBUG
DEBUG_LEVEL(random),
#endif
};
#undef DEBUG_LEVEL
/**
* Dump the available debug facility names in the help text.
* @param buf Start address for storing the output.
* @param last Last valid address for storing the output.
* @return Next free position in the output.
*/
char *DumpDebugFacilityNames(char *buf, char *last)
{
size_t length = 0;
for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
if (length == 0) {
buf = strecpy(buf, "List of debug facility names:\n", last);
} else {
buf = strecpy(buf, ", ", last);
length += 2;
}
buf = strecpy(buf, i->name, last);
length += strlen(i->name);
}
if (length > 0) {
buf = strecpy(buf, "\n\n", last);
}
return buf;
}
#if !defined(NO_DEBUG_MESSAGES)
/**
* Internal function for outputting the debug line.
* @param dbg Debug category.
* @param buf Text line to output.
*/
static void debug_print(const char *dbg, const char *buf)
{
#if defined(ENABLE_NETWORK)
if (_debug_socket != INVALID_SOCKET) {
char buf2[1024 + 32];
snprintf(buf2, lengthof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
send(_debug_socket, buf2, (int)strlen(buf2), 0);
return;
}
#endif /* ENABLE_NETWORK */
if (strcmp(dbg, "desync") == 0) {
static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
if (f == NULL) return;
fprintf(f, "%s%s\n", GetLogPrefix(), buf);
fflush(f);
#ifdef RANDOM_DEBUG
} else if (strcmp(dbg, "random") == 0) {
static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
if (f == NULL) return;
fprintf(f, "%s\n", buf);
fflush(f);
#endif
} else {
char buffer[512];
seprintf(buffer, lastof(buffer), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
#if defined(WINCE)
NKDbgPrintfW(OTTD2FS(buffer));
#elif defined(WIN32) || defined(WIN64)
_fputts(OTTD2FS(buffer, true), stderr);
#else
fputs(buffer, stderr);
#endif
#ifdef ENABLE_NETWORK
NetworkAdminConsole(dbg, buf);
#endif /* ENABLE_NETWORK */
IConsoleDebug(dbg, buf);
}
}
/**
* 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.
*/
void CDECL debug(const char *dbg, const char *format, ...)
{
char buf[1024];
va_list va;
va_start(va, format);
vsnprintf(buf, lengthof(buf), format, va);
va_end(va);
debug_print(dbg, buf);
}
#endif /* NO_DEBUG_MESSAGES */
/**
* 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.
*/
void SetDebugString(const char *s)
{
int v;
char *end;
const char *t;
/* 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'", (int)(s - t), t);
return;
}
}
}
/**
* Print out the current debug-level.
* Just return a string with the values of all the debug categories.
* @return string with debug-levels
*/
const char *GetDebugString()
{
const DebugLevel *i;
static char dbgstr[150];
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);
strecat(dbgstr, dbgval, lastof(dbgstr));
}
return dbgstr;
}
/**
* 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;
}
|