File Coverage

blib/lib/PerlIO/via/Logger.pm
Criterion Covered Total %
statement 26 30 86.6
branch 4 6 66.6
condition 0 2 0.0
subroutine 8 9 88.8
pod 4 4 100.0
total 42 51 82.3


line stmt bran cond sub pod time code
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             $VERSION = '1.01';
11 2     2   98710 use strict;
  2         6  
  2         164  
12 2     2   12 use warnings;
  2         4  
  2         67  
13 2     2   1884 use POSIX qw(strftime);
  2         16999  
  2         16  
14              
15             # Set default format
16              
17             # Format for strftime(3)
18             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             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             sub format {
35              
36             # Set new default format if one specified
37             # Return current default format
38              
39 2 100   2 1 1184 $timestr = $_[1] if defined $_[1];
40 2         25 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             sub PUSHED {
52              
53             # Die now if strange mode
54             # Create the object with the right fields
55              
56 2     2 1 2985 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             sub FILL {
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     1 1 4 my ($timeformat) = @{$_[0]}{qw(timestr)};
  1         4  
73            
74 1         63 my $now_string = strftime $timeformat, localtime;
75 1 50       21 if (defined( my $line = readline( $_[1] ) )) {
76 1         12 return $now_string . $line;
77             }
78 0         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             sub logify {
87 0   0 0 1 0 my $fh = shift||return;
88 0         0 binmode($fh, ":via(Logger)");
89 0         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             sub WRITE {
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     1   5 my ($timeformat) = @{$_[0]}{qw(timestr)};
  1         12  
106            
107 1         259 my $now_string = strftime $timeformat, localtime;
108 1         33 foreach (split( m#(?<=$/)#,$_[1] )) {
109 2 50       4 return -1 unless print {$_[2]} $now_string . $_;
  2         22  
110             }
111 1         17 length( $_[1] );
112             } #WRITE
113              
114             #-----------------------------------------------------------------------
115             # IN: 1 class for which to import
116             # 2..N parameters passed with -use-
117              
118             sub import {
119              
120             # Obtain the parameters
121             # Loop for all the value pairs specified
122              
123 2     2   21 my ($class,%param) = @_;
124 2         169 $class->$_( $param{$_} ) foreach keys %param;
125             } #import
126              
127             #-----------------------------------------------------------------------
128              
129             __END__