blib/lib/PerlIO/via/Logger.pm

filter: Statementstmt 87.8% 29 / 33 Branchbran 66.6% 4 / 6 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 88.8% 8 / 9 Podpod 100% 4 / 4 totaltotal 81.8% 45 / 55 CC 7 SCAR 9.1
CC
7
Cov
80%
logify
6.0
WRITE
3.0
FILL
2.1
CRAP 2.5
↑ Summary
1 # Perl I/O Layer for Logging
2 #
3 # Author: Adam J Kaplan
4 # Email: akaplan@cpan.org
5 #
6 # This script is distributed under the same license as Perl itself.
7 #
8 package PerlIO::via::Logger;
9  
10 3 $VERSION = '1.01';
11 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # Set default format
16  
17 # Format for strftime(3)
18 3 my $timestr = '[%b %d, %Y %H:%M:%S] ';
19 # I prefer the string below, but it is not as portable as the one above
20 #my $timestr = '%b %d, %Y %r: ';
21  
22 # Satisfy require
23 3 1;
24  
25 #-----------------------------------------------------------------------
26 # Class methods
27 #-----------------------------------------------------------------------
28  
29 #-----------------------------------------------------------------------
30 # IN: 1 class (ignored)
31 # 2 new value for time format string
32 # OUT: 1 current default time format string
33  
34 2 ▶sub format {
Subroutine format: called CC=2 SCAR=6.9
Pod documented
35  
36 # Set new default format if one specified
37 # Return current default format
38  
39 2 ▶ $timestr = $_[1] if defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 1 1 2
40 2 return $timestr;
41 } #format
42  
43 #-----------------------------------------------------------------------
44 # Subroutines for standard Perl features
45 #-----------------------------------------------------------------------
46 # IN: 1 class to bless with
47 # 2 mode string (ignored)
48 # 3 file handle of PerlIO layer below (ignored)
49 # OUT: 1 blessed object
50  
51 2 ▶sub PUSHED {
Subroutine PUSHED: called CC=1 SCAR=0.0
Pod documented
52  
53 # Die now if strange mode
54 # Create the object with the right fields
55  
56 2 bless {timestr => $timestr },$_[0];
57 } #PUSHED
58  
59 #-----------------------------------------------------------------------
60 # IN: 1 instantiated object
61 # 2 handle to read from
62 # OUT: 1 processed string
63  
64 1 ▶sub FILL {
Subroutine FILL: called CC=2 SCAR=7.2
Pod documented
65  
66 # Obtain local copy of the formatting variables
67 # If there is a line to be read from the handle
68 # Append the generated string
69 # Return the result
70 # Return indicating end reached
71  
72 1 my ($timeformat) = @{$_[0]}{qw(timestr)};
73
74 1 my $now_string = strftime $timeformat, localtime;
75 1 ▶ if (defined( my $line = readline( $_[1] ) )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $_[1])) 1 0 1
76 1 return $now_string . $line;
77 }
78 0 undef;
79 } #FILL
80  
81 #-----------------------------------------------------------------------
82 # IN: 1 Valid file handle GLOB
83 # 2 Optional output. Default is to insert the io layer in place
84 # OUT: 1 undef
85  
86 0 ▶sub logify {
Subroutine logify: not called CC=2 SCAR=17.9
Pod documented
87 0 ▶ my $fh = shift||return;
Condition0%0/2
shift() || (return)
l!l
00
MC/DC0%0/1
shift() || (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
88 0 binmode($fh, ":via(Logger)");
89 0 undef;
90 }
91  
92  
93 #-----------------------------------------------------------------------
94 # IN: 1 instantiated object
95 # 2 buffer to be written
96 # 3 handle to write to
97 # OUT: 1 number of bytes written
98 1 ▶sub WRITE {
Subroutine WRITE: called CC=3 SCAR=11.0
99  
100 # Obtain local copies of format vars
101 # For all of the lines in this bunch (includes delimiter at end)
102 # Return with error if print failed
103 # Return total number of octets handled
104  
105 1 my ($timeformat) = @{$_[0]}{qw(timestr)};
106
107 1 my $now_string = strftime $timeformat, localtime;
108 1 foreach (split( m#(?<=$/)#,$_[1] )) {
109 2 ▶ return -1 unless print {$_[2]} $now_string . $_;
Branch: 1 branch
BranchTrueFalseTotal
unless print {$_[2];} $now_string . $_ 0 2 2
110 }
111 1 length( $_[1] );
112 } #WRITE
113  
114 #-----------------------------------------------------------------------
115 # IN: 1 class for which to import
116 # 2..N parameters passed with -use-
117  
118 2 ▶sub import {
Subroutine import: called CC=2 SCAR=6.9
119  
120 # Obtain the parameters
121 # Loop for all the value pairs specified
122  
123 2 my ($class,%param) = @_;
124 2 $class->$_( $param{$_} ) foreach keys %param;
125 } #import
126  
127 #-----------------------------------------------------------------------
128  
129 __END__
↑ Summary