# debug.cpp

Go to the documentation of this file.

    00001 /* $Id: debug.cpp 11979 2008-01-24 18:47:05Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include <stdio.h>
    00007 #include <stdarg.h>
    00008 #include "openttd.h"
    00009 #include "console.h"
    00010 #include "debug.h"
    00011 #include "string_func.h"
    00012 #include "network/core/core.h"
    00013
    00014 #if defined(ENABLE_NETWORK)
    00015 SOCKET _debug_socket = INVALID_SOCKET;
    00016 #endif /* ENABLE_NETWORK */
    00017
    00018 int _debug_ai_level;
    00019 int _debug_driver_level;
    00020 int _debug_grf_level;
    00021 int _debug_map_level;
    00022 int _debug_misc_level;
    00023 int _debug_ms_level;
    00024 int _debug_net_level;
    00025 int _debug_sprite_level;
    00026 int _debug_oldloader_level;
    00027 int _debug_ntp_level;
    00028 int _debug_npf_level;
    00029 int _debug_yapf_level;
    00030 int _debug_freetype_level;
    00031 int _debug_sl_level;
    00032 int _debug_station_level;
    00033
    00034
    00035 struct DebugLevel {
    00036   const char *name;
    00037   int *level;
    00038 };
    00039
    00040 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
    00041   static const DebugLevel debug_level[] = {
    00042   DEBUG_LEVEL(ai),
    00043   DEBUG_LEVEL(driver),
    00044   DEBUG_LEVEL(grf),
    00045   DEBUG_LEVEL(map),
    00046   DEBUG_LEVEL(misc),
    00047   DEBUG_LEVEL(ms),
    00048   DEBUG_LEVEL(net),
    00049   DEBUG_LEVEL(sprite),
    00050   DEBUG_LEVEL(oldloader),
    00051   DEBUG_LEVEL(ntp),
    00052   DEBUG_LEVEL(npf),
    00053   DEBUG_LEVEL(yapf),
    00054   DEBUG_LEVEL(freetype),
    00055   DEBUG_LEVEL(sl),
    00056   DEBUG_LEVEL(station),
    00057   };
    00058 #undef DEBUG_LEVEL
    00059
    00060 #if !defined(NO_DEBUG_MESSAGES)
    00061
    00062 void CDECL debug(const char *dbg, ...)
    00063 {
    00064   va_list va;
    00065   va_start(va, dbg);
    00066   const char *s;
    00067   char buf[1024];
    00068
    00069   s = va_arg(va, const char*);
    00070   vsnprintf(buf, lengthof(buf), s, va);
    00071   va_end(va);
    00072 #if defined(ENABLE_NETWORK)
    00073   if (_debug_socket != INVALID_SOCKET) {
    00074     char buf2[lengthof(buf) + 32];
    00075
    00076     snprintf(buf2, lengthof(buf2), "dbg: [%s] %s\n", dbg, buf);
    00077     send(_debug_socket, buf2, strlen(buf2), 0);
    00078   } else
    00079 #endif /* ENABLE_NETWORK */
    00080   {
    00081 #if defined(WINCE)
    00082     /* We need to do OTTD2FS twice, but as it uses a static buffer, we need to store one temporary */
    00083     TCHAR tbuf[512];
    00084     _sntprintf(tbuf, sizeof(tbuf), _T("%s"), OTTD2FS(dbg));
    00085     NKDbgPrintfW(_T("dbg: [%s] %s\n"), tbuf, OTTD2FS(buf));
    00086 #else
    00087     fprintf(stderr, "dbg: [%s] %s\n", dbg, buf);
    00088 #endif
    00089     IConsoleDebug(dbg, buf);
    00090   }
    00091 }
    00092 #endif /* NO_DEBUG_MESSAGES */
    00093
    00094 void SetDebugString(const char *s)
    00095 {
    00096   int v;
    00097   char *end;
    00098   const char *t;
    00099
    00100   /* global debugging level? */
    00101   if (*s >= '0' && *s <= '9') {
    00102     const DebugLevel *i;
    00103
    00104     v = strtoul(s, &end, 0);
    00105     s = end;
    00106
    00107     for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
    00108   }
    00109
    00110   /* individual levels */
    00111   for (;;) {
    00112     const DebugLevel *i;
    00113     int *p;
    00114
    00115     /* skip delimiters */
    00116     while (*s == ' ' || *s == ',' || *s == '\t') s++;
    00117     if (*s == '\0') break;
    00118
    00119     t = s;
    00120     while (*s >= 'a' && *s <= 'z') s++;
    00121
    00122     /* check debugging levels */
    00123     p = NULL;
    00124     for (i = debug_level; i != endof(debug_level); ++i)
    00125       if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
    00126         p = i->level;
    00127         break;
    00128       }
    00129
    00130     if (*s == '=') s++;
    00131     v = strtoul(s, &end, 0);
    00132     s = end;
    00133     if (p != NULL) {
    00134       *p = v;
    00135     } else {
    00136       ShowInfoF("Unknown debug level '%.*s'", s - t, t);
    00137       return;
    00138     }
    00139   }
    00140 }
    00141
    00146 const char *GetDebugString()
    00147 {
    00148   const DebugLevel *i;
    00149   static char dbgstr[150];
    00150   char dbgval[20];
    00151
    00152   memset(dbgstr, 0, sizeof(dbgstr));
    00153   i = debug_level;
    00154   snprintf(dbgstr, sizeof(dbgstr), "%s=%d", i->name, *i->level);
    00155
    00156   for (i++; i != endof(debug_level); i++) {
    00157     snprintf(dbgval, sizeof(dbgval), ", %s=%d", i->name, *i->level);
    00158     ttd_strlcat(dbgstr, dbgval, sizeof(dbgstr));
    00159   }
    00160
    00161   return dbgstr;
    00162 }
    00163
    00164 #ifdef DEBUG_DUMP_COMMANDS
    00165 #include "fileio.h"
    00166
    00167 void CDECL DebugDumpCommands(const char *s, ...)
    00168 {
    00169   static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
    00170   if (f == NULL) return;
    00171
    00172   va_list va;
    00173   va_start(va, s);
    00174   vfprintf(f, s, va);
    00175   va_end(va);
    00176
    00177   fflush(f);
    00178 }
    00179 #endif /* DEBUG_DUMP_COMMANDS */