87 | | |
---|
| 89 | char msg[MB_BUFFERSIZE]; |
---|
| 90 | |
---|
| 91 | // format the message such that it remains together |
---|
| 92 | int chars_written=0; |
---|
| 93 | int retval=0; |
---|
| 94 | |
---|
| 95 | va_start( arg, format ); |
---|
| 96 | retval = vsnprintf(msg+chars_written, MB_BUFFERSIZE, format, arg); |
---|
| 97 | va_end( arg ); |
---|
| 98 | if (retval >= 0) { // ignore errors |
---|
| 99 | chars_written += retval; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | // output a warning if the message was truncated |
---|
| 103 | if (chars_written == MB_BUFFERSIZE) { |
---|
| 104 | snprintf(msg+MB_BUFFERSIZE-warning_size, warning_size, "%s", warning); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // print to backlog if necessary |
---|
125 | | uint32_t ts_usec=(uint32_t)(ts.tv_sec * 1000000LL + ts.tv_nsec / 1000LL); |
---|
126 | | |
---|
| 143 | long unsigned int ts_usec=(uint32_t)(ts.tv_sec * 1000000LL + ts.tv_nsec / 1000LL); |
---|
| 144 | |
---|
| 145 | // format the message such that it remains together |
---|
| 146 | int chars_written=0; |
---|
| 147 | int retval=0; |
---|
| 148 | retval = snprintf(msg, MB_BUFFERSIZE, "%010lu: %s (%s)[%4d] %s: ", |
---|
| 149 | ts_usec, getPreSequence( level ), |
---|
| 150 | fname, line, function ); |
---|
| 151 | if (retval >= 0) chars_written += retval; // ignore errors |
---|
| 152 | |
---|
| 153 | va_start( arg, format ); |
---|
| 154 | retval = vsnprintf( msg + chars_written, |
---|
| 155 | MB_BUFFERSIZE - chars_written, |
---|
| 156 | format, arg); |
---|
| 157 | va_end( arg ); |
---|
| 158 | if (retval >= 0) chars_written += retval; // ignore errors |
---|
| 159 | |
---|
| 160 | retval = snprintf( msg + chars_written, |
---|
| 161 | MB_BUFFERSIZE - chars_written, |
---|
| 162 | "%s", getPostSequence( level ) ); |
---|
| 163 | if (retval >= 0) chars_written += retval; // ignore errors |
---|
| 164 | |
---|
| 165 | // output a warning if the message was truncated |
---|
| 166 | if (chars_written == MB_BUFFERSIZE) { |
---|
| 167 | snprintf(msg + MB_BUFFERSIZE - warning_size, |
---|
| 168 | warning_size, |
---|
| 169 | "%s", warning); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | // print to backlog if necessary |
---|
128 | | va_start( arg, format ); |
---|
129 | | DebugModuleManager::instance()->backlog_print( "%010lu: %s (%s)[%4d] %s: ", |
---|
130 | | ts_usec, getPreSequence( level ), |
---|
131 | | fname, line, function ); |
---|
132 | | DebugModuleManager::instance()->backlog_va_print( format, arg ); |
---|
133 | | DebugModuleManager::instance()->backlog_print( "%s", getPostSequence( level ) ); |
---|
134 | | va_end( arg ); |
---|
135 | | } |
---|
136 | | |
---|
137 | | if ( level > m_level ) { |
---|
138 | | return; |
---|
139 | | } |
---|
140 | | |
---|
141 | | va_start( arg, format ); |
---|
142 | | DebugModuleManager::instance()->print( "%010lu: %s (%s)[%4d] %s: ", |
---|
143 | | ts_usec, getPreSequence( level ), |
---|
144 | | fname, line, function ); |
---|
145 | | DebugModuleManager::instance()->va_print( format, arg ); |
---|
146 | | DebugModuleManager::instance()->print( "%s", getPostSequence( level ) ); |
---|
147 | | va_end( arg ); |
---|
| 174 | DebugModuleManager::instance()->backlog_print( msg ); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | // print to stderr if necessary |
---|
| 178 | if ( level <= m_level ) { |
---|
| 179 | DebugModuleManager::instance()->print( msg ); |
---|
| 180 | } |
---|
482 | | void |
---|
483 | | DebugModuleManager::backlog_va_print (const char *fmt, va_list ap) |
---|
484 | | { |
---|
485 | | const char *warning = "WARNING: message truncated!\n"; |
---|
486 | | const int warning_size = 32; |
---|
487 | | char msg[MB_BUFFERSIZE]; |
---|
488 | | unsigned int ntries; |
---|
489 | | struct timespec wait = {0,50000}; |
---|
490 | | |
---|
491 | | /* format the message first */ |
---|
492 | | if (vsnprintf(msg, BACKLOG_MB_BUFFERSIZE-warning_size, fmt, ap) |
---|
493 | | >= BACKLOG_MB_BUFFERSIZE-warning_size) { |
---|
494 | | // output a warning if the message was truncated |
---|
495 | | snprintf(msg+BACKLOG_MB_BUFFERSIZE-warning_size, warning_size, "%s", warning); |
---|
496 | | } |
---|
497 | | |
---|
498 | | // the backlog |
---|
499 | | ntries=5; |
---|
500 | | while (ntries) { // try a few times |
---|
501 | | if (pthread_mutex_trylock(&bl_mb_write_lock) == 0) { |
---|
502 | | strncpy(bl_mb_buffers[bl_mb_inbuffer], msg, BACKLOG_MB_BUFFERSIZE); |
---|
503 | | bl_mb_inbuffer = BACKLOG_MB_NEXT(bl_mb_inbuffer); |
---|
504 | | pthread_mutex_unlock(&bl_mb_write_lock); |
---|
505 | | break; |
---|
506 | | } else { |
---|
507 | | nanosleep(&wait, NULL); |
---|
508 | | ntries--; |
---|
509 | | } |
---|
510 | | } |
---|
511 | | // just bail out should it have failed |
---|
512 | | } |
---|
513 | | |
---|
514 | | void |
---|
515 | | DebugModuleManager::va_print (const char *fmt, va_list ap) |
---|
516 | | { |
---|
517 | | const char *warning = "WARNING: message truncated!\n"; |
---|
518 | | const int warning_size = 32; |
---|
519 | | char msg[MB_BUFFERSIZE]; |
---|
520 | | |
---|
521 | | #ifdef DO_MESSAGE_BUFFER_PRINT |
---|
522 | | unsigned int ntries; |
---|
523 | | struct timespec wait = {0,50000}; |
---|
524 | | #endif |
---|
525 | | |
---|
526 | | /* format the message first, to reduce lock contention */ |
---|
527 | | if (vsnprintf(msg, MB_BUFFERSIZE-warning_size, fmt, ap) >= MB_BUFFERSIZE-warning_size) { |
---|
528 | | // output a warning if the message was truncated |
---|
529 | | snprintf(msg+MB_BUFFERSIZE-warning_size, warning_size, "%s", warning); |
---|
530 | | } |
---|
531 | | |
---|
532 | | if (!mb_initialized) { |
---|
533 | | /* Unable to print message with realtime safety. |
---|
534 | | * Complain and print it anyway. */ |
---|
535 | | fprintf(stderr, "ERROR: messagebuffer not initialized: %s", |
---|
536 | | msg); |
---|
537 | | return; |
---|
538 | | } |
---|
539 | | |
---|
540 | | #ifdef DO_MESSAGE_BUFFER_PRINT |
---|
541 | | ntries=5; |
---|
542 | | while (ntries) { // try a few times |
---|
543 | | if (pthread_mutex_trylock(&mb_write_lock) == 0) { |
---|
544 | | strncpy(mb_buffers[mb_inbuffer], msg, MB_BUFFERSIZE); |
---|
545 | | mb_inbuffer = MB_NEXT(mb_inbuffer); |
---|
546 | | pthread_cond_signal(&mb_ready_cond); |
---|
547 | | pthread_mutex_unlock(&mb_write_lock); |
---|
548 | | break; |
---|
549 | | } else { |
---|
550 | | nanosleep(&wait, NULL); |
---|
551 | | ntries--; |
---|
552 | | } |
---|
553 | | } |
---|
554 | | |
---|
555 | | if (ntries==0) { /* lock collision */ |
---|
556 | | // atomic_add(&mb_overruns, 1); |
---|
557 | | // FIXME: atomicity |
---|
558 | | mb_overruns++; // skip the atomicness for now |
---|
559 | | } |
---|
560 | | #else |
---|
561 | | fprintf(stderr,msg); |
---|
562 | | #endif |
---|
563 | | } |
---|
564 | | |
---|