File Coverage

blib/lib/App/MaMGal/Logger.pm
Criterion Covered Total %
statement 40 40 100.0
branch 16 18 88.8
condition 7 8 87.5
subroutine 7 7 100.0
pod 0 3 0.0
total 70 76 92.1


line stmt bran cond sub pod time code
1             # mamgal - a program for creating static image galleries
2             # Copyright 2007-2009 Marcin Owsiany
3             # See the README file for license information
4             # A logging subsystem class
5             package App::MaMGal::Logger;
6 1     1   2584 use strict;
  1         1  
  1         27  
7 1     1   5 use warnings;
  1         2  
  1         32  
8 1     1   4 use base 'App::MaMGal::Base';
  1         2  
  1         532  
9 1     1   5 use Carp;
  1         2  
  1         433  
10              
11             sub init
12             {
13 6     6 0 7 my $self = shift;
14 6 100       74 my $fh = shift or croak "filehandle arg required";
15 5         14 $self->{fh} = $fh;
16             }
17              
18             sub log_message
19             {
20 21     21 0 938 my $self = shift;
21 21         23 my $msg = shift;
22 21   100     49 my $prefix = shift || '';
23 21 100       42 $prefix .= ': ' if $prefix;
24 21         106 $self->{fh}->printf("%s%s\n", $prefix, $msg);
25             }
26              
27             our $not_available_warned_before = 0;
28             our $exe_failure_warned_before = 0;
29              
30             sub log_exception
31             {
32 11     11 0 568 my $self = shift;
33 11         13 my $e = shift;
34 11         14 my $prefix = shift;
35 11 100       26 if ($e->isa('App::MaMGal::MplayerWrapper::NotAvailableException')) {
    100          
    100          
36             # TODO this needs to be made thread-safe
37 2 100       35 return if $not_available_warned_before;
38 1         2 $not_available_warned_before = 1;
39             } elsif ($e->isa('App::MaMGal::MplayerWrapper::ExecutionFailureException')) {
40             # TODO this needs to be made thread-safe
41 3 100 66     135 goto JUST_LOG if $exe_failure_warned_before or (! $e->stdout and ! $e->stderr);
      100        
42 1         57 $exe_failure_warned_before = 1;
43 1         5 $self->log_message($e->message, $prefix);
44 1         52 $self->log_message('--------------------- standard output messages -------------------', $prefix);
45 1 50       49 $self->log_message($_, $prefix) for $e->stdout ? @{$e->stdout} : ();
  1         45  
46 1         48 $self->log_message('--------------------- standard error messages --------------------', $prefix);
47 1 50       48 $self->log_message($_, $prefix) for $e->stderr ? @{$e->stderr} : ();
  1         50  
48 1         45 $self->log_message('------------------------------------------------------------------', $prefix);
49 1         45 return;
50             } elsif ($e->isa('App::MaMGal::SystemException')) {
51 2         104 $self->log_message($e->interpolated_message);
52 2         123 return;
53             }
54             JUST_LOG:
55 7         371 $self->log_message($e->message, $prefix);
56             }
57              
58             1;