]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - guardian
Use "Logger" module for logging purposes.
[people/stevee/guardian.git] / guardian
index d3f7de0ea0c62d3965ce2b7266a57828fb2be1f6..ba8e99cf52400c8c92cebdbdc2a1441cf53cd95c 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,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 = ();
@@ -91,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();
@@ -156,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
@@ -227,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
@@ -238,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);
@@ -279,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);
        }
@@ -295,8 +314,9 @@ sub StopWorkers () {
        foreach my $worker (@running_workers) {
                # Send the worker the "KILL" signal and detach the
                # thread so perl can do an automatically clean-up.
-               $worker->kill('KILL')->detach();
+               $worker->kill('KILL');
        }
+       $logger->Log("debug", "All workers are stopped now...");
 }
 
 #
@@ -307,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();
 
@@ -327,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();
 
@@ -338,6 +364,9 @@ sub Shutdown () {
        # exiting.
        sleep(1);
 
+       # Log good bye message.
+       $logger->Log("debug", "Good Bye!");
+
        # Exit guardian.
        exit;
 }