line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::Peep::Log; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
require 5.00503; |
4
|
3
|
|
|
3
|
|
2109
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
105
|
|
5
|
3
|
|
|
3
|
|
13
|
use Carp; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
246
|
|
6
|
|
|
|
|
|
|
# use warnings; # commented out for 5.005 compatibility |
7
|
3
|
|
|
3
|
|
2516
|
use Time::HiRes qw{ gettimeofday tv_interval }; |
|
3
|
|
|
|
|
5596
|
|
|
3
|
|
|
|
|
16
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
require Exporter; |
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
775
|
use vars qw{ @ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION $debug $logfile $__LOGFILE $__LOGHANDLE }; |
|
3
|
|
|
|
|
3
|
|
|
3
|
|
|
|
|
7767
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
14
|
|
|
|
|
|
|
%EXPORT_TAGS = ( 'all' => [ qw( ) ] ); |
15
|
|
|
|
|
|
|
@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
16
|
|
|
|
|
|
|
@EXPORT = qw( ); |
17
|
|
|
|
|
|
|
$VERSION = do { my @r = (q$Revision: 1.6 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$debug = $__LOGFILE = 0; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
$|++; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub new { |
24
|
|
|
|
|
|
|
|
25
|
31
|
|
|
31
|
0
|
71
|
my $self = shift; |
26
|
31
|
|
33
|
|
|
164
|
my $class = ref($self) || $self; |
27
|
31
|
|
|
|
|
97
|
my $this = {}; |
28
|
31
|
|
|
|
|
151
|
bless $this, $class |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
} # end sub new |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub log { |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
0
|
0
|
0
|
my $self = shift; |
35
|
0
|
0
|
|
|
|
0
|
if ($logfile) { |
36
|
0
|
0
|
|
|
|
0
|
open(LOGFILE,">>$logfile") || confess "Cannot open $logfile: $!"; |
37
|
0
|
|
|
|
|
0
|
print LOGFILE $self->__beautify(@_); |
38
|
0
|
|
|
|
|
0
|
close(LOGFILE); |
39
|
|
|
|
|
|
|
} else { |
40
|
0
|
|
|
|
|
0
|
print STDERR $self->__beautify(@_); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
} # end sub log |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub debug { |
46
|
|
|
|
|
|
|
|
47
|
435
|
|
|
435
|
0
|
892
|
my $self = shift; |
48
|
435
|
|
|
|
|
580
|
my $level = shift; |
49
|
|
|
|
|
|
|
|
50
|
435
|
100
|
|
|
|
887
|
if ($debug >= $level) { |
51
|
|
|
|
|
|
|
|
52
|
75
|
50
|
|
|
|
138
|
if ($logfile) { |
53
|
0
|
0
|
|
|
|
0
|
open(LOGFILE,">>$logfile") || confess "Cannot open $logfile: $!"; |
54
|
0
|
|
|
|
|
0
|
print LOGFILE $self->__beautify(@_); |
55
|
0
|
|
|
|
|
0
|
close(LOGFILE); |
56
|
|
|
|
|
|
|
} else { |
57
|
75
|
|
|
|
|
164
|
print STDERR $self->__beautify(@_); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
435
|
|
|
|
|
1360
|
return 1; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
} # end sub debug |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub __logHandle { |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# this method has been deprecated |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
71
|
|
|
|
|
|
|
|
72
|
0
|
0
|
|
|
|
0
|
if (defined($__LOGHANDLE) ) { |
|
|
0
|
|
|
|
|
|
73
|
0
|
|
|
|
|
0
|
return $__LOGHANDLE; |
74
|
|
|
|
|
|
|
} elsif (defined $logfile) { |
75
|
0
|
|
|
|
|
0
|
print STDERR ref($self),": Opening logfile $logfile ...\n"; |
76
|
0
|
0
|
|
|
|
0
|
open(LOGFILE,">>$logfile") || die "Cannot open $logfile: $!"; |
77
|
0
|
|
|
|
|
0
|
$__LOGHANDLE = \*LOGFILE; |
78
|
0
|
|
|
|
|
0
|
$__LOGFILE++; |
79
|
0
|
|
|
|
|
0
|
return $__LOGHANDLE; |
80
|
|
|
|
|
|
|
} else { |
81
|
0
|
|
|
|
|
0
|
$__LOGHANDLE = \*STDOUT; |
82
|
0
|
|
|
|
|
0
|
return $__LOGHANDLE; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
} # end sub __logHandle |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub __beautify { |
88
|
|
|
|
|
|
|
|
89
|
75
|
|
|
75
|
|
98
|
my $self = shift; |
90
|
|
|
|
|
|
|
|
91
|
75
|
|
|
|
|
132
|
my $message = join '', @_; |
92
|
|
|
|
|
|
|
|
93
|
75
|
|
|
|
|
146
|
my @return; |
94
|
|
|
|
|
|
|
|
95
|
75
|
|
|
|
|
206
|
for my $line (split /\n/, $message) { |
96
|
75
|
|
|
|
|
1899
|
my $time = "[" . scalar(localtime) . "]"; |
97
|
75
|
|
|
|
|
136
|
chomp($line); |
98
|
75
|
|
|
|
|
174
|
$line = "$time $line"; |
99
|
75
|
|
|
|
|
121
|
$line =~ s/\n/\n$time /sg; |
100
|
75
|
50
|
|
|
|
181
|
$line .= "\n" unless $line =~ /\n$/s; |
101
|
75
|
|
|
|
|
203
|
push @return, $line; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
75
|
|
|
|
|
4761
|
return @return; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
} # end sub __beautify |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
sub mark { |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# set a time against which future benchmarks can be measured |
111
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
112
|
0
|
|
0
|
|
|
|
my $identifier = shift || confess "Cannot set mark: No identifier specified."; |
113
|
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
my $timeofday = gettimeofday; |
115
|
0
|
|
|
|
|
|
$self->{"__MARK"}->{$identifier} = [$timeofday]; |
116
|
0
|
|
|
|
|
|
$self->debug(9,"Mark [$identifier:$timeofday] set."); |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
return 1; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
} # end sub mark |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
sub benchmark { |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
# set a time against which future benchmarks can be measured |
125
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
126
|
0
|
|
0
|
|
|
|
my $identifier = shift || confess "Cannot acquire benchmark: No identifier specified."; |
127
|
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
confess "Cannot acquire benchmark: No mark has been set with identifier '$identifier'." |
129
|
|
|
|
|
|
|
unless exists $self->{"__MARK"}->{$identifier}; |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
|
my $interval = tv_interval($self->{"__MARK"}->{$identifier},[gettimeofday]); |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
|
$self->debug(9,"$interval seconds have passed since mark [$identifier:".@{$self->{"__MARK"}->{$identifier}}."]"); |
|
0
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
return $interval; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
} # end sub benchmark |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# one should endeavor to always exit gracefully |
140
|
|
|
|
|
|
|
|
141
|
3
|
50
|
|
3
|
|
2474
|
END { close LOGFILE if $__LOGFILE; } |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
1; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
__END__ |