File Coverage

blib/lib/Log/WithCallbacks.pm
Criterion Covered Total %
statement 34 94 36.1
branch 3 38 7.8
condition 3 13 23.0
subroutine 9 15 60.0
pod 8 8 100.0
total 57 168 33.9


line stmt bran cond sub pod time code
1             package Log::WithCallbacks;
2            
3 1     1   961 use strict;
  1         1  
  1         36  
4 1     1   5 use Carp;
  1         2  
  1         75  
5 1     1   968 use Symbol;
  1         1083  
  1         83  
6 1     1   7 use Fcntl;
  1         2  
  1         589  
7 1     1   869 use IO::File;
  1         16418  
  1         142  
8            
9 1     1   18 use vars qw( $VERSION );
  1         2  
  1         1283  
10            
11             $VERSION = '1.00';
12            
13             my $standard_format = sub {
14             my $message = shift;
15            
16             chomp $message; # Clean up any accidental double lines.
17             return scalar localtime(time) . ": $message\n";
18             };
19            
20             sub new {
21 2     2 1 958 my $class = shift;
22 2   66     304 my $file = shift || croak 'Must supply a filename';
23 1         3 my $format = shift;
24            
25 1         5 my $self = {
26             LOGFILE => IO::File->new(),
27             file => $file,
28             'format' => '',
29             'status' => 'closed',
30             mode => 'append',
31             };
32            
33 1         63 $self->{LOGFILE}->autoflush(1);
34 1         56 bless $self, $class;
35            
36             # Set format to default if no format is provided.
37 1 50       5 $format = defined $format ? $format : 'standard';
38 1 50       4 $self->format($format) or $self->format('standard');
39            
40 1         6 return $self;
41             }
42            
43             sub open {
44 0     0 1 0 my $self=shift;
45 0   0     0 my $mode= shift || $self->mode() || 'append';
46 0         0 my $format = shift;
47            
48 0         0 $self->mode($mode);
49 0         0 $self->format($format);
50 0         0 $self->_setStatus('opening');
51            
52             # Assemble mode
53 0         0 my $fcntl_mode = O_WRONLY | O_CREAT;
54            
55 0 0       0 if ($self->mode eq 'append') {
    0          
56 0         0 $fcntl_mode |= O_APPEND;
57             } elsif ($self->mode eq 'overwrite') {
58 0         0 $fcntl_mode |= O_TRUNC;
59             } else {
60 0         0 my $badmode = $self->mode;
61 0         0 croak "Illegal mode: cannot open logfile in mode $badmode"
62             }
63             # Open IO::File object.
64 0 0       0 $self->{LOGFILE}->open($self->{file}, $fcntl_mode)
65             or croak "Unable to open logfile $self->{file}: $!";
66            
67 0         0 $self->_setStatus('open');
68             }
69            
70             sub close {
71 0     0 1 0 my $self = shift;
72            
73 0 0       0 carp('Cannot close a logfile that is not open'), return 0
74             unless $self->status eq 'open';
75            
76 0         0 $self->_setStatus('closing');
77            
78 0 0       0 close $self->{LOGFILE} or croak "Unable to close logfile: $!";
79            
80 0         0 $self->_setStatus('closed');
81 0         0 return 'closed';
82             }
83            
84             sub exit {
85 0     0 1 0 my $self = shift;
86            
87 0         0 my $error_code = shift;
88 0         0 my ($message) = @_;
89            
90 0         0 $self->entry("Script terminating - $error_code", @_);
91            
92 0         0 $self->entry( @_ );
93            
94 0         0 $!=$error_code;
95 0         0 croak "$message";
96             }
97            
98             sub entry {
99 0     0 1 0 my $self = shift;
100            
101 0         0 my $message = shift;
102 0         0 my $format = shift;
103            
104 0 0 0     0 carp "Format '$format' is not a code reference or string literal 'standard'."
      0        
105             if (
106             ( $format ) &&
107             ( ref($format) ne 'CODE' ) &&
108             ( $format ne 'standard' )
109             );
110            
111 0 0       0 $format = $standard_format if $format eq 'standard';
112            
113 0 0       0 $format = $self->{'format'} unless ref($format) eq 'CODE';
114            
115 0 0       0 carp "Cannot log entry unless log status is 'open'"
116             if $self->status ne 'open';
117            
118 0         0 my $string = $format->($message);
119 0         0 print {$self->{LOGFILE}} $string;
  0         0  
120            
121 0         0 return $string;
122             }
123            
124             sub status {
125 1     1 1 770 my $self = shift;
126            
127 1         6 return $self->{status};
128             }
129            
130             sub mode {
131 0     0 1 0 my $self = shift;
132 0         0 my $mode = shift;
133            
134 0         0 my %mode;
135 0         0 @mode{qw(overwrite append)} = (1) x 2;
136            
137 0 0       0 if ($mode) {
138 0 0       0 if (exists $mode{$mode}) {
139 0 0       0 if ($self->status eq 'closed') {
    0          
140 0         0 $self->{mode} = $mode;
141             }
142             elsif ($self->{mode} ne $mode) {
143 0         0 carp "Can only set mode when logfile is closed";
144             }
145             } else {
146 0         0 carp "Illegal mode $mode, mode remains set to $self->{mode}";
147 0         0 return 0;
148             }
149             }
150            
151 0         0 return $self->{mode};
152             }
153            
154             sub format {
155 1     1 1 1 my $self = shift;
156 1   50     4 my $format = shift || '';
157            
158 1 50       3 if ($format eq 'standard') {
    0          
159 1         5 $self->{'format'} = $standard_format;
160             }
161             elsif ($format) {
162 0 0       0 unless ( ref $format eq 'CODE' ) {
163 0         0 croak "Format must be a code reference or 'standard'";
164 0         0 return 0;
165             }
166 0         0 $self->{'format'} = $format;
167             }
168            
169 1         7 return $self->{'format'};
170             }
171            
172             sub _setStatus {
173 0     0     my $self = shift;
174 0           my $status = shift;
175            
176 0           my %status;
177 0           @status{qw(open closed opening closing)} = (1) x 4;
178            
179 0 0         croak "Illegal logfile status $status" unless exists $status{$status};
180 0           $self->{'status'} = $status;
181             }
182            
183             1;
184             __END__