blib/lib/Clustericious/Log/CommandLine.pm

filter: Statementstmt 94.7% 90 / 95 Branchbran 95.5% 65 / 68 Conditioncond 76.4% 13 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 91.0% 184 / 202 CC 44 SCAR 27.4
CC
44
Cov
93%
import
24.0
INIT
12.7
handlelogoptions
10.0
CRAP 15.5
« blib/lib/Clustericious/Command/apache.pm ↑ Summary blib/lib/Clustericious/Command/configdebug.pm »
1 package Clustericious::Log::CommandLine;
2  
3 47 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 47 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 47 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: Simple Command Line Interface for Log4perl
9 47 our $VERSION = '1.29'; # VERSION
10  
11  
12 47 my %init; # logconfig, loginit, logfile, logcategory, noinit
13 my %options; # options set on command line
14  
15 47 my %levelmap =
16 (
17 q => 'off',
18 quiet => 'off',
19 v => 'info',
20 verbose => 'info',
21 d => 'debug'
22 );
23  
24 sub import
25 46 ▶{
Subroutine import: called CC=24 SCAR=31.8
26 46 my $class = shift;
27  
28 46 my $caller = caller;
29  
30 46 my @getoptlist;
31 my $next;
32 46 foreach (@_)
33 {
34 92 ▶ if ($next)
Branch: 1 branch
BranchTrueFalseTotal
if ($next) 20 72 92
35 {
36 20 $init{$next} = $_;
37 20 $next = undef;
38 20 next;
39 }
40  
41 72 ▶ /^:(log(?:config|file|init|category))$/ and $next = $1; # Grab next arg
Branch: 1 branch
BranchTrueFalseTotal
/^:(log(?:config|file|init|category))$/ and $next = $1 20 52 72
42  
43 72 ▶ /^(?:trace|:levels|:all)$/ and push(@getoptlist, 'trace:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:trace|:levels|:all)$/ and push @getoptlist, "trace:s\@" 46 26 72
44 72 ▶ /^(?:debug|:levels|:all)$/ and push(@getoptlist, 'debug:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:debug|:levels|:all)$/ and push @getoptlist, "debug:s\@" 46 26 72
45 72 ▶ /^(?:info|:levels|:all)$/ and push(@getoptlist, 'info:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:info|:levels|:all)$/ and push @getoptlist, "info:s\@" 46 26 72
46 72 ▶ /^(?:warn|:levels|:all)$/ and push(@getoptlist, 'warn:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:warn|:levels|:all)$/ and push @getoptlist, "warn:s\@" 46 26 72
47 72 ▶ /^(?:error|:levels|:all)$/ and push(@getoptlist, 'error:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:error|:levels|:all)$/ and push @getoptlist, "error:s\@" 46 26 72
48 72 ▶ /^(?:fatal|:levels|:all)$/ and push(@getoptlist, 'fatal:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:fatal|:levels|:all)$/ and push @getoptlist, "fatal:s\@" 46 26 72
49 72 ▶ /^(?:off|:levels|:all)$/ and push(@getoptlist, 'off:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:off|:levels|:all)$/ and push @getoptlist, "off:s\@" 46 26 72
50  
51 72 ▶ /^(?:quiet|:long|:all)$/ and push(@getoptlist, 'quiet:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:quiet|:long|:all)$/ and push @getoptlist, "quiet:s\@" 46 26 72
52 72 ▶ /^(?:verbose|:long|:all)$/ and push(@getoptlist, 'verbose:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:verbose|:long|:all)$/ and push @getoptlist, "verbose:s\@" 46 26 72
53  
54 72 ▶ /^(?:q|:short|:all)$/ and push(@getoptlist, 'q:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:q|:short|:all)$/ and push @getoptlist, "q:s\@" 46 26 72
55 72 ▶ /^(?:v|:short|:all)$/ and push(@getoptlist, 'v:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:v|:short|:all)$/ and push @getoptlist, "v:s\@" 46 26 72
56 72 ▶ /^(?:d|:short|:all)$/ and push(@getoptlist, 'd:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:d|:short|:all)$/ and push @getoptlist, "d:s\@" 46 26 72
57  
58 72 ▶ /^(?:loglevel|:logopts|:all)$/ and push(@getoptlist, 'loglevel:s@');
Branch: 1 branch
BranchTrueFalseTotal
/^(?:loglevel|:logopts|:all)$/ and push @getoptlist, "loglevel:s\@" 46 26 72
59  
60 /^(?:logconfig|:logopts|:all)$/ and
61 72 ▶ push(@getoptlist, 'logconfig=s' => \$init{logconfig});
Branch: 1 branch
BranchTrueFalseTotal
/^(?:logconfig|:logopts|:all)$/ and push @getoptlist, "logconfig=s", \$init{"logconfig"} 46 26 72
62  
63 /^(?:logfile|:logopts|:all)$/ and
64 72 ▶ push(@getoptlist, 'logfile=s' => \$init{logfile});
Branch: 1 branch
BranchTrueFalseTotal
/^(?:logfile|:logopts|:all)$/ and push @getoptlist, "logfile=s", \$init{"logfile"} 46 26 72
65  
66 47 ▶ { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
67 /^handlelogoptions$/ and
68 72 ▶ *{"$caller\::handlelogoptions"} = *handlelogoptions;
Branch: 1 branch
BranchTrueFalseTotal
/^handlelogoptions$/ and *{"${caller}::handlelogoptions";} = *handlelogoptions 3 69 72
69 }
70  
71 72 ▶ /^:noinit$/ and $init{noinit} = 1;
Branch: 1 branch
BranchTrueFalseTotal
/^:noinit$/ and $init{"noinit"} = 1 3 69 72
72 }
73  
74 46 my $getopt = Getopt::Long::Parser->new
75 ( config => [qw(pass_through no_auto_abbrev
76 no_ignore_case)] );
77  
78 46 $getopt->getoptions(\%options, @getoptlist);
79  
80 # Allow: --option --option foo --option foo,bar
81 46 ▶ while (my ($opt, $cats) = each %options)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %options) 39 46 85
82 {
83 39 ▶ $options{$opt} = [ map { length $_ ? split(',') : '' } @$cats ];
Branch: 1 branch
BranchTrueFalseTotal
length $_ ? : 10 29 39
84 }
85  
86 # --loglevel category=level or --loglevel level
87 46 foreach (@{$options{loglevel}})
88 {
89 0 my ($category, $level) = /^([^=]*?)=?([^=]+)$/;
90 0 push(@{$options{$level}}, $category);
91 }
92 46 delete $options{loglevel};
93 }
94  
95 47 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
96 INIT
97 46 ▶{
Subroutine INIT: called CC=12 SCAR=25.4
98 47 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 46 ▶ return if $init{noinit};
Branch: 1 branch
BranchTrueFalseTotal
if $init{"noinit"} 3 43 46
100  
101 43 ▶ if (defined $init{logconfig} and -f $init{logconfig} and -r _)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $init{'logconfig'} and -f $init{'logconfig'} and -r _) { } 4 39 43
Condition66%4/6
defined $init{"logconfig"} and -f $init{"logconfig"} and -r _
!ll&&!rl&&r
3904
defined $init{"logconfig"} and -f $init{"logconfig"}
!ll&&!rl&&r
3904
MC/DC33%1/3
defined $init{"logconfig"} and -f $init{"logconfig"} and -r _
defined $init{"logconfig"} -f $init{"logconfig"} -r _
Truth table50%2/4
A and B and C
A= defined $init{"logconfig"}B= -f $init{"logconfig"}C= -r _
ABCresult
0XX0
10X0
1100
1111
102 {
103 4 Log::Log4perl->init($init{logconfig});
104 }
105 else
106 {
107 39 ▶ if ($init{loginit} and not ref $init{loginit})
Branch: 2 branches
BranchTrueFalseTotal
if ($init{'loginit'} and not ref $init{'loginit'}) { } 7 32 39
elsif ($init{'loginit'} and ref $init{'loginit'} eq 'ARRAY') { } 0 32 32
Condition100%3/3
$init{"loginit"} and not ref $init{"loginit"}
!ll&&!rl&&r
22107
MC/DC100%2/2
$init{"loginit"} and not ref $init{"loginit"}
$init{"loginit"} not ref $init{"loginit"}
Truth table100%3/3
A and not(B)
A= $init{"loginit"}B= not ref $init{"loginit"}
ABresult
0X0
100
111
108 {
109 7 Log::Log4perl->init(\$init{loginit});
110 }
111 ▶ elsif ($init{loginit} and ref $init{loginit} eq 'ARRAY')
Condition66%2/3
$init{"loginit"} and ref $init{"loginit"} eq "ARRAY"
!ll&&!rl&&r
22100
MC/DC0%0/2
$init{"loginit"} and ref $init{"loginit"} eq "ARRAY"
$init{"loginit"} ref $init{"loginit"} eq "ARRAY"
Truth table66%2/3
A and B
A= $init{"loginit"}B= ref $init{"loginit"} eq "ARRAY"
ABresult
0X0
100
111
112 {
113 0 Log::Log4perl->easy_init(@{$init{loginit}});
114 }
115 else
116 {
117 32 ▶ my $init = ref $init{loginit} eq 'HASH' ? $init{loginit} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $init{'loginit'} eq 'HASH' ? : 10 22 32
118  
119 32 ▶ $init->{level} ||= $ERROR;
Condition66%2/3
$init->{"level"} ||= $Clustericious::Log::CommandLine::ERROR
l!l&&r!l&&!r
3290
MC/DC0%0/2
$init->{"level"} ||= $Clustericious::Log::CommandLine::ERROR
$init->{"level"} $Clustericious::Log::CommandLine::ERROR
Truth table66%2/3
A ||= B
A= $init->{"level"}B= $Clustericious::Log::CommandLine::ERROR
ABresult
1X1
011
000
120 32 ▶ $init->{layout} ||= '[%-5p] %m%n';
Condition100%2/2
$init->{"layout"} ||= "[%-5p] %m%n"
l!l
725
MC/DC100%1/1
$init->{"layout"} ||= "[%-5p] %m%n"
$init->{"layout"}
Truth table100%2/2
A
A= $init->{"layout"}
Aresult
11
00
121  
122 32 Log::Log4perl->easy_init($init);
123 }
124 }
125  
126 43 handlelogoptions();
127 }
128 47 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
129  
130  
131 sub handlelogoptions
132 49 ▶{
Subroutine handlelogoptions: called CC=10 SCAR=23.1
Pod documented
133 49 ▶ if ($init{logfile})
Branch: 1 branch
BranchTrueFalseTotal
if ($init{"logfile"}) 2 47 49
134 {
135 2 my $logfile = $init{logfile};
136 2 my $layout = '%d %c %m%n';
137  
138 2 ▶ if ($logfile =~ s/\|(.*)$//) # "logfilename|logpattern"
Branch: 1 branch
BranchTrueFalseTotal
if ($logfile =~ s/\|(.*)$//) 1 1 2
139 {
140 1 $layout = $1;
141 }
142  
143 2 my $file_appender = Log::Log4perl::Appender->new(
144 "Log::Log4perl::Appender::File",
145 name => 'logfile',
146 filename => $logfile);
147  
148 2 $file_appender->layout(Log::Log4perl::Layout::PatternLayout->new(
149 $layout));
150  
151 2 get_logger('')->add_appender($file_appender);
152 }
153  
154 49 ▶ while (my ($level, $vals) = each %options)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %options) 41 49 90
155 {
156 41 ▶ $level = $levelmap{$level} if exists $levelmap{$level};
Branch: 1 branch
BranchTrueFalseTotal
if exists $levelmap{$level} 34 7 41
157  
158 41 my $level_id = Log::Log4perl::Level::to_priority(uc $level);
159  
160 41 foreach my $category (@$vals)
161 {
162 41 ▶ if ($category eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($category eq "") 30 11 41
163 {
164 $category = defined($init{logcategory})
165 ? $init{logcategory}
166 30 ▶ : $level_id >= $INFO ? '' : 'main';
Branch: 2 branches
BranchTrueFalseTotal
defined $init{'logcategory'} ? : 0 30 30
$level_id >= $Clustericious::Log::CommandLine::INFO ? : 25 5 30
167 }
168  
169 41 ▶ $category = '' if $category eq 'root';
Branch: 1 branch
BranchTrueFalseTotal
if $category eq "root" 0 41 41
170  
171 41 get_logger($category)->level($level_id);
172 }
173 }
174 }
175  
176 47 1;
177  
178 __END__
« blib/lib/Clustericious/Command/apache.pm ↑ Summary blib/lib/Clustericious/Command/configdebug.pm »