From: Andrew DeFaria Date: Tue, 19 Dec 2017 19:15:05 +0000 (-0800) Subject: Added additional reporting to setbg X-Git-Url: https://defaria.com/gitweb/?a=commitdiff_plain;h=a5e947bf363fa107c5f7de50e6334ddcec415eb0;p=clearscm.git Added additional reporting to setbg Chnaged setbg to report to ~/.setbg.hist to show the history of images displayed over the course of a run. Also changed it to output ~/.setbg.stats to display statistics about how many images have been displayed and are available. --- diff --git a/bin/setbg b/bin/setbg index 09311f5..981c77f 100755 --- a/bin/setbg +++ b/bin/setbg @@ -64,6 +64,7 @@ use lib "$FindBin::Bin/../lib"; use DateUtils; use Display; +use Logger; use Utils; my $VERSION = '$Revision: 1.10 $'; @@ -71,6 +72,7 @@ my $VERSION = '$Revision: 1.10 $'; my $processes = new Proc::ProcessTable; my @imgDirs; +my %totals; foreach my $process (@{$processes->table}) { if ($process->cmndline =~ /setbg/ and @@ -81,19 +83,59 @@ foreach my $process (@{$processes->table}) { } # if } # foreach +sub displayStats () { + my $statsFile = Logger->new( + name => ".$FindBin::Script.stats", + path => $ENV{HOME}, + extension => '', + ); + + $statsFile->log('At ' . localtime()); + + Stats \%totals, $statsFile; + + return; +} # displayStats + sub fillPictures () { my @images; - + foreach (@imgDirs) { my @pics = `find "$_" -type f -name "*.jpg"`; - + chomp @pics; push @images, @pics; } # foreach - return @images; + $totals{images} = scalar @images; + + displayStats; + + return @images; } # fillPictures +sub updateSetBG ($) { + my ($image) = @_; + + open my $log, '>', "$ENV{HOME}/.$FindBin::Script" + or error "Unable to open $ENV{HOME}/.$FindBin::Script for writing - $!", 1; + + display $image, $log; + + close $log; + + open $log, '>>', "$ENV{HOME}/.$FindBin::Script.hist" + or error "Unable to open $ENV{HOME}/.$FindBin::Script.hist for append - $!", 1; + + my $msg = localtime() . ":$image"; + + display $msg, $log; + + close $log; + + return; +} # updateSetBG + $0 = "$FindBin::Script " . join ' ', @ARGV; verbose "$FindBin::Script v$VERSION"; @@ -110,22 +152,23 @@ GetOptions ( for (my $i = 0; $i < scalar @imgDirs; $i++) { error "$imgDirs[$i] is not a directory", 1 unless -d $imgDirs[$i]; - + $imgDirs[$i] = File::Spec->rel2abs ($imgDirs[$i]); } # foreach # Using gsettings -my $setbg = "gsettings"; -my $setbgOpts = "set org.gnome.desktop.background picture-uri \"file://"; +my $setbg = "gsettings"; +my $setbgOpts= "set org.gnome.desktop.background picture-uri \"file://"; my @images = fillPictures; Usage "No images to display. Must specify -bgdir" unless @images; - + sub SwitchWallPaper { # We don't need to do anything here, just handle the interrupt and # let the while loop continue. debug 'SwitchWallPaper: Interrupt received'; + displayStats; } # SwitchWallPaper $SIG{USR2} = \&SwitchWallPaper; @@ -134,32 +177,31 @@ $SIG{USR1} = \&fillPictures; my $debugger = $DB::OUT; my $today; +truncate "$ENV{HOME}/.$FindBin::Script.hist", 0; + EnterDaemonMode unless defined $DB::OUT; while () { my $image = escapeHTML ($images[int (rand $#images)]); - open my $log, '>', "$ENV{HOME}/.$FindBin::Script" - or error "Unable to open $ENV{HOME}/.$FindBin::Script for writing - $!", 1; - - display $image, $log; - my $cmd = "$setbg $setbgOpts$image\" 2> /dev/null"; my @output = `$cmd`; if ($? != 0) { - display "ERROR Trying to set background - command used \"$cmd\"", $log; - display "Output:"; - display join "\n", @output, $log; + error "Trying to set background - command used \"$cmd\"\n\nOutput\n\n" . + join "\n", @output; + $totals{errors}++; + } else { + $totals{'Images displayed'}++; + + updateSetBG $image; } # if - - close $log; - - $today = YMD; - + + $today = YMD; + sleep $sleep; - + if ($today ne YMD){ @images = fillPictures; } # if diff --git a/lib/Logger.pm b/lib/Logger.pm index f57b6ef..f442cc6 100644 --- a/lib/Logger.pm +++ b/lib/Logger.pm @@ -88,7 +88,7 @@ my $me; BEGIN { # Extract relative path and basename from script name. $me = $FindBin::Script; - + # Remove .pl for Perl scripts that have that extension $me =~ s/\.pl$//; } # BEGIN @@ -164,10 +164,11 @@ Returns: my $disposition = $parms{disposition} ? $parms{disposition} : 'perm'; my $timestamped = $parms{timestamped} ? $parms{timestamped} : 'FALSE'; my $append = $parms{append} ? '>>' : '>'; - my $extension = $parms{extension} ? $parms{extension} : 'log'; my $logfile; - $name = "$name.$extension"; + if (defined $parms{extension}) { + $name .= ".$parms{extension}" unless $parms{extension} eq ''; + } # if open $logfile, $append, "$path/$name" or error "Unable to open logfile $path/$name - $!", 1; @@ -175,10 +176,8 @@ Returns: # Set unbuffered output $logfile->autoflush (); - set_verbose - if $ENV{VERBOSE}; - set_debug - if $ENV{DEBUG}; + set_verbose if $ENV{VERBOSE}; + set_debug if $ENV{DEBUG}; return bless { path => $path, @@ -236,13 +235,13 @@ Returns: } # while close $file; - + return; } # append sub name () { my ($self) = @_; - + =pod =head3 name @@ -280,7 +279,7 @@ Returns: sub fullname () { my ($self) = @_; - + =pod =head3 fullname @@ -359,9 +358,9 @@ Returns: =cut $self->log ($msg, $nolinefeed); - + verbose $msg, undef, $nolinefeed; - + return; } # msg @@ -408,9 +407,9 @@ Returns: =cut $self->log ($msg, $nolinefeed); - + display $msg, undef, $nolinefeed; - + return; } # disp @@ -452,13 +451,15 @@ Returns: =cut $increment ||= 1; - + $self->{errors} += $increment; + + return; } # incrementErr sub err ($;$) { my ($self, $msg, $errno) = @_; - + =pod =head3 err ($msg, $errno) @@ -511,11 +512,11 @@ Returns: } # if $self->log ($msg); - + $self->incrementErr; - + exit $errno if $errno; - + return; } # err @@ -591,10 +592,10 @@ Returns: footing => $footing, data => $logfile ); - + close $logfile or error "Unable to close logfile $log_filename", 1; - + return; } # maillog @@ -644,7 +645,7 @@ Returns: $msg = "$me: " . YMDHM . ": $msg" if $self->{timestamped}; display $msg, $self->{handle}, $nolinefeed; - + return; } # log @@ -709,7 +710,7 @@ Returns: sub loglines () { my ($self) = @_; - + =pod =head3 loglines @@ -789,7 +790,7 @@ Returns: =cut warning $msg, $warnno; - + if ($warnno) { $msg = "WARNING #$warnno: $msg"; } else { @@ -798,13 +799,13 @@ Returns: $self->log ($msg); $self->{warnings}++; - + return; } # warn sub errors () { my ($self) = @_; - + =pod =head3 errors () @@ -842,7 +843,7 @@ Returns: sub warnings () { my ($self) = @_; - + =pod =head3 warnings () @@ -885,11 +886,11 @@ sub DESTROY () { if ($self->{disposition} eq 'temp') { if ($self->{errors} == 0 and - $self->{warnings} == 0) { + $self->{warnings} == 0) { unlink $self->fullname; } # if } # if - + return; } # destroy diff --git a/lib/Utils.pm b/lib/Utils.pm index 9666d81..78af171 100644 --- a/lib/Utils.pm +++ b/lib/Utils.pm @@ -89,9 +89,9 @@ our @EXPORT = qw ( sub _restoreTerm () { # In case the user hits Ctrl-C print "\nControl-C\n"; - + ReadMode 'normal'; - + exit; } # _restoreTerm @@ -142,7 +142,7 @@ Returns: $errorlog ||= $NULL; my $file; - + # Redirect STDIN to $NULL open STDIN, '<', $NULL or error "Can't read $NULL ($!)", 1; @@ -169,7 +169,7 @@ Returns: # Now the parent exits exit if $pid; - + # Write pidfile if specified if ($pidfile) { $pidfile = File::Spec->rel2abs ($pidfile); @@ -178,14 +178,14 @@ Returns: or warning "Unable to open pidfile $pidfile for writing - $!"; print $file "$$\n"; - + close $file; } # if - + # Set process to be session leader setsid () or error "Can't start a new session ($!)", 1; - + return; } # EnterDaemonMode @@ -240,7 +240,7 @@ STDOUT then do so in the $command passed in. my @output = `$cmd`; my $status = $?; - + local $SIG{CHLD} = $sigchld; chomp @output; @@ -349,20 +349,20 @@ Returns: $prompt ||= 'Password'; - + my $password; - + local $| = 1; - + print "$prompt:"; - + $SIG{INT} = \&_restoreTerm; - + ReadMode 'cbreak'; while () { my $key; - + while (not defined ($key = ReadKey -1)) { } if ($key =~ /(\r|\n)/) { @@ -372,14 +372,14 @@ Returns: } # if print '*'; - + $password .= $key; } # while - + ReadMode 'restore'; # Reset tty mode before exiting. $SIG{INT} = 'DEFAULT'; - + return $password; } # GetPassword @@ -468,16 +468,16 @@ In a scalar context just the 1 minute load average. # TODO: Make it work on Windows... return if $^O =~ /win/i; - + open my $loadAvg, '/proc/loadavg' or croak "Unable to open /proc/loadavg\n"; - + my $load = <$loadAvg>; - + close $loadAvg; - + my @loadAvgs = split /\s/, $load; - + if (wantarray) { return @loadAvgs; } else { @@ -530,10 +530,10 @@ Returns: if ($existingPipe) { close $existingPipe; - + open $existingPipe, '|-', $to or error "Unable to open pipe - $!", 1; - + return $existingPipe; } else { open $pipe, '|-', $to @@ -686,7 +686,7 @@ Returns: $pipeToStop ||= $pipe; close $pipeToStop if $pipeToStop; - + return; } # StopPipe @@ -739,7 +739,7 @@ Returns: sub RedirectOutput ($$@) { my ($to, $mode, @output) = @_; - + =pod =head2 RedirectOutput ($to, @ouput) @@ -788,7 +788,7 @@ Returns: chomp; print $out "$_\n"; } # foreach - + return; } # RedirectOutput @@ -835,27 +835,27 @@ Returns: open my $file, '<', $filename or error "Unable to open $filename ($!)", 1; - + if (wantarray) { local $/ = "\n"; my @lines = <$file>; - + close $file or error "Unable to close $filename ($!)", 1; - + my @cleansed_lines; - + foreach (@lines) { chomp; chop if /\r/; push @cleansed_lines, $_ if !/^#/; # Discard comment lines } # foreach - + return @cleansed_lines; } else { local $/ = undef; - + return <$file>; } # if } # ReadFile @@ -904,12 +904,12 @@ Returns: =cut my $msg = "$FindBin::Script Run Statistics:"; - + if ($log and ref $log eq 'Logger') { $total->{errors} = $log->{errors}; $total->{warnings} = $log->{warnings}; } # if - + if (keys %$total) { # Display statistics (if any) if ($log) { @@ -920,7 +920,7 @@ Returns: foreach (sort keys %$total) { $msg = $total->{$_} . "\t $_"; - + if ($log) { $log->msg ($total->{$_} . "\t $_"); } else { @@ -928,7 +928,7 @@ Returns: } # if } # foreach } # if - + return; } # Stats