Fu.pm

filter: Statementstmt 94.6% 196 / 207 Branchbran 69.3% 43 / 62 Conditioncond 59.0% 13 / 22 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 96.6% 29 / 30 Podpod 100% 4 / 4 totaltotal 85.7% 289 / 337 CC 44 SCAR 23.2
CC
44
Cov
87%
_logger
29.9
import
7.0
Configure
5.6
CRAP 10.1
« Fu/Color.pm ↑ Summary Fu/Common.pm »
1 package Log::Fu;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Log::Fu::Common qw(:levels %Config LEVELS fu_term_is_ansi);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Log::Fu::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Log::Fu::Color;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Log::Fu::Chomp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Carp qw(carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Sys::Syslog;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use Constant::Generate [ map {"FLD_$_"} (qw(FH LVL FMT COLOR ISATTY CODE))];
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Constant::Generate [qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
15 LOGFN_TYPE_LIST
16 LOGFN_TYPE_SUB_PLAIN
17 LOGFN_TYPE_SUB_FMT
18 5 )], -start_at => 1;
19  
20 5 our @EXPORT = ( map { ("log_$_", "elog_$_" ) } (LEVELS) );
21 5 push @EXPORT, map "log_$_"."f", LEVELS;
22 5 push @EXPORT, map "elog_$_"."f", LEVELS;
23 5 our @EXPORT_OK = qw(set_log_level);
24  
25 5 our $VERSION = '0.31';
26  
27 5 our $SHUSH = 0;
28 5 our $LINE_PREFIX = "";
29 5 our $LINE_SUFFIX = "";
30  
31 5 our $TERM_ANSI = fu_term_is_ansi();
32 5 our $TERM_CLEAR_LINE= $TERM_ANSI;
33  
34 5 our $USE_WATCHDOG = $ENV{LOG_FU_WATCHDOG};
35 5 our $NO_STRIP = $ENV{LOG_FU_NO_STRIP};
36  
37 5 our $FORCE_COLOR = $ENV{LOG_FU_FORCE_COLOR};
38  
39 5 our $DISPLAY_SEVERITY = $ENV{LOG_FU_DISPLAY_SEVERITY};
40 5 ▶$DISPLAY_SEVERITY ||= 0;
Condition50%1/2
$Log::Fu::DISPLAY_SEVERITY ||= 0
l!l
05
MC/DC0%0/1
$Log::Fu::DISPLAY_SEVERITY ||= 0
$Log::Fu::DISPLAY_SEVERITY
Truth table50%1/2
A
A= $Log::Fu::DISPLAY_SEVERITY
Aresult
11
00
41  
42 5 my $ENABLE_SYSLOG;
43 my $SYSLOG_FACILITY;
44 5 my $SYSLOG_STDERR_ECHO = 0;
45  
46 5 my $CLEAR_LINE_ESC = "\033[0J";
47 #From 0.20
48 3 ▶sub Configure {
Subroutine Configure: called CC=5 SCAR=17.2
Pod documented
49 3 my %options = @_;
50
51 3 ▶ if($USE_WATCHDOG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Fu::USE_WATCHDOG) 0 3 3
52 0 carp "Changing global logging options..";
53 }
54
55 3 foreach my $k (keys %Config) {
56 21 ▶ if(exists $options{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $options{$k}) 6 15 21
57 6 $Config{$k} = delete $options{$k};
58 }
59 }
60 3 ▶ if(%options) {
Branch: 1 branch
BranchTrueFalseTotal
if (%options) 0 3 3
61 0 die "Unknown options: " . join(",", keys %options);
62 }
63 }
64  
65 #From 0.20
66 5 *AddHandler = *Log::Fu::Chomp::AddHandler;
67 5 *DelHandler = *Log::Fu::Chomp::DelHandler;
68  
69 5 my (%sources,$log_target);
70 5 $$log_target = *STDERR;
71 5 my $def_target_can_use_color = -t STDERR;
72  
73 11 ▶sub _set_source_level {
Subroutine _set_source_level: called CC=4 SCAR=13.9
74 11 my ($source,%params) = @_;
75 11 my @datum;
76 11 my $h = \%params;
77 11 ▶ $params{level} = LOG_INFO unless defined $params{level};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"level"} 5 6 11
78 11 ▶ $params{target} = $log_target unless defined $params{target};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"target"} 6 5 11
79 11 @datum[FLD_LVL, FLD_FH] = @params{qw(level target)};
80 11 ▶ if(ref $params{target} eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params{'target'} eq 'CODE') { } 5 6 11
81 5 $datum[FLD_CODE] = $params{target};
82 } else {
83 6 $datum[FLD_COLOR] = -t $datum[FLD_FH];
84 6 $datum[FLD_ISATTY] = -t $datum[FLD_FH];
85 }
86 11 $sources{$source} = \@datum;
87 }
88  
89 5 my $defpkg_key = '__LOG_FU_DEFAULTS__';
90  
91 5 _set_source_level($defpkg_key);
92  
93  
94  
95 6 ▶sub import {
Subroutine import: called CC=7 SCAR=19.5
96 6 my $h;
97 #check if we're passed an option hashref
98 6 foreach my $i (0..$#_) {
99 12 ▶ if(ref($_[$i]) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[$i] eq "HASH") 6 6 12
100 6 $h = delete $_[$i];
101 }
102 }
103 #get the filename of the code that's using us.
104 6 my $pkgname = caller();
105
106 my($ulevel,$target,$subs,$prefix) =
107 6 delete @{$h}{qw(level target subs function_prefix)};
108
109 6 ▶ $prefix ||= "";
Condition100%2/2
$prefix ||= ""
l!l
15
MC/DC100%1/1
$prefix ||= ""
$prefix
Truth table100%2/2
A
A= $prefix
Aresult
11
00
110 6 ▶ $ulevel = "info" unless defined $ulevel;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ulevel 2 4 6
111 6 my $level = eval("LOG_".uc($ulevel));
112 6 ▶ die "Unknown level $ulevel" unless defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 0 6 6
113 6 _set_source_level($pkgname, level => $level, target => $target);
114 6 ▶ if($subs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subs) { } 1 5 6
115 1 _gen_subsubs($pkgname, $prefix);
116 } else {
117 5 _gen_listsubs($pkgname, $prefix);
118 5 _gen_subsubs($pkgname, $prefix."e");
119 }
120 6 return 1;
121 }
122  
123 #Called to get stuff for per-package personalization
124 25 ▶sub _get_pkg_params {
Subroutine _get_pkg_params: called CC=3 SCAR=11.2
125 #clandestinely does level checking
126 25 my ($pkgname, $level) = @_;
127 25 my $ret = $sources{$pkgname};
128 25 ▶ $ret ||= $sources{$defpkg_key};
Condition33%1/3
$ret ||= $sources{$defpkg_key}
l!l&&r!l&&!r
2500
MC/DC0%0/2
$ret ||= $sources{$defpkg_key}
$ret $sources{$defpkg_key}
Truth table33%1/3
A ||= B
A= $retB= $sources{$defpkg_key}
ABresult
1X1
011
000
129 25 ▶ if($level < $ret->[FLD_LVL]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level < $ret->[1]) 6 19 25
130 6 $ret = undef;
131 }
132 25 return $ret;
133 }
134  
135  
136 25 ▶sub _logger {
Subroutine _logger: called CC=19 SCAR=34.0
137 25 ▶ return if $SHUSH; #no logging wanted!
Branch: 1 branch
BranchTrueFalseTotal
if $Log::Fu::SHUSH 0 25 25
138 25 my ($level_number, $level_name, $stack_offset, $type, @messages) = @_;
139
140 25 my ($pkgname,$filename,$line) = caller(0+$stack_offset);
141 25 my $pparams = _get_pkg_params($pkgname, $level_number);
142 25 ▶ return if !defined $pparams;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pparams 6 19 25
143  
144 19 my (undef,undef,undef,$subroutine) = caller(1+$stack_offset);
145 19 ▶ $subroutine ||= "-";
Condition100%2/2
$subroutine ||= "-"
l!l
811
MC/DC100%1/1
$subroutine ||= "-"
$subroutine
Truth table100%2/2
A
A= $subroutine
Aresult
11
00
146 19 my $outfile = $pparams->[FLD_FH];
147 19 my $basename = basename($filename);
148 19 my $level_str = "[$level_name] ";
149
150 19 my $message;
151 19 ▶ if($type == LOGFN_TYPE_LIST) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type == 1) { } 17 2 19
152 17 $message = join(" ", @messages);
153 } else {
154 2 ▶ my $sub = $messages[0]
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $messages[0] or die "Sub-style logging requested but no sub provided" 2 0 2
155 or die "Sub-style logging requested but no sub provided";
156 2 @messages = $sub->();
157
158 2 ▶ if($type == LOGFN_TYPE_SUB_PLAIN) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type == 2) { } 1 1 2
elsif ($type == 3) { } 1 0 1
159 1 $message = join(" ", @messages);
160 } elsif($type == LOGFN_TYPE_SUB_FMT) {
161 1 my $fmt_str = $messages[0];
162 1 $message = sprintf($fmt_str, @messages[1..$#messages]);
163 } else {
164 0 die("Unknown logging mode $type");
165 }
166 }
167  
168 #Color stuff...
169 19 ▶ if( ($Log::Fu::Color::USE_COLOR && $pparams->[FLD_COLOR]) || $FORCE_COLOR) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Log::Fu::Color::USE_COLOR and $pparams->[3] or $Log::Fu::FORCE_COLOR) { } 0 19 19
elsif ($Log::Fu::DISPLAY_SEVERITY == -1) { } 1 18 19
Condition33%2/6
$Log::Fu::Color::USE_COLOR and $pparams->[3] or $Log::Fu::FORCE_COLOR
l!l&&r!l&&!r
0019
$Log::Fu::Color::USE_COLOR and $pparams->[3]
!ll&&!rl&&r
0190
MC/DC0%0/3
$Log::Fu::Color::USE_COLOR and $pparams->[3] or $Log::Fu::FORCE_COLOR
$Log::Fu::Color::USE_COLOR $pparams->[3] $Log::Fu::FORCE_COLOR
Truth table20%1/5
A and B or C
A= $Log::Fu::Color::USE_COLORB= $pparams->[3]C= $Log::Fu::FORCE_COLOR
ABCresult
11X1
0X11
1011
0X00
1000
170 0 $message = fu_colorize($level_number, $message);
171 0 ▶ if($DISPLAY_SEVERITY <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Fu::DISPLAY_SEVERITY <= 0) 0 0 0
172 0 $level_str = "";
173 }
174 } elsif($DISPLAY_SEVERITY == -1) {
175 1 $level_str = "";
176 }
177  
178 19 $subroutine = fu_chomp($subroutine);
179
180 19 my $msg = "$level_str$basename:$line ($subroutine): $message\n";
181
182 19 ▶ if ($LINE_PREFIX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Fu::LINE_PREFIX) 0 19 19
183 0 $msg =~ s/^(.)/$LINE_PREFIX$1/mg;
184 }
185 19 ▶ if($LINE_SUFFIX) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Log::Fu::LINE_SUFFIX) 0 19 19
186 0 $msg =~ s/(.)$/$1$LINE_SUFFIX/mg;
187 }
188 19 ▶ if($pparams->[FLD_ISATTY] && $TERM_CLEAR_LINE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pparams->[4] and $Log::Fu::TERM_CLEAR_LINE) 0 19 19
Condition33%1/3
$pparams->[4] and $Log::Fu::TERM_CLEAR_LINE
!ll&&!rl&&r
1900
MC/DC0%0/2
$pparams->[4] and $Log::Fu::TERM_CLEAR_LINE
$pparams->[4] $Log::Fu::TERM_CLEAR_LINE
Truth table33%1/3
A and B
A= $pparams->[4]B= $Log::Fu::TERM_CLEAR_LINE
ABresult
0X0
100
111
189 0 $msg =~ s/^(.)/$CLEAR_LINE_ESC$1/mg
190 #Clear the rest of the line, too:
191 }
192 19 ▶ if(ref $outfile eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $outfile eq 'CODE') { } 19 0 19
193 19 $outfile->($msg);
194 } else {
195 0 print $outfile $msg;
196 }
197
198 19 ▶ if ($ENABLE_SYSLOG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENABLE_SYSLOG) 2 17 19
199 2 syslog(syslog_level($level_number), $msg);
200 }
201 }
202  
203 5 foreach my $level (LEVELS) {
204 #Plain wrappers
205 25 my $fn_name = "log_$level";
206 5 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
207 25 my $const = &{uc("LOG_" . $level)};
208 #Offset wrappers
209 25 ▶ *{ $fn_name . "_with_offset" } = sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
210 0 _logger($const, uc($level), 1 + shift, LOGFN_TYPE_LIST, @_);
211 25 };
212 }
213  
214 5 my %export_cache;
215  
216 10 ▶sub _gen_listsubs {
Subroutine _gen_listsubs: called CC=4 SCAR=13.9
217 10 my ($pkgname,$prefix) = @_;
218 10 ▶ $prefix ||= "";
Condition100%2/2
$prefix ||= ""
l!l
19
MC/DC100%1/1
$prefix ||= ""
$prefix
Truth table100%2/2
A
A= $prefix
Aresult
11
00
219 10 foreach my $level (LEVELS) {
220 50 my $fn_name = $pkgname . "::$prefix" . "log_$level";
221 50 ▶ next if exists $export_cache{$fn_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $export_cache{$fn_name} 0 50 50
222 50 $export_cache{$fn_name} = 1;
223 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
224 50 my $const = &{uc("LOG_$level")};
225 50 ▶ *{ $fn_name } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
226 18 @_ = ($const,uc($level),0,LOGFN_TYPE_LIST,@_);
227 18 goto &_logger;
228 50 };
229
230 50 ▶ *{ $fn_name . "f" } = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
231 2 @_ = ($const,uc($level),0,LOGFN_TYPE_LIST,
232 sprintf($_[0], @_[1..$#_]));
233 2 goto &_logger;
234 50 };
235 }
236 }
237  
238 11 ▶sub _gen_subsubs {
Subroutine _gen_subsubs: called CC=4 SCAR=13.9
239 11 my ($pkgname,$prefix) = @_;
240 11 ▶ $prefix ||= "";
Condition100%2/2
$prefix ||= ""
l!l
101
MC/DC100%1/1
$prefix ||= ""
$prefix
Truth table100%2/2
A
A= $prefix
Aresult
11
00
241 11 foreach my $level (LEVELS) {
242 55 my $fn_name = $pkgname . "::$prefix" . "log_$level";
243 55 ▶ next if exists $export_cache{$fn_name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $export_cache{$fn_name} 0 55 55
244 55 $export_cache{$fn_name} = 1;
245
246 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
247 55 my $const = &{uc("LOG_".$level)};
248
249 55 ▶ *{$fn_name} = sub (&) {
Subroutine __ANON__: called CC=1 SCAR=0.0
250 2 @_ = ($const,uc($level), 0, LOGFN_TYPE_SUB_PLAIN, $_[0]);
251 2 goto &_logger;
252 55 };
253
254 55 ▶ *{$fn_name . "f"} = sub (&) {
Subroutine __ANON__: called CC=1 SCAR=0.0
255 3 @_ = ($const, uc($level), 0, LOGFN_TYPE_SUB_FMT, $_[0]);
256 3 goto &_logger;
257 55 };
258 }
259 }
260  
261 5 _gen_subsubs(__PACKAGE__, "e");
262 5 _gen_listsubs(__PACKAGE__, "");
263  
264 #From 0.03
265 3 ▶sub set_log_level {
Subroutine set_log_level: called CC=3 SCAR=11.2
Pod documented
266 3 my ($pkgname,$level) = @_;
267 3 $level = eval("LOG_".uc($level));
268 3 ▶ return if !defined $level;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level 0 3 3
269 3 ▶ return if !exists $sources{$pkgname};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $sources{$pkgname} 0 3 3
270 3 $sources{$pkgname}->[FLD_LVL] = $level;
271 3 return 1;
272 }
273  
274 #From 0.04
275 1 ▶sub start_syslog {
Subroutine start_syslog: called CC=2 SCAR=7.1
Pod documented
276 #Take standard openlog options,
277 1 my $ok = openlog(@_);
278 1 ▶ $ENABLE_SYSLOG = 1 if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 0 1
279 1 return $ok;
280 }
281  
282 1 ▶sub stop_syslog {
Subroutine stop_syslog: called CC=2 SCAR=7.1
Pod documented
283 1 my $ok = closelog();
284 1 ▶ $ENABLE_SYSLOG = 0 if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 0 1
285 1 return $ok;
286 }
287  
288 5 1;
289  
290 __END__
« Fu/Color.pm ↑ Summary Fu/Common.pm »