| 1 | package Net::Peep::Log; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 23 | require 5.00503; | |||||||||||||||||||
| 4 | 23 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 23 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | # use warnings; # commented out for 5.005 compatibility | ||||||||||||||||||||
| 7 | 23 | ▶ | use Time::HiRes qw{ gettimeofday tv_interval }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 23 | require Exporter; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 23 | ▶ | use vars qw{ @ISA %EXPORT_TAGS @EXPORT_OK @EXPORT $VERSION $debug $logfile $__LOGFILE $__LOGHANDLE }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 23 | @ISA = qw(Exporter); | |||||||||||||||||||
| 14 | 23 | %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); | |||||||||||||||||||
| 15 | 23 | @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||||||||||||
| 16 | 23 | @EXPORT = qw( ); | |||||||||||||||||||
| 17 | 23 | $VERSION = do { my @r = (q$Revision: 1.6 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 23 | $debug = $__LOGFILE = 0; | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | 23 | $|++; | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 77 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 77 | my $self = shift; | |||||||||||||||||||
| 26 | 77 | ▶ | my $class = ref($self) || $self; | ||||||||||||||||||
|
Condition33%1/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table33%1/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 27 | 77 | my $this = {}; | |||||||||||||||||||
| 28 | 77 | bless $this, $class | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | } # end sub new | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 0 | ▶ | sub log { | ||||||||||||||||||
|
Subroutine
log: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0 | my $self = shift; | |||||||||||||||||||
| 35 | 0 | ▶ | if ($logfile) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 0 | ▶ | open(LOGFILE,">>$logfile") || confess "Cannot open $logfile: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 0 | print LOGFILE $self->__beautify(@_); | |||||||||||||||||||
| 38 | 0 | close(LOGFILE); | |||||||||||||||||||
| 39 | } else { | ||||||||||||||||||||
| 40 | 0 | print STDERR $self->__beautify(@_); | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | } # end sub log | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 441 | ▶ | sub debug { | ||||||||||||||||||
|
Subroutine
debug: called CC=4 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | 441 | my $self = shift; | |||||||||||||||||||
| 48 | 441 | my $level = shift; | |||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 441 | ▶ | if ($debug >= $level) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | 75 | ▶ | if ($logfile) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | 0 | ▶ | open(LOGFILE,">>$logfile") || confess "Cannot open $logfile: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | 0 | print LOGFILE $self->__beautify(@_); | |||||||||||||||||||
| 55 | 0 | close(LOGFILE); | |||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||
| 57 | 75 | print STDERR $self->__beautify(@_); | |||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 441 | return 1; | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | } # end sub debug | ||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 0 | ▶ | sub __logHandle { | ||||||||||||||||||
|
Subroutine
__logHandle: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | # this method has been deprecated | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 0 | my $self = shift; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 0 | ▶ | if (defined($__LOGHANDLE) ) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 73 | 0 | return $__LOGHANDLE; | |||||||||||||||||||
| 74 | } elsif (defined $logfile) { | ||||||||||||||||||||
| 75 | 0 | print STDERR ref($self),": Opening logfile $logfile ...\n"; | |||||||||||||||||||
| 76 | 0 | ▶ | open(LOGFILE,">>$logfile") || die "Cannot open $logfile: $!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 77 | 0 | $__LOGHANDLE = \*LOGFILE; | |||||||||||||||||||
| 78 | 0 | $__LOGFILE++; | |||||||||||||||||||
| 79 | 0 | return $__LOGHANDLE; | |||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||
| 81 | 0 | $__LOGHANDLE = \*STDOUT; | |||||||||||||||||||
| 82 | 0 | return $__LOGHANDLE; | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | } # end sub __logHandle | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | 75 | ▶ | sub __beautify { | ||||||||||||||||||
|
Subroutine
__beautify: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | 75 | my $self = shift; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 75 | my $message = join '', @_; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 75 | my @return; | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 75 | for my $line (split /\n/, $message) { | |||||||||||||||||||
| 96 | 75 | my $time = "[" . scalar(localtime) . "]"; | |||||||||||||||||||
| 97 | 75 | chomp($line); | |||||||||||||||||||
| 98 | 75 | $line = "$time $line"; | |||||||||||||||||||
| 99 | 75 | $line =~ s/\n/\n$time /sg; | |||||||||||||||||||
| 100 | 75 | ▶ | $line .= "\n" unless $line =~ /\n$/s; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | 75 | push @return, $line; | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 75 | return @return; | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | } # end sub __beautify | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | ▶ | sub mark { | ||||||||||||||||||
|
Subroutine
mark: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | # set a time against which future benchmarks can be measured | ||||||||||||||||||||
| 111 | 0 | my $self = shift; | |||||||||||||||||||
| 112 | 0 | ▶ | my $identifier = shift || confess "Cannot set mark: No identifier specified."; | ||||||||||||||||||
|
Condition0%0/2
shift() || confess('Cannot set mark: No identifier specified.')
MC/DC0%0/1
shift() || confess('Cannot set mark: No identifier specified.')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 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 | 0 | ▶ | sub benchmark { | ||||||||||||||||||
|
Subroutine
benchmark: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | # set a time against which future benchmarks can be measured | ||||||||||||||||||||
| 125 | 0 | my $self = shift; | |||||||||||||||||||
| 126 | 0 | ▶ | my $identifier = shift || confess "Cannot acquire benchmark: No identifier specified."; | ||||||||||||||||||
|
Condition0%0/2
shift() || confess('Cannot acquire benchmark: No identifier specified.')
MC/DC0%0/1
shift() || confess('Cannot acquire benchmark: No identifier specified.')
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | confess "Cannot acquire benchmark: No mark has been set with identifier '$identifier'." | ||||||||||||||||||||
| 129 | 0 | ▶ | unless exists $self->{"__MARK"}->{$identifier}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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}}."]"); | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 0 | return $interval; | |||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | } # end sub benchmark | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | # one should endeavor to always exit gracefully | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | 23 | ▶ | END { close LOGFILE if $__LOGFILE; } | ||||||||||||||||||
|
Subroutine
END: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 23 | 1; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||||