blib/lib/Net/Peep/Log.pm

filter: Statementstmt 59.4% 47 / 79 Branchbran 22.7% 5 / 22 Conditioncond 14.2% 1 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 66.6% 8 / 12 Podpod 0.0% 0 / 5 totaltotal 47.2% 61 / 129 CC 16 SCAR 20.3
CC
16
Cov
49%
__logHandle
20.0
log
12.0
benchmark
12.0
CRAP 7.6
« blib/lib/Net/Peep/Parser.pm ↑ Summary blib/lib/Net/Peep/Client/Pinger/Host.pm »
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
l!l&&r!l&&!r
0770
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($Net::Peep::Log::logfile) { } 0 0 0
36 0 ▶ open(LOGFILE,">>$logfile") || confess "Cannot open $logfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
open LOGFILE, ">>$Net::Peep::Log::logfile" or confess("Cannot open $Net::Peep::Log::logfile: $!") 0 0 0
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
BranchTrueFalseTotal
if ($Net::Peep::Log::debug >= $level) 75 366 441
51  
52 75 ▶ if ($logfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Peep::Log::logfile) { } 0 75 75
53 0 ▶ open(LOGFILE,">>$logfile") || confess "Cannot open $logfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
open LOGFILE, ">>$Net::Peep::Log::logfile" or confess("Cannot open $Net::Peep::Log::logfile: $!") 0 0 0
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
BranchTrueFalseTotal
if (defined $Net::Peep::Log::__LOGHANDLE) { } 0 0 0
elsif (defined $Net::Peep::Log::logfile) { } 0 0 0
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
BranchTrueFalseTotal
open LOGFILE, ">>$Net::Peep::Log::logfile" or die "Cannot open $Net::Peep::Log::logfile: $!" 0 0 0
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
BranchTrueFalseTotal
unless $line =~ /\n$/s 75 0 75
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.')
l!l
00
MC/DC0%0/1
shift() || confess('Cannot set mark: No identifier specified.')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
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.')
l!l
00
MC/DC0%0/1
shift() || confess('Cannot acquire benchmark: No identifier specified.')
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
127  
128 confess "Cannot acquire benchmark: No mark has been set with identifier '$identifier'."
129 0 ▶ unless exists $self->{"__MARK"}->{$identifier};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"__MARK"}{$identifier} 0 0 0
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
BranchTrueFalseTotal
if $Net::Peep::Log::__LOGFILE 0 23 23
142  
143 23 1;
144  
145 __END__
« blib/lib/Net/Peep/Parser.pm ↑ Summary blib/lib/Net/Peep/Client/Pinger/Host.pm »