Changeset 9388


Ignore:
Timestamp:
Oct 23, 2009, 5:00:16 AM (13 years ago)
Author:
charles
Message:

(trunk) #2527: daemon should log messages to syslog

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/configure.ac

    r9169 r9388  
    7979AC_HEADER_TIME
    8080
    81 AC_CHECK_FUNCS([lrintf strlcpy daemon dirname basename strcasecmp localtime_r posix_fallocate memmem strtold])
     81AC_CHECK_FUNCS([lrintf strlcpy daemon dirname basename strcasecmp localtime_r posix_fallocate memmem strtold syslog])
    8282AC_PROG_INSTALL
    8383AC_PROG_MAKE_SET
  • trunk/daemon/daemon.c

    r9387 r9388  
    2222#include <fcntl.h> /* open */
    2323#include <signal.h>
     24#ifdef HAVE_SYSLOG
     25#include <syslog.h>
     26#endif
    2427#include <unistd.h> /* daemon */
    2528
     
    218221}
    219222
     223static void
     224pumpLogMessages( tr_bool foreground )
     225{
     226    const tr_msg_list * l;
     227    tr_msg_list * list = tr_getQueuedMessages( );
     228
     229    for( l=list; l!=NULL; l=l->next )
     230    {
     231#ifdef HAVE_SYSLOG
     232        if( foreground )
     233        {
     234            char timestr[64];
     235            tr_getLogTimeStr( timestr, sizeof( timestr ) );
     236            if( l->name )
     237                fprintf( stderr, "[%s] %s %s (%s:%d)\n", timestr, l->name, l->message, l->file, l->line );
     238            else
     239                fprintf( stderr, "[%s] %s (%s:%d)\n", timestr, l->message, l->file, l->line );
     240        }
     241        else /* daemon... write to syslog */
     242        {
     243            int priority;
     244
     245            /* figure out the syslog priority */
     246            switch( l->level ) {
     247                case TR_MSG_ERR: priority = LOG_ERR; break;
     248                case TR_MSG_DBG: priority = LOG_DEBUG; break;
     249                default: priority = LOG_INFO; break;
     250            }
     251
     252            if( l->name )
     253                syslog( priority, "%s %s (%s:%d)", l->name, l->message, l->file, l->line );
     254            else
     255                syslog( priority, "%s (%s:%d)", l->message, l->file, l->line );
     256        }
     257#else
     258        {
     259            char timestr[64];
     260            tr_getLogTimeStr( timestr, sizeof( timestr ) );
     261            if( l->name )
     262                fprintf( stderr, "[%s] %s %s (%s:%d)\n", timestr, l->name, l->message, l->file, l->line );
     263            else
     264                fprintf( stderr, "[%s] %s (%s:%d)\n", timestr, l->message, l->file, l->line );
     265        }
     266#endif
     267    }
     268
     269    tr_freeMessageList( list );
     270}
     271
    220272int
    221273main( int argc, char ** argv )
     
    325377
    326378    /* start the session */
    327     mySession = tr_sessionInit( "daemon", configDir, FALSE, &settings );
     379    mySession = tr_sessionInit( "daemon", configDir, TRUE, &settings );
    328380    tr_sessionSaveSettings( mySession, configDir, &settings );
    329381
     
    357409    }
    358410
    359     while( !closing )
    360     {
     411#ifdef HAVE_SYSLOG
     412    if( !foreground )
     413        openlog( MY_NAME, LOG_CONS, LOG_DAEMON );
     414#endif
     415
     416    while( !closing ) {
    361417        tr_wait( 1000 ); /* sleep one second */
    362418        dtr_watchdir_update( watchdir );
    363     }
     419        pumpLogMessages( foreground );
     420    }
     421
     422    closelog( );
    364423
    365424    /* shutdown */
     425#if HAVE_SYSLOG
     426    if( !foreground )
     427        syslog( LOG_INFO, "%s", "Closing session" );
     428#endif
    366429    printf( "Closing transmission session..." );
    367430    tr_sessionSaveSettings( mySession, configDir, &settings );
  • trunk/libtransmission/session.c

    r9387 r9388  
    590590                tr_benc     * clientSettings )
    591591{
     592    int64_t i;
    592593    tr_session * session;
    593594    struct init_data data;
     
    602603    session->magicNumber = SESSION_MAGIC_NUMBER;
    603604    tr_bencInitList( &session->removedTorrents, 0 );
     605
     606    /* nice to start logging at the very beginning */
     607    if( tr_bencDictFindInt( clientSettings, TR_PREFS_KEY_MSGLEVEL, &i ) )
     608        tr_setMessageLevel( i );
    604609
    605610    /* start the libtransmission thread */
     
    723728    assert( tr_amInEventThread( session ) );
    724729
     730    if( tr_bencDictFindInt( settings, TR_PREFS_KEY_MSGLEVEL, &i ) )
     731        tr_setMessageLevel( i );
     732
    725733    if( tr_bencDictFindInt( settings, TR_PREFS_KEY_UMASK, &i ) ) {
    726734        session->umask = (mode_t)i;
     
    733741    if( tr_bencDictFindInt( settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, &i ) )
    734742        tr_sessionSetPeerLimitPerTorrent( session, i );
    735     if( tr_bencDictFindInt( settings, TR_PREFS_KEY_MSGLEVEL, &i ) )
    736         tr_setMessageLevel( i );
    737743    if( tr_bencDictFindBool( settings, TR_PREFS_KEY_PEX_ENABLED, &boolVal ) )
    738744        tr_sessionSetPexEnabled( session, boolVal );
     
    857863    else if( tr_bencDictFindBool( settings, TR_PREFS_KEY_ALT_SPEED_ENABLED, &boolVal ) )
    858864        useAltSpeed( session, boolVal, FALSE );
    859 
    860 
    861 
    862     fprintf( stderr, "what was passed in: \n%s\n", tr_bencToStr( settings, TR_FMT_JSON, NULL ) );
    863 
    864     fprintf( stderr, "-=-=-=-=-=-\n" );
    865 
    866     {
    867     tr_benc tmp;
    868     tr_bencInitDict( &tmp, 0 );
    869     tr_sessionGetSettings( session, &tmp );
    870     fprintf( stderr, "and the session's state now: \n%s\n", tr_bencToStr( &tmp, TR_FMT_JSON, NULL ) );
    871     tr_bencFree( &tmp );
    872     }
    873865
    874866    --session->waiting;
Note: See TracChangeset for help on using the changeset viewer.