/* $Id: global.cpp,v 1.19 2005/07/21 07:56:31 uwe Exp $ */ #include "global.hpp" #include #include #include #include #include "wopsettings.hpp" /********************************************************** * MANUAL settings for this file **********************************************************/ ////////////////////////////////////////////////// // // The message ring buffer: // The continuous printing of INFO and CHECK messages can be // suppressed by using a ring buffer of size N, that collects // only the last N messages and prints them out in case of a // failed ASSERT. To switch on this feature, #define the // constant USE_MESSAGE_RING_BUFFER. //#define USE_MESSAGE_RING_BUFFER // The sensitize the ring buffers flush to be triggered also // by a failed CHECK, #define PRINT_MESSAGE_RING_ON_WARNING. //#define PRINT_MESSAGE_RING_ON_WARNING // On a buffer flush, the last N messages are printed to the // stream "logFile". If the previous flush lays back less than // N messages, only the new messages are printed, introduced // by a hint to this fact and the number of new messages. The // maximal number of kept messages N can be #defined optionally // with the constant MESSAGE_RING_BUFFER_SIZE (default 30). //#define MESSAGE_RING_BUFFER_SIZE 100 // ////////////////////////////////////////////////// /**********************************************************/ #ifdef SUPPRESS_COLORED_OUTPUT #define BLACK(s) s #define BLINKINK_BLACK_ON_RED(s) s #define GREEN(s) s #define RED(s) s #else #define BLACK(s) "\033[0;1m"s"\033[0m" #define BLINKINK_BLACK_ON_RED(s) "\033[05;1;41m"s"\033[0m" #define GREEN(s) "\033[32m"s"\033[0m" #define RED(s) "\033[31m"s"\033[0m" #endif /**********************************************************/ #define VFPRINTF va_list arg;\ va_start( arg, format );\ vfprintf( logFile, format, arg );\ va_end( arg ); /********************************************************** * collection of messages in ring buffer **********************************************************/ #ifdef USE_MESSAGE_RING_BUFFER #ifndef MESSAGE_RING_BUFFER_SIZE #define MESSAGE_RING_BUFFER_SIZE 30 #endif #include "ringbuffer.hpp" #include "string.hpp" RingBuffer msgRing( MESSAGE_RING_BUFFER_SIZE, true ); #define VFORMAT( string, intro ) \ va_list arg;\ va_start( arg, format );\ string = intro; \ String tmpFormat; \ tmpFormat.vformat( format, arg ); \ string += tmpFormat; \ va_end( arg ); #endif /**********************************************************/ static FILE* logFile = stderr; static int flushCounter = 0; /**********************************************************/ void INFO( const char* format, ... ) { #ifdef USE_MESSAGE_RING_BUFFER VFORMAT( msgRing[msgRing.getMaxSize()-1], GREEN("INFO: ") ) msgRing.shift( 1 ); #else fprintf( logFile, GREEN("INFO: ") ); VFPRINTF if( ++flushCounter >= 20 ) { FLUSH_LOG(); } #endif } /**********************************************************/ bool CHECK( const bool test, const char* format, ... ) { if( ! test ) { #ifdef USE_MESSAGE_RING_BUFFER VFORMAT( msgRing[msgRing.getMaxSize()-1], RED("WARNING: ") ) msgRing.shift( 1 ); #ifdef PRINT_MESSAGE_RING_ON_WARNING FLUSH_LOG(); #endif #else fprintf( logFile, RED("WARNING: ") ); VFPRINTF FLUSH_LOG(); #endif } return test; } /**********************************************************/ bool ASSERT( const bool test, const char* format, ... ) { if( test ) return true; #ifdef USE_MESSAGE_RING_BUFFER VFORMAT( msgRing[msgRing.getMaxSize()-1], BLACK("ERROR: ") ) msgRing.shift( 1 ); #else fprintf( logFile, BLACK("ERROR: ") ); VFPRINTF #endif FLUSH_LOG(); if ( WopSettings::getInstance()->getDebug() ) { abort(); } else { exit( 1 ); } } /**********************************************************/ void FLUSH_LOG() { #ifdef USE_MESSAGE_RING_BUFFER Uint32 numMsg = 0; for( Uint32 i = 0; i < msgRing.getSize(); i++ ) { if( msgRing[i].getString() ) numMsg++; } fprintf( logFile, numMsg == MESSAGE_RING_BUFFER_SIZE ? BLACK("LAST %d MESSAGES:\n"), numMsg ) : BLACK("%d MESSAGES since last flush"), numMsg ); // print only the valid messages in the ring buffer for( Uint32 i = 0; i < msgRing.getSize(); i++ ) { if( msgRing[i].getString() ) { fprintf( logFile, msgRing[i].getString() ); // reset all messages in flush, to mark them // as already printed msgRing[i].reset(); } } #endif fflush( logFile ); flushCounter = 0; } /**********************************************************/ void REDIRECT_LOG( const char* filename ) { logFile = stderr; if( filename != NULL ) { logFile = fopen( filename, "w" ); if( logFile == NULL ) { logFile = stderr; CHECK( false, "INIT_LOG: could not open log file '%s' " "=> printing to stderr\n", filename ); } } } /**********************************************************/ void makePace() { static int delay = DESIRED_DIFF - 10; static int lastTicks; const int currentTicks = SDL_GetTicks(), diff = currentTicks - lastTicks; lastTicks = currentTicks; if( diff < DESIRED_DIFF ) delay++; else if( diff > DESIRED_DIFF && delay > 0 ) delay--; // LOG( 2 ) INFO( "MakePace: Delay is %i\n", delay ); SDL_Delay( delay ); } /**********************************************************/