src/lib/trema.c in trema-0.2.2.1 vs src/lib/trema.c in trema-0.2.3
- old
+ new
@@ -48,11 +48,11 @@
#ifdef init_log
#undef init_log
#endif
#define init_log mock_init_log
-bool mock_init_log( const char *ident, const char *log_directory, bool run_as_daemon );
+bool mock_init_log( const char *ident, const char *log_directory, logging_type type );
#ifdef error
#undef error
#endif
#define error mock_error
@@ -273,21 +273,23 @@
static char *trema_name = NULL;
static char *executable_name = NULL;
static char *trema_log = NULL;
static char *trema_pid = NULL;
static char *trema_sock = NULL;
+static logging_type log_output_type = LOGGING_TYPE_FILE;
static pthread_mutex_t mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
static struct option long_options[] = {
{ "name", 1, NULL, 'n' },
{ "daemonize", 0, NULL, 'd' },
{ "logging_level", 1, NULL, 'l' },
+ { "syslog", 0, NULL, 'g' },
{ "help", 0, NULL, 'h' },
{ NULL, 0, NULL, 0 },
};
-static char short_options[] = "n:dl:h";
+static char short_options[] = "n:dl:gh";
/**
* Default usage function shown on <tt>-h</tt> or <tt>--help</tt>.
*
@@ -300,10 +302,11 @@
"Usage: %s [OPTION]...\n"
"\n"
" -n, --name=SERVICE_NAME service name\n"
" -d, --daemonize run in the background\n"
" -l, --logging_level=LEVEL set logging level\n"
+ " -g, --syslog output log messages to syslog\n"
" -h, --help display this help and exit\n",
executable_name
);
}
@@ -435,10 +438,13 @@
run_as_daemon = true;
break;
case 'l':
set_logging_level( optarg );
break;
+ case 'g':
+ log_output_type = LOGGING_TYPE_SYSLOG;
+ break;
case 'h':
usage();
xfree( trema_name );
xfree( executable_name );
exit( EXIT_SUCCESS );
@@ -493,10 +499,32 @@
sigaction( SIGTERM, &signal_exit, NULL );
}
static void
+do_restart_log() {
+ restart_log( NULL );
+}
+
+
+static void
+set_do_restart_log_as_external_callback() {
+ set_external_callback( do_restart_log );
+}
+
+
+static void
+set_hup_handler() {
+ struct sigaction signal_hup;
+
+ memset( &signal_hup, 0, sizeof( struct sigaction ) );
+ signal_hup.sa_handler = ( void * ) set_do_restart_log_as_external_callback;
+ sigaction( SIGHUP, &signal_hup, NULL );
+}
+
+
+static void
set_dump_stats_as_external_callback() {
set_external_callback( dump_stats );
}
@@ -559,18 +587,23 @@
trema_log = NULL;
executable_name = NULL;
initialized = false;
trema_started = false;
run_as_daemon = false;
+ log_output_type = LOGGING_TYPE_FILE;
parse_argv( argc, argv );
set_trema_home();
set_trema_tmp();
check_trema_tmp();
- init_log( get_trema_name(), get_trema_log(), run_as_daemon );
+ if ( !run_as_daemon ) {
+ log_output_type |= LOGGING_TYPE_STDOUT;
+ }
+ init_log( get_trema_name(), get_trema_log(), log_output_type );
ignore_sigpipe();
set_exit_handler();
+ set_hup_handler();
set_usr1_handler();
set_usr2_handler();
init_messenger( get_trema_sock() );
init_stat();
init_timer();
@@ -636,17 +669,17 @@
set_trema_name( const char *name ) {
assert( name != NULL );
if ( trema_name != NULL ) {
if ( trema_started ) {
rename_pid( get_trema_pid(), trema_name, name );
- rename_log( trema_name, name, get_trema_log() );
+ rename_log( name );
}
xfree( trema_name );
}
trema_name = xstrdup( name );
if ( initialized ) {
- restart_log( trema_name, get_trema_log() );
+ restart_log( trema_name );
}
}
/**