Delegate/File.pm

filter: Statementstmt 36.9% 17 / 46 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 55.5% 5 / 9 Podpod 100% 1 / 1 totaltotal 28.4% 23 / 81 CC 11 SCAR 28.5
CC
11
Cov
25%
_connect
30.0
new
30.0
_write
6.0
CRAP 17.3
« DebugPrinter.pm ↑ Summary Delegate/DB.pm »
1 #================================ File.pm ====================================
2 # Filename: File.pm
3 # Description: File logger delegate.
4 # Original Author: Dale M. Amon
5 # Revised by: $Author: amon $
6 # Date: $Date: 2008-08-28 23:20:19 $
7 # Version: $Revision: 1.9 $
8 # License: LGPL 2.1, Perl Artistic or BSD
9 #
10 #=============================================================================
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Fault::Delegate;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Fault::Msg;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Fault::ErrorHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 package Fault::Delegate::File;
17 1 ▶use vars qw{@ISA};
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 @ISA = qw( Fault::Delegate );
19  
20 #=============================================================================
21 # Family internal methods
22 #=============================================================================
23  
24 0 ▶sub _write ($$) {
Subroutine _write: not called CC=2 SCAR=17.9
25 0 my ($self,$msg) = @_;
26 0 my $str = $msg->stamped_log_line;
27 0 my $fh = $self->{'fd'};
28  
29 0 ▶ if (!print $fh ("$str\n")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (print $fh "$str\n") 0 0 0
30 0 $self->warn
31 ("Failed log write: \'$str\' to \'$self->{'filepath'}\': $!");
32 0 return 0;
33 }
34 0 return 1;
35 }
36  
37 #-----------------------------------------------------------------------------
38  
39 0 ▶sub _connect ($) {
Subroutine _connect: not called CC=5 SCAR=34.0
40 0 my $self = shift;
41 0 my $path = $self->{'filepath'};
42  
43 0 ▶ return 1 if (defined $self->{'fd'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"fd"} 0 0 0
44  
45 0 ▶ if (!open ($self->{'fd'}, ">>$path")) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $self->{"fd"}, ">>$path") 0 0 0
46 0 $self->warn ("Failed to open log file at \'$path\': $!");
47 0 return 0;
48 }
49 0 ▶ if (!$self->{'init'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"init"}) 0 0 0
50 0 my $msg = Fault::Msg->new
51 ("Initialized log file $self->{'filepath'}",'INFO','notice');
52 0 ▶ $self->{'init'} = 1 if ($self->_write ($msg));
Branch: 1 branch
BranchTrueFalseTotal
if $self->_write($msg) 0 0 0
53 }
54 0 return 1;
55 }
56  
57 #-----------------------------------------------------------------------------
58  
59 0 ▶sub _disconnect ($) {
Subroutine _disconnect: not called CC=2 SCAR=17.9
60 0 my ($self) = shift;
61 0 ▶ if ($self->{'fd'}) {close $self->{'fd'}; $self->{'fd'} = undef; }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fd"}) 0 0 0
62 0 return 1;
63 }
64  
65 #=============================================================================
66 # CLASS METHODS
67 #=============================================================================
68  
69 0 ▶sub new ($$) {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
70 0 my ($class,$filepath) = @_;
71 0 my $self = bless {}, $class;
72  
73 0 ▶ if (!defined $filepath or (ref $filepath) or !POSIX::isprint $filepath) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $filepath or ref $filepath or not $filepath->POSIX::isprint) 0 0 0
Condition0%0/6
not defined $filepath or ref $filepath or not $filepath->POSIX::isprint
l!l&&r!l&&!r
000
not defined $filepath or ref $filepath
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $filepath or ref $filepath or not $filepath->POSIX::isprint
not defined $filepath ref $filepath not $filepath->POSIX::isprint
Truth table0%0/4
not(A) or B or not(C)
A= not defined $filepathB= ref $filepathC= not $filepath->POSIX::isprint
ABCresult
1XX1
01X1
0011
0000
74 0 $self->warn
75 ("Fail: filepath string is invalid or undefined!");
76 0 return undef;
77 }
78 0 @$self{'filepath','init'} = ($filepath,0);
79  
80 0 ▶ return ($self->test) ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->test ? : 0 0 0
81 }
82  
83 #=============================================================================
84 # Logger Delegate Protocol
85 #=============================================================================
86 # Utilizes Fault::Delegate parent methods with subclass overrides seen above.
87
88 #=============================================================================
89 # Pod Documentation
90 #=============================================================================
91 # You may extract and format the documentation section with the 'perldoc' cmd.
92  
93 =head1 NAME
94  
95 Fault::Delegate::File - File logger delegate.
96  
97 =head1 SYNOPSIS
98  
99 use Fault::Delegate::File;
100 $self = Fault::Delegate::File->new ($filepath);
101 $okay = $self->log ($msg);
102  
103 =head1 Inheritance
104  
105 UNIVERSAL
106 Fault::Delegate
107 Fault::Delegate::File
108  
109 =head1 Description
110  
111 This is a delegate that writes log messages to a specified file. The file must
112 be writeable to the calling program.
113  
114 Fault::Delegate::File satisfies the minimum requirements of the
115 Fault::Delegate logger delegate protocol.
116  
117 =head1 Examples
118  
119 use Fault::Delegate::File;
120 use Fault::Logger;
121 use Fault::Msg;
122  
123 my $msg = Fault::Msg ("Arf!");
124 my $baz = Fault::Delegate::File->new ("/tmp/mylogfile");
125 my $waslogged = $baz->log ($msg);
126  
127 Fault::Logger->new ($baz);
128 my $waslogged = Fault::Logger->log ("Bow! Wow!");
129  
130 [See Fault::Logger for a detailed example.]
131  
132 =head1 Instance Variables
133  
134 filepath Full path to the log file.
135 init True if the log file has been successfully opened at
136 least once.
137 fd Transient storage for filehandle.
138  
139 =head1 Class Methods
140  
141 =over 4
142  
143 =item B<$delegate = Fault::Delegate::File-E<gt>new ($filepath)>
144  
145 Create a logger delegate object that writes log messages to the designated
146 file.
147  
148 A warning is issued if there is no $filepath argument and in that case
149 undef is returned to indicate that a delegate could not be created.
150  
151 If the initialization message cannot be written a warning is issued and
152 undef is returned.
153  
154 =back 4
155  
156 =head1 Logger Protocol Instance Methods
157  
158 =over 4
159  
160 =item B<$okay = $self-E<gt>log ($msg)>
161  
162 Prints a time-stamped message to the associated log file using information
163 taken from Fault::Msg object $msg:
164  
165 $date $time UTC> $processname: $type($priority): $msg\n
166  
167 for example:
168  
169 20021207 223010 UTC> MyProcess: NOTE(notice): Nothing happened again.\n
170  
171 and return true if we succeeded in doing so.
172  
173 =back 4
174  
175 =head1 Private Class Methods
176  
177 None.
178  
179 =head1 Private Instance Methods
180  
181 =over 4
182  
183 =item B<$bool = $self-E<gt>_write ($msg)>
184  
185 =item B<$bool = $self-E<gt>_connect>
186  
187 =item B<$bool = $self-E<gt>_disconnect>
188  
189 Impliments the above overrides to the internal family protocol utilized by
190 the Fault:Delegate log and test methods.
191  
192 =back 4
193  
194 =head1 Errors and Warnings
195  
196 Local warning messages are issued if the db file cannot be opened or has
197 any problems whatever.
198  
199 =head1 KNOWN BUGS
200  
201 See TODO.
202  
203 =head1 SEE ALSO
204  
205 Fault::Logger, Fault::Delegate, Fault::Msg,
206 Fault::ErrorHandler
207  
208 =head1 AUTHOR
209  
210 Dale Amon <amon@vnl.com>
211  
212 =cut
213
214 #=============================================================================
215 # CVS HISTORY
216 #=============================================================================
217 # $Log: File.pm,v $
218 # Revision 1.9 2008-08-28 23:20:19 amon
219 # perldoc section regularization.
220 #
221 # Revision 1.8 2008-08-17 21:56:37 amon
222 # Make all titles fit CPAN standard.
223 #
224 # Revision 1.7 2008-05-09 18:24:55 amon
225 # Bugs and changes due to pre-release testing
226 #
227 # Revision 1.6 2008-05-08 20:22:50 amon
228 # Minor bug fixes; shifted fault table and initfault from Logger to List
229 #
230 # Revision 1.5 2008-05-07 18:14:55 amon
231 # Simplification and standardization. Much more is inherited from Fault::Delegate.
232 #
233 # Revision 1.4 2008-05-05 19:25:49 amon
234 # Catch any small changes before implimenting major changes
235 #
236 # Revision 1.3 2008-05-04 14:44:18 amon
237 # Updates to perl doc; minor code changes.
238 #
239 # Revision 1.2 2008-05-03 00:56:57 amon
240 # Changed standard argument ordering.
241 #
242 # Revision 1.1.1.1 2008-05-02 16:37:39 amon
243 # Fault and Log System. Pared off of DMA base lib.
244 #
245 # Revision 1.6 2008-04-18 11:34:39 amon
246 # Wrote logger delegate abstract superclass to simplify the code in all the
247 # delegate classes.
248 #
249 # Revision 1.5 2008-04-11 22:25:23 amon
250 # Add blank line after cut.
251 #
252 # Revision 1.4 2008-04-11 18:56:35 amon
253 # Fixed quoting problem with formfeeds.
254 #
255 # Revision 1.3 2008-04-11 18:39:15 amon
256 # Implimented new standard for headers and trailers.
257 #
258 # Revision 1.2 2008-04-10 15:01:08 amon
259 # Added license to headers, removed claim that the documentation section still
260 # relates to the old doc file.
261 #
262 # 20041203 Dale Amon <amon@vnl.com>
263 # Modified old Document::LogFile code into
264 # a very simple delegate that prints to a file.
265 1 1;
« DebugPrinter.pm ↑ Summary Delegate/DB.pm »