Changeset 5689


Ignore:
Timestamp:
Apr 25, 2008, 2:57:33 AM (14 years ago)
Author:
charles
Message:

fold most of the "deep logging" functions into a single function

Location:
trunk/libtransmission
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r5455 r5689  
    4141#endif
    4242#include <unistd.h>
    43 #include <libgen.h> /* basename, dirname */
     43#include <libgen.h> /* dirname */
    4444#include <fcntl.h> /* O_LARGEFILE */
    4545
     
    6060#endif
    6161
    62 /**
    63 ***
    64 **/
    65 
    66 static void
    67 myDebug( const char * file, int line, const char * fmt, ... )
    68 {
    69     FILE * fp = tr_getLog( );
    70     if( fp != NULL )
    71     {
    72         va_list args;
    73         char s[64];
    74         struct evbuffer * buf = evbuffer_new( );
    75         char * myfile = tr_strdup( file );
    76 
    77         evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( s, sizeof(s) ) );
    78         va_start( args, fmt );
    79         evbuffer_add_vprintf( buf, fmt, args );
    80         va_end( args );
    81         evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line );
    82         fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp );
    83 
    84         tr_free( myfile );
    85         evbuffer_free( buf );
    86     }
    87 }
    88 
    89 #define dbgmsg(fmt...) myDebug(__FILE__, __LINE__, ##fmt )
     62#define dbgmsg(fmt...) tr_deepLog( __FILE__, __LINE__, NULL, ##fmt )
    9063
    9164/**
  • trunk/libtransmission/handshake.c

    r5632 r5689  
    1717#include <string.h>
    1818#include <stdio.h>
    19 #include <libgen.h> /* basename */
    2019
    2120#include <event.h>
     
    135134**/
    136135
    137 static void
    138 myDebug( const char * file, int line, const tr_handshake * handshake, const char * fmt, ... )
    139 {
    140     FILE * fp = tr_getLog( );
    141     if( fp != NULL )
    142     {
    143         va_list args;
    144         char timestr[64];
    145         struct evbuffer * buf = evbuffer_new( );
    146         char * myfile = tr_strdup( file );
    147 
    148         evbuffer_add_printf( buf, "[%s] %s: ",
    149                              tr_getLogTimeStr( timestr, sizeof(timestr) ),
    150                              tr_peerIoGetAddrStr( handshake->io ) );
    151         va_start( args, fmt );
    152         evbuffer_add_vprintf( buf, fmt, args );
    153         va_end( args );
    154         evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line );
    155         fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp );
    156 
    157         tr_free( myfile );
    158         evbuffer_free( buf );
    159     }
    160 }
    161 
    162 #define dbgmsg(handshake, fmt...) myDebug(__FILE__, __LINE__, handshake, ##fmt )
     136#define dbgmsg(handshake, fmt...) tr_deepLog( __FILE__, __LINE__, tr_peerIoGetAddrStr( handshake->io ), ##fmt )
    163137
    164138static const char* getStateName( short state )
  • trunk/libtransmission/peer-mgr.c

    r5650 r5689  
    1616#include <stdio.h> /* printf */
    1717#include <limits.h> /* INT_MAX */
    18 
    19 #include <libgen.h> /* basename */
    2018
    2119#include <event.h>
     
    157155};
    158156
    159 /**
    160 ***
    161 **/
    162 
    163 static void
    164 myDebug( const char * file, int line, const Torrent * t, const char * fmt, ... )
    165 {
    166     FILE * fp = tr_getLog( );
    167     if( fp != NULL )
    168     {
    169         va_list args;
    170         char timestr[64];
    171         struct evbuffer * buf = evbuffer_new( );
    172         char * myfile = tr_strdup( file );
    173 
    174         evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( timestr, sizeof(timestr) ) );
    175         if( t != NULL )
    176             evbuffer_add_printf( buf, "%s ", t->tor->info.name );
    177         va_start( args, fmt );
    178         evbuffer_add_vprintf( buf, fmt, args );
    179         va_end( args );
    180         evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line );
    181         fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp );
    182 
    183         tr_free( myfile );
    184         evbuffer_free( buf );
    185     }
    186 }
    187 
    188 #define tordbg(t, fmt...) myDebug(__FILE__, __LINE__, t, ##fmt )
     157#define tordbg(t, fmt...) tr_deepLog( __FILE__, __LINE__, t->tor->info.name, ##fmt )
    189158
    190159/**
     
    11121081        tr_peerIo * io;
    11131082        tr_handshake * handshake;
    1114 
    1115         tordbg( NULL, "Got an INCOMING connection with %s", tr_peerIoAddrStr( addr, port ) );
    11161083
    11171084        io = tr_peerIoNewIncoming( manager->handle, addr, port, socket );
  • trunk/libtransmission/tracker.c

    r5673 r5689  
    1515#include <stdlib.h>
    1616#include <string.h> /* strcmp, strchr */
    17 #include <libgen.h> /* basename */
    1817
    1918#include <event.h>
     
    127126};
    128127
    129 /**
    130 ***
    131 **/
    132 
    133 static void
    134 myDebug( const char * file, int line, const tr_tracker * t, const char * fmt, ... )
    135 {   
    136     FILE * fp = tr_getLog( );
    137     if( fp != NULL )
    138     {
    139         va_list args;
    140         char timestr[64];
    141         struct evbuffer * buf = evbuffer_new( );
    142         char * myfile = tr_strdup( file );
    143 
    144         evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( timestr, sizeof(timestr) ) );
    145         if( t )
    146             evbuffer_add_printf( buf, "%s ", t->name );
    147         va_start( args, fmt );
    148         evbuffer_add_vprintf( buf, fmt, args );
    149         va_end( args );
    150         evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line );
    151         fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp );
    152 
    153         tr_free( myfile );
    154         evbuffer_free( buf );
    155     }
    156 }
    157 
    158 #define dbgmsg(t, fmt...) myDebug(__FILE__, __LINE__, t, ##fmt )
     128#define dbgmsg(name, fmt...) tr_deepLog(__FILE__, __LINE__, name, ##fmt )
    159129
    160130/***
     
    366336        return;
    367337
    368     dbgmsg( t, "tracker response: %d", responseCode );
     338    dbgmsg( t->name, "tracker response: %d", responseCode );
    369339    tr_ndbg( t->name, "tracker response: %d", responseCode );
    370340    t->lastAnnounceResponse = responseCode;
     
    389359
    390360            if(( tr_bencDictFindInt( &benc, "interval", &i ))) {
    391                 dbgmsg( t, "setting interval to %d", (int)i );
     361                dbgmsg( t->name, "setting interval to %d", (int)i );
    392362                t->announceIntervalSec = i;
    393363            }
    394364
    395365            if(( tr_bencDictFindInt( &benc, "min interval", &i ))) {
    396                 dbgmsg( t, "setting min interval to %d", (int)i );
     366                dbgmsg( t->name, "setting min interval to %d", (int)i );
    397367                t->announceMinIntervalSec = i;
    398368            }
     
    441411    {
    442412        const int interval = t->announceIntervalSec + t->randOffset;
    443         dbgmsg( t, "request succeeded. reannouncing in %d seconds", interval );
     413        dbgmsg( t->name, "request succeeded. reannouncing in %d seconds", interval );
    444414        t->reannounceAt = time( NULL ) + interval;
    445415        t->manualAnnounceAllowedAt = time( NULL ) + t->announceMinIntervalSec;
     
    450420         * parsed the redirect, all that's left is to retry */
    451421        const int interval = 5;
    452         dbgmsg( t, "got a redirect. retrying in %d seconds", interval );
     422        dbgmsg( t->name, "got a redirect. retrying in %d seconds", interval );
    453423        t->reannounceAt = time( NULL ) + interval;
    454424        t->manualAnnounceAllowedAt = time( NULL ) + t->announceMinIntervalSec;
     
    475445    {
    476446        /* WTF did we get?? */
    477         dbgmsg( t, "Invalid response from tracker... retrying in two minutes." );
     447        dbgmsg( t->name, "Invalid response from tracker... retrying in two minutes." );
    478448        t->manualAnnounceAllowedAt = ~(time_t)0;
    479449        t->reannounceAt = time( NULL ) + t->randOffset + 120;
     
    497467        return;
    498468
    499     dbgmsg( t, "scrape response: %ld\n", responseCode );
     469    dbgmsg( t->name, "scrape response: %ld\n", responseCode );
    500470    tr_ndbg( t->name, "scrape response: %d", responseCode );
    501471    t->lastScrapeResponse = responseCode;
     
    556526    {
    557527        const int interval = t->scrapeIntervalSec + t->randOffset;
    558         dbgmsg( t, "request succeeded. rescraping in %d seconds", interval );
     528        dbgmsg( t->name, "request succeeded. rescraping in %d seconds", interval );
    559529        tr_ndbg( t->name, "request succeeded. rescraping in %d seconds", interval );
    560530        t->scrapeAt = time( NULL ) + interval;
     
    563533    {
    564534        const int interval = 5;
    565         dbgmsg( t, "got a redirect. retrying in %d seconds", interval );
     535        dbgmsg( t->name, "got a redirect. retrying in %d seconds", interval );
    566536        t->scrapeAt = time( NULL ) + interval;
    567537    }
     
    569539    {
    570540        const int interval = t->retryScrapeIntervalSec + t->randOffset;
    571         dbgmsg( t, "Tracker responded to scrape with %d.  Retrying in %d seconds.",
     541        dbgmsg( t->name, "Tracker responded to scrape with %d.  Retrying in %d seconds.",
    572542                   responseCode,  interval );
    573543        t->retryScrapeIntervalSec *= 2;
  • trunk/libtransmission/utils.c

    r5673 r5689  
    3131#include <string.h> /* strerror */
    3232
     33#include <libgen.h> /* basename */
    3334#include <sys/time.h>
    3435#include <sys/types.h>
     
    8485}
    8586
     87void
     88tr_setMessageLevel( int level )
     89{
     90    tr_msgInit();
     91    tr_lockLock( messageLock );
     92    messageLevel = MAX( 0, level );
     93    tr_lockUnlock( messageLock );
     94}
     95
     96int
     97tr_getMessageLevel( void )
     98{
     99    int ret;
     100
     101    tr_msgInit();
     102    tr_lockLock( messageLock );
     103    ret = messageLevel;
     104    tr_lockUnlock( messageLock );
     105
     106    return ret;
     107}
     108
     109void
     110tr_setMessageQueuing( int enabled )
     111{
     112    tr_msgInit();
     113    tr_lockLock( messageLock );
     114    messageQueuing = enabled;
     115    tr_lockUnlock( messageLock );
     116}
     117
     118tr_msg_list *
     119tr_getQueuedMessages( void )
     120{
     121    tr_msg_list * ret;
     122
     123    assert( NULL != messageLock );
     124    tr_lockLock( messageLock );
     125    ret = messageQueue;
     126    messageQueue = NULL;
     127    messageQueueTail = &messageQueue;
     128    tr_lockUnlock( messageLock );
     129
     130    return ret;
     131}
     132
     133void
     134tr_freeMessageList( tr_msg_list * list )
     135{
     136    tr_msg_list * next;
     137
     138    while( NULL != list )
     139    {
     140        next = list->next;
     141        free( list->message );
     142        free( list->name );
     143        free( list );
     144        list = next;
     145    }
     146}
     147
     148/**
     149***
     150**/
     151
    86152char*
    87153tr_getLogTimeStr( char * buf, int buflen )
     
    109175
    110176void
    111 tr_setMessageLevel( int level )
    112 {
    113     tr_msgInit();
    114     tr_lockLock( messageLock );
    115     messageLevel = MAX( 0, level );
    116     tr_lockUnlock( messageLock );
    117 }
    118 
    119 int
    120 tr_getMessageLevel( void )
    121 {
    122     int ret;
    123 
    124     tr_msgInit();
    125     tr_lockLock( messageLock );
    126     ret = messageLevel;
    127     tr_lockUnlock( messageLock );
    128 
    129     return ret;
    130 }
    131 
    132 void
    133 tr_setMessageQueuing( int enabled )
    134 {
    135     tr_msgInit();
    136     tr_lockLock( messageLock );
    137     messageQueuing = enabled;
    138     tr_lockUnlock( messageLock );
    139 }
    140 
    141 tr_msg_list *
    142 tr_getQueuedMessages( void )
    143 {
    144     tr_msg_list * ret;
    145 
    146     assert( NULL != messageLock );
    147     tr_lockLock( messageLock );
    148     ret = messageQueue;
    149     messageQueue = NULL;
    150     messageQueueTail = &messageQueue;
    151     tr_lockUnlock( messageLock );
    152 
    153     return ret;
    154 }
    155 
    156 void
    157 tr_freeMessageList( tr_msg_list * list )
    158 {
    159     tr_msg_list * next;
    160 
    161     while( NULL != list )
    162     {
    163         next = list->next;
    164         free( list->message );
    165         free( list->name );
    166         free( list );
    167         list = next;
    168     }
    169 }
     177tr_deepLog( const char * file, int line, const char * name, const char * fmt, ... )
     178{
     179    FILE * fp = tr_getLog( );
     180    if( fp != NULL )
     181    {
     182        va_list args;
     183        char timestr[64];
     184        struct evbuffer * buf = evbuffer_new( );
     185        char * myfile = tr_strdup( file );
     186
     187        evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( timestr, sizeof(timestr) ) );
     188        if( name )
     189            evbuffer_add_printf( buf, "%s ", name );
     190        va_start( args, fmt );
     191        evbuffer_add_vprintf( buf, fmt, args );
     192        va_end( args );
     193        evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line );
     194        fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp );
     195
     196        tr_free( myfile );
     197        evbuffer_free( buf );
     198    }
     199}
     200
     201/***
     202****
     203***/
    170204
    171205int
  • trunk/libtransmission/utils.h

    r5673 r5689  
    5555#define tr_inf( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_INF, NULL, ## a )
    5656#define tr_dbg( a... ) tr_msg( __FILE__, __LINE__, TR_MSG_DBG, NULL, ## a )
    57 void tr_msg  ( const char * file, int line, int level, const char * torrent, const char * msg, ... );
     57void tr_msg  ( const char * file, int line, int level, const char * torrent, const char * fmt, ... );
    5858FILE* tr_getLog( void );
     59
     60void tr_deepLog( const char * file, int line, const char * name, const char * fmt, ... );
    5961
    6062char* tr_getLogTimeStr( char * buf, int buflen );
  • trunk/libtransmission/web.c

    r5686 r5689  
    2424
    2525#if CURL_CHECK_VERSION(7,16,0)
    26 #define HAVE_CURL_MULTI_SOCKET
     26#define USE_CURL_MULTI_SOCKET
    2727#else
    2828#define PULSE_MSEC 200
    2929static void pulse( int socket UNUSED, short action UNUSED, void * vweb );
    3030#endif
     31
     32#define dbgmsg(fmt...) tr_deepLog( __FILE__, __LINE__, "web", ##fmt )
    3133
    3234struct tr_web
     
    4042struct tr_web_task
    4143{
    42     unsigned int tag;
     44    unsigned long tag;
    4345    struct evbuffer * response;
    4446    tr_web_done_func * done_func;
     
    4749
    4850static size_t
    49 writeFunc( void * ptr, size_t size, size_t nmemb, void * vtask )
     51writeFunc( void * ptr, size_t size, size_t nmemb, void * task )
    5052{
    5153    const size_t byteCount = size * nmemb;
    52     struct tr_web_task * task = vtask;
    53     evbuffer_add( task->response, ptr, byteCount );
     54    evbuffer_add( ((struct tr_web_task*)task)->response, ptr, byteCount );
    5455    return byteCount;
    5556}
     
    5960{
    6061    CURLMcode rc;
    61     do {
    62 #ifdef HAVE_CURL_MULTI_SOCKET
     62    do
     63#ifdef USE_CURL_MULTI_SOCKET
    6364        rc = curl_multi_socket_all( web->cm, &web->remain );
    6465#else
    6566        rc = curl_multi_perform( web->cm, &web->remain );
    6667#endif
    67 fprintf( stderr, "remaining: %d\n", web->remain );
    68     } while( rc == CURLM_CALL_MULTI_PERFORM );
     68    while( rc == CURLM_CALL_MULTI_PERFORM );
     69    dbgmsg( "%d tasks remain", web->remain );
     70    if ( rc != CURLM_OK  )
     71        tr_err( "%s", curl_multi_strerror(rc) );
    6972}
    7073
     
    7578           void               * done_func_user_data )
    7679{
    77     static unsigned int tag = 0;
     80    static unsigned long tag = 0;
    7881    struct tr_web_task * task;
    7982    struct tr_web * web = session->web;
     
    8689    task->response = evbuffer_new( );
    8790
    88 fprintf( stderr, "new web tag %u [%s]\n", task->tag, url );
     91    dbgmsg( "adding task #%lu [%s]", task->tag, url );
    8992    ++web->remain;
    9093
     
    99102    curl_multi_add_handle( web->cm, ch );
    100103
     104#ifdef USE_CURL_MULTI_SOCKET
    101105    pump( web );
    102 
    103 #ifndef HAVE_CURL_MULTI_SOCKET
     106#else
    104107    if( !evtimer_initialized( &web->timer ) )
    105     {
     108        evtimer_set( &web->timer, pulse, web );
     109    if( !evtimer_pending( &web->timer, NULL ) ) {
    106110        struct timeval tv = tr_timevalMsec( PULSE_MSEC );
    107         evtimer_set( &web->timer, pulse, web );
    108         fprintf( stderr, "no timer running yet... starting one\n" );
    109111        evtimer_add( &web->timer, &tv );
    110112    }
     
    113115
    114116static void
    115 responseHandler( tr_web * web )
    116 {
    117     int remaining = 0;
     117processCompletedTasks( tr_web * web )
     118{
     119    int more = 0;
    118120
    119121    do {
    120         CURLMsg * msg = curl_multi_info_read( web->cm, &remaining );
     122        CURLMsg * msg = curl_multi_info_read( web->cm, &more );
    121123        if( msg && ( msg->msg == CURLMSG_DONE ) )
    122124        {
     
    132134            curl_easy_getinfo( ch, CURLINFO_RESPONSE_CODE, &response_code );
    133135
    134 fprintf( stderr, "web task %u done\n", task->tag );
     136            dbgmsg( "task #%lu done", task->tag );
    135137            task->done_func( web->session,
    136138                             response_code,
     
    146148        }
    147149    }
    148     while( remaining );
     150    while( more );
    149151
    150152    /* remove timeout if there are no transfers left */
     
    153155}
    154156
    155 #ifdef HAVE_CURL_MULTI_SOCKET
    156 /* libevent says that sock is ready to be processed, so wake up libcurl */
     157#ifdef USE_CURL_MULTI_SOCKET
     158/* libevent says that sock is ready to be processed, so tell libcurl */
    157159static void
    158160event_callback( int sock, short action, void * vweb )
     
    161163    CURLMcode rc;
    162164    int mask;
    163 
    164 #if 0
    165     static const char *strings[] = {
    166         "NONE","TIMEOUT","READ","TIMEOUT|READ","WRITE","TIMEOUT|WRITE",
    167         "READ|WRITE","TIMEOUT|READ|WRITE","SIGNAL" };
    168     fprintf( stderr, "Event on socket %d (%s)\n", sock, strings[action] );
    169 #endif
    170165
    171166    switch (action & (EV_READ|EV_WRITE)) {
     
    173168        case EV_WRITE: mask = CURL_CSELECT_OUT; break;
    174169        case EV_READ|EV_WRITE: mask = CURL_CSELECT_IN|CURL_CSELECT_OUT; break;
    175         default: tr_err( "Unknown event %d\n", (int)action ); return;
     170        default: tr_err( "Unknown event %hd\n", action ); return;
    176171    }
    177172
     
    183178        tr_err( "%s (%d)", curl_multi_strerror(rc), (int)sock );
    184179
    185     responseHandler( web );
     180    processCompletedTasks( web );
    186181}
    187182
     
    205200    }
    206201
    207 #if 0
    208     {
    209         static const char *actions[] = {"NONE", "IN", "OUT", "INOUT", "REMOVE"};
    210         fprintf( stderr, "Callback on socket %d (%s)\n", (int)sock, actions[action]);
    211     }
    212 #endif
    213 
    214202    switch (action) {
    215203        case CURL_POLL_IN: events |= EV_READ; break;
     
    226214}
    227215
    228 /* libevent says that timeout_ms have passed, so wake up libcurl */
     216/* libevent says that timeout_ms have passed, so tell libcurl */
    229217static void
    230218timeout_callback( int socket UNUSED, short action UNUSED, void * vweb )
    231219{
     220    tr_web * web = vweb;
    232221    CURLMcode rc;
    233     tr_web * web = vweb;
    234 
    235     do
    236         rc = curl_multi_socket( web->cm, CURL_SOCKET_TIMEOUT, &web->remain );
     222
     223    do rc = curl_multi_socket( web->cm, CURL_SOCKET_TIMEOUT, &web->remain );
    237224    while( rc == CURLM_CALL_MULTI_PERFORM );
    238 
    239225    if( rc != CURLM_OK )
    240226        tr_err( "%s", curl_multi_strerror( rc ) );
     
    262248
    263249    pump( web );
    264     responseHandler( web );
     250    processCompletedTasks( web );
    265251
    266252    if( web->remain > 0 ) {
     
    290276    web->cm = curl_multi_init( );
    291277    web->session = session;
    292     web->remain = 0;
    293 
    294 #ifdef HAVE_CURL_MULTI_SOCKET
     278
     279#ifdef USE_CURL_MULTI_SOCKET
    295280    curl_multi_setopt( web->cm, CURLMOPT_SOCKETDATA, web );
    296281    curl_multi_setopt( web->cm, CURLMOPT_SOCKETFUNCTION, socket_callback );
Note: See TracChangeset for help on using the changeset viewer.