From 1815cdf0f999a04eb0b6a51b15ff8c8fed9a3074 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Thu, 13 Aug 2009 00:57:20 +0200 Subject: Use Log::Log4perl. --- localmarkdown2sms | 52 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/localmarkdown2sms b/localmarkdown2sms index fcd96a8..1cbaf5a 100755 --- a/localmarkdown2sms +++ b/localmarkdown2sms @@ -23,7 +23,8 @@ use strict; use warnings; -use Env qw[$DEBUG $DUMMY $NOSLEEP]; +use Env qw[$debug $info $warn $dummy $nosleep]; +use Log::Log4perl qw(:easy); use File::Spec; use File::Slurp; use Time::Duration::Parse; @@ -35,6 +36,18 @@ use Proc::Fork; my (%file, %delay, %reply); my ($path) = shift @ARGV; +my ($phone) = shift @ARGV; +my ($key) = lc (shift @ARGV); + +if ($debug) { + Log::Log4perl->easy_init($DEBUG); +} elsif ($INFO) { + Log::Log4perl->easy_init($INFO); +} elsif ($WARN) { + Log::Log4perl->easy_init($WARN); +} elsif ($ERROR) { + Log::Log4perl->easy_init($ERROR); +} foreach my $file (read_dir( $path )) { my ($key, $i, $skipkey, $skipcontent); @@ -59,14 +72,14 @@ foreach my $file (read_dir( $path )) { $skipkey = undef; $skipcontent = undef; if (lc($content) ne $key) { - print STDERR "key \"$key\" extracted from fuzzy string \"$content\" in file \"$file\"\n"; + WARN "key \"$key\" extracted from fuzzy string \"$content\" in file \"$file\""; } if (!defined( $delay{$key})) { $delay{$key}[0] = 0; $warn_nonkey_delay = undef; $warn_nonkey_content = undef; } else { - print STDERR "skipping non-unique key \"$key\" in file \"$file\"\n"; + WARN "skipping non-unique key \"$key\" in file \"$file\""; $key = undef; $skipkey = 1; $skipcontent = 1; @@ -82,17 +95,17 @@ foreach my $file (read_dir( $path )) { } $delay{$key}[$i] += $delay; if ($content ne $1) { - print STDERR "delay (${delay}s) resolved from fuzzy string \"$content\" in file \"$file\"\n"; + WARN "delay (${delay}s) resolved from fuzzy string \"$content\" in file \"$file\""; } } elsif ($skipkey) { # skipping - already warned about it... } else { - print STDERR "ignoring non-key'ed delay line \"$1\" in file \"$file\"\n" unless ($warn_nonkey_delay); + WARN "ignoring non-key'ed delay line \"$1\" in file \"$file\"" unless ($warn_nonkey_delay); $warn_nonkey_delay = 1; $skipcontent = 1; } } else { - print STDERR "ignoring non-parsable headline \"$line\" in file \"$file\"\n"; + WARN "ignoring non-parsable headline \"$line\" in file \"$file\""; $skipcontent = 1; } # reply @@ -115,7 +128,7 @@ foreach my $file (read_dir( $path )) { } elsif ($skipkey or $skipcontent) { # skipping - already warned about it... } else { - print STDERR "skipping non-key'ed content \"$line\" in file \"$file\"\n" unless ($warn_nonkey_content); + WARN "skipping non-key'ed content \"$line\" in file \"$file\"" unless ($warn_nonkey_content); $warn_nonkey_content = 1; } } @@ -129,19 +142,16 @@ foreach my $file (read_dir( $path )) { sub sendmsg { my ($phone, $desc, $msg) = @_; - unless ($DUMMY) { - print STDERR "Exec'ing \"...sendsms $phone ...\"\n" if ($DEBUG); + unless ($dummy) { + DEBUG "Exec'ing \"...sendsms $phone ...\""; system '/usr/share/kannel/contrib/sendsms', $phone, $msg; - print STDERR "Done $desc\n" if ($DEBUG); + DEBUG "Done $desc"; } else { - print STDERR "\n --> $phone: $desc\n"; - print STDERR $msg . "\n"; + INFO "--> $phone: $desc"; + INFO $msg; } } -my ($phone) = shift @ARGV; -my ($key) = lc (shift @ARGV); - my $num_children = $#{ $reply{$key} } + 1; # How many children we'll create if (0 == $num_children) { @@ -155,23 +165,23 @@ if (0 == $num_children) { $SIG{CHLD} = 'IGNORE'; # Don't worry about reaping zombies # Spawn off some children -if ($DEBUG) { - print STDERR "queueing $num_children replies:"; +if ($debug) { + DEBUG "queueing $num_children replies:"; for my $num ( 0 .. $num_children - 1 ) { - print STDERR " [" . $delay{$key}[$num] . "s]"; + DEBUG " [" . $delay{$key}[$num] . "s]"; } - print STDERR "\n"; +# DEBUG "\n"; } for my $num ( 0 .. $num_children - 1 ) { run_fork { child { - sleep($delay{$key}[$num]) unless ($NOSLEEP); + sleep($delay{$key}[$num]) unless ($nosleep); &sendmsg($phone, "reply #$num [" . $delay{$key}[$num] . "s]", $reply{$key}[$num]); exit; } parent { - if ($DEBUG) { + if ($debug) { my $child_pid = shift; waitpid $child_pid, 0; } -- cgit v1.2.3