use Sys::Hostname;
use Socket;
use POSIX;
+use Getopt::Long;
use Spong::Daemon;
use Spong::Status qw(status);
+use Spong::Log;
srand( time() ^ ($$ + ($$ << 15 )) );
-if( $ARGV[0] eq "--debug" ) { $debug = 1; shift; }
-if( $ARGV[0] eq "--restart" ) { $restart = 1; shift; }
-if( $ARGV[0] eq "--kill" ) { $kill = 1; shift; }
-if( $ARGV[0] eq "--nosleep" ) { $nosleep = 1; shift; }
-if( $ARGV[0] eq "--refresh" ) { $nosleep = 1; shift; }
+$debug = $restart = $kill = $nosleep = 0;
+
+Getopt::Long::Configure('pass_through');
+if ( ! GetOptions("debug:i" => \$debuglevel, "restart" => \$restart,
+ "kill" => \$kill, "nosleep|refresh" => \$nosleep ) ) {
+ &usage();
+ exit 1;
+}
+
+#if( $ARGV[0] eq "--debug" ) { $debug = 1; shift; }
+#if( $ARGV[0] eq "--restart" ) { $restart = 1; shift; }
+#if( $ARGV[0] eq "--kill" ) { $kill = 1; shift; }
+#if( $ARGV[0] eq "--nosleep" ) { $nosleep = 1; shift; }
+#if( $ARGV[0] eq "--refresh" ) { $nosleep = 1; shift; }
+
+# Initial debugging for preconfiguration debugging
+Spong::Log::set_debug_context( 'debuglevel' => $debuglevel );
$me = "@@BINDIR@@/spong-client";
$conf_file = $ARGV[0] || "@@ETCDIR@@/spong.conf";
$CHECKS = "";
&load_config_files(); # Loads the user specified configuration information
+&init_logging(); # Initialize logging contexts
Spong::Daemon::Daemonize # Daemonize if not signalling or debugging
unless ($restart || $kill || $nosleep || $debug);
&handle_signals(); # Set up handlers, and signal the current server if asked
exit(0);
-
# ===========================================================================
# Utility functions, and signal handlers...
# ===========================================================================
+sub usage {
+
+ print qq
+(Usage:
+ $0 [--debug n] [--nosleep|--refresh] [config_file]
+ $0 --kill | --restart
+
+ --debug n
+ Run in the foreround and print debugging output
+ --nosleep
+ --refresh
+ Run one cycle of checks in the foreground and exit
+ --restart
+ Signal a running spong-client to restart.
+ --kill
+ Signal a running spong-client to terminate.
+ config_file
+ Use the named file as the configuration file
+);
+
+}
+
+
+# This function initializes the debug and error logging contexts in the
+# Log module.
+
+sub init_logging {
+ if (defined $debuglevel) {
+ $debug = ($debuglevel == 0) ? 1 : $debuglevel
+ }
+
+ Spong::Log::set_debug_context( 'debuglevel' => $debug );
+
+ my $filename = ($SPONG_LOG_FILE) ? "$SPONGTMP/spong-client.log" : "";
+ my $syslog = ($SPONG_LOG_SYSLOG) ? 1 : 0;
+
+ Spong::Log::set_error_context( syslog => $syslog,
+ ident => 'spong-client',
+ logopt => 'pid cons',
+ priority => 'ERR',
+ filename => $filename,
+ );
+
+}
+
+
# Load our configuration variables, including anything specific to the host
# that we are running on.
# Output functions, one for debugging information, the other for errors.
-sub debug { print scalar localtime, " ", $_[0], "\n" if $main::debug; }
-sub error { warn scalar localtime(), " Error: ", $_[0], "\n"; }
-
-
-# Fork from the parent and become a daemon
-sub daemonize {
-
- my( $pid );
-
- # Try to fork
- $pid = fork();
- if (! defined $pid) {
- die "ERROR: Could not fork: $!";
- } elsif ($pid) {
- # I'm the parent, so just exit gracefully
- exit(0);
- } else {
- # I'm the child
-
- # If we are not debuging, disconnect from the console
- if ( ! $debug ) {
- open(STDIN,"</dev/null");
- open(STDOUT,">/dev/null");
- open(STDERR,">/dev/null");
- }
-
- # Become session group leader
- POSIX::setsid();
- }
-}
-
+sub debug { Spong::Log::debug($_[0],$_[1]); }
+sub error { Spong::Log::error($_[0]); }
# Signal handlers...