Show
Ignore:
Timestamp:
10/22/07 14:55:31 (16 years ago)
Author:
ppalmers
Message:

- make the backlog actually do what it is supposed to do
- make the backlog thread safe

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/libffado/src/debugmodule/debugmodule.cpp

    r670 r671  
    8484                         ... ) const 
    8585{ 
     86    va_list arg; 
     87 
     88    va_start( arg, format ); 
     89    DebugModuleManager::instance()->backlog_va_print( format, arg ); 
     90    va_end( arg ); 
     91 
    8692    if ( level > m_level ) { 
    8793        return; 
    8894    } 
    8995 
    90     va_list arg; 
    91  
    9296    va_start( arg, format ); 
    93  
    9497    DebugModuleManager::instance()->va_print( format, arg ); 
    95  
    9698    va_end( arg ); 
    9799} 
     
    105107                    ... ) const 
    106108{ 
    107     if ( level > m_level ) { 
    108         return; 
    109     } 
    110  
    111109    va_list arg; 
    112110    va_start( arg, format ); 
     
    125123    uint32_t ts_usec=(uint32_t)(ts.tv_sec * 1000000LL + ts.tv_nsec / 1000LL); 
    126124     
     125    va_start( arg, format ); 
     126    DebugModuleManager::instance()->backlog_print( "%010lu: %s (%s)[%4d] %s: ",  
     127                 ts_usec, getPreSequence( level ), 
     128                 fname,  line,  function ); 
     129    DebugModuleManager::instance()->backlog_va_print( format, arg ); 
     130    DebugModuleManager::instance()->backlog_print( "%s", getPostSequence( level ) ); 
     131    va_end( arg ); 
     132 
     133    if ( level > m_level ) { 
     134        return; 
     135    } 
     136 
     137    va_start( arg, format ); 
    127138    DebugModuleManager::instance()->print( "%010lu: %s (%s)[%4d] %s: ",  
    128139                 ts_usec, getPreSequence( level ), 
     
    214225    mb_overruns = 0; 
    215226    mb_initialized = 1; 
     227 
     228    pthread_mutex_init(&bl_mb_write_lock, NULL); 
    216229 
    217230    if (pthread_create(&mb_writer_thread, NULL, &mb_thread_func, (void *)this) != 0) 
     
    368381 
    369382    return NULL; 
     383} 
     384 
     385void 
     386DebugModuleManager::backlog_print(const char *fmt, ...) 
     387{ 
     388    char msg[MB_BUFFERSIZE]; 
     389    va_list ap; 
     390 
     391    unsigned int ntries; 
     392    struct timespec wait = {0,50000}; 
     393 
     394    /* format the message first */ 
     395    va_start(ap, fmt); 
     396    if (vsnprintf(msg, MB_BUFFERSIZE, fmt, ap) >= MB_BUFFERSIZE) { 
     397        print("WARNING: message truncated!\n"); 
     398    } 
     399    va_end(ap); 
     400 
     401    // the backlog 
     402    ntries=5; 
     403    while (ntries) { // try a few times 
     404        if (pthread_mutex_trylock(&bl_mb_write_lock) == 0) { 
     405            strncpy(bl_mb_buffers[bl_mb_inbuffer], msg, BACKLOG_MB_BUFFERSIZE); 
     406            bl_mb_inbuffer = BACKLOG_MB_NEXT(bl_mb_inbuffer); 
     407            pthread_mutex_unlock(&bl_mb_write_lock); 
     408            break; 
     409        } else { 
     410            nanosleep(&wait, NULL); 
     411            ntries--; 
     412        } 
     413    } 
     414    // just bail out should it have failed 
    370415} 
    371416 
     
    425470 
    426471void 
     472DebugModuleManager::backlog_va_print (const char *fmt, va_list ap) 
     473{ 
     474    char msg[MB_BUFFERSIZE]; 
     475    unsigned int ntries; 
     476    struct timespec wait = {0,50000}; 
     477 
     478    /* format the message first */ 
     479    if (vsnprintf(msg, MB_BUFFERSIZE, fmt, ap) >= MB_BUFFERSIZE) { 
     480        print("WARNING: message truncated!\n"); 
     481    } 
     482 
     483    // the backlog 
     484    ntries=5; 
     485    while (ntries) { // try a few times 
     486        if (pthread_mutex_trylock(&bl_mb_write_lock) == 0) { 
     487            strncpy(bl_mb_buffers[bl_mb_inbuffer], msg, BACKLOG_MB_BUFFERSIZE); 
     488            bl_mb_inbuffer = BACKLOG_MB_NEXT(bl_mb_inbuffer); 
     489            pthread_mutex_unlock(&bl_mb_write_lock); 
     490            break; 
     491        } else { 
     492            nanosleep(&wait, NULL); 
     493            ntries--; 
     494        } 
     495    } 
     496    // just bail out should it have failed 
     497} 
     498 
     499void 
    427500DebugModuleManager::va_print (const char *fmt, va_list ap) 
    428501{ 
  • trunk/libffado/src/debugmodule/debugmodule.h

    r670 r671  
    233233    void va_print(const char *fmt, va_list); 
    234234 
     235    void backlog_print(const char *fmt, ...); 
     236    void backlog_va_print(const char *fmt, va_list); 
     237 
    235238private: 
    236239    DebugModuleManager(); 
     
    255258    char bl_mb_buffers[BACKLOG_MB_BUFFERS][BACKLOG_MB_BUFFERSIZE]; 
    256259    unsigned int bl_mb_inbuffer; 
    257     void bl_mb_flush()
     260    pthread_mutex_t bl_mb_write_lock
    258261 
    259262    static DebugModuleManager* m_instance;