Fine/Handle/File.pm

filter: Statementstmt 98.0% 50 / 51 Branchbran 61.5% 16 / 26 Conditioncond 48.1% 13 / 27 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 72.0% 93 / 129 CC 23 SCAR 19.8
CC
23
Cov
76%
DESTROY
12.4
fileHandle
8.9
_init
8.6
CRAP 7.2
« Fine/Logger.pm ↑ Summary Fine/Handle/File/Timestamp.pm »
1  
2 =head1 NAME
3  
4 Log::Fine::Handle::File - Output log messages to a file
5  
6 =head1 SYNOPSIS
7  
8 Provides logging to a file
9  
10 use Log::Fine;
11 use Log::Fine::Handle::File;
12  
13 # Get a new logger
14 my $log = Log::Fine->logger("foo");
15  
16 # Create a file handle (default values shown)
17 my $handle = Log::Fine::Handle::File
18 ->new( name => 'file0',
19 mask => LOGMASK_EMERG | LOGMASK_ALERT | LOGMASK_CRIT | LOGMASK_ERR | LOGMASK_WARNING | LOGMASK_NOTICE | LOGMASK_INFO,
20 dir => "/var/log",
21 file => "myapp.log",
22 autoflush => 0 );
23  
24 # Register the handle
25 $log->registerHandle($handle);
26  
27 # Log something
28 $log->(INFO, "Opened new log handle");
29  
30 =head1 DESCRIPTION
31  
32 Log::Fine::Handle::File provides logging to a file. Note that this
33 module will log messages to a specific file. Support for dynamic
34 time-stamps in file names (e.g., C<myapp-080523.log>) is provided by
35 L<Log::Fine::Handle::File::Timestamp>. Further features, such as log
36 file rotation I<a la> L<syslog> can be added by sub-classing this class.
37  
38 =head2 Constructor Parameters
39  
40 The following parameters can be passed to
41 Log::Fine::Handle::File->new():
42  
43 =over
44  
45 =item * name
46  
47 [optional] Name of this object (see L<Log::Fine>). Will be autoset if
48 not specified.
49  
50 =item * mask
51  
52 Mask to set the handle to (see L<Log::Fine::Handle>)
53  
54 =item * dir
55  
56 Directory to place the log file
57  
58 =item * file
59  
60 Name of the log file. Note that if the given file is an absolute
61 path, then C<dir> will be ignored.
62  
63 =item * autoclose
64  
65 [default: 0] If set to true, will close the filehandle after every
66 invocation of L</"msgWrite">. B<NOTE:> will I<significantly> slow
67 down logging speed if multiple messages are logged at once. Consider
68 autoflush instead
69  
70 =item * autoflush
71  
72 [default: 0] If set to true, will force file flush after every write or
73 print (see L<FileHandle> and L<perlvar>)
74  
75 =back
76  
77 =cut
78  
79 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 package Log::Fine::Handle::File;
83  
84 7 ▶use base qw( Log::Fine::Handle );
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 7 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 7 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 7 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
89 7 ▶use Log::Fine;
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 7 our $VERSION = $Log::Fine::Handle::VERSION;
92  
93 =head1 METHODS
94  
95 =head2 fileHandle
96  
97 Getter for file handle. If a file handle is not defined, then one
98 will be created.
99  
100 Override this method if you wish to support
101 features such as time-stamped and/or rotating files.
102  
103 =head3 Returns
104  
105 A L<FileHandle> object
106  
107 =cut
108  
109 sub fileHandle
110 13 ▶{
Subroutine fileHandle: called CC=8 SCAR=21.8
Pod documented
111  
112 13 my $self = shift;
113  
114 # Should we already have a file handle defined, return it
115 return $self->{_filehandle}
116 if ( defined $self->{_filehandle}
117 and ref $self->{_filehandle}
118 and UNIVERSAL::can($self->{_filehandle}, 'isa')
119 and $self->{_filehandle}->isa("IO::File")
120 13 ▶ and defined fileno($self->{_filehandle}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_filehandle"} and ref $self->{"_filehandle"} and UNIVERSAL::can($self->{"_filehandle"}, "isa") and $self->{"_filehandle"}->isa("IO::File") and defined fileno $self->{"_filehandle"} 8 5 13
Condition66%8/12
defined $self->{"_filehandle"} and ref $self->{"_filehandle"} and UNIVERSAL::can($self->{"_filehandle"}, "isa") and $self->{"_filehandle"}->isa("IO::File") and defined fileno $self->{"_filehandle"}
!ll&&!rl&&r
508
defined $self->{"_filehandle"} and ref $self->{"_filehandle"} and UNIVERSAL::can($self->{"_filehandle"}, "isa") and $self->{"_filehandle"}->isa("IO::File")
!ll&&!rl&&r
508
defined $self->{"_filehandle"} and ref $self->{"_filehandle"} and UNIVERSAL::can($self->{"_filehandle"}, "isa")
!ll&&!rl&&r
508
defined $self->{"_filehandle"} and ref $self->{"_filehandle"}
!ll&&!rl&&r
508
MC/DC20%1/5
defined $self->{"_filehandle"} and ref $self->{"_filehandle"} and UNIVERSAL::can($self->{"_filehandle"}, "isa") and $self->{"_filehandle"}->isa("IO::File") and defined fileno $self->{"_filehandle"}
defined $self->{"_filehandle"} ref $self->{"_filehandle"} UNIVERSAL::can($self->{"_filehandle"}, "isa") $self->{"_filehandle"}->isa("IO::File") defined fileno $self->{"_filehandle"}
Truth table33%2/6
A and B and C and D and E
A= defined $self->{"_filehandle"}B= ref $self->{"_filehandle"}C= UNIVERSAL::can($self->{"_filehandle"}, "isa")D= $self->{"_filehandle"}->isa("IO::File")E= defined fileno $self->{"_filehandle"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
121  
122 # Generate file name
123 my $filename =
124 ($self->{dir} =~ /\w/)
125 ? catdir($self->{dir}, $self->{file})
126 5 ▶ : $self->{file};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'dir'} =~ /\w/ ? : 0 5 5
127  
128 # Otherwise create a new one
129 5 $self->{_filehandle} = FileHandle->new(">> " . $filename);
130  
131 $self->_fatal("Unable to open log file $filename : $!\n")
132 5 ▶ unless defined $self->{_filehandle};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_filehandle"} 0 5 5
133  
134 # Set autoflush if necessary
135 5 $self->{_filehandle}->autoflush($self->{autoflush});
136  
137 5 return $self->{_filehandle};
138  
139 } # fileHandle()
140  
141 =head2 msgWrite
142  
143 See L<Log::Fine::Handle/msgWrite>
144  
145 =cut
146  
147 sub msgWrite
148 11 ▶{
Subroutine msgWrite: called CC=5 SCAR=16.3
Pod documented
149  
150 11 my $self = shift;
151 11 my $lvl = shift;
152 11 my $msg = shift;
153 11 my $skip = shift;
154  
155 # Grab a ref to our file handle
156 11 my $fh = $self->fileHandle();
157  
158 # Should we have a formatter defined, then use that,
159 # otherwise, just print the raw message
160 $msg = $self->{formatter}->format($lvl, $msg, $skip)
161 11 ▶ if defined $self->{formatter};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"formatter"} 11 0 11
162  
163 11 ▶ print $fh $msg or $self->_error("Cannot write to file handle : $!");
Branch: 1 branch
BranchTrueFalseTotal
print $fh $msg or $self->_error("Cannot write to file handle : $!") 11 0 11
164  
165 # Should {autoclose} be set, then close the file handle. This
166 # will force the creation of a new filehandle the next time
167 # this method is called
168 11 ▶ if ($self->{autoclose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autoclose"}) 1 10 11
169 $fh->close()
170 || $self->_error(
171 1 ▶ sprintf("Unable to close filehandle to %s : %s", catdir($self->{dir}, $self->{file}), $!));
Branch: 1 branch
BranchTrueFalseTotal
$fh->close or $self->_error(sprintf("Unable to close filehandle to %s : %s", catdir($self->{"dir"}, $self->{"file"}), $!)) 1 0 1
172 }
173  
174 11 return $self;
175  
176 } # msgWrite()
177  
178 # --------------------------------------------------------------------
179  
180 ##
181 # Initializes our object
182  
183 sub _init
184 7 ▶{
Subroutine _init: called CC=7 SCAR=21.5
185  
186 7 my $self = shift;
187  
188 # Perform any necessary upper class initializations
189 7 $self->SUPER::_init();
190  
191 # Default directory is the current directory unless file is an
192 # absolute path
193 7 ▶ if ($self->{file} =~ /^\/|^[A-Za-z]:\\/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'file'} =~ m[^/|^[A-Za-z]:\\]) { } 0 7 7
elsif (not defined $self->{'dir'} && -d $self->{'dir'}) { } 7 0 7
194 0 $self->{dir} = "";
195 ▶ } elsif (not defined $self->{dir} or not -d $self->{dir}) {
Condition33%1/3
defined $self->{'dir'} && -d $self->{'dir'}
!ll&&!rl&&r
700
MC/DC0%0/2
defined $self->{'dir'} && -d $self->{'dir'}
defined $self->{'dir'} -d $self->{'dir'}
Truth table33%1/3
A && B
A= defined $self->{'dir'}B= -d $self->{'dir'}
ABresult
0X0
100
111
196 7 $self->{dir} = "./";
197 }
198  
199 # Default file name is the name of the invoking program
200 # suffixed with ".log"
201 $self->{file} = basename($0) . ".log"
202 7 ▶ unless defined $self->{file};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"file"} 0 7 7
203  
204 # autoflush is disabled by default
205 $self->{autoflush} = 0
206 7 ▶ unless defined $self->{autoflush};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"autoflush"} 0 7 7
207  
208 # autoclose is disabled by default
209 $self->{autoclose} = 0
210 7 ▶ unless defined $self->{autoclose};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"autoclose"} 6 1 7
211  
212 7 return $self;
213  
214 } # _init()
215  
216 ##
217 # Called when this object is destroyed
218  
219 sub DESTROY
220 5 ▶{
Subroutine DESTROY: called CC=6 SCAR=25.2
221  
222 5 my $self = shift;
223  
224 # Close our filehandle if necessary.
225 $self->{_filehandle}->close()
226 if ( defined $self->{_filehandle}
227 and ref $self->{_filehandle}
228 and UNIVERSAL::can($self->{_filehandle}, 'isa')
229 and $self->{_filehandle}->isa("IO::File")
230 5 ▶ and defined fileno($self->{_filehandle}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_filehandle"} and ref $self->{"_filehandle"} and UNIVERSAL::can($self->{"_filehandle"}, "isa") and $self->{"_filehandle"}->isa("IO::File") and defined fileno $self->{"_filehandle"} 0 5 5
Condition33%4/12
defined $self->{"_filehandle"} and ref $self->{"_filehandle"} and UNIVERSAL::can($self->{"_filehandle"}, "isa") and $self->{"_filehandle"}->isa("IO::File") and defined fileno $self->{"_filehandle"}
!ll&&!rl&&r
050
defined $self->{"_filehandle"} and ref $self->{"_filehandle"} and UNIVERSAL::can($self->{"_filehandle"}, "isa") and $self->{"_filehandle"}->isa("IO::File")
!ll&&!rl&&r
005
defined $self->{"_filehandle"} and ref $self->{"_filehandle"} and UNIVERSAL::can($self->{"_filehandle"}, "isa")
!ll&&!rl&&r
005
defined $self->{"_filehandle"} and ref $self->{"_filehandle"}
!ll&&!rl&&r
005
MC/DC0%0/5
defined $self->{"_filehandle"} and ref $self->{"_filehandle"} and UNIVERSAL::can($self->{"_filehandle"}, "isa") and $self->{"_filehandle"}->isa("IO::File") and defined fileno $self->{"_filehandle"}
defined $self->{"_filehandle"} ref $self->{"_filehandle"} UNIVERSAL::can($self->{"_filehandle"}, "isa") $self->{"_filehandle"}->isa("IO::File") defined fileno $self->{"_filehandle"}
Truth table16%1/6
A and B and C and D and E
A= defined $self->{"_filehandle"}B= ref $self->{"_filehandle"}C= UNIVERSAL::can($self->{"_filehandle"}, "isa")D= $self->{"_filehandle"}->isa("IO::File")E= defined fileno $self->{"_filehandle"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
231  
232 } # DESTROY
233  
234 =head1 BUGS
235  
236 Please report any bugs or feature requests to
237 C<bug-log-fine at rt.cpan.org>, or through the web interface at
238 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Log-Fine>.
239 I will be notified, and then you'll automatically be notified of progress on
240 your bug as I make changes.
241  
242 =head1 SUPPORT
243  
244 You can find documentation for this module with the perldoc command.
245  
246 perldoc Log::Fine
247  
248 You can also look for information at:
249  
250 =over 4
251  
252 =item * AnnoCPAN: Annotated CPAN documentation
253  
254 L<http://annocpan.org/dist/Log-Fine>
255  
256 =item * CPAN Ratings
257  
258 L<http://cpanratings.perl.org/d/Log-Fine>
259  
260 =item * RT: CPAN's request tracker
261  
262 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Log-Fine>
263  
264 =item * Search CPAN
265  
266 L<http://search.cpan.org/dist/Log-Fine>
267  
268 =back
269  
270 =head1 AUTHOR
271  
272 Christopher M. Fuhrman, C<< <cfuhrman at pobox.com> >>
273  
274 =head1 SEE ALSO
275  
276 L<perl>, L<Log::Fine>, L<Log::Fine::Handle>
277  
278 =head1 COPYRIGHT & LICENSE
279  
280 Copyright (c) 2008, 2010-2011, 2013 Christopher M. Fuhrman,
281 All rights reserved.
282  
283 This program is free software licensed under the...
284  
285 The BSD License
286  
287 The full text of the license can be found in the
288 LICENSE file included with this module.
289  
290 =cut
291  
292 7 1; # End of Log::Fine::Handle::File
« Fine/Logger.pm ↑ Summary Fine/Handle/File/Timestamp.pm »