Usul/TraitFor/OutputLogging.pm

filter: Statementstmt 78.0% 64 / 82 Branchbran 54.5% 12 / 22 Conditioncond 56.6% 17 / 30 MC/DCmcdc 37.5% 6 / 16 Subroutinesub 90.0% 18 / 20 Podpod 100% 8 / 8 totaltotal 70.2% 125 / 178 CC 29 SCAR 19.7
CC
29
Cov
69%
warning
30.0
add_leader
8.8
__ANON__
6.0
CRAP 7.1
« Usul/Getopt/Usage.pm ↑ Summary Usul/TraitFor/UntaintedGetopts.pm »
1 package Class::Usul::TraitFor::OutputLogging;
2  
3 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 17 ▶use Class::Usul::Constants qw( BRK FAILED FALSE NUL TRUE WIDTH );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use Class::Usul::Functions qw( abs_path emit emit_err throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Class::Usul::Types qw( Bool SimpleStr );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use Text::Autoformat qw( autoformat );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use Class::Usul::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 17 requires qw( config log );
13  
14 # Public attributes
15 option 'locale' => is => 'lazy', isa => SimpleStr, format => 's',
16 documentation => 'Loads the specified language message catalogue',
17 3 ▶ builder => sub { $_[ 0 ]->config->locale }, short => 'L';
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine _build_locale: called CC=1 SCAR=0.0
18  
19 17 option 'quiet' => is => 'ro', isa => Bool, default => FALSE,
20 documentation => 'Quiet the display of information messages',
21 reader => 'quiet_flag', short => 'q';
22  
23 # Private attributes
24 has '_quiet_flag' => is => 'rw', isa => Bool,
25 4 ▶ builder => sub { $_[ 0 ]->quiet_flag },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 17 lazy => TRUE, writer => '_set__quiet_flag';
27  
28 # Private methods
29 5 ▶my $_loc = sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
30 5 ▶ my ($self, $text, $opts, $quote) = @_; $opts //= {};
Condition100%2/2
$opts //= {}
l!l
41
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
31  
32 return $self->localize( $text // '[no message]', {
33 locale => $self->locale,
34 no_quote_bind_values => $quote // $opts->{no_quote_bind_values} // FALSE,
35 5 ▶ params => $opts->{args} // [] } );
Condition100%9/9
$text // "[no message]"
l!l
41
($quote // $opts->{"no_quote_bind_values"}) // 0
l!l
41
$quote // $opts->{"no_quote_bind_values"}
l!l&&r!l&&!r
311
$opts->{"args"} // []
l!l
14
MC/DC100%1/1
$text // "[no message]"
$text
MC/DC100%2/2
($quote // $opts->{"no_quote_bind_values"}) // 0
$quote $opts->{"no_quote_bind_values"}
MC/DC100%1/1
$opts->{"args"} // []
$opts->{"args"}
Truth table100%2/2
A
A= $text
Aresult
11
00
Truth table100%3/3
A // B
A= $quoteB= $opts->{"no_quote_bind_values"}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $opts->{"args"}
Aresult
11
00
36 17 };
37  
38 # Public methods
39 7 ▶sub add_leader {
Subroutine add_leader: called CC=7 SCAR=21.8
Pod documented
40 7 ▶ my ($self, $text, $opts) = @_; $text or return NUL; $opts //= {};
Branch: 1 branch
BranchTrueFalseTotal
$text or return "" 5 2 7
Condition100%2/2
$opts //= {}
l!l
23
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
41  
42 5 ▶ my $leader = $opts->{no_lead} ? NUL : (ucfirst $self->config->name).BRK;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'no_lead'} ? : 0 5 5
43  
44 5 ▶ if ($opts->{fill}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"fill"}) 0 5 5
45 0 ▶ my $width = $opts->{width} // WIDTH;
Condition0%0/2
$opts->{"width"} // 80
l!l
00
MC/DC0%0/1
$opts->{"width"} // 80
$opts->{"width"}
Truth table0%0/2
A
A= $opts->{"width"}
Aresult
11
00
46  
47 0 $text = autoformat $text, { right => $width - 1 - length $leader };
48 }
49  
50 5 ▶ return join "\n", map { (m{ \A $leader }mx ? NUL : $leader).$_ }
Branch: 1 branch
BranchTrueFalseTotal
/ \A $leader /mx ? : 0 5 5
51 split m{ \n }mx, $text;
52 }
53  
54 2 ▶sub error {
Subroutine error: called CC=2 SCAR=6.9
Pod documented
55 2 my ($self, $text, $opts) = @_; $text = $self->$_loc( $text, $opts );
56  
57 2 $self->log->error( $_ ) for (split m{ \n }mx, "${text}");
58  
59 2 emit_err $self->add_leader( $text, $opts );
60  
61 2 return TRUE;
62 }
63  
64 0 ▶sub fatal {
Subroutine fatal: not called CC=2 SCAR=17.9
Pod documented
65 0 my ($self, $text, $opts) = @_; my (undef, $file, $line) = caller 0;
66  
67 0 my $posn = ' at '.abs_path( $file )." line ${line}";
68  
69 0 $text = $self->$_loc( $text, $opts ).$posn;
70  
71 0 $self->log->alert( $_ ) for (split m{ \n }mx, $text);
72  
73 0 emit_err $self->add_leader( $text, $opts );
74  
75 0 exit FAILED;
76 }
77  
78 1 ▶sub info {
Subroutine info: called CC=5 SCAR=17.5
Pod documented
79 1 my ($self, $text, $opts) = @_;
80  
81 1 ▶ $opts //= {}; $text = $self->$_loc( $text, $opts, TRUE );
Condition50%1/2
$opts //= {}
l!l
01
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
82  
83 1 $self->log->info( $_ ) for (split m{ [\n] }mx, $text);
84  
85 1 ▶ $self->quiet or $opts->{quiet} or emit $self->add_leader( $text, $opts );
Branch: 1 branch
BranchTrueFalseTotal
$self->quiet or $opts->{"quiet"} or &emit($self->add_leader($text, $opts)) 1 0 1
Condition33%1/3
$self->quiet or $opts->{"quiet"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->quiet or $opts->{"quiet"}
$self->quiet $opts->{"quiet"}
Truth table33%1/3
A or B
A= $self->quietB= $opts->{"quiet"}
ABresult
1X1
011
000
86  
87 1 return TRUE;
88 }
89  
90 4 ▶sub loc {
Subroutine loc: called CC=1 SCAR=0.0
Pod documented
91 4 my $self = shift; return $self->l10n->localizer( $self->locale, @_ );
92 }
93  
94 2 ▶sub output {
Subroutine output: called CC=4 SCAR=14.2
Pod documented
95 2 my ($self, $text, $opts) = @_;
96  
97 2 ▶ $opts //= {}; $text = $self->$_loc( $text, $opts, TRUE );
Condition50%1/2
$opts //= {}
l!l
20
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
98  
99 4 ▶ my $code = sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
100 4 ▶ $opts->{to} && $opts->{to} eq 'err' ? emit_err( @_ ) : emit( @_ );
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'to'} && $opts->{'to'} eq 'err' ? : 0 4 4
Condition33%1/3
$opts->{"to"} and $opts->{"to"} eq "err"
!ll&&!rl&&r
400
MC/DC0%0/2
$opts->{"to"} and $opts->{"to"} eq "err"
$opts->{"to"} $opts->{"to"} eq "err"
Truth table33%1/3
A and B
A= $opts->{"to"}B= $opts->{"to"} eq "err"
ABresult
0X0
100
111
101 2 };
102  
103 2 ▶ $code->() if $opts->{cl};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"cl"} 1 1 2
104 2 $code->( $self->add_leader( $text, $opts ) );
105 2 ▶ $code->() if $opts->{nl};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"nl"} 1 1 2
106 2 return TRUE;
107 }
108  
109 8 ▶sub quiet {
Subroutine quiet: called CC=3 SCAR=17.3
Pod documented
110 0 ▶ my ($self, $v) = @_; defined $v or return $self->_quiet_flag; $v = !!$v;
Branch: 1 branch
BranchTrueFalseTotal
defined $v or return $self->_quiet_flag 0 8 8
111  
112 0 ▶ $v != TRUE and throw 'Cannot turn quiet mode off';
Branch: 1 branch
BranchTrueFalseTotal
$v != 1 and &throw("Cannot turn quiet mode off") 0 0 0
113  
114 0 return $self->_set__quiet_flag( $v );
115 }
116  
117 0 ▶sub warning {
Subroutine warning: not called CC=5 SCAR=34.0
Pod documented
118 0 my ($self, $text, $opts) = @_;
119  
120 0 ▶ $opts //= {}; $text = $self->$_loc( $text, $opts );
Condition0%0/2
$opts //= {}
l!l
00
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
121  
122 0 $self->log->warn( $_ ) for (split m{ \n }mx, $text);
123  
124 0 ▶ $self->quiet or $opts->{quiet} or emit $self->add_leader( $text, $opts );
Branch: 1 branch
BranchTrueFalseTotal
$self->quiet or $opts->{"quiet"} or &emit($self->add_leader($text, $opts)) 0 0 0
Condition0%0/3
$self->quiet or $opts->{"quiet"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->quiet or $opts->{"quiet"}
$self->quiet $opts->{"quiet"}
Truth table0%0/3
A or B
A= $self->quietB= $opts->{"quiet"}
ABresult
1X1
011
000
125  
126 0 return TRUE;
127 }
128  
129 17 1;
130  
131 __END__
« Usul/Getopt/Usage.pm ↑ Summary Usul/TraitFor/UntaintedGetopts.pm »