]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - guardian
Pass logger object to the mainsettings hash for a usage
[people/stevee/guardian.git] / guardian
index f3c287b593305caf7769618a3439d33c5131cbf8..9245f9004827548fda1f18cecb747c1c11e85bdd 100644 (file)
--- a/guardian
+++ b/guardian
@@ -29,11 +29,15 @@ use Time::HiRes qw[ time sleep ];
 
 require Guardian::Base;
 require Guardian::Config;
+require Guardian::Logger;
 require Guardian::Parser;
 require Guardian::Socket;
 
 use warnings;
 
+# Disable warnings of unjoinded threads when stopping guardian.
+no warnings 'threads';
+
 # Define version.
 my $version ="2.0";
 
@@ -65,13 +69,24 @@ 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...");
+
+# Add the logger object to the mainsettings for passing
+# it to the modules.
+$mainsettings{Logger} = $logger;
+
+# Redirect perls "die" messages to the logger before exiting.
+$SIG{__DIE__} = sub { $logger->Log("err", "@_"); };
+
 # Shared hash between the main process and all threads. It will store all
 # monitored files and their current file position.
 my %monitored_files :shared = ();
 
 # Create the main queue. It is used to store and process all events which are
 # reported and enqueued by the worker threads.
-my $queue :shared = new Thread::Queue or die "Could not create new, empty queue. $!\n";;
+my $queue :shared = new Thread::Queue or die "Could not create new, empty queue. $!";;
 
 # Array to store all currently running worker objects.
 # (Does not include the socket thread)
@@ -91,7 +106,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();
@@ -148,14 +164,17 @@ sub Worker ($) {
        $SIG{'KILL'} = sub { threads->exit(); };
 
        # Create inotify watcher.
-       my $watcher = new Linux::Inotify2 or die "Could not use inotify. $!\n";
+       my $watcher = new Linux::Inotify2 or die "Could not use inotify. $!";
 
        # Monitor the specified file.
-       $watcher->watch("$file", IN_MODIFY) or die "Could not monitor $file. $!\n";
+       $watcher->watch("$file", IN_MODIFY) or die "Could not monitor $file. $!";
 
        # 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
@@ -167,7 +186,7 @@ sub Worker ($) {
                        my $fileposition = $monitored_files{$file};
 
                        # Open the file.
-                       open (FILE, $file) or die "Could not open $file. $!\n";
+                       open (FILE, $file) or die "Could not open $file. $!";
 
                        # Seek to the last known position.
                        seek (FILE, $fileposition, 0);
@@ -227,6 +246,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
@@ -238,6 +260,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);
@@ -279,6 +304,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);
        }
@@ -297,6 +323,7 @@ sub StopWorkers () {
                # thread so perl can do an automatically clean-up.
                $worker->kill('KILL');
        }
+       $logger->Log("debug", "All workers are stopped now...");
 }
 
 #
@@ -307,12 +334,21 @@ 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();
 
        # Re-read configuration file.
        %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"});
 
+       # Update Logger settings.
+       $logger = Guardian::Logger->Update(%mainsettings);
+
+       # Update logger object in mainsettings hash.
+       $mainsettings{Logger} = $logger;
+
        # Re-generate hash of monitored files.
        %monitored_files = &Guardian::Base::GenerateMonitoredFiles(\%mainsettings, \%monitored_files);
 
@@ -327,6 +363,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();
 
@@ -338,6 +377,9 @@ sub Shutdown () {
        # exiting.
        sleep(1);
 
+       # Log good bye message.
+       $logger->Log("debug", "Good Bye!");
+
        # Exit guardian.
        exit;
 }