tags 705706 patch
thanks

Hello,

The attached patch adds the -l option to implement this feature.

Regards
Simon
-- 
+ privacy is necessary
+ using gnupg http://gnupg.org
+ public key id: 0x92FEFDB7E44C32F9
--- /usr/bin/chronic	2014-02-27 17:02:48.000000000 +0100
+++ chronic	2014-04-12 00:27:05.302766552 +0200
@@ -6,7 +6,7 @@
 
 =head1 SYNOPSIS
 
-chronic COMMAND...
+B<chronic> [B<-l> I<LOGFILE>] I<COMMAND...>
 
 =head1 DESCRIPTION
 
@@ -22,6 +22,10 @@
 
 	0 1 * * * chronic backup # instead of backup >/dev/null 2>&1
 
+If I<LOGFILE> is specified then in addition to the behavior explained above,
+the output is always written to I<LOGFILE>, regardless if the program fails or
+succeeds.
+
 =head1 AUTHOR
 
 Copyright 2010 by Joey Hess <[email protected]>
@@ -37,7 +41,14 @@
 use IPC::Run qw( start pump finish timeout );
 
 if (! @ARGV) {
-	die "usage: chronic COMMAND...\n";
+	die "usage: chronic [-l LOGFILE] COMMAND...\n";
+}
+
+my $logfile;
+if (@ARGV >= 3 and $ARGV[0] eq '-l') {
+	$logfile = $ARGV[1];
+	shift @ARGV;
+	shift @ARGV;
 }
 
 my ($out, $err);
@@ -45,6 +56,15 @@
 $h->finish;
 my $ret=$h->full_result;
 
+if (defined $logfile) {
+	open my $fh, '>', $logfile
+		or showout_and_die("failed to open logfile '$logfile'");
+	print $fh $err; # stderr is more interesting, show it first
+	print $fh $out;
+	close $fh
+		or showout_and_die("failed to close logfile '$logfile'");
+}
+
 if ($ret >> 8) { # child failed
 	showout();
 	exit ($ret >> 8);
@@ -61,3 +81,8 @@
 	print STDOUT $out;
 	print STDERR $err;
 }
+sub showout_and_die {
+	my $errno = $!;
+	showout();
+	die "chronic: $_[0]: $errno\n";
+}

Attachment: signature.asc
Description: Digital signature

Reply via email to