| 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 //= {}
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||||
| 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]"
($quote // $opts->{"no_quote_bind_values"}) // 0
$quote // $opts->{"no_quote_bind_values"}
$opts->{"args"} // []
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
Truth table100%3/3
A // B
A= $quoteB= $opts->{"no_quote_bind_values"}
Truth table100%2/2
A
A= $opts->{"args"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$opts //= {}
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | 5 | ▶ | my $leader = $opts->{no_lead} ? NUL : (ucfirst $self->config->name).BRK; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | 5 | ▶ | if ($opts->{fill}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $width = $opts->{width} // WIDTH; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{"width"} // 80
MC/DC0%0/1
$opts->{"width"} // 80
$opts->{"width"}
Truth table0%0/2
A
A= $opts->{"width"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||
| 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 //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->quiet or $opts->{"quiet"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 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 //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$opts->{"to"} and $opts->{"to"} eq "err"
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"
| |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||
| 103 | 2 | ▶ | $code->() if $opts->{cl}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 104 | 2 | $code->( $self->add_leader( $text, $opts ) ); | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | ▶ | $code->() if $opts->{nl}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | $v != TRUE and throw 'Cannot turn quiet mode off'; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 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 //= {}
MC/DC0%0/1
$opts //= {}
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$self->quiet or $opts->{"quiet"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | return TRUE; | |||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | 17 | 1; | |||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||
| 131 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||