Back to index

extremetuxracer  0.5beta
debug.cpp
Go to the documentation of this file.
00001 /* 
00002  * PPRacer 
00003  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00004  * 
00005  * Copyright (C) 1999-2001 Jasmin F. Patry
00006  * 
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020  */
00021 
00022 #include "debug.h"
00023 #include "os_util.h"
00024 
00025 #include "game_config.h"
00026 
00027 #define BUGREPORT_FILE "diagnostic_log.txt"
00028 
00029 static bool debug_setting[ NUM_DEBUG_MODES ];
00030 static char* debug_desc[ NUM_DEBUG_MODES ] = {
00031     "ode",
00032     "quadtree",
00033     "control",
00034     "sound",
00035     "texture",
00036     "view",
00037     "gl_ext",
00038     "font",
00039     "ui",
00040     "game_logic",
00041     "save",
00042     "joystick",
00043     "gl_info"
00044 };
00045 
00046 /* Parse the debug parameter, fill in the debug_setting array */
00047 void init_debug()
00048 {
00049     char *debug_str, *tmp_str;
00050     char *p;
00051     int i;
00052     bool new_setting;
00053 
00054     for ( i=0; i<NUM_DEBUG_MODES; i++ ) {
00055        debug_setting[i] = false;
00056     }
00057 
00058     debug_str = getparam_debug();
00059     tmp_str = debug_str;
00060 
00061     while ( (p = strtok( tmp_str, " " )) != NULL ) {
00062        tmp_str = NULL;
00063 
00064        new_setting = true;
00065 
00066        if ( *p == '-' ) {
00067            p++;
00068            new_setting = false;
00069 
00070            if ( *p == '\0' ) {
00071               print_warning( CONFIGURATION_WARNING, 
00072                             "solitary `-' in debug parameter -- ignored." );
00073               continue;
00074            }
00075        }
00076 
00077        if ( *p == '\0' ) {
00078            continue;
00079        }
00080 
00081 
00082        if ( strcmp( p, "all" ) == 0 ) {
00083            for (i=0; i<NUM_DEBUG_MODES; i++) {
00084               debug_setting[i] = new_setting;
00085            }
00086        } else {
00087            for ( i=0; i<NUM_DEBUG_MODES; i++ ) {
00088               if ( strcmp( p, debug_desc[i] ) == 0 ) {
00089                   debug_setting[i] = new_setting;
00090                   break;
00091               }
00092            }
00093 
00094            if ( i == NUM_DEBUG_MODES ) {
00095               print_warning( CONFIGURATION_WARNING,
00096                             "unrecognized debug mode `%s'", p );
00097            }
00098        }
00099     }
00100 }
00101 
00102 bool debug_mode_is_active( debug_mode_t mode )
00103 {
00104     return debug_setting[ mode ];
00105 }
00106 
00107 void debug_mode_set_active( debug_mode_t mode, bool active )
00108 {
00109     check_assertion( mode >= 0 &&
00110                    mode < NUM_DEBUG_MODES,
00111                    "Invalid debug mode" );
00112 
00113     debug_setting[ mode ] = active;
00114 }
00115 
00116 void print_debug( debug_mode_t mode, char *fmt, ... )
00117 {
00118     va_list args;
00119 
00120     check_assertion( 0 <= mode && mode < NUM_DEBUG_MODES,
00121                    "invalid debugging mode" );
00122 
00123     if ( ! debug_mode_is_active( mode ) ) {
00124        return;
00125     }
00126 
00127     va_start( args, fmt );
00128 
00129     fprintf( stderr, PROG_NAME " debug (%s): ", debug_desc[ mode ] );
00130     vfprintf( stderr, fmt, args );
00131     fprintf( stderr, "\n" );
00132 
00133     va_end( args );
00134 }
00135 
00136 
00137 
00138 /*---------------------------------------------------------------------------*/
00146 void setup_diagnostic_log()
00147 {
00148     FILE *newfp;
00149     time_t t;
00150     char os_buff[BUFF_LEN];
00151     char time_buff[BUFF_LEN];
00152 
00153     /* Activate a bunch of debugging modes */
00154     debug_mode_set_active( DEBUG_QUADTREE, true );
00155     debug_mode_set_active( DEBUG_CONTROL, true );
00156     debug_mode_set_active( DEBUG_SOUND, true );
00157     debug_mode_set_active( DEBUG_TEXTURE, true );
00158     debug_mode_set_active( DEBUG_VIEW, true );
00159     debug_mode_set_active( DEBUG_GL_EXT, true );
00160     debug_mode_set_active( DEBUG_FONT, true );
00161     debug_mode_set_active( DEBUG_UI, true );
00162     debug_mode_set_active( DEBUG_GAME_LOGIC, true );
00163     debug_mode_set_active( DEBUG_SAVE, true );
00164     debug_mode_set_active( DEBUG_JOYSTICK, true );
00165     debug_mode_set_active( DEBUG_GL_INFO, true );
00166 
00167     /* Redirect stderr to file; taken from SDL_main.c, which is in the 
00168        public domain */
00169     newfp = freopen(BUGREPORT_FILE, "w", stderr);
00170     if ( newfp == NULL ) {  /* This happens on NT */
00171 #if !defined(stderr)
00172        stderr = fopen(BUGREPORT_FILE, "w");
00173 #else
00174        newfp = fopen(BUGREPORT_FILE, "w");
00175        if ( newfp ) {
00176            *stderr = *newfp;
00177        }
00178 #endif
00179     }
00180 
00181     /* Write bug report header */
00182     fprintf( stderr, "PlanetPenguin Racer Diagnostic Log\n\n" );
00183 
00184     /* Generate time string */
00185     t = time( NULL );
00186     sprintf( time_buff, "%s", asctime( gmtime( &t ) ) );
00187     time_buff[ strlen(time_buff)-1 ] = (char)0; /* remove trailing newline */
00188 
00189     fprintf( stderr, "Generated:       %s GMT\n", time_buff );
00190     fprintf( stderr, "PPRacer Version:      %s\n", VERSION );
00191     fprintf( stderr, "OS:              " );
00192 
00193     if ( get_os_version( os_buff, sizeof( os_buff ) ) == 0 ) {
00194        fprintf( stderr, "%s\n", os_buff );
00195     } else {
00196        fprintf( stderr, "Could not determine!\n" );
00197     }
00198 
00199     fprintf( stderr, "\n" );
00200 }