| 1 | package Tie::Handle::Filter::Output::Timestamp::EveryLine; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | # ABSTRACT: prepend every line of filehandle output with a timestamp | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 2 | ▶ | use 5.008; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 2 | ▶ | use base 'Tie::Handle::Filter'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use English '-no_match_vars'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use POSIX 'strftime'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 2 | 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<tie|perlfunc/tie> function to | ||||||||||||||||||||
| 29 | #pod prepend all output with a timestamp, optionally formatted according to | ||||||||||||||||||||
| 30 | #pod the L<POSIX C<strftime>|POSIX/strftime> function. Unlike | ||||||||||||||||||||
| 31 | #pod L<Tie::Handle::Filter::Output::Timestamp|Tie::Handle::Filter::Output::Timestamp>, | ||||||||||||||||||||
| 32 | #pod I<every> line gets a timestamp, rather than just the beginning of | ||||||||||||||||||||
| 33 | #pod strings given to L<C<print>|perlfunc/print>, | ||||||||||||||||||||
| 34 | #pod L<C<printf>|perlfunc/printf>, L<C<syswrite>|perlfunc/syswrite>, and | ||||||||||||||||||||
| 35 | #pod L<C<say>|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<C<strftime>|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<PerlIO::via::Logger>, which offers a L<PerlIO|perlapio>-based logging | ||||||||||||||||||||
| 46 | #pod layer and may be slightly faster. | ||||||||||||||||||||
| 47 | #pod | ||||||||||||||||||||
| 48 | #pod =cut | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 2 | ▶ | my $NEWLINE = $PERL_VERSION lt 'v5.10' | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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<tie *glob, 'Tie::Handle::Filter::Output::Timestamp', *glob>. | ||||||||||||||||||||
| 58 | #pod You may also specify a L<C<strftime>|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 | 1 | ▶ | sub TIEHANDLE { | ||||||||||||||||||
|
Subroutine
TIEHANDLE: called CC=2 SCAR=7.2
| |||||||||||||||||||||
| 66 | 1 | my ( $class, $handle_glob, $format ) = @_; | |||||||||||||||||||
| 67 | 1 | ▶ | return $class->SUPER::TIEHANDLE( $handle_glob, | ||||||||||||||||||
|
Condition50%1/2
$format || '%x %X '
MC/DC0%0/1
$format || '%x %X '
$format
Truth table50%1/2
A
A= $format
| |||||||||||||||||||||
| 68 | _filter_closure( $format || '%x %X ' ) ); | ||||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 1 | ▶ | sub _filter_closure { | ||||||||||||||||||
|
Subroutine
_filter_closure: called CC=1 SCAR=0.1
| |||||||||||||||||||||
| 72 | 1 | my $format = shift; | |||||||||||||||||||
| 73 | 1 | my $string_beginning = 1; | |||||||||||||||||||
| 74 | 2 | ▶ | return sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||||||||||||
| 75 | 2 | my $string = join q() => @_; | |||||||||||||||||||
| 76 | 2 | $string | |||||||||||||||||||
| 77 | 3 | =~ s/ ($NEWLINE) (?=.) / $1 . strftime($format, localtime) /egmsx; | |||||||||||||||||||
| 78 | 2 | ▶ | if ($string_beginning) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 2 | $string =~ s/ \A / strftime($format, localtime) /emsx; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | 2 | ▶ | $string_beginning = $string =~ / $NEWLINE \z/msx | ||||||||||||||||||
|
Condition33%1/3
$string =~ / $NEWLINE \z/msx || $Tie::Handle::Filter::Output::Timestamp::EveryLine::OUTPUT_RECORD_SEPARATOR eq "\n"
MC/DC0%0/2
$string =~ / $NEWLINE \z/msx || $Tie::Handle::Filter::Output::Timestamp::EveryLine::OUTPUT_RECORD_SEPARATOR eq "\n"
$string =~ / $NEWLINE \z/msx
$Tie::Handle::Filter::Output::Timestamp::EveryLine::OUTPUT_RECORD_SEPARATOR eq "\n"
Truth table33%1/3
A || B
A= $string =~ / $NEWLINE \z/msxB= $Tie::Handle::Filter::Output::Timestamp::EveryLine::OUTPUT_RECORD_SEPARATOR eq "\n"
| |||||||||||||||||||||
| 82 | || $OUTPUT_RECORD_SEPARATOR eq "\n"; | ||||||||||||||||||||
| 83 | 2 | return $string; | |||||||||||||||||||
| 84 | 1 | }; | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 2 | 1; | |||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||