Index: /trunk/Nebulous-Server/Changes
===================================================================
--- /trunk/Nebulous-Server/Changes	(revision 23653)
+++ /trunk/Nebulous-Server/Changes	(revision 23654)
@@ -3,4 +3,5 @@
 0.17
     - retry database transactions when a deadlock is detected
+    - add log4perl logging to nebdiskd
 
 0.16
Index: /trunk/Nebulous-Server/bin/nebdiskd
===================================================================
--- /trunk/Nebulous-Server/bin/nebdiskd	(revision 23653)
+++ /trunk/Nebulous-Server/bin/nebdiskd	(revision 23654)
@@ -16,4 +16,5 @@
 use File::Spec;
 use Filesys::Df;
+use Log::Log4perl;
 use Nebulous::Server::SQL;
 use Net::Server::Daemonize qw( daemonize unlink_pid_file );
@@ -85,4 +86,24 @@
     unless $db && $dbuser && $dbpass;
 
+# start up logging
+my $conf =<<END;
+    log4perl.category.nebdiskd = WARN, Screen, SERVERLOGFILE
+
+    log4perl.appender.Screen        = Log::Log4perl::Appender::Screen
+    log4perl.appender.Screen.stderr = 1
+    log4perl.appender.Screen.layout = Log::Log4perl::Layout::PatternLayout
+    log4perl.appender.Screen.layout.ConversionPattern = %d | %H | %p | %M - %m%n
+
+    log4perl.appender.SERVERLOGFILE           = Log::Log4perl::Appender::File
+    log4perl.appender.SERVERLOGFILE.filename  = /tmp/nebdiskd.log
+    log4perl.appender.SERVERLOGFILE.mode      = append
+    log4perl.appender.SERVERLOGFILE.layout    = Log::Log4perl::Layout::PatternLayout
+    log4perl.appender.SERVERLOGFILE.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} | %H | %p | %M - %m%n
+END
+Log::Log4perl::init(\$conf);
+my $log = Log::Log4perl::get_logger("nebdiskd");
+$log->level('WARN');
+$log->level('DEBUG') if $debug;
+
 daemonize(
     $user,     # User
@@ -104,8 +125,8 @@
                 poll_interval   => $poll_interval,
                 debug           => $debug,
-        ) or die "poll_mounts() should not have returned";
+        );
     };
-    if ($!) {
-        warn $@;
+    if ($@) {
+        $log->warn($@);
     }
 
@@ -113,5 +134,5 @@
 }
 
-die "poll loop exited -- THIS SHOULD NOT HAPPEN";
+$log->logdie("poll loop exited -- THIS SHOULD NOT HAPPEN");
 
 
@@ -141,14 +162,14 @@
         # determine valid mountpoints
         foreach my $mnt (@$mounts) {
-            print "checking $mnt\n" if $debug;
+            $log->debug("checking $mnt");
             # this /SHOULD/ fail if the mount point is handled by the
             # automounter and it fails to mount
             eval {
                 unless (is_mountpoint($mnt)) {
-                    die "$mnt is not a valid mountpoint\n";
+                    $log->warn("$mnt is not a valid mountpoint");
                 }
             };
             if ($@) {
-                print $@ if $debug;
+                $log->warn($@);
                 $d_query->execute($mnt);
                 next;
@@ -160,10 +181,10 @@
             my $dev_info = df($mnt, 1024);
             unless (defined $dev_info) {
-                print "can't find device info for $mnt\n" if $debug;
+                $log->error("can't find device info for $mnt");
                 next;
             }
 
             $r_query->execute($mnt, @$dev_info{qw( blocks used )});
-            print "adding $mnt to db\n" if $debug;
+            $log->debug("adding $mnt to db");
 
         }
@@ -172,10 +193,10 @@
 
         $dbh->commit;
-        print "commited to database\n" if $debug;
+        $log->debug("commited to database");
     };
     if ($@) {
         $dbh->rollback;
-        print "rolledback transaction\n" if $debug;
-        warn $@;
+        $log->debug("rolledback transaction");
+        $log->logdie($@);
     }
 }
@@ -188,6 +209,6 @@
 
     if (!-f $rcfile) {
-        open(my $fh, '>', $rcfile) or die "can't open file: $!";
-        close($fh) or die "can't close file:$!";
+        open(my $fh, '>', $rcfile) or $log->logdie("can't open file: $!");
+        close($fh) or $log->logdie("can't close file:$!");
     }
 
@@ -226,6 +247,6 @@
     };
     if ($@) { 
-        $db->rollback;
-        die $@;
+        $dbh->rollback;
+        $log->logdie($@);
     }
 
@@ -242,7 +263,7 @@
     foreach my $path (@$mounts) {
         if (stat File::Spec->canonpath($path)) {
-            print "stated $path\n" if $debug;
+            $log->debug("stated $path");
         } else {
-            warn "can not stat path: $path";
+            $log->warn("can not stat path: $path");
         }
     }
@@ -263,9 +284,9 @@
   ### get the currently listed pid
   if( ! open(_PID,$pid_file) ){
-    die "Couldn't open existant pid_file \"$pid_file\" [$!]\n";
+    $log->logdie("Couldn't open existant pid_file \"$pid_file\" [$!]");
   }
   my $_current_pid = <_PID>;
   close _PID;
-  my $current_pid = $_current_pid =~ /^(\d{1,10})/ ? $1 : die "Couldn't find pid in existing pid_file";
+  my $current_pid = $_current_pid =~ /^(\d{1,10})/ ? $1 : $log->logdie("Couldn't find pid in existing pid_file");
 
   my $exists = undef;
@@ -289,10 +310,10 @@
 
         if( $current_pid == $$ ){
-            warn "Pid_file created by this same process. Doing nothing.\n";
+            $log->warn("Pid_file created by this same process. Doing nothing.");
             return 1;
         }else{
             kill 'TERM', $current_pid
-                or die "Failed to signal process ($current_pid)";
-            unlink $pid_file || die "Couldn't remove pid_file \"$pid_file\" [$!]\n";
+                or $log->logdie("Failed to signal process ($current_pid)");
+            unlink $pid_file || $log->logdie("Couldn't remove pid_file \"$pid_file\" [$!]");
         }
     }
