debug.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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_ai_level;
00028 int _debug_driver_level;
00029 int _debug_grf_level;
00030 int _debug_map_level;
00031 int _debug_misc_level;
00032 int _debug_net_level;
00033 int _debug_sprite_level;
00034 int _debug_oldloader_level;
00035 int _debug_npf_level;
00036 int _debug_yapf_level;
00037 int _debug_freetype_level;
00038 int _debug_sl_level;
00039 int _debug_gamelog_level;
00040 int _debug_desync_level;
00041 int _debug_console_level;
00042 int _debug_cargodest_level;
00043 
00044 uint32 _realtime_tick = 0;
00045 
00046 struct DebugLevel {
00047   const char *name;
00048   int *level;
00049 };
00050 
00051 #define DEBUG_LEVEL(x) { #x, &_debug_##x##_level }
00052   static const DebugLevel debug_level[] = {
00053   DEBUG_LEVEL(ai),
00054   DEBUG_LEVEL(driver),
00055   DEBUG_LEVEL(grf),
00056   DEBUG_LEVEL(map),
00057   DEBUG_LEVEL(misc),
00058   DEBUG_LEVEL(net),
00059   DEBUG_LEVEL(sprite),
00060   DEBUG_LEVEL(oldloader),
00061   DEBUG_LEVEL(npf),
00062   DEBUG_LEVEL(yapf),
00063   DEBUG_LEVEL(freetype),
00064   DEBUG_LEVEL(sl),
00065   DEBUG_LEVEL(gamelog),
00066   DEBUG_LEVEL(desync),
00067   DEBUG_LEVEL(console),
00068   DEBUG_LEVEL(cargodest),
00069   };
00070 #undef DEBUG_LEVEL
00071 
00072 #if !defined(NO_DEBUG_MESSAGES)
00073 
00079 static void debug_print(const char *dbg, const char *buf)
00080 {
00081 #if defined(ENABLE_NETWORK)
00082   if (_debug_socket != INVALID_SOCKET) {
00083     char buf2[1024 + 32];
00084 
00085     snprintf(buf2, lengthof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
00086     send(_debug_socket, buf2, (int)strlen(buf2), 0);
00087     return;
00088   }
00089 #endif /* ENABLE_NETWORK */
00090   if (strcmp(dbg, "desync") != 0) {
00091 #if defined(WINCE)
00092     /* We need to do OTTD2FS twice, but as it uses a static buffer, we need to store one temporary */
00093     TCHAR tbuf[512];
00094     _sntprintf(tbuf, sizeof(tbuf), _T("%s"), OTTD2FS(dbg));
00095     NKDbgPrintfW(_T("dbg: [%s] %s\n"), tbuf, OTTD2FS(buf));
00096 #else
00097     fprintf(stderr, "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
00098 #endif
00099 #ifdef ENABLE_NETWORK
00100     NetworkAdminConsole(dbg, buf);
00101 #endif /* ENABLE_NETWORK */
00102     IConsoleDebug(dbg, buf);
00103   } else {
00104     static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
00105     if (f == NULL) return;
00106 
00107     fprintf(f, "%s%s\n", GetLogPrefix(), buf);
00108     fflush(f);
00109   }
00110 }
00111 
00118 void CDECL debug(const char *dbg, const char *format, ...)
00119 {
00120   char buf[1024];
00121 
00122   va_list va;
00123   va_start(va, format);
00124   vsnprintf(buf, lengthof(buf), format, va);
00125   va_end(va);
00126 
00127   debug_print(dbg, buf);
00128 }
00129 #endif /* NO_DEBUG_MESSAGES */
00130 
00137 void SetDebugString(const char *s)
00138 {
00139   int v;
00140   char *end;
00141   const char *t;
00142 
00143   /* global debugging level? */
00144   if (*s >= '0' && *s <= '9') {
00145     const DebugLevel *i;
00146 
00147     v = strtoul(s, &end, 0);
00148     s = end;
00149 
00150     for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
00151   }
00152 
00153   /* individual levels */
00154   for (;;) {
00155     const DebugLevel *i;
00156     int *p;
00157 
00158     /* skip delimiters */
00159     while (*s == ' ' || *s == ',' || *s == '\t') s++;
00160     if (*s == '\0') break;
00161 
00162     t = s;
00163     while (*s >= 'a' && *s <= 'z') s++;
00164 
00165     /* check debugging levels */
00166     p = NULL;
00167     for (i = debug_level; i != endof(debug_level); ++i) {
00168       if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
00169         p = i->level;
00170         break;
00171       }
00172     }
00173 
00174     if (*s == '=') s++;
00175     v = strtoul(s, &end, 0);
00176     s = end;
00177     if (p != NULL) {
00178       *p = v;
00179     } else {
00180       ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
00181       return;
00182     }
00183   }
00184 }
00185 
00191 const char *GetDebugString()
00192 {
00193   const DebugLevel *i;
00194   static char dbgstr[150];
00195   char dbgval[20];
00196 
00197   memset(dbgstr, 0, sizeof(dbgstr));
00198   i = debug_level;
00199   snprintf(dbgstr, sizeof(dbgstr), "%s=%d", i->name, *i->level);
00200 
00201   for (i++; i != endof(debug_level); i++) {
00202     snprintf(dbgval, sizeof(dbgval), ", %s=%d", i->name, *i->level);
00203     strecat(dbgstr, dbgval, lastof(dbgstr));
00204   }
00205 
00206   return dbgstr;
00207 }
00208 
00214 const char *GetLogPrefix()
00215 {
00216   static char _log_prefix[24];
00217   if (_settings_client.gui.show_date_in_logs) {
00218     time_t cur_time = time(NULL);
00219     strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
00220   } else {
00221     *_log_prefix = '\0';
00222   }
00223   return _log_prefix;
00224 }
00225 

Generated on Mon May 9 05:18:52 2011 for OpenTTD by  doxygen 1.6.1