Framework/Feature/Logging.pm

filter: Statementstmt 86.3% 63 / 73 Branchbran 66.6% 16 / 24 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 100% 5 / 5 totaltotal 80.5% 95 / 118 CC 16 SCAR 18.2
CC
16
Cov
80%
logging
11.5
_start_logging
4.3
new
2.1
CRAP 6.2
« Framework/Extension/Filter.pm ↑ Summary Framework/Base/Object.pm »
1 package App::Framework::Feature::Logging ;
2  
3 =head1 NAME
4  
5 App::Framework::Feature::Logging - Application logging
6  
7 =head1 SYNOPSIS
8  
9 # Include logging feature by:
10 use App::Framework '+Logging' ;
11  
12  
13 =head1 DESCRIPTION
14  
15 Logging feature that provides log file handling for applications.
16  
17 If the user specified -log command line option and specifies a valid log filename, then this module will
18 manage any logging() calls, writing the data into the specified log file.
19  
20 =cut
21  
22 3 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 3 our $VERSION = "1.001" ;
26  
27  
28 #============================================================================================
29 # USES
30 #============================================================================================
31  
32 3 ▶use App::Framework::Feature ;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use App::Framework::Base ;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 3 ▶use App::Framework::Base::Object::DumpObj qw/prtstr_data/ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 #============================================================================================
38 # OBJECT HIERARCHY
39 #============================================================================================
40 3 our @ISA = qw(App::Framework::Feature) ;
41  
42 #============================================================================================
43 # GLOBALS
44 #============================================================================================
45  
46 =head2 FIELDS
47  
48 The following fields should be defined either in the call to 'new()', as part of a 'set()' call, or called by their accessor method
49 (which is the same name as the field):
50  
51  
52 =over 4
53  
54 =item B<logfile> - Name of log file
55  
56 Created by the object. When the application starts, if the -log option has been specified, then the filename is copied into this
57 field.
58  
59 =item B<mode> - Log file create mode
60  
61 May be 'truncate' or 'append': 'truncate' clears any previous log file contents; 'append' appends the logging to previous
62 file contents. Default is 'truncate'
63  
64 =item B<to_stdout> - flag to echo logging
65  
66 When set, causes all logging to be echoed to STDOUT
67  
68 =back
69  
70 =cut
71  
72  
73 3 my %FIELDS = (
74 'logfile' => undef,
75 'mode' => 'truncate',
76 'to_stdout' => 0,
77
78 ## private
79 '_started' => 0,
80 ) ;
81  
82  
83 =head2 ADDITIONAL COMMAND LINE OPTIONS
84  
85 This feature adds the following additional command line options to any application:
86  
87 =over 4
88  
89 =item B<-log> - Specify a log file
90  
91 If a logfile is specified at the command line, then the file is created and all logging messages get written to that file.
92 Otherwise, log messages are ignored.
93  
94 =back
95  
96 =cut
97  
98  
99 3 my @OPTIONS = (
100 ['log=s', 'Log file', 'Specify a log file', ],
101 ) ;
102  
103  
104 #============================================================================================
105  
106 =head2 CONSTRUCTOR
107  
108 =over 4
109  
110 =cut
111  
112 #============================================================================================
113  
114  
115 =item B< new([%args]) >
116  
117 Create a new Logging.
118  
119 The %args are specified as they would be in the B<set> method (see L</Fields>).
120  
121 =cut
122  
123 sub new
124 2 ▶{
Subroutine new: called CC=2 SCAR=7.4
Pod documented
125 2 my ($obj, %args) = @_ ;
126  
127 2 ▶ my $class = ref($obj) || $obj ;
Condition33%1/3
ref $obj || $obj
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $obj || $obj
ref $obj $obj
Truth table33%1/3
A || B
A= ref $objB= $obj
ABresult
1X1
011
000
128  
129 # Create object
130 2 my $this = $class->SUPER::new(%args,
131 'feature_options' => \@OPTIONS,
132 'registered' => [qw/application_entry/],
133 ) ;
134  
135 #$this->debug(2);
136  
137
138 2 return($this) ;
139 }
140  
141  
142  
143 #============================================================================================
144  
145 =back
146  
147 =head2 CLASS METHODS
148  
149 =over 4
150  
151 =cut
152  
153 #============================================================================================
154  
155  
156 #-----------------------------------------------------------------------------
157  
158 =item B< init_class([%args]) >
159  
160 Initialises the Logging object class variables.
161  
162 =cut
163  
164 sub init_class
165 2 ▶{
Subroutine init_class: called CC=1 SCAR=0.0
Pod documented
166 2 my $class = shift ;
167 2 my (%args) = @_ ;
168  
169 # Add extra fields
170 2 $class->add_fields(\%FIELDS, \%args) ;
171  
172 # init class
173 2 $class->SUPER::init_class(%args) ;
174  
175 }
176  
177 #============================================================================================
178  
179 =back
180  
181 =head2 OBJECT METHODS
182  
183 =over 4
184  
185 =cut
186  
187 #============================================================================================
188  
189 #----------------------------------------------------------------------------
190  
191 =item B<application_entry()>
192  
193 Called by the application framework at the start of the application.
194
195 This method checks for the user specifying any of the options described above (see L</ADDITIONAL COMMAND LINE OPTIONS>) and handles
196 them if so.
197  
198 =cut
199  
200  
201 sub application_entry
202 2 ▶{
Subroutine application_entry: called CC=2 SCAR=7.0
Pod documented
203 2 my $this = shift ;
204  
205 2 $this->_dbg_prt(["application_entry()\n"]) ;
206  
207 ## Handle special options
208 2 my $app = $this->app ;
209 2 my %opts = $app->options() ;
210  
211 2 $this->_dbg_prt(["logging options=",\%opts]) ;
212  
213 2 ▶ if ($opts{'log'})
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"log"}) 2 0 2
214 {
215 2 $this->logfile($opts{'log'}) ;
216 }
217
218 }
219  
220  
221 #----------------------------------------------------------------------------
222  
223 =item B<logging($arg1, [$arg2, ....])>
224  
225 Log the argument(s) to the log file iff a log file has been specified.
226  
227 The list of arguments may be: SCALAR, ARRAY reference, HASH reference, SCALAR reference. SCALAR and SCALAR ref are printed
228 as-is without any extra newlines. ARRAY ref is printed out one entry per line with a newline added. The HASH ref is printed out
229 in the format produced by L<App::Framework::Base::Object::DumpObj>.
230  
231  
232 =cut
233  
234 sub logging
235 15 ▶{
Subroutine logging: called CC=11 SCAR=24.4
Pod documented
236 15 my $this = shift ;
237 15 my (@args) = @_ ;
238  
239 15 my $tolog = "" ;
240 15 foreach my $arg (@args)
241 {
242 20 ▶ if (ref($arg) eq 'ARRAY')
Branch: 4 branches
BranchTrueFalseTotal
if (ref $arg eq 'ARRAY') { } 4 16 20
elsif (ref $arg eq 'HASH') { } 1 15 16
elsif (ref $arg eq 'SCALAR') { } 1 14 15
elsif (not ref $arg) { } 14 0 14
243 {
244 4 foreach (@$arg)
245 {
246 8 $tolog .= "$_\n" ;
247 }
248 }
249 elsif (ref($arg) eq 'HASH')
250 {
251 1 $tolog .= prtstr_data($arg) . "\n" ;
252 }
253 elsif (ref($arg) eq 'SCALAR')
254 {
255 1 $tolog .= $$arg ;
256 }
257 elsif (!ref($arg))
258 {
259 14 $tolog .= $arg ;
260 }
261 else
262 {
263 0 $tolog .= prtstr_data($arg) . "\n" ;
264 }
265 }
266
267 ## Log
268 15 my $logfile = $this->logfile ;
269 15 ▶ if ($logfile)
Branch: 1 branch
BranchTrueFalseTotal
if ($logfile) 15 0 15
270 {
271 ## start if we haven't yet
272 15 ▶ if (!$this->_started)
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->_started) 2 13 15
273 {
274 2 $this->_start_logging() ;
275 }
276  
277 15 ▶ open my $fh, ">>$logfile" or $this->throw_fatal("Error: unable to append to logfile \"$logfile\" : $!") ;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">>$logfile" or $this->throw_fatal(qq[Error: unable to append to logfile "$logfile" : $!]) 15 0 15
278 15 print $fh $tolog ;
279 15 close $fh ;
280 }
281  
282 ## Echo
283 15 ▶ if ($this->to_stdout)
Branch: 1 branch
BranchTrueFalseTotal
if ($this->to_stdout) 0 15 15
284 {
285 0 print $tolog ;
286 }
287  
288 15 return($this) ;
289 }
290
291 #----------------------------------------------------------------------------
292  
293 =item B<echo_logging($arg1, [$arg2, ....])>
294  
295 Same as L</logging> but echoes output to STDOUT.
296  
297 =cut
298  
299 sub echo_logging
300 0 ▶{
Subroutine echo_logging: not called CC=1 SCAR=6.9
Pod documented
301 0 my $this = shift ;
302 0 my (@args) = @_ ;
303
304 # Temporarily force echoing to STDOUT on, then do logging
305 0 my $to_stdout = $this->to_stdout ;
306 0 $this->to_stdout(1) ;
307 0 $this->logging(@args) ;
308 0 $this->to_stdout($to_stdout) ;
309  
310 0 return($this) ;
311 }
312
313 #----------------------------------------------------------------------------
314  
315 =item B< Logging([%args]) >
316  
317 Alias to L</logging>
318  
319 =cut
320  
321 3 *Logging = \&logging ;
322  
323  
324  
325 # ============================================================================================
326 # PRIVATE METHODS
327 # ============================================================================================
328  
329 #----------------------------------------------------------------------------
330 #
331 #=item B<_start_logging()>
332 #
333 #Create/append log file
334 #
335 #=cut
336 #
337 sub _start_logging
338 2 ▶{
Subroutine _start_logging: called CC=4 SCAR=14.6
339 2 my $this = shift ;
340  
341 2 my $logfile = $this->logfile() ;
342 2 ▶ if ($logfile)
Branch: 1 branch
BranchTrueFalseTotal
if ($logfile) 2 0 2
343 {
344 2 my $mode = ">" ;
345 2 ▶ if ($this->mode eq 'append')
Branch: 1 branch
BranchTrueFalseTotal
if ($this->mode eq "append") 0 2 2
346 {
347 0 $mode = ">>" ;
348 }
349
350 2 ▶ open my $fh, "$mode$logfile" or $this->throw_fatal("Unable to write to logfile \"$logfile\" : $!") ;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "$mode$logfile" or $this->throw_fatal(qq[Unable to write to logfile "$logfile" : $!]) 2 0 2
351 2 close $fh ;
352
353 ## set flag
354 2 $this->_started(1) ;
355 }
356 }
357
358  
359  
360 # ============================================================================================
361 # END OF PACKAGE
362  
363 =back
364  
365 =head1 DIAGNOSTICS
366  
367 Setting the debug flag to level 1 prints out (to STDOUT) some debug messages, setting it to level 2 prints out more verbose messages.
368  
369 =head1 AUTHOR
370  
371 Steve Price C<< <sdprice at cpan.org> >>
372  
373 =head1 BUGS
374  
375 None that I know of!
376  
377 =cut
378  
379  
380 3 1;
381  
382 __END__
« Framework/Extension/Filter.pm ↑ Summary Framework/Base/Object.pm »