cosmetic/code moved docu to pod2usage, handle db errors
authorHeiko Schlittermann <hs@schlittermann.de>
Mon, 07 Sep 2009 00:57:51 +0200
changeset 27 10c207a978e2
parent 26 cf9761eb2538
child 28 714d4a0ea0bc
cosmetic/code moved docu to pod2usage, handle db errors
debian/control
log.pl
modules
--- a/debian/control	Mon Sep 07 00:37:00 2009 +0200
+++ b/debian/control	Mon Sep 07 00:57:51 2009 +0200
@@ -8,6 +8,7 @@
 Package: logbuch
 Architecture: all
 Suggests: mercurial
+Recommends: vim, perl-doc
 Depends: ${perl:Depends}, libdbi-perl, libdbd-mysql-perl, libmailtools-perl,
          libfile-which-perl, libclass-accessor-perl
 Description: Logbuch for server maintainance
--- a/log.pl	Mon Sep 07 00:37:00 2009 +0200
+++ b/log.pl	Mon Sep 07 00:57:51 2009 +0200
@@ -1,21 +1,5 @@
 #! /usr/bin/perl 
-# $Id$
-# $URL$
-my $USAGE = <<'#';
-$ME [options]
-    --[no]db	 insert into log database [$opt_db]
-    --[no]mail	 send mails to @config::mailto [$opt_mail]
-    --init-dir=s initialize specified directory with VCS repository
-    --message=s  message
-                - if the message starts with '\@' it's considered
-		  to be a message FILE.
-		- message can be: apt[:text]\@<FILE> - this will
-		  start special file processing (assuming output
-		  from apt, with text (default: APT: upgrade) going
-		  to the first line)
-    --logfile=s	  use this(!) logfile
-#
-
+# © 2009 Schlittermann - internet & unix support
 # something about charsets
 # * We assume the LOG file is always UTF-8!
 #   (I know, it's not true for historical entries, may be we can
@@ -47,6 +31,7 @@
 use MIME::QuotedPrint;
 use I18N::Langinfo qw(langinfo CODESET);
 use Text::Iconv;
+use Pod::Usage;
 
 use Logbuch::HG;
 
@@ -93,13 +78,15 @@
 MAIN: {
 
     GetOptions(
-        "db!"        => \$opt_db,
-        "mail!"      => \$opt_mail,
-        "message=s"  => \$opt_message,
-        "type=s"     => \$opt_apt,
-        "init-dir=s" => \$opt_initdir,
-        "file=s"     => \$opt_file,
-    ) or die eval "\"$USAGE\"";
+        "db!"         => \$opt_db,
+        "mail!"       => \$opt_mail,
+        "m|message=s" => \$opt_message,
+        "type=s"      => \$opt_apt,
+        "init-dir=s"  => \$opt_initdir,
+        "f|file=s"    => \$opt_file,
+        "man"         => sub { pod2usage(-verbose => 2, -exit => 0) },
+        "help"        => sub { pod2usage(-verbose => 0, -exit => 0) },
+    ) or pod2usage();
 
     if ($opt_message =~ /^@(.*)/) {
         @ARGV = $1;
@@ -158,9 +145,11 @@
     }
 
     if ($opt_db) {
-        $Dbh = DBI->connect($DSN, $USER, $PW, { RaiseError => 1 })
-          or die $DBI::errstr;
         END { $Dbh->disconnect() if $Dbh; }
+        $Dbh = DBI->connect($DSN, $USER, $PW, { RaiseError => 0 });
+        if (!$Dbh or !$Dbh->connected) {
+            warn $DBI::errstr;
+        }
     }
 
     # Temporärfile öffnen
@@ -229,7 +218,7 @@
         truncate $fh, $pos;
         seek $fh, 0, 2;
 
-        if ($opt_db) {
+        if ($opt_db and $Dbh) {
             my $sth = $Dbh->prepare("
 		    INSERT INTO log (host, date, user, mailto, text)
 		    VALUES(?, now(), ?, ?, ?)");
@@ -314,6 +303,57 @@
 EOF
     }
 }
+__END__
+
+=head1 NAME
+
+    log -- log utility (ius)
+
+=head1 SYNOPSIS
+
+    log [--[no]db] [--[no]mail] [--init-dir <dir>] [--message <msg>] [-f|--logfile <file>]
+
+=head1 DESCRIPTION
+
+This (ius) tool prepends a custom log message to a log file (typically
+/root/LOG.<hostname>). 
+
+=head1 OPTIONS
+
+B<Beware>: the defaults of some of these options may be changed through
+a configuration file.
+
+=over
+
+=item B<--[no]db>
+
+(Don't) write the log messages into the specified database (default: on)
+
+=item B<--[no]mail>
+
+(Don't) send the log message as mail. (default: on)
+
+=item B<--init-dir> I<dir>
+
+Initialize the directory I<dir> with a VCS repository. When you've done
+this, further changes to I<dir> will be logged too.
+
+=item B<--message> I<msg>
+
+The message to use. Otherwise an editor ($ENV{EDITOR}) is started.
+If the message starts with a "@", it's considered to be a file.
+
+When the messages starts as "apt[:I<text>]@I<FILE>" it starts special
+apt message processing. The default I<text> is "APT: upgrade".
+This text becomes the first line of the log message.
+
+=item B<-f>|B<--file> I<file>
+
+The logfile to use. (default: F< /root/LOG.<NODENAME>>)
+
+=back
+
+=cut
 
 # vim:sts=4 sw=4 aw ai sm:
 
--- a/modules	Mon Sep 07 00:37:00 2009 +0200
+++ b/modules	Mon Sep 07 00:57:51 2009 +0200
@@ -9,3 +9,4 @@
 Mail::Mailer
 MIME::QuotedPrint
 Text::Iconv
+Pod::Usage