static void printMessage( FILE * logfile, int level, const char * name, const char * message, const char * file, int line ) { if( logfile != NULL ) { char timestr[64]; tr_getLogTimeStr( timestr, sizeof( timestr ) ); if( name ) fprintf( logfile, "[%s] %s %s (%s:%d)\n", timestr, name, message, file, line ); else fprintf( logfile, "[%s] %s (%s:%d)\n", timestr, message, file, line ); } #ifdef HAVE_SYSLOG else /* daemon... write to syslog */ { int priority; /* figure out the syslog priority */ switch( level ) { case TR_MSG_ERR: priority = LOG_ERR; break; case TR_MSG_DBG: priority = LOG_DEBUG; break; default: priority = LOG_INFO; break; } if( name ) syslog( priority, "%s %s (%s:%d)", name, message, file, line ); else syslog( priority, "%s (%s:%d)", message, file, line ); } #endif }
void tr_deepLog( const char * file, int line, const char * name, const char * fmt, ... ) { FILE * fp = tr_getLog( ); if( fp || IsDebuggerPresent( ) ) { va_list args; char timestr[64]; struct evbuffer * buf = evbuffer_new( ); char * base = tr_basename( file ); evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( timestr, sizeof( timestr ) ) ); if( name ) evbuffer_add_printf( buf, "%s ", name ); va_start( args, fmt ); evbuffer_add_vprintf( buf, fmt, args ); va_end( args ); evbuffer_add_printf( buf, " (%s:%d)\n", base, line ); /* FIXME(libevent2) ifdef this out for nonwindows platforms */ OutputDebugString( evbuffer_pullup( buf, -1 ) ); if( fp ) fputs( (const char*)evbuffer_pullup( buf, -1 ), fp ); tr_free( base ); evbuffer_free( buf ); } }
void tr_deepLog( const char * file, int line, const char * name, const char * fmt, ... ) { FILE * fp = tr_getLog( ); if( fp || IsDebuggerPresent( ) ) { va_list args; char timestr[64]; struct evbuffer * buf = evbuffer_new( ); char * base = tr_basename( file ); evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( timestr, sizeof( timestr ) ) ); if( name ) evbuffer_add_printf( buf, "%s ", name ); va_start( args, fmt ); evbuffer_add_vprintf( buf, fmt, args ); va_end( args ); evbuffer_add_printf( buf, " (%s:%d)\n", base, line ); /* FIXME(libevent2) ifdef this out for nonwindows platforms */ OutputDebugString( EVBUFFER_DATA( buf ) ); if(fp) /* FIXME(libevent2) tr_getLog() should return an fd, then use evbuffer_write() here ) */ (void) fwrite( EVBUFFER_DATA( buf ), 1, EVBUFFER_LENGTH( buf ), fp ); tr_free( base ); evbuffer_free( buf ); } }
void tr_msg( const char * file, int line, tr_msg_level level, const char * name, const char * fmt, ... ) { const int err = errno; /* message logging shouldn't affect errno */ char buf[1024]; va_list ap; tr_lockLock( getMessageLock( ) ); /* build the text message */ *buf = '\0'; va_start( ap, fmt ); evutil_vsnprintf( buf, sizeof( buf ), fmt, ap ); va_end( ap ); OutputDebugString( buf ); if( *buf ) { if( messageQueuing ) { tr_msg_list * newmsg; newmsg = tr_new0( tr_msg_list, 1 ); newmsg->level = level; newmsg->when = tr_time( ); newmsg->message = tr_strdup( buf ); newmsg->file = file; newmsg->line = line; newmsg->name = tr_strdup( name ); *messageQueueTail = newmsg; messageQueueTail = &newmsg->next; ++messageQueueCount; if( messageQueueCount > TR_MAX_MSG_LOG ) { tr_msg_list * old = messageQueue; messageQueue = old->next; old->next = NULL; tr_freeMessageList(old); --messageQueueCount; assert( messageQueueCount == TR_MAX_MSG_LOG ); } } else { char timestr[64]; FILE * fp; fp = tr_getLog( ); if( fp == NULL ) fp = stderr; tr_getLogTimeStr( timestr, sizeof( timestr ) ); if( name ) fprintf( fp, "[%s] %s: %s\n", timestr, name, buf ); else fprintf( fp, "[%s] %s\n", timestr, buf ); fflush( fp ); } } tr_lockUnlock( getMessageLock( ) ); errno = err; }