debug.cpp

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

Generated on Fri Dec 31 17:15:30 2010 for OpenTTD by  doxygen 1.6.1