File Coverage

blib/lib/Tie/Handle/Filter/Output/Timestamp/EveryLine.pm
Criterion Covered Total %
statement 30 30 100.0
branch 1 2 50.0
condition 2 5 40.0
subroutine 9 9 100.0
pod n/a
total 42 46 91.3


line stmt bran cond sub pod time code
1             package Tie::Handle::Filter::Output::Timestamp::EveryLine;
2              
3             # ABSTRACT: prepend every line of filehandle output with a timestamp
4              
5 1     1   20793 use 5.008;
  1         3  
6 1     1   3 use strict;
  1         2  
  1         14  
7 1     1   2 use warnings;
  1         1  
  1         18  
8 1     1   3 use base 'Tie::Handle::Filter';
  1         0  
  1         376  
9 1     1   5 use English '-no_match_vars';
  1         0  
  1         4  
10 1     1   272 use POSIX 'strftime';
  1         1  
  1         4  
11             our $VERSION = '0.011';
12              
13             #pod =head1 SYNOPSIS
14             #pod
15             #pod use Tie::Handle::Filter::Output::Timestamp::EveryLine;
16             #pod tie *STDOUT, 'Tie::Handle::Filter::Output::Timestamp::EveryLine', *STDOUT;
17             #pod
18             #pod print "Everything I print will be prepended with a timestamp.\n";
19             #pod print <<'END_OUTPUT';
20             #pod Even multi-line output will have every line prepended.
21             #pod Including this one.
22             #pod END_OUTPUT
23             #pod
24             #pod =cut
25              
26             #pod =head1 DESCRIPTION
27             #pod
28             #pod This class may be used with Perl's L function to
29             #pod prepend all output with a timestamp, optionally formatted according to
30             #pod the L|POSIX/strftime> function. Unlike
31             #pod L,
32             #pod I line gets a timestamp, rather than just the beginning of
33             #pod strings given to L|perlfunc/print>,
34             #pod L|perlfunc/printf>, L|perlfunc/syswrite>, and
35             #pod L|perlfunc/say> (in Perl > v5.10).
36             #pod
37             #pod =head1 BUGS AND LIMITATIONS
38             #pod
39             #pod Because the date and time format is specified using
40             #pod L|POSIX/strftime>, portable code should restrict itself to
41             #pod formats using ANSI C89 specifiers.
42             #pod
43             #pod =head1 SEE ALSO
44             #pod
45             #pod L, which offers a L-based logging
46             #pod layer and may be slightly faster.
47             #pod
48             #pod =cut
49              
50             my $NEWLINE = $PERL_VERSION lt 'v5.10'
51             ? '(?>\x0D\x0A|\n)' ## no critic (RequireInterpolationOfMetachars)
52             : '\R';
53              
54             #pod =method TIEHANDLE
55             #pod
56             #pod Invoked by the command
57             #pod C.
58             #pod You may also specify a L|POSIX/strftime> string as an
59             #pod additional parameter to format the timestamp; by default the format is
60             #pod C<%x %X >, which is the local representation of the date and time
61             #pod followed by a space.
62             #pod
63             #pod =cut
64              
65             sub TIEHANDLE {
66 1     1   188 my ( $class, $handle_glob, $format ) = @_;
67 1   50     9 return $class->SUPER::TIEHANDLE( $handle_glob,
68             _filter_closure( $format || '%x %X ' ) );
69             }
70              
71             sub _filter_closure {
72 1     1   2 my $format = shift;
73 1         1 my $string_beginning = 1;
74             return sub {
75 2     2   6 my $string = join q() => @_;
76 2         23 $string
77 3         126 =~ s/ ($NEWLINE) (?=.) / $1 . strftime($format, localtime) /egmsx;
78 2 50       58 if ($string_beginning) {
79 2         4 $string =~ s/ \A / strftime($format, localtime) /emsx;
  2         5  
80             }
81 2   33     62 $string_beginning = $string =~ / $NEWLINE \z/msx
82             || $OUTPUT_RECORD_SEPARATOR eq "\n";
83 2         18 return $string;
84 1         14 };
85             }
86              
87             1;
88              
89             __END__