debug.cpp

Go to the documentation of this file.
00001 /* $Id: debug.cpp 23854 2012-01-26 17:24:56Z rubidium $ */
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 
00076 #if !defined(NO_DEBUG_MESSAGES)
00077 
00083 static void debug_print(const char *dbg, const char *buf)
00084 {
00085 #if defined(ENABLE_NETWORK)
00086   if (_debug_socket != INVALID_SOCKET) {
00087     char buf2[1024 + 32];
00088 
00089     snprintf(buf2, lengthof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
00090     send(_debug_socket, buf2, (int)strlen(buf2), 0);
00091     return;
00092   }
00093 #endif /* ENABLE_NETWORK */
00094   if (strcmp(dbg, "desync") == 0) {
00095     static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
00096     if (f == NULL) return;
00097 
00098     fprintf(f, "%s%s\n", GetLogPrefix(), buf);
00099     fflush(f);
00100 #ifdef RANDOM_DEBUG
00101   } else if (strcmp(dbg, "random") == 0) {
00102     static FILE *f = FioFOpenFile("random-out.log", "wb", AUTOSAVE_DIR);
00103     if (f == NULL) return;
00104 
00105     fprintf(f, "%s\n", buf);
00106     fflush(f);
00107 #endif
00108   } else {
00109 #if defined(WINCE)
00110     /* We need to do OTTD2FS twice, but as it uses a static buffer, we need to store one temporary */
00111     TCHAR tbuf[512];
00112     _sntprintf(tbuf, sizeof(tbuf), _T("%s"), OTTD2FS(dbg));
00113     NKDbgPrintfW(_T("dbg: [%s] %s\n"), tbuf, OTTD2FS(buf));
00114 #else
00115     fprintf(stderr, "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
00116 #endif
00117 #ifdef ENABLE_NETWORK
00118     NetworkAdminConsole(dbg, buf);
00119 #endif /* ENABLE_NETWORK */
00120     IConsoleDebug(dbg, buf);
00121   }
00122 }
00123 
00130 void CDECL debug(const char *dbg, const char *format, ...)
00131 {
00132   char buf[1024];
00133 
00134   va_list va;
00135   va_start(va, format);
00136   vsnprintf(buf, lengthof(buf), format, va);
00137   va_end(va);
00138 
00139   debug_print(dbg, buf);
00140 }
00141 #endif /* NO_DEBUG_MESSAGES */
00142 
00149 void SetDebugString(const char *s)
00150 {
00151   int v;
00152   char *end;
00153   const char *t;
00154 
00155   /* global debugging level? */
00156   if (*s >= '0' && *s <= '9') {
00157     const DebugLevel *i;
00158 
00159     v = strtoul(s, &end, 0);
00160     s = end;
00161 
00162     for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
00163   }
00164 
00165   /* individual levels */
00166   for (;;) {
00167     const DebugLevel *i;
00168     int *p;
00169 
00170     /* skip delimiters */
00171     while (*s == ' ' || *s == ',' || *s == '\t') s++;
00172     if (*s == '\0') break;
00173 
00174     t = s;
00175     while (*s >= 'a' && *s <= 'z') s++;
00176 
00177     /* check debugging levels */
00178     p = NULL;
00179     for (i = debug_level; i != endof(debug_level); ++i) {
00180       if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
00181         p = i->level;
00182         break;
00183       }
00184     }
00185 
00186     if (*s == '=') s++;
00187     v = strtoul(s, &end, 0);
00188     s = end;
00189     if (p != NULL) {
00190       *p = v;
00191     } else {
00192       ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
00193       return;
00194     }
00195   }
00196 }
00197 
00203 const char *GetDebugString()
00204 {
00205   const DebugLevel *i;
00206   static char dbgstr[150];
00207   char dbgval[20];
00208 
00209   memset(dbgstr, 0, sizeof(dbgstr));
00210   i = debug_level;
00211   snprintf(dbgstr, sizeof(dbgstr), "%s=%d", i->name, *i->level);
00212 
00213   for (i++; i != endof(debug_level); i++) {
00214     snprintf(dbgval, sizeof(dbgval), ", %s=%d", i->name, *i->level);
00215     strecat(dbgstr, dbgval, lastof(dbgstr));
00216   }
00217 
00218   return dbgstr;
00219 }
00220 
00226 const char *GetLogPrefix()
00227 {
00228   static char _log_prefix[24];
00229   if (_settings_client.gui.show_date_in_logs) {
00230     time_t cur_time = time(NULL);
00231     strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
00232   } else {
00233     *_log_prefix = '\0';
00234   }
00235   return _log_prefix;
00236 }
00237