1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
|
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2010 Warzone 2100 Project
Warzone 2100 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; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Debug.c
*
* Various debugging output functions.
*
*/
#include "frame.h"
#include <string.h>
#include <stdio.h>
#include <time.h>
#include "frameint.h"
#include "string_ext.h"
#if defined(WZ_HAVE_MOTIF)
# include "x-motif-messagebox.h"
#endif
#include "lib/gamelib/gtime.h"
#include "src/warzoneconfig.h" // to determine if we are in full screen mode or not for the system dialogs
#define MAX_LEN_LOG_LINE 512
char last_called_script_event[MAX_EVENT_NAME_LEN];
UDWORD traceID = -1;
static debug_callback * callbackRegistry = NULL;
bool enabled_debug[LOG_LAST]; // global
#ifdef DEBUG
bool assertEnabled = true;
#else
bool assertEnabled = false;
#endif
#if defined(WZ_OS_MAC)
#include <Carbon/Carbon.h>
#endif
/*
* This list _must_ match the enum in debug.h!
* Names must be 8 chars long at max!
*/
static const char *code_part_names[] = {
"all",
"main",
"sound",
"video",
"wz",
"3d",
"texture",
"net",
"memory",
"warning",
"error",
"never",
"script",
"movement",
"attack",
"fog",
"sensor",
"gui",
"map",
"save",
"sync",
"death",
"life",
"gateway",
"message",
"info",
"terrain",
"feature",
"fatal",
"input",
"popup",
"console",
"last"
};
static char inputBuffer[2][MAX_LEN_LOG_LINE];
static bool useInputBuffer1 = false;
static bool debug_flush_stderr = false;
/**
* Convert code_part names to enum. Case insensitive.
*
* \return Codepart number or LOG_LAST if can't match.
*/
static code_part code_part_from_str(const char *str)
{
unsigned int i;
for (i = 0; i < LOG_LAST; i++) {
if (strcasecmp(code_part_names[i], str) == 0) {
return i;
}
}
return LOG_LAST;
}
/**
* Callback for outputing to stderr
*
* \param data Ignored. Use NULL.
* \param outputBuffer Buffer containing the preprocessed text to output.
*/
void debug_callback_stderr( WZ_DECL_UNUSED void ** data, const char * outputBuffer )
{
if ( !strchr( outputBuffer, '\n' ) ) {
fprintf( stderr, "%s\n", outputBuffer );
} else {
fprintf( stderr, "%s", outputBuffer );
}
// Make sure that all output is flushed to stderr when requested by the user
if (debug_flush_stderr)
{
fflush(stderr);
}
}
/**
* Callback for outputting to a win32 debugger
*
* \param data Ignored. Use NULL.
* \param outputBuffer Buffer containing the preprocessed text to output.
*/
#if defined WIN32 && defined DEBUG
void debug_callback_win32debug(WZ_DECL_UNUSED void ** data, const char * outputBuffer)
{
char tmpStr[MAX_LEN_LOG_LINE];
sstrcpy(tmpStr, outputBuffer);
if (!strchr(tmpStr, '\n'))
{
sstrcat(tmpStr, "\n");
}
OutputDebugStringA( tmpStr );
}
#endif // WIN32
/**
* Callback for outputing to a file
*
* \param data Filehandle to output to.
* \param outputBuffer Buffer containing the preprocessed text to output.
*/
void debug_callback_file( void ** data, const char * outputBuffer )
{
FILE * logfile = (FILE*)*data;
if ( !strchr( outputBuffer, '\n' ) ) {
fprintf( logfile, "%s\n", outputBuffer );
} else {
fprintf( logfile, "%s", outputBuffer );
}
}
/**
* Setup the file callback
*
* Sets data to the filehandle opened for the filename found in data.
*
* \param[in,out] data In: The filename to output to.
* Out: The filehandle.
*/
bool debug_callback_file_init(void ** data)
{
const char * filename = (const char *)*data;
FILE* const logfile = fopen(filename, "w");
if (!logfile)
{
fprintf(stderr, "Could not open %s for appending!\n", filename);
return false;
}
setbuf(logfile, NULL);
fprintf(logfile, "\n--- Starting log ---\n");
*data = logfile;
return true;
}
/**
* Shutdown the file callback
*
* Closes the logfile.
*
* \param data The filehandle to close.
*/
void debug_callback_file_exit( void ** data )
{
FILE * logfile = (FILE*)*data;
fclose( logfile );
*data = NULL;
}
void debugFlushStderr()
{
debug_flush_stderr = true;
}
// MSVC specific rotuines to set/clear allocation tracking
#if defined(WZ_CC_MSVC) && defined(DEBUG)
void debug_MEMCHKOFF(void)
{
// Disable allocation tracking
int flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flags &= ~_CRTDBG_ALLOC_MEM_DF;
_CrtSetDbgFlag(flags);
}
void debug_MEMCHKON(void)
{
// Enable allocation tracking
int flags = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flags |= _CRTDBG_ALLOC_MEM_DF;
_CrtSetDbgFlag(flags);
}
void debug_MEMSTATS(void)
{
_CrtMemState state;
_CrtMemCheckpoint(&state);
_CrtMemDumpStatistics(&state);
}
#endif
void debug_init(void)
{
/*** Initialize the debug subsystem ***/
#if defined(WZ_CC_MSVC) && defined(DEBUG)
int tmpDbgFlag;
_CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_DEBUG ); // Output CRT info to debugger
tmpDbgFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ); // Grab current flags
# if defined(DEBUG_MEMORY)
tmpDbgFlag |= _CRTDBG_CHECK_ALWAYS_DF; // Check every (de)allocation
# endif // DEBUG_MEMORY
tmpDbgFlag |= _CRTDBG_ALLOC_MEM_DF; // Check allocations
tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF; // Check for memleaks
_CrtSetDbgFlag( tmpDbgFlag );
#endif // WZ_CC_MSVC && DEBUG
STATIC_ASSERT(ARRAY_SIZE(code_part_names) - 1 == LOG_LAST); // enums start at 0
memset( enabled_debug, false, sizeof(enabled_debug) );
enabled_debug[LOG_ERROR] = true;
enabled_debug[LOG_INFO] = true;
enabled_debug[LOG_FATAL] = true;
enabled_debug[LOG_POPUP] = true;
inputBuffer[0][0] = '\0';
inputBuffer[1][0] = '\0';
#ifdef DEBUG
enabled_debug[LOG_WARNING] = true;
#endif
}
void debug_exit(void)
{
debug_callback * curCallback = callbackRegistry, * tmpCallback = NULL;
while ( curCallback )
{
if ( curCallback->exit )
curCallback->exit( &curCallback->data );
tmpCallback = curCallback->next;
free( curCallback );
curCallback = tmpCallback;
}
callbackRegistry = NULL;
}
void debug_register_callback( debug_callback_fn callback, debug_callback_init init, debug_callback_exit exit, void * data )
{
debug_callback * curCallback = callbackRegistry, * tmpCallback = NULL;
tmpCallback = (debug_callback*)malloc(sizeof(*tmpCallback));
tmpCallback->next = NULL;
tmpCallback->callback = callback;
tmpCallback->init = init;
tmpCallback->exit = exit;
tmpCallback->data = data;
if (tmpCallback->init
&& !tmpCallback->init(&tmpCallback->data))
{
debug(LOG_ERROR, "Failed to initialise debug callback");
free(tmpCallback);
return;
}
if ( !curCallback )
{
callbackRegistry = tmpCallback;
return;
}
while ( curCallback->next )
curCallback = curCallback->next;
curCallback->next = tmpCallback;
}
bool debug_enable_switch(const char *str)
{
code_part part = code_part_from_str(str);
if (part != LOG_LAST) {
enabled_debug[part] = !enabled_debug[part];
}
if (part == LOG_ALL) {
memset(enabled_debug, true, sizeof(enabled_debug));
}
return (part != LOG_LAST);
}
/** Send the given string to all debug callbacks.
*
* @param str The string to send to debug callbacks.
*/
static void printToDebugCallbacks(const char * const str)
{
debug_callback * curCallback;
// Loop over all callbacks, invoking them with the given data string
for (curCallback = callbackRegistry; curCallback != NULL; curCallback = curCallback->next)
{
curCallback->callback(&curCallback->data, str);
}
}
void _realObjTrace(int id, const char *function, const char *str, ...)
{
char vaBuffer[MAX_LEN_LOG_LINE];
char outputBuffer[MAX_LEN_LOG_LINE];
va_list ap;
va_start(ap, str);
vssprintf(vaBuffer, str, ap);
va_end(ap);
ssprintf(outputBuffer, "[%6d]: [%s] %s", id, function, vaBuffer);
printToDebugCallbacks(outputBuffer);
}
void _debug( code_part part, const char *function, const char *str, ... )
{
va_list ap;
static char outputBuffer[MAX_LEN_LOG_LINE];
static unsigned int repeated = 0; /* times current message repeated */
static unsigned int next = 2; /* next total to print update */
static unsigned int prev = 0; /* total on last update */
va_start(ap, str);
vssprintf(outputBuffer, str, ap);
va_end(ap);
ssprintf(inputBuffer[useInputBuffer1 ? 1 : 0], "[%s] %s", function, outputBuffer);
if (sstrcmp(inputBuffer[0], inputBuffer[1]) == 0)
{
// Received again the same line
repeated++;
if (repeated == next) {
if (repeated > 2) {
ssprintf(outputBuffer, "last message repeated %u times (total %u repeats)", repeated - prev, repeated);
} else {
ssprintf(outputBuffer, "last message repeated %u times", repeated - prev);
}
printToDebugCallbacks(outputBuffer);
prev = repeated;
next *= 2;
}
} else {
// Received another line, cleanup the old
if (repeated > 0 && repeated != prev && repeated != 1) {
/* just repeat the previous message when only one repeat occurred */
if (repeated > 2) {
ssprintf(outputBuffer, "last message repeated %u times (total %u repeats)", repeated - prev, repeated);
} else {
ssprintf(outputBuffer, "last message repeated %u times", repeated - prev);
}
printToDebugCallbacks(outputBuffer);
}
repeated = 0;
next = 2;
prev = 0;
}
if (!repeated)
{
time_t rawtime;
struct tm * timeinfo;
char ourtime[15]; //HH:MM:SS
time ( &rawtime );
timeinfo = localtime ( &rawtime );
strftime (ourtime,15,"%I:%M:%S",timeinfo);
// Assemble the outputBuffer:
ssprintf(outputBuffer, "%-8s|%s: %s", code_part_names[part], ourtime, useInputBuffer1 ? inputBuffer[1] : inputBuffer[0]);
printToDebugCallbacks(outputBuffer);
// Throw up a dialog box for windows users since most don't have a clue to check the stderr.txt file for information
// Use for (duh) Fatal errors, that force us to terminate the game.
// FIXME: This only works for windowed mode now, screenToggleMode() doesn't seem to toggle us back and forth for some reason.
if (part == LOG_FATAL && !war_getFullscreen())
{
#if defined(WZ_OS_WIN)
char* popupBuf;
sasprintf(&popupBuf, "%s\n\nPlease check your WZlog file in your <config dir>/logs directory for more details. \
\nDo not forget to upload both the WZlog<date><time>.txt file and the warzone2100.rpt file in your bug reports!", useInputBuffer1 ? inputBuffer[1] : inputBuffer[0]);
MessageBoxA(NULL, popupBuf, "Warzone has terminated unexpectedly", MB_OK|MB_ICONERROR);
#elif defined(WZ_HAVE_MOTIF)
const char* popupBuf = useInputBuffer1 ? inputBuffer[1] : inputBuffer[0];
XmMessageBox(part, code_part_names[part], popupBuf);
#elif defined(WZ_OS_MAC)
AlertStdCFStringAlertParamRec param;
DialogRef dialog;
OSStatus err;
DialogItemIndex itemHit;
char aBuffer[512];
GetStandardAlertDefaultParams( ¶m, kStdCFStringAlertVersionOne );
param.movable = true;
ssprintf(aBuffer, "%s\n\nPlease check your <config dir>/logs/WZlog file for more details.\n", useInputBuffer1 ? inputBuffer[1] : inputBuffer[0] );
err = CreateStandardAlert( kAlertStopAlert, CFStringCreateWithCString( nil, aBuffer, kCFStringEncodingMacRoman),
CFSTR( "Run Console.app and search for wz2100 and copy that to a file.\
\n\nFor the Crash report on 10.4/10.5 check\
\n~/Library/Logs/CrashReporter,\
\non 10.6 check ~/Library/Logs/DiagnosticReports\
\nDo not forget to upload and attach those to a bug report at http://developer.wz2100.net/newticket\
\nThanks!" ), ¶m, &dialog );
SetWindowTitleWithCFString( GetDialogWindow( dialog ), CFSTR( "Warzone has terminated unexpectedly" ) );
RunStandardAlert( dialog, NULL, &itemHit );
#endif
}
// Throw up a dialog box for windows users since most don't have a clue to check the stderr.txt file for information
// This is a popup dialog used for times when the error isn't fatal, but we still need to notify user what is going on.
// FIXME: This only works for windowed mode now
if (part == LOG_POPUP && !war_getFullscreen())
{
#if defined(WZ_OS_WIN) || defined(WZ_HAVE_MOTIF)
char* popupBuf;
sasprintf(&popupBuf, "A non fatal error has occurred.\n\n%s.\n\nCheck your <config dir>/logs/WZlog file for more details.", useInputBuffer1 ? inputBuffer[1] : inputBuffer[0]);
# if defined(WZ_OS_WIN)
MessageBoxA(NULL, popupBuf, "Warzone has detected a problem.", MB_OK|MB_ICONINFORMATION);
# elif defined(WZ_HAVE_MOTIF)
XmMessageBox(part, code_part_names[part], popupBuf);
# endif
#elif defined (WZ_OS_MAC)
AlertStdCFStringAlertParamRec param;
DialogRef dialog;
OSStatus err;
DialogItemIndex itemHit;
char aBuffer[512];
GetStandardAlertDefaultParams( ¶m, kStdCFStringAlertVersionOne );
param.movable = true;
ssprintf(aBuffer, "A non fatal error has occurred.\n\n%s\n\nCheck your <config dir>/logs/WZlog file for more details.", useInputBuffer1 ? inputBuffer[1] : inputBuffer[0] );
err = CreateStandardAlert( kAlertNoteAlert, CFStringCreateWithCString( nil, aBuffer, kCFStringEncodingMacRoman), NULL, ¶m, &dialog );
SetWindowTitleWithCFString( GetDialogWindow( dialog ), CFSTR( "Warzone has detected a problem" ) );
RunStandardAlert( dialog, NULL, &itemHit );
#endif
}
}
useInputBuffer1 = !useInputBuffer1; // Swap buffers
}
bool debugPartEnabled(code_part codePart)
{
return enabled_debug[codePart];
}
void debugDisableAssert()
{
assertEnabled = false;
}
|