debug.cpp

Go to the documentation of this file.
00001 /* $Id: debug.cpp 18809 2010-01-15 16:41:15Z 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/core/os_abstraction.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_ms_level;
00033 int _debug_net_level;
00034 int _debug_sprite_level;
00035 int _debug_oldloader_level;
00036 int _debug_ntp_level;
00037 int _debug_npf_level;
00038 int _debug_yapf_level;
00039 int _debug_freetype_level;
00040 int _debug_sl_level;
00041 int _debug_gamelog_level;
00042 int _debug_desync_level;
00043 
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(ms),
00058   DEBUG_LEVEL(net),
00059   DEBUG_LEVEL(sprite),
00060   DEBUG_LEVEL(oldloader),
00061   DEBUG_LEVEL(ntp),
00062   DEBUG_LEVEL(npf),
00063   DEBUG_LEVEL(yapf),
00064   DEBUG_LEVEL(freetype),
00065   DEBUG_LEVEL(sl),
00066   DEBUG_LEVEL(gamelog),
00067   DEBUG_LEVEL(desync),
00068   };
00069 #undef DEBUG_LEVEL
00070 
00071 #if !defined(NO_DEBUG_MESSAGES)
00072 
00073 static void debug_print(const char *dbg, const char *buf)
00074 {
00075 #if defined(ENABLE_NETWORK)
00076   if (_debug_socket != INVALID_SOCKET) {
00077     char buf2[1024 + 32];
00078 
00079     snprintf(buf2, lengthof(buf2), "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
00080     send(_debug_socket, buf2, (int)strlen(buf2), 0);
00081     return;
00082   }
00083 #endif /* ENABLE_NETWORK */
00084   if (strcmp(dbg, "desync") != 0) {
00085 #if defined(WINCE)
00086     /* We need to do OTTD2FS twice, but as it uses a static buffer, we need to store one temporary */
00087     TCHAR tbuf[512];
00088     _sntprintf(tbuf, sizeof(tbuf), _T("%s"), OTTD2FS(dbg));
00089     NKDbgPrintfW(_T("dbg: [%s] %s\n"), tbuf, OTTD2FS(buf));
00090 #else
00091     fprintf(stderr, "%sdbg: [%s] %s\n", GetLogPrefix(), dbg, buf);
00092 #endif
00093     IConsoleDebug(dbg, buf);
00094   } else {
00095     static FILE *f = FioFOpenFile("commands-out.log", "wb", AUTOSAVE_DIR);
00096     if (f == NULL) return;
00097 
00098     fprintf(f, "%s%s", GetLogPrefix(), buf);
00099     fflush(f);
00100   }
00101 }
00102 
00103 void CDECL debug(const char *dbg, const char *format, ...)
00104 {
00105   char buf[1024];
00106 
00107   va_list va;
00108   va_start(va, format);
00109   vsnprintf(buf, lengthof(buf), format, va);
00110   va_end(va);
00111 
00112   debug_print(dbg, buf);
00113 }
00114 #endif /* NO_DEBUG_MESSAGES */
00115 
00116 void SetDebugString(const char *s)
00117 {
00118   int v;
00119   char *end;
00120   const char *t;
00121 
00122   /* global debugging level? */
00123   if (*s >= '0' && *s <= '9') {
00124     const DebugLevel *i;
00125 
00126     v = strtoul(s, &end, 0);
00127     s = end;
00128 
00129     for (i = debug_level; i != endof(debug_level); ++i) *i->level = v;
00130   }
00131 
00132   /* individual levels */
00133   for (;;) {
00134     const DebugLevel *i;
00135     int *p;
00136 
00137     /* skip delimiters */
00138     while (*s == ' ' || *s == ',' || *s == '\t') s++;
00139     if (*s == '\0') break;
00140 
00141     t = s;
00142     while (*s >= 'a' && *s <= 'z') s++;
00143 
00144     /* check debugging levels */
00145     p = NULL;
00146     for (i = debug_level; i != endof(debug_level); ++i)
00147       if (s == t + strlen(i->name) && strncmp(t, i->name, s - t) == 0) {
00148         p = i->level;
00149         break;
00150       }
00151 
00152     if (*s == '=') s++;
00153     v = strtoul(s, &end, 0);
00154     s = end;
00155     if (p != NULL) {
00156       *p = v;
00157     } else {
00158       ShowInfoF("Unknown debug level '%.*s'", (int)(s - t), t);
00159       return;
00160     }
00161   }
00162 }
00163 
00168 const char *GetDebugString()
00169 {
00170   const DebugLevel *i;
00171   static char dbgstr[150];
00172   char dbgval[20];
00173 
00174   memset(dbgstr, 0, sizeof(dbgstr));
00175   i = debug_level;
00176   snprintf(dbgstr, sizeof(dbgstr), "%s=%d", i->name, *i->level);
00177 
00178   for (i++; i != endof(debug_level); i++) {
00179     snprintf(dbgval, sizeof(dbgval), ", %s=%d", i->name, *i->level);
00180     strecat(dbgstr, dbgval, lastof(dbgstr));
00181   }
00182 
00183   return dbgstr;
00184 }
00185 
00191 const char *GetLogPrefix()
00192 {
00193   static char _log_prefix[24];
00194   if (_settings_client.gui.show_date_in_logs) {
00195     time_t cur_time = time(NULL);
00196     strftime(_log_prefix, sizeof(_log_prefix), "[%Y-%m-%d %H:%M:%S] ", localtime(&cur_time));
00197   } else {
00198     *_log_prefix = '\0';
00199   }
00200   return _log_prefix;
00201 }
00202 

Generated on Wed Jan 20 23:38:35 2010 for OpenTTD by  doxygen 1.5.6