From: Stefan Schantl Date: Wed, 25 Nov 2015 14:27:07 +0000 (+0100) Subject: Use "Logger" module for logging purposes. X-Git-Tag: 2.0~62 X-Git-Url: http://git.ipfire.org/?p=people%2Fstevee%2Fguardian.git;a=commitdiff_plain;h=c95dfa5b7f099f1463de60b7baa2a0a1433d4b46 Use "Logger" module for logging purposes. Signed-off-by: Stefan Schantl --- diff --git a/guardian b/guardian index f0d890d..ba8e99c 100644 --- a/guardian +++ b/guardian @@ -29,6 +29,7 @@ use Time::HiRes qw[ time sleep ]; require Guardian::Base; require Guardian::Config; +require Guardian::Logger; require Guardian::Parser; require Guardian::Socket; @@ -68,6 +69,10 @@ if (defined($cmdargs{"help"})) { # Push the may be given config file argument. my %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"}); +# Initialize Logger. +my $logger = Guardian::Logger->New(%mainsettings); +$logger->Log("debug", "Logger successfully initialized..."); + # Shared hash between the main process and all threads. It will store all # monitored files and their current file position. my %monitored_files :shared = (); @@ -94,7 +99,8 @@ while(1) { # Grab the data of the top enqueued event. my $event = $queue->peek(); - print "Got event: $event\n"; + # Log processed event. + $logger->Log("debug", "QUEUE - Processed event: $event"); # Drop processed event from queue. $queue->dequeue(); @@ -159,6 +165,9 @@ sub Worker ($) { # Switch watcher into non-blocking mode. $watcher->blocking(0); + # Log successfully spawned worker. + $logger->Log("debug", "Spawned worker thread for: $file"); + # Infinite loop. while(1) { # Check for any events and perform them, if there @@ -230,6 +239,9 @@ sub Socket () { # Create the Server socket by calling the responsible function. my $server = &Guardian::Socket::Server(); + # Log successfull creation of socket. + $logger->Log("debug", "Listening to Socket..."); + # Accept incomming connections from the socket. while (my $connection = $server->accept()) { # Autoflush the socket after the data @@ -241,6 +253,9 @@ sub Socket () { # Remove any newlines. chomp($message); + # Log recieved socket command. + $logger->Log("debug", "Socket - Recieved message: $message"); + # Send the recieved data message to the # socket parser. my $action = &Guardian::Socket::Message_Parser($message); @@ -282,6 +297,7 @@ sub StartWorkers () { # Loop through the hash which contains the monitored files and start # a worker thread for each single one. foreach my $file (keys %monitored_files) { + $logger->Log("debug", "Starting worker thread for $file"); # Create worker thread for the file. push @running_workers, threads->create(\&Worker,$file); } @@ -300,6 +316,7 @@ sub StopWorkers () { # thread so perl can do an automatically clean-up. $worker->kill('KILL'); } + $logger->Log("debug", "All workers are stopped now..."); } # @@ -310,6 +327,9 @@ sub StopWorkers () { ## reloading all configure options and stopping/starting the worker threads. # sub Reload () { + # Log reload. + $logger->Log("info", "Reload configuration..."); + # Stop all running workers. &StopWorkers(); @@ -330,6 +350,9 @@ sub Reload () { ## by the signal handler when recieving INT (2), QUIT (3) and TERM (15) signals. # sub Shutdown () { + # Log shutdown. + $logger->Log("info", "Shutting down..."); + # Stop all workers. &StopWorkers(); @@ -341,6 +364,9 @@ sub Shutdown () { # exiting. sleep(1); + # Log good bye message. + $logger->Log("debug", "Good Bye!"); + # Exit guardian. exit; }