blib/lib/Log/Delimited.pm

filter: Statementstmt 85.2% 110 / 129 Branchbran 46.0% 35 / 76 Conditioncond 36.1% 13 / 36 MC/DCmcdc 4.3% 1 / 23 Subroutinesub 83.3% 15 / 18 Podpod 0.0% 0 / 12 totaltotal 59.1% 174 / 294 CC 55 SCAR 22.8
CC
55
Cov
66%
zip
20.0
unzip
20.0
handle_log_cols
15.0
CRAP 9.8
↑ Summary
1 #!/usr/bin/perl -w
2  
3 package Log::Delimited;
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use vars qw(@EXPORT @EXPORT_OK @ISA $LOG_BASE_DIR $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 @ISA = ('Exporter');
10 2 @EXPORT = qw(log);
11 2 @EXPORT_OK = qw($LOG_BASE_DIR);
12  
13 2 ▶use File::Path qw(mkpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Storable qw(freeze);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Sys::Hostname qw(hostname);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 $LOG_BASE_DIR = "/tmp/logs";
18 2 $VERSION = '0.90';
19  
20 sub O_RDWR () { 2 }
21 sub O_CREAT () { 64 }
22  
23  
24 1 ▶sub new {
Subroutine new: called CC=6 SCAR=19.7
Pod undocumented
25 1 my $type = shift;
26 1 ▶ my @PASSED_ARGS = (ref $_[0] eq 'HASH') ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 1 0 1
27 1 my @DEFAULT_ARGS = (
28 base_dir => $LOG_BASE_DIR,
29  
30 delimiter => '|',
31 log_cols => '',
32  
33 # log_info is an array ref of the info you would like to log
34 log_info => '',
35 log_name => '',
36 log_node => 'main',
37  
38 no_hostname => 0,
39 no_pid => 0,
40 no_time => 0,
41 );
42 1 my %ARGS = (@DEFAULT_ARGS, @PASSED_ARGS);
43 1 foreach(qw(log_node log_name)) {
44 2 ▶ unless($ARGS{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ARGS{$_}) 0 2 2
45 0 ▶ ($ARGS{$_}) = ($0 =~ m@.*/(.+)@) || $0;
Condition0%0/3
$0 =~ m[.*/(.+)] || $0
l!l&&r!l&&!r
000
MC/DC0%0/2
$0 =~ m[.*/(.+)] || $0
$0 =~ m[.*/(.+)] $0
Truth table0%0/3
A || B
A= $0 =~ m[.*/(.+)]B= $0
ABresult
1X1
011
000
46 }
47 }
48 1 my $self = bless \%ARGS, $type;
49 1 ▶ die "need a \$self->{log_name}" unless($self->{log_name});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"log_name"} 0 1 1
50 1 $self->handle_dirs;
51 1 $self->mkpath_dirs;
52 1 return $self;
53 }
54  
55 12 ▶sub URLEncode {
Subroutine URLEncode: called CC=3 SCAR=11.2
Pod undocumented
56 12 my $arg = shift;
57 12 ▶ my ($ref,$return) = ref($arg) ? ($arg,0) : (\$arg,1) ;
Branch: 1 branch
BranchTrueFalseTotal
ref $arg ? : 12 0 12
58  
59 12 $$ref =~ s/([^\w\.\ -])/sprintf("%%%02X",ord($1))/eg;
60 12 $$ref =~ tr/\ /+/;
61  
62 12 ▶ return $return ? $$ref : '';
Branch: 1 branch
BranchTrueFalseTotal
$return ? : 0 12 12
63 }
64  
65 1 ▶sub handle_dirs {
Subroutine handle_dirs: called CC=7 SCAR=21.2
Pod undocumented
66 1 my $self = shift;
67 1 ▶ return if($self->{handled_dirs});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handled_dirs"} 0 1 1
68 1 ▶ die "need a \$self->{base_dir}" unless($self->{base_dir});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"base_dir"} 0 1 1
69 1 ▶ die "need a \$self->{log_name}" unless($self->{log_name});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"log_name"} 0 1 1
70 1 ▶ $self->{log_dir} ||= "$self->{base_dir}/$self->{log_node}";
Condition50%1/2
$self->{"log_dir"} ||= qq[$self->{"base_dir"}/$self->{"log_node"}]
l!l
01
MC/DC0%0/1
$self->{"log_dir"} ||= qq[$self->{"base_dir"}/$self->{"log_node"}]
$self->{"log_dir"}
Truth table50%1/2
A
A= $self->{"log_dir"}
Aresult
11
00
71 1 ▶ $self->{log_filename} ||= "$self->{log_dir}/$self->{log_name}";
Condition50%1/2
$self->{"log_filename"} ||= qq[$self->{"log_dir"}/$self->{"log_name"}]
l!l
01
MC/DC0%0/1
$self->{"log_filename"} ||= qq[$self->{"log_dir"}/$self->{"log_name"}]
$self->{"log_filename"}
Truth table50%1/2
A
A= $self->{"log_filename"}
Aresult
11
00
72 1 ▶ $self->{zip_filename} ||= "$self->{log_dir}/$self->{log_name}.gz";
Condition50%1/2
$self->{"zip_filename"} ||= qq[$self->{"log_dir"}/$self->{"log_name"}.gz]
l!l
01
MC/DC0%0/1
$self->{"zip_filename"} ||= qq[$self->{"log_dir"}/$self->{"log_name"}.gz]
$self->{"zip_filename"}
Truth table50%1/2
A
A= $self->{"zip_filename"}
Aresult
11
00
73 1 $self->{handled_dirs} = 1;
74 }
75  
76 1 ▶sub mkpath_dirs {
Subroutine mkpath_dirs: called CC=7 SCAR=20.1
Pod undocumented
77 1 my $self = shift;
78 1 ▶ return if($self->{mkpathd});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"mkpathd"} 0 1 1
79 1 foreach(keys %{$self}) {
80 13 ▶ next unless(/_dir$/ && $self->{$_} =~ m@^/@);
Branch: 1 branch
BranchTrueFalseTotal
unless /_dir$/ and $self->{$_} =~ m[^/] 11 2 13
Condition66%2/3
/_dir$/ and $self->{$_} =~ m[^/]
!ll&&!rl&&r
1102
MC/DC50%1/2
/_dir$/ and $self->{$_} =~ m[^/]
/_dir$/ $self->{$_} =~ m[^/]
Truth table66%2/3
A and B
A= /_dir$/B= $self->{$_} =~ m[^/]
ABresult
0X0
100
111
81 2 ▶ mkpath ($self->{$_}) unless(-d $self->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{$_} 2 0 2
82 2 ▶ die "couldn't mkpath $self->{$_}" unless(-d $self->{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless -d $self->{$_} 0 2 2
83 }
84 1 $self->{mkpathd} = 1;
85 }
86  
87 1 ▶sub handle_log_cols {
Subroutine handle_log_cols: called CC=9 SCAR=27.1
Pod undocumented
88 1 my $self = shift;
89 1 ▶ if(ref $self->{log_cols} eq 'ARRAY' && !$self->{cols_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'log_cols'} eq 'ARRAY' and not $self->{'cols_string'}) { } 1 0 1
Condition33%1/3
ref $self->{"log_cols"} eq "ARRAY" and not $self->{"cols_string"}
!ll&&!rl&&r
001
MC/DC0%0/2
ref $self->{"log_cols"} eq "ARRAY" and not $self->{"cols_string"}
ref $self->{"log_cols"} eq "ARRAY" not $self->{"cols_string"}
Truth table33%1/3
A and not(B)
A= ref $self->{"log_cols"} eq "ARRAY"B= not $self->{"cols_string"}
ABresult
0X0
100
111
90 1 ▶ unshift @{$self->{log_cols}}, 'hostname' unless($self->{unshifted_hostname} || $self->{no_hostname});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"unshifted_hostname"} or $self->{"no_hostname"} 1 0 1
Condition33%1/3
$self->{"unshifted_hostname"} or $self->{"no_hostname"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"unshifted_hostname"} or $self->{"no_hostname"}
$self->{"unshifted_hostname"} $self->{"no_hostname"}
Truth table33%1/3
A or B
A= $self->{"unshifted_hostname"}B= $self->{"no_hostname"}
ABresult
1X1
011
000
91 1 ▶ unshift @{$self->{log_cols}}, 'pid' unless($self->{unshifted_pid} || $self->{no_pid});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"unshifted_pid"} or $self->{"no_pid"} 1 0 1
Condition33%1/3
$self->{"unshifted_pid"} or $self->{"no_pid"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"unshifted_pid"} or $self->{"no_pid"}
$self->{"unshifted_pid"} $self->{"no_pid"}
Truth table33%1/3
A or B
A= $self->{"unshifted_pid"}B= $self->{"no_pid"}
ABresult
1X1
011
000
92 1 ▶ unshift @{$self->{log_cols}}, 'time' unless($self->{unshifted_time} || $self->{no_time});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"unshifted_time"} or $self->{"no_time"} 1 0 1
Condition33%1/3
$self->{"unshifted_time"} or $self->{"no_time"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$self->{"unshifted_time"} or $self->{"no_time"}
$self->{"unshifted_time"} $self->{"no_time"}
Truth table33%1/3
A or B
A= $self->{"unshifted_time"}B= $self->{"no_time"}
ABresult
1X1
011
000
93 1 $self->{cols_string} = join($self->{delimiter}, @{$self->{log_cols}});
94 } else {
95 0 $self->{cols_string} = $self->{log_cols};
96 }
97 }
98  
99 2 ▶sub handle_log_info {
Subroutine handle_log_info: called CC=7 SCAR=21.3
Pod undocumented
100 2 my $self = shift;
101 2 my $ref = shift;
102 2 ▶ die "\$self->{log_info} is required" unless($ref);
Branch: 1 branch
BranchTrueFalseTotal
unless $ref 0 2 2
103 2 ▶ die "\$self->{log_info} needs to be an array ref" unless(ref $ref && ref $ref eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref and ref $ref eq "ARRAY" 0 2 2
Condition33%1/3
ref $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $ref and ref $ref eq "ARRAY"
ref $ref ref $ref eq "ARRAY"
Truth table33%1/3
A and B
A= ref $refB= ref $ref eq "ARRAY"
ABresult
0X0
100
111
104 2 ▶ unshift @{$ref}, hostname unless($self->{no_hostname});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_hostname"} 2 0 2
105 2 ▶ unshift @{$ref}, $$ unless($self->{no_pid});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_pid"} 2 0 2
106 2 ▶ unshift @{$ref}, time unless($self->{no_time});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_time"} 2 0 2
107 }
108  
109 2 ▶sub ref2string {
Subroutine ref2string: called CC=9 SCAR=26.5
Pod undocumented
110 2 my $self = shift;
111 2 my $ref = shift;
112 2 ▶ unless(ref $ref && ref $ref eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $ref and ref $ref eq "ARRAY") 0 2 2
Condition33%1/3
ref $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
002
MC/DC0%0/2
ref $ref and ref $ref eq "ARRAY"
ref $ref ref $ref eq "ARRAY"
Truth table33%1/3
A and B
A= ref $refB= ref $ref eq "ARRAY"
ABresult
0X0
100
111
113 0 $ref = [$ref];
114 }
115 2 my @this_array = @{$ref};
116 2 foreach(@this_array) {
117 12 ▶ $_ = '' unless((defined $_) && length $_);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ and length $_ 0 12 12
Condition33%1/3
defined $_ and length $_
!ll&&!rl&&r
0012
MC/DC0%0/2
defined $_ and length $_
defined $_ length $_
Truth table33%1/3
A and B
A= defined $_B= length $_
ABresult
0X0
100
111
118 12 ▶ if(ref $_ && ref $_ eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_ and ref $_ eq "HASH") 0 12 12
Condition33%1/3
ref $_ and ref $_ eq "HASH"
!ll&&!rl&&r
1200
MC/DC0%0/2
ref $_ and ref $_ eq "HASH"
ref $_ ref $_ eq "HASH"
Truth table33%1/3
A and B
A= ref $_B= ref $_ eq "HASH"
ABresult
0X0
100
111
119 0 $_ = freeze $_;
120 }
121 12 ▶ URLEncode \$_ unless($self->{no_URLEncode});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"no_URLEncode"} 12 0 12
122 }
123 2 my $string = join($self->{delimiter}, @this_array);
124 2 return $string;
125 }
126  
127 2 ▶sub log {
Subroutine log: called CC=7 SCAR=19.6
Pod undocumented
128 2 my $self = shift;
129 2 ▶ die "need a " . __PACKAGE__ . " object" unless(ref $self eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self eq __PACKAGE__ 0 2 2
130  
131 2 ▶ open(LOG, ">>$self->{log_filename}") || die "couldn't opendir $self->{log_filename}: $!";
Branch: 1 branch
BranchTrueFalseTotal
open LOG, qq[>>$self->{"log_filename"}] or die qq[couldn't opendir $self->{"log_filename"}: $!] 2 0 2
132 2 ▶ if(!-s $self->{log_filename}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-s $self->{"log_filename"}) 1 1 2
133 1 ▶ if($self->{log_cols}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"log_cols"}) 1 0 1
134 1 $self->handle_log_cols;
135 1 print LOG "$self->{cols_string}\n";
136 }
137 }
138  
139 2 ▶ unless(ref $self->{log_info}[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"log_info"}[0] eq "ARRAY") 2 0 2
140 2 $self->{log_info} = [$self->{log_info}];
141 }
142  
143 2 ▶ for(my $i=0;$i<@{$self->{log_info}};$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"log_info"};}) 2 2 4
144 2 my (@this_array) = @{$self->{log_info}[$i]};
145 2 $self->handle_log_info(\@this_array);
146 2 my $this_string = $self->ref2string(\@this_array);
147 2 print LOG "$this_string\n";
148 }
149 2 close(LOG);
150 2 delete $self->{log_info};
151 }
152  
153 1 ▶sub wipe {
Subroutine wipe: called CC=2 SCAR=7.1
Pod undocumented
154 1 my $self = shift;
155 1 ▶ if(-e $self->{log_filename}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->{"log_filename"}) 1 0 1
156 1 return unlink $self->{log_filename};
157 }
158 }
159  
160 0 ▶sub zip {
Subroutine zip: not called CC=4 SCAR=30.0
Pod undocumented
161 0 my $self = shift;
162 0 ▶ die "need a \$self->{log_filename}" unless($self->{log_filename});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"log_filename"} 0 0 0
163 0 ▶ die "need a \$self->{zip_filename}" unless($self->{zip_filename});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"zip_filename"} 0 0 0
164 0 ▶ return unless(-s $self->{log_filename});
Branch: 1 branch
BranchTrueFalseTotal
unless -s $self->{"log_filename"} 0 0 0
165 0 system("gzip -c < $self->{log_filename} > $self->{zip_filename}");
166 }
167  
168 0 ▶sub unzip {
Subroutine unzip: not called CC=4 SCAR=30.0
Pod undocumented
169 0 my $self = shift;
170 0 ▶ die "need a \$self->{log_filename}" unless($self->{log_filename});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"log_filename"} 0 0 0
171 0 ▶ die "need a \$self->{zip_filename}" unless($self->{zip_filename});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"zip_filename"} 0 0 0
172 0 ▶ return unless(-s $self->{zip_filename});
Branch: 1 branch
BranchTrueFalseTotal
unless -s $self->{"zip_filename"} 0 0 0
173 0 system("gzip -dc < $self->{zip_filename} > $self->{log_filename}");
174 }
175  
176 0 ▶sub log_zipped {
Subroutine log_zipped: not called CC=1 SCAR=6.9
Pod undocumented
177 0 my $self = shift;
178 0 $self->unzip;
179 0 $self->log;
180 0 $self->zip;
181 0 $self->wipe;
182 }
183  
184 2 1;
185  
186 __END__
↑ Summary