File Coverage

blib/lib/BeamerReveal/Log.pm
Criterion Covered Total %
statement 12 74 16.2
branch 0 14 0.0
condition 0 20 0.0
subroutine 4 14 28.5
pod 3 8 37.5
total 19 130 14.6


line stmt bran cond sub pod time code
1             # -*- cperl -*-
2             # ABSTRACT: Log
3              
4              
5             package BeamerReveal::Log;
6             our $VERSION = '20260208.1851'; # VERSION
7              
8 1     1   8 use parent 'Exporter';
  1         2  
  1         11  
9 1     1   116 use Carp;
  1         3  
  1         71  
10              
11 1     1   651 use BeamerReveal::Log::Ansi;
  1         3  
  1         67  
12              
13 1     1   663 use IO::File;
  1         2465  
  1         1618  
14              
15             our $logger;
16              
17 0     0 0   sub max { $_[$_[0] < $_[1] ] }
18              
19              
20              
21             sub new {
22 0 0   0 1   $logger->fatal( "Error: internal error - attempt to create two logger objects\n" )
23             if ( defined( $logger ) );
24 0           my $class = shift;
25 0           my $args = { @_ };
26             exists $args->{logfilename}
27             and exists $args->{opening}
28             and exists $args->{closing}
29             and exists $args->{labelsize}
30             and exists $args->{activitysize}
31 0 0 0       or die( "Error: missing argument to (child of) BeamerReveal::Log::new()\n" );
      0        
      0        
      0        
32 0           my $self = BeamerReveal::Log::Ansi->new( %$args );
33              
34 0           $self->{tasks} = [];
35              
36 0           $self->{termwidth} = $self->_terminal_width();
37 0           $self->{barsize} = $self->{termwidth} - $self->{labelsize} - $self->{activitysize} - 12;
38            
39 0           $self->{logfile} = IO::File->new();
40 0 0         $self->{logfile}->open( ">$self->{logfilename}" )
41             or die( "Error cannot open log file $self->{logfilename}" );
42              
43             # build opening lines for logfile
44 0           print {$self->{logfile}} _formatLines( $self->{opening}, 76 );
  0            
45             # build opening lines for terminal
46 0           print _formatLines( $self->{opening}, $self->{termwidth} );
47            
48 0           $logger = $self;
49            
50 0           return $self;
51             }
52              
53              
54             sub log {
55 0     0 1   my $self = shift;
56 0           my ( $indent, $message ) = @_;
57              
58 0           say {$self->{logfile}} ( ' ' x $indent ) . $message;
  0            
59             }
60              
61             sub fatal {
62 0     0 0   my $self = shift;
63 0           my ( $message ) = @_;
64              
65 0           say {$self->{logfile}} $message;
  0            
66 0           $self->{logfile}->close();
67            
68 0           die( "$message\n" .
69             "Check the logfile $self->{logfilename} for more information.\n" );
70             }
71              
72              
73             sub registerTask {
74 0     0 1   my $self = shift;
75 0           my $args = { @_ };
76             exists $args->{label}
77             and exists $args->{progress}
78             and exists $args->{total}
79 0 0 0       or $self->fatal( "Error: missing argument to BeamerReveal::Log::registerTask\n" );
      0        
80             # fill out default value for activity message
81 0           $args->{activity} = '';
82            
83 0           my $index = @{$self->{tasks}};
  0            
84 0           push @{$self->{tasks}}, $args;
  0            
85 0           return $index;
86             }
87              
88             sub activate {
89 0     0 0   my $self = shift;
90 0           $self->fatal( "Error: internal error - activation through BeamerReveal::Log base class\n" );
91             }
92              
93             sub progress {
94 0     0 0   my $self = shift;
95 0           $self->fatal( "Error: internal error - progress reported through BeamerReveal::Log base class\n" );
96             }
97              
98             sub finalize {
99 0     0 0   my $self = shift;
100 0           $self->fatal( "Error: internal error - finalization through BeamerReveal::Log base class\n" );
101             }
102              
103             sub _bar_line {
104 0     0     my ($label, $labelsize, $activity, $activitysize, $done, $total, $width) = @_;
105            
106 0           my $pct = $done / $total;
107 0 0         $pct = 1 if $pct > 1;
108            
109 0           my $filled = int($pct * $width);
110 0           my $empty = $width - $filled;
111              
112 0 0         if ( $width < 0 ) {
113 0           return sprintf("%s: %s / %5.1f%%",
114             $label,
115             $activity,
116             $pct * 100
117             );
118             }
119             else {
120 0           return sprintf("%-${labelsize}s: %-${activitysize}s [%s%s] %5.1f%%",
121             $label,
122             $activity,
123             '#' x $filled,
124             '-' x $empty,
125             $pct * 100
126             );
127             }
128             }
129              
130             sub _formatLines {
131 0     0     my ( $linearrayref, $width, $extra ) = @_;
132 0   0       $extra ||= '';
133 0           my $openinglines;
134 0           foreach my $line ( @{$linearrayref} ) {
  0            
135 0           my ( $left, $right) = split( /\|/, $line );
136 0 0         if( $left eq $right ) {
137 0           $openinglines .= $extra . ( $left x $width ) . "\n";
138             }
139             else {
140 0           my $llen = length( $left );
141 0           my $rlen = length( $right );
142 0           my $midspace = max( 1, $width - $llen - $rlen );
143 0           $openinglines .= $extra . $left . ( ' ' x $midspace ) . $right . "\n";
144             }
145             }
146 0           return $openinglines;
147             }
148             1;
149              
150             __END__