mirror of
https://github.com/transmission/transmission
synced 2024-12-25 09:13:06 +00:00
(trunk) #2527: daemon should log messages to syslog
This commit is contained in:
parent
c85ee09fef
commit
37aaf66d4e
3 changed files with 75 additions and 20 deletions
|
@ -78,7 +78,7 @@ fi
|
||||||
AC_HEADER_STDC
|
AC_HEADER_STDC
|
||||||
AC_HEADER_TIME
|
AC_HEADER_TIME
|
||||||
|
|
||||||
AC_CHECK_FUNCS([lrintf strlcpy daemon dirname basename strcasecmp localtime_r posix_fallocate memmem strtold])
|
AC_CHECK_FUNCS([lrintf strlcpy daemon dirname basename strcasecmp localtime_r posix_fallocate memmem strtold syslog])
|
||||||
AC_PROG_INSTALL
|
AC_PROG_INSTALL
|
||||||
AC_PROG_MAKE_SET
|
AC_PROG_MAKE_SET
|
||||||
ACX_PTHREAD
|
ACX_PTHREAD
|
||||||
|
|
|
@ -21,6 +21,9 @@
|
||||||
|
|
||||||
#include <fcntl.h> /* open */
|
#include <fcntl.h> /* open */
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#ifdef HAVE_SYSLOG
|
||||||
|
#include <syslog.h>
|
||||||
|
#endif
|
||||||
#include <unistd.h> /* daemon */
|
#include <unistd.h> /* daemon */
|
||||||
|
|
||||||
#include <event.h>
|
#include <event.h>
|
||||||
|
@ -217,6 +220,55 @@ onFileAdded( tr_session * session, const char * dir, const char * file )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
pumpLogMessages( tr_bool foreground )
|
||||||
|
{
|
||||||
|
const tr_msg_list * l;
|
||||||
|
tr_msg_list * list = tr_getQueuedMessages( );
|
||||||
|
|
||||||
|
for( l=list; l!=NULL; l=l->next )
|
||||||
|
{
|
||||||
|
#ifdef HAVE_SYSLOG
|
||||||
|
if( foreground )
|
||||||
|
{
|
||||||
|
char timestr[64];
|
||||||
|
tr_getLogTimeStr( timestr, sizeof( timestr ) );
|
||||||
|
if( l->name )
|
||||||
|
fprintf( stderr, "[%s] %s %s (%s:%d)\n", timestr, l->name, l->message, l->file, l->line );
|
||||||
|
else
|
||||||
|
fprintf( stderr, "[%s] %s (%s:%d)\n", timestr, l->message, l->file, l->line );
|
||||||
|
}
|
||||||
|
else /* daemon... write to syslog */
|
||||||
|
{
|
||||||
|
int priority;
|
||||||
|
|
||||||
|
/* figure out the syslog priority */
|
||||||
|
switch( l->level ) {
|
||||||
|
case TR_MSG_ERR: priority = LOG_ERR; break;
|
||||||
|
case TR_MSG_DBG: priority = LOG_DEBUG; break;
|
||||||
|
default: priority = LOG_INFO; break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if( l->name )
|
||||||
|
syslog( priority, "%s %s (%s:%d)", l->name, l->message, l->file, l->line );
|
||||||
|
else
|
||||||
|
syslog( priority, "%s (%s:%d)", l->message, l->file, l->line );
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
{
|
||||||
|
char timestr[64];
|
||||||
|
tr_getLogTimeStr( timestr, sizeof( timestr ) );
|
||||||
|
if( l->name )
|
||||||
|
fprintf( stderr, "[%s] %s %s (%s:%d)\n", timestr, l->name, l->message, l->file, l->line );
|
||||||
|
else
|
||||||
|
fprintf( stderr, "[%s] %s (%s:%d)\n", timestr, l->message, l->file, l->line );
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
tr_freeMessageList( list );
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main( int argc, char ** argv )
|
main( int argc, char ** argv )
|
||||||
{
|
{
|
||||||
|
@ -324,7 +376,7 @@ main( int argc, char ** argv )
|
||||||
}
|
}
|
||||||
|
|
||||||
/* start the session */
|
/* start the session */
|
||||||
mySession = tr_sessionInit( "daemon", configDir, FALSE, &settings );
|
mySession = tr_sessionInit( "daemon", configDir, TRUE, &settings );
|
||||||
tr_sessionSaveSettings( mySession, configDir, &settings );
|
tr_sessionSaveSettings( mySession, configDir, &settings );
|
||||||
|
|
||||||
if( tr_bencDictFindBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, &boolVal ) && boolVal )
|
if( tr_bencDictFindBool( &settings, TR_PREFS_KEY_RPC_AUTH_REQUIRED, &boolVal ) && boolVal )
|
||||||
|
@ -356,13 +408,24 @@ main( int argc, char ** argv )
|
||||||
tr_ctorFree( ctor );
|
tr_ctorFree( ctor );
|
||||||
}
|
}
|
||||||
|
|
||||||
while( !closing )
|
#ifdef HAVE_SYSLOG
|
||||||
{
|
if( !foreground )
|
||||||
|
openlog( MY_NAME, LOG_CONS, LOG_DAEMON );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
while( !closing ) {
|
||||||
tr_wait( 1000 ); /* sleep one second */
|
tr_wait( 1000 ); /* sleep one second */
|
||||||
dtr_watchdir_update( watchdir );
|
dtr_watchdir_update( watchdir );
|
||||||
|
pumpLogMessages( foreground );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
closelog( );
|
||||||
|
|
||||||
/* shutdown */
|
/* shutdown */
|
||||||
|
#if HAVE_SYSLOG
|
||||||
|
if( !foreground )
|
||||||
|
syslog( LOG_INFO, "%s", "Closing session" );
|
||||||
|
#endif
|
||||||
printf( "Closing transmission session..." );
|
printf( "Closing transmission session..." );
|
||||||
tr_sessionSaveSettings( mySession, configDir, &settings );
|
tr_sessionSaveSettings( mySession, configDir, &settings );
|
||||||
dtr_watchdir_free( watchdir );
|
dtr_watchdir_free( watchdir );
|
||||||
|
|
|
@ -589,6 +589,7 @@ tr_sessionInit( const char * tag,
|
||||||
tr_bool messageQueuingEnabled,
|
tr_bool messageQueuingEnabled,
|
||||||
tr_benc * clientSettings )
|
tr_benc * clientSettings )
|
||||||
{
|
{
|
||||||
|
int64_t i;
|
||||||
tr_session * session;
|
tr_session * session;
|
||||||
struct init_data data;
|
struct init_data data;
|
||||||
|
|
||||||
|
@ -602,6 +603,10 @@ tr_sessionInit( const char * tag,
|
||||||
session->magicNumber = SESSION_MAGIC_NUMBER;
|
session->magicNumber = SESSION_MAGIC_NUMBER;
|
||||||
tr_bencInitList( &session->removedTorrents, 0 );
|
tr_bencInitList( &session->removedTorrents, 0 );
|
||||||
|
|
||||||
|
/* nice to start logging at the very beginning */
|
||||||
|
if( tr_bencDictFindInt( clientSettings, TR_PREFS_KEY_MSGLEVEL, &i ) )
|
||||||
|
tr_setMessageLevel( i );
|
||||||
|
|
||||||
/* start the libtransmission thread */
|
/* start the libtransmission thread */
|
||||||
tr_netInit( ); /* must go before tr_eventInit */
|
tr_netInit( ); /* must go before tr_eventInit */
|
||||||
tr_eventInit( session );
|
tr_eventInit( session );
|
||||||
|
@ -722,6 +727,9 @@ sessionSetImpl( void * vdata )
|
||||||
assert( tr_bencIsDict( settings ) );
|
assert( tr_bencIsDict( settings ) );
|
||||||
assert( tr_amInEventThread( session ) );
|
assert( tr_amInEventThread( session ) );
|
||||||
|
|
||||||
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_MSGLEVEL, &i ) )
|
||||||
|
tr_setMessageLevel( i );
|
||||||
|
|
||||||
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_UMASK, &i ) ) {
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_UMASK, &i ) ) {
|
||||||
session->umask = (mode_t)i;
|
session->umask = (mode_t)i;
|
||||||
umask( session->umask );
|
umask( session->umask );
|
||||||
|
@ -732,8 +740,6 @@ sessionSetImpl( void * vdata )
|
||||||
tr_sessionSetLazyBitfieldEnabled( session, boolVal );
|
tr_sessionSetLazyBitfieldEnabled( session, boolVal );
|
||||||
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, &i ) )
|
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_PEER_LIMIT_TORRENT, &i ) )
|
||||||
tr_sessionSetPeerLimitPerTorrent( session, i );
|
tr_sessionSetPeerLimitPerTorrent( session, i );
|
||||||
if( tr_bencDictFindInt( settings, TR_PREFS_KEY_MSGLEVEL, &i ) )
|
|
||||||
tr_setMessageLevel( i );
|
|
||||||
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_PEX_ENABLED, &boolVal ) )
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_PEX_ENABLED, &boolVal ) )
|
||||||
tr_sessionSetPexEnabled( session, boolVal );
|
tr_sessionSetPexEnabled( session, boolVal );
|
||||||
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_DHT_ENABLED, &boolVal ) )
|
if( tr_bencDictFindBool( settings, TR_PREFS_KEY_DHT_ENABLED, &boolVal ) )
|
||||||
|
@ -857,20 +863,6 @@ sessionSetImpl( void * vdata )
|
||||||
else if( tr_bencDictFindBool( settings, TR_PREFS_KEY_ALT_SPEED_ENABLED, &boolVal ) )
|
else if( tr_bencDictFindBool( settings, TR_PREFS_KEY_ALT_SPEED_ENABLED, &boolVal ) )
|
||||||
useAltSpeed( session, boolVal, FALSE );
|
useAltSpeed( session, boolVal, FALSE );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fprintf( stderr, "what was passed in: \n%s\n", tr_bencToStr( settings, TR_FMT_JSON, NULL ) );
|
|
||||||
|
|
||||||
fprintf( stderr, "-=-=-=-=-=-\n" );
|
|
||||||
|
|
||||||
{
|
|
||||||
tr_benc tmp;
|
|
||||||
tr_bencInitDict( &tmp, 0 );
|
|
||||||
tr_sessionGetSettings( session, &tmp );
|
|
||||||
fprintf( stderr, "and the session's state now: \n%s\n", tr_bencToStr( &tmp, TR_FMT_JSON, NULL ) );
|
|
||||||
tr_bencFree( &tmp );
|
|
||||||
}
|
|
||||||
|
|
||||||
--session->waiting;
|
--session->waiting;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue