# debug.cpp

Go to the documentation of this file.

    00001 /* $Id: debug.cpp 24109 2012-04-09 13:08:20Z michi_cc $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include <stdarg.h>
    00014 #include "console_func.h"
    00015 #include "debug.h"
    00016 #include "string_func.h"
    00017 #include "fileio_func.h"
    00018 #include "settings_type.h"
    00019
    00020 #include <time.h>
    00021
    00022 #if defined(ENABLE_NETWORK)
    00023 #include "network/network_admin.h"
    00024 SOCKET _debug_socket = INVALID_SOCKET;
    00025 #endif /* ENABLE_NETWORK */
    00026
    00027 int _debug_driver_level;
    00028 int _debug_grf_level;
    00029 int _debug_map_level;
    00030 int _debug_misc_level;
    00031 int _debug_net_level;
    00032 int _debug_sprite_level;
    00033 int _debug_oldloader_level;
    00034 int _debug_npf_level;
    00035 int _debug_yapf_level;
    00036 int _debug_freetype_level;
    00037 int _debug_script_level;
    00038 int _debug_sl_level;
    00039 int _debug_gamelog_level;
    00040 int _debug_desync_level;
    00041 int _debug_console_level;
    00042 #ifdef RANDOM_DEBUG
    00043 int _debug_random_level;
    00044 #endif
    00045
    00046 uint32 _realtime_tick = 0;
    00047
    00048 struct DebugLevel {
    00049   const char *name;
    00050   int *level;
    00051 };
    00052
    00053 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
    00054   static const DebugLevel debug_level[] = {
    00055   DEBUG_LEVEL(driver),
    00056   DEBUG_LEVEL(grf),
    00057   DEBUG_LEVEL(map),
    00058   DEBUG_LEVEL(misc),
    00059   DEBUG_LEVEL(net),
    00060   DEBUG_LEVEL(sprite),
    00061   DEBUG_LEVEL(oldloader),
    00062   DEBUG_LEVEL(npf),
    00063   DEBUG_LEVEL(yapf),
    00064   DEBUG_LEVEL(freetype),
    00065   DEBUG_LEVEL(script),
    00066   DEBUG_LEVEL(sl),
    00067   DEBUG_LEVEL(gamelog),
    00068   DEBUG_LEVEL(desync),
    00069   DEBUG_LEVEL(console),
    00070 #ifdef RANDOM_DEBUG
    00071   DEBUG_LEVEL(random),
    00072 #endif
    00073   };
    00074 #undef DEBUG_LEVEL
    00075
    00082 char *DumpDebugFacilityNames(char *buf, char *last)
    00083 {
    00084   size_t length = 0;
    00085   for (const DebugLevel *i = debug_level; i != endof(debug_level); ++i) {
    00086     if (length == 0) {
    00087       buf = strecpy(buf, "List of debug facility names:\n", last);
    00088     } else {
    00089       buf = strecpy(buf, ", ", last);
    00090       length += 2;
    00091     }
    00092     buf = strecpy(buf, i->name, last);
    00093     length += strlen(i->name);
    00094   }
    00095   if (length > 0) {
    00096     buf = strecpy(buf, "\n\n", last);
    00097   }
    00098   return buf;
    00099 }
    00100
    00101 #if !defined(NO_DEBUG_MESSAGES)
    00102
    00108 static void debug_print(const char *dbg, const char *buf)
    00109 {
    00110 #if defined(ENABLE_NETWORK)
    00111   if (_debug_socket != INVALID_SOCKET) {
    00112     char buf2[1024 + 32];
    00113
    00114     snprintf(buf2, lengthof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
    00115     send(_debug_socket, buf2, (int)strlen(buf2), 0);
    00116     return;
    00117   }
    00118 #endif /* ENABLE_NETWORK */
    00119   if (strcmp(dbg, "desync") == 0) {
    00120     static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
    00121     if (f == NULL) return;
    00122
    00123     fprintf(f, "%s%s\n", GetLogPrefix(), buf);
    00124     fflush(f);
    00125 #ifdef RANDOM_DEBUG
    00126   } else if (strcmp(dbg, "random") == 0) {
    00127     static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
    00128     if (f == NULL) return;
    00129
    00130     fprintf(f, "%s\n", buf);
    00131     fflush(f);
    00132 #endif
    00133   } else {
    00134 #if defined(WINCE)
    00135     /* We need to do OTTD2FS twice, but as it uses a static buffer, we need to store one temporary */
    00136     TCHAR tbuf[512];
    00137     _sntprintf(tbuf, sizeof(tbuf), _T("%s"), OTTD2FS(dbg));
    00138     NKDbgPrintfW(_T("dbg: [%s] %s\n"), tbuf, OTTD2FS(buf));
    00139 #else
    00140     fprintf(stderr, "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
    00141 #endif
    00142 #ifdef ENABLE_NETWORK
    00143     NetworkAdminConsole(dbg, buf);
    00144 #endif /* ENABLE_NETWORK */
    00145     IConsoleDebug(dbg, buf);
    00146   }
    00147 }
    00148
    00155 void CDECL debug(const char *dbg, const char *format, ...)
    00156 {
    00157   char buf[1024];
    00158
    00159   va_list va;
    00160   va_start(va, format);
    00161   vsnprintf(buf, lengthof(buf), format, va);
    00162   va_end(va);
    00163
    00164   debug_print(dbg, buf);
    00165 }
    00166 #endif /* NO_DEBUG_MESSAGES */
    00167
    00174 void SetDebugString(const char *s)
    00175 {
    00176   int v;
    00177   char *end;
    00178   const char *t;
    00179
    00180   /* global debugging level? */
    00181   if (*s >= '0' && *s <= '9') {
    00182     const DebugLevel *i;
    00183
    00184     v = strtoul(s, &end, 0);
    00185     s = end;
    00186
    00187     for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
    00188   }
    00189
    00190   /* individual levels */
    00191   for (;;) {
    00192     const DebugLevel *i;
    00193     int *p;
    00194
    00195     /* skip delimiters */
    00196     while (*s == ' ' || *s == ',' || *s == '\t') s++;
    00197     if (*s == '\0') break;
    00198
    00199     t = s;
    00200     while (*s >= 'a' && *s <= 'z') s++;
    00201
    00202     /* check debugging levels */
    00203     p = NULL;
    00204     for (i = debug_level; i != endof(debug_level); ++i) {
    00205       if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
    00206         p = i->level;
    00207         break;
    00208       }
    00209     }
    00210
    00211     if (*s == '=') s++;
    00212     v = strtoul(s, &end, 0);
    00213     s = end;
    00214     if (p != NULL) {
    00215       *p = v;
    00216     } else {
    00217       ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
    00218       return;
    00219     }
    00220   }
    00221 }
    00222
    00228 const char *GetDebugString()
    00229 {
    00230   const DebugLevel *i;
    00231   static char dbgstr[150];
    00232   char dbgval[20];
    00233
    00234   memset(dbgstr, 0, sizeof(dbgstr));
    00235   i = debug_level;
    00236   snprintf(dbgstr, sizeof(dbgstr), "%s=%d", i->name, *i->level);
    00237
    00238   for (i++; i != endof(debug_level); i++) {
    00239     snprintf(dbgval, sizeof(dbgval), ", %s=%d", i->name, *i->level);
    00240     strecat(dbgstr, dbgval, lastof(dbgstr));
    00241   }
    00242
    00243   return dbgstr;
    00244 }
    00245
    00251 const char *GetLogPrefix()
    00252 {
    00253   static char _log_prefix[24];
    00254   if (_settings_client.gui.show_date_in_logs) {
    00255     time_t cur_time = time(NULL);
    00256     strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
    00257   } else {
    00258     *_log_prefix = '\0';
    00259   }
    00260   return _log_prefix;
    00261 }
    00262