| 1 | package Tie::LogFile; | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # $Id: LogFile.pm,v 1.1.1.1 2002/10/16 10:08:08 ctriv Exp $ | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Symbol (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 5 | our $VERSION = 0.1; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | our %defaults = ( | |||||||||||||||||||||||||||
| 14 | 'format' => '[%d] %m', | ||||||||||||||||||||||||||||
| 15 | 'tformat' => '', | ||||||||||||||||||||||||||||
| 16 | 'mode' => '>>', | ||||||||||||||||||||||||||||
| 17 | 'force_newline' => 1, | ||||||||||||||||||||||||||||
| 18 | 'autoflush' => 0, | ||||||||||||||||||||||||||||
| 19 | ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | our %formats = ( | ||||||||||||||||||||||||||||
| 22 | 10 | ▶ | 'd' => sub { $_[0]->fmtime }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 22 | ▶ | 'm' => sub { $_[1] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | ▶ | 'p' => sub { $$ }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 10 | ▶ | 'c' => sub { $_[0]->count }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | '%' => sub { '%' }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 5 | ); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 10 | ▶ | sub TIEHANDLE { | ||||||||||||||||||||||||||
|
Subroutine
TIEHANDLE: called CC=9 SCAR=25.3
| |||||||||||||||||||||||||||||
| 31 | 10 | my $class = shift; | |||||||||||||||||||||||||||
| 32 | 10 | ▶ | my $filename = shift || return; | ||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 10 | ▶ | if (@_ % 2 != 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | carp "$class must be passed an even number of arguments."; | |||||||||||||||||||||||||||
| 36 | 0 | return; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 10 | my %options = @_; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 10 | ▶ | if ($options{'format'} && $options{'format'} !~ m/%m/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$options{"format"} and not $options{"format"} =~ /%m/
MC/DC0%0/2
$options{"format"} and not $options{"format"} =~ /%m/
$options{"format"}
not $options{"format"} =~ /%m/
Truth table66%2/3
A and not(B)
A= $options{"format"}B= not $options{"format"} =~ /%m/
| |||||||||||||||||||||||||||||
| 42 | 0 | carp "Syntax error in 'format' option, must contain a message tag (%m)\n"; | |||||||||||||||||||||||||||
| 43 | 0 | return; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 10 | ▶ | if ($options{'mode'} && $options{'mode'} !~ m/^>>?$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$options{"mode"} and not $options{"mode"} =~ /^>>?$/
MC/DC0%0/2
$options{"mode"} and not $options{"mode"} =~ /^>>?$/
$options{"mode"}
not $options{"mode"} =~ /^>>?$/
Truth table66%2/3
A and not(B)
A= $options{"mode"}B= not $options{"mode"} =~ /^>>?$/
| |||||||||||||||||||||||||||||
| 47 | # We don't support no stinking reading, at least for now | ||||||||||||||||||||||||||||
| 48 | 0 | carp __PACKAGE__ . " only supports writing file modes.\n"; | |||||||||||||||||||||||||||
| 49 | 0 | return; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 10 | my $self = Symbol::gensym(); #The lazy way... :) | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 10 | bless($self, $class); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 10 | $$self = { | |||||||||||||||||||||||||||
| 57 | %defaults, | ||||||||||||||||||||||||||||
| 58 | %options, | ||||||||||||||||||||||||||||
| 59 | count => 0, | ||||||||||||||||||||||||||||
| 60 | }; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 10 | ▶ | open($self, $$self->{'mode'} , $filename) || return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 10 | ▶ | if ($$self->{'autoflush'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 1 | $self->autoflush($$self->{'autoflush'}); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 10 | return $self; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 21 | ▶ | sub PRINT { | ||||||||||||||||||||||||||
|
Subroutine
PRINT: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 72 | 21 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | 21 | $self->_print(@_); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 1 | ▶ | sub PRINTF { | ||||||||||||||||||||||||||
|
Subroutine
PRINTF: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 78 | 1 | my $fmt = shift; | |||||||||||||||||||||||||||
| 79 | 1 | my $line = sprintf($fmt, @_); | |||||||||||||||||||||||||||
| 80 | 1 | $self->_print($line); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 10 | ▶ | sub CLOSE { | ||||||||||||||||||||||||||
|
Subroutine
CLOSE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 85 | 10 | close($self); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub UNTIE { | ||||||||||||||||||||||||||
|
Subroutine
UNTIE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 89 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 90 | 0 | close($self); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 22 | ▶ | sub _print { | ||||||||||||||||||||||||||
|
Subroutine
_print: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||
| 94 | 22 | my $self = shift; | |||||||||||||||||||||||||||
| 95 | 22 | my $msg = "@_"; | |||||||||||||||||||||||||||
| 96 | 22 | my $line = $$self->{'format'}; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 22 | $$self->{'count'}++; | |||||||||||||||||||||||||||
| 99 | # This isn't the fastest way to do things, but it's flexible and easy. | ||||||||||||||||||||||||||||
| 100 | # If people start ccontacting me and asking for more speed, I have a few | ||||||||||||||||||||||||||||
| 101 | # ideas, but I don't see that happening. | ||||||||||||||||||||||||||||
| 102 | 22 | ▶ | $line =~ s/%(_?[%a-zA-Z])/$formats{$1} ? $formats{$1}->($self, $msg) : ''/eg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 22 | ▶ | if ($$self->{'force_newline'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 20 | ▶ | $line .= "\n" if $line !~ m/\n$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 22 | print $self $line; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | sub autoflush { | ||||||||||||||||||||||||||
|
Subroutine
autoflush: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 1 | my ($self, $af) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | ▶ | if (defined $af) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | # set self to autoflush. | ||||||||||||||||||||||||||||
| 116 | 1 | select((select($self), $| = $af)[0]); | |||||||||||||||||||||||||||
| 117 | 1 | $$self->{'autoflush'} = $af; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | return $$self->{'autoflush'}; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 10 | ▶ | sub fmtime { | ||||||||||||||||||||||||||
|
Subroutine
fmtime: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 10 | ▶ | if ($$self->{'tformat'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 1 | return Tie::LogFile::misc::time2str($$self->{'tformat'}); | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 9 | return scalar localtime; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 10 | ▶ | sub count { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 136 | 10 | return $$self->{'count'}; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub format { | ||||||||||||||||||||||||||
|
Subroutine
format: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | my $new_fmt = shift; | |||||||||||||||||||||||||||
| 143 | 0 | $$self->{'format'} = $new_fmt; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 0 | return $$self->{'format'}; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | package Tie::LogFile::misc; | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 151 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 152 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 5 | our $loaded = 0; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 3 | ▶ | sub time2str { | ||||||||||||||||||||||||||
|
Subroutine
time2str: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 157 | 3 | my $fmt = shift; | |||||||||||||||||||||||||||
| 158 | 3 | ▶ | if (!$loaded) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 2 | ▶ | load_date_mod() || croak "Couldn't load a date module (Tried Data::Format and POSIX)\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 3 | return _time_formater($fmt, time); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 2 | ▶ | sub load_date_mod { | ||||||||||||||||||||||||||
|
Subroutine
load_date_mod: called CC=3 SCAR=16.8
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 2 | ▶ | if (eval "require Date::Format") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 2 | $loaded = 1; | |||||||||||||||||||||||||||
| 169 | 2 | *_time_formater = \&Date::Format::time2str; | |||||||||||||||||||||||||||
| 170 | 2 | return 1; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if (eval "require POSIX") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | $loaded = 1; | |||||||||||||||||||||||||||
| 175 | 0 | ▶ | *_time_formater = sub { POSIX::strftime($_[0], localtime($_[1])); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 176 | 0 | return 1; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 0 | return; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 5 | 1; | |||||||||||||||||||||||||||
| 185 | __END__ | ||||||||||||||||||||||||||||