Show
Ignore:
Timestamp:
06/13/09 04:10:36 (4 years ago)
Author:
gabriel@…
Message:

Change logging queue from std::vector to std::list and reduce locking.

The Logger class was locking up the logging queue while outputting
messages to stdout. This was commit:

* Changes the queue implementation from std::vector to

std::list, which reduces the need for locking and
prevents a possible total reallocation of memory
(when std::vector::max_size() is exceeded).

* Consolidates the logging functions to a single

Logger::log() function.

* Reimplements the macros to allow quick and fast

log filtering by class (Error, Warning, Info, etc.).

* Removes some dead code in object.cpp.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/jack_zombies/libs/hydrogen/src/object.cpp

    r683 r1181  
    2626#include <iostream> 
    2727#include <pthread.h> 
     28#include <cassert> 
    2829 
    2930#ifdef WIN32 
     
    3940std::map<QString, int> Object::__object_map; 
    4041 
    41  
    42  
     42namespace H2Core { 
     43    unsigned logLevel = ~0;  // Turn on all logging. 
     44} 
    4345 
    4446/** 
     
    197199                Sleep( 1000 ); 
    198200#else 
    199                 usleep( 1000000 ); 
    200 #endif 
    201                 pthread_mutex_lock( &pLogger->__logger_mutex ); 
    202  
    203  
    204                 vector<QString>::iterator it; 
     201                usleep( 999999 ); 
     202#endif 
     203 
     204                Logger::queue_t& queue = pLogger->__msg_queue; 
     205                Logger::queue_t::iterator it, last; 
    205206                QString tmpString; 
    206                 while ( ( it  = pLogger->__msg_queue.begin() ) != pLogger->__msg_queue.end() ) { 
    207                         tmpString = *it; 
    208                         pLogger->__msg_queue.erase( it ); 
    209                         printf( tmpString.toAscii() ); 
    210  
    211                         if ( pLogFile ) { 
    212                                 fprintf( pLogFile, tmpString.toAscii() ); 
     207                for( it = last = queue.begin() ; it != queue.end() ; ++it ) { 
     208                        last = it; 
     209                        printf( it->toLocal8Bit() ); 
     210                        if( pLogFile ) { 
     211                                fprintf( pLogFile, it->toLocal8Bit() ); 
    213212                                fflush( pLogFile ); 
    214213                        } 
    215214                } 
    216                 pthread_mutex_unlock( &pLogger->__logger_mutex ); 
     215                pthread_mutex_lock( &pLogger->__mutex ); 
     216                queue.erase( queue.begin(), last ); 
     217                if( ! queue.empty() ) queue.pop_front(); 
     218                pthread_mutex_unlock( &pLogger->__mutex ); 
    217219        } 
    218220 
     
    236238} 
    237239 
    238  
    239240Logger* Logger::get_instance() 
    240241{ 
     
    244245        return __instance; 
    245246} 
    246  
    247247 
    248248/** 
     
    255255        pthread_attr_t attr; 
    256256        pthread_attr_init( &attr ); 
    257  
    258         pthread_mutex_init( &__logger_mutex, NULL ); 
    259  
    260  
     257        pthread_mutex_init( &__mutex, NULL ); 
    261258        pthread_create( &loggerThread, &attr, loggerThread_func, this ); 
    262259} 
    263  
    264  
    265  
    266260 
    267261/** 
     
    275269} 
    276270 
    277  
    278  
    279  
    280  
    281 void Logger::info_log( const char* funcname, const QString& class_name, const QString& logMsg ) 
    282 { 
    283         if ( !Object::is_using_verbose_log() ) return; 
    284  
    285         pthread_mutex_lock( &__logger_mutex ); 
    286  
    287         QString prefix = "(I) " + class_name + "\t" + funcname; 
    288  
    289 #ifdef WIN32 
    290         __msg_queue.push_back( QString( "%1 %2\n" ).arg(prefix).arg(logMsg).toUtf8() ); 
    291 #else 
    292         __msg_queue.push_back( QString( "\033[32m%1 %2 \033[0m\n" ).arg(prefix).arg(logMsg) ); 
    293 #endif 
    294  
    295         pthread_mutex_unlock( &__logger_mutex ); 
    296  
    297 } 
    298  
    299  
    300  
    301  
    302 void Logger::warning_log( const char* funcname, const QString& class_name, const QString& logMsg ) 
    303 { 
    304         pthread_mutex_lock( &__logger_mutex ); 
    305         QString prefix = "(W) " + class_name + "\t" + funcname; 
    306  
    307 #ifdef WIN32 
    308         __msg_queue.push_back( prefix + logMsg + "\n" ); 
    309 #else 
    310         __msg_queue.push_back( QString( "\033[36m%1 %2\033[0m\n" ).arg( prefix ).arg( logMsg ) ); 
    311 #endif 
    312  
    313         pthread_mutex_unlock( &__logger_mutex ); 
    314 } 
    315  
    316  
    317  
    318 void Logger::error_log( const char* funcname, const QString& class_name, const QString& logMsg ) 
    319 { 
    320         pthread_mutex_lock( &__logger_mutex ); 
    321  
    322         QString prefix = QString( "(E) %1\t%2" ).arg( class_name ).arg( funcname ); 
    323  
    324 #ifdef WIN32 
    325         __msg_queue.push_back( prefix + logMsg + "\n" ); 
    326 #else 
    327         __msg_queue.push_back( QString( "\033[31m%1 %2\033[0m\n" ).arg( prefix ).arg( logMsg ) ); 
    328 #endif 
    329  
    330         pthread_mutex_unlock( &__logger_mutex ); 
    331 } 
    332  
    333  
    334  
    335  
    336 /* 
    337 #ifndef WIN32 
    338  
    339  
    340 static const WORD bgMask( BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY ); 
    341  
    342  
    343 void Logger__setREDColor() 
    344 { 
    345         CONSOLE_SCREEN_BUFFER_INFO csbi; 
    346         HANDLE hCon = GetStdHandle( STD_OUTPUT_HANDLE ); 
    347         GetConsoleScreenBufferInfo(hCon, &csbi); 
    348  
    349 //      WORD wRGBI = GetRGBI(fgColor, FOREGROUND_INTENSITY, FOREGROUND_RED, FOREGROUND_GREEN, FOREGROUND_BLUE); 
    350         WORD color = 10 | 255; 
    351         csbi.wAttributes &= bgMask; 
    352         csbi.wAttributes |= color; 
    353  
    354         SetConsoleTextAttribute( hCon, csbi.wAttributes ); 
    355  
    356 //      wRGBI = GetRGBI(bgColor, BACKGROUND_INTENSITY, BACKGROUND_RED, BACKGROUND_GREEN, BACKGROUND_BLUE); 
    357  
    358 //      csbi.wAttributes &= fgMask; 
    359 //      csbi.wAttributes |= wRGBI; 
    360  
    361         SetConsoleTextAttribute( hCon, csbi.wAttributes ); 
    362 } 
    363  
    364  
    365 #endif 
    366 */ 
    367  
     271void Logger::log( Logger::log_level_t level, 
     272                  const char* funcname, 
     273                  const QString& class_name, 
     274                  const QString& msg ) 
     275{ 
     276        if( level == None ) return; 
     277 
     278        const char* prefix[] = { "(E)", "(W)", "(I)", "(D)" }; 
     279#ifdef WIN32 
     280        const char* color[] = { "", "", "", "" }; 
     281#else 
     282        const char* color[] = { "\033[31m", "\033[36m", "\033[32m", "" }; 
     283#endif // WIN32 
     284 
     285        int i; 
     286        switch(level) { 
     287        case None: 
     288                assert(false); 
     289                i = 0; 
     290                break; 
     291        case Error: 
     292                i = 0; 
     293                break; 
     294        case Warning: 
     295                i = 1; 
     296                break; 
     297        case Info: 
     298                i = 2; 
     299                break; 
     300        case Debug: 
     301                i = 3; 
     302                break; 
     303        default: 
     304                i = 0; 
     305                break; 
     306        } 
     307 
     308        QString tmp = QString("%1%2 %3\t%4 %5 \033[0m\n") 
     309                .arg(color[i]) 
     310                .arg(prefix[i]) 
     311                .arg(class_name) 
     312                .arg(funcname) 
     313                .arg(msg); 
     314 
     315        pthread_mutex_lock( &__mutex); 
     316        __msg_queue.push_back( tmp ); 
     317        pthread_mutex_unlock( &__mutex ); 
     318}