(trunmk libT) #4162 "daemon should reopen logfile on SIGHUP" -- patch from gvdl to reopen the logfile on SIGHUP s.t. log rotation can be used by outside tools

This commit is contained in:
Jordan Lee 2012-05-30 17:59:52 +00:00
parent d717248e75
commit 6e6f20da91
1 changed files with 18 additions and 4 deletions

View File

@ -57,9 +57,13 @@
#define SPEED_G_STR "GB/s" #define SPEED_G_STR "GB/s"
#define SPEED_T_STR "TB/s" #define SPEED_T_STR "TB/s"
#define LOGFILE_MODE_STR "a+"
static bool paused = false; static bool paused = false;
static bool closing = false; static bool closing = false;
static bool seenHUP = false; static bool seenHUP = false;
static const char *logfileName = NULL;
static FILE *logfile = NULL;
static tr_session * mySession = NULL; static tr_session * mySession = NULL;
/*** /***
@ -150,7 +154,16 @@ gotsig( int sig )
else else
{ {
tr_benc settings; tr_benc settings;
const char * configDir = tr_sessionGetConfigDir( mySession ); const char * configDir;
/* reopen the logfile to allow for log rotation */
if( logfileName ) {
logfile = freopen( logfileName, LOGFILE_MODE_STR, logfile );
if( !logfile )
fprintf( stderr, "Couldn't reopen \"%s\": %s\n", logfileName, tr_strerror( errno ) );
}
configDir = tr_sessionGetConfigDir( mySession );
tr_inf( "Reloading settings from \"%s\"", configDir ); tr_inf( "Reloading settings from \"%s\"", configDir );
tr_bencInitDict( &settings, 0 ); tr_bencInitDict( &settings, 0 );
tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_ENABLED, true ); tr_bencDictAddBool( &settings, TR_PREFS_KEY_RPC_ENABLED, true );
@ -353,7 +366,6 @@ main( int argc, char ** argv )
const char * configDir = NULL; const char * configDir = NULL;
const char * pid_filename; const char * pid_filename;
dtr_watchdir * watchdir = NULL; dtr_watchdir * watchdir = NULL;
FILE * logfile = NULL;
bool pidfile_created = false; bool pidfile_created = false;
tr_session * session = NULL; tr_session * session = NULL;
@ -392,8 +404,10 @@ main( int argc, char ** argv )
break; break;
case 'd': dumpSettings = true; case 'd': dumpSettings = true;
break; break;
case 'e': logfile = fopen( optarg, "a+" ); case 'e': logfile = fopen( optarg, LOGFILE_MODE_STR );
if( logfile == NULL ) if( logfile )
logfileName = optarg;
else
fprintf( stderr, "Couldn't open \"%s\": %s\n", optarg, tr_strerror( errno ) ); fprintf( stderr, "Couldn't open \"%s\": %s\n", optarg, tr_strerror( errno ) );
break; break;
case 'f': foreground = true; case 'f': foreground = true;