blib/lib/Log/MixedColor.pm

filter: Statementstmt 100% 92 / 92 Branchbran 83.3% 15 / 18 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 97.7% 130 / 133 CC 11 SCAR 13.3
CC
11
Cov
97%
info_msg
8.0
fatal_err
3.0
debug_msg
2.0
CRAP 3.8
↑ Summary
1 # Author: Matthew Mallard
2 # Website: www.q-technologies.com.au
3 # Date: 6th October 2016
4  
5 # ABSTRACT: Outputs messages in multiple colors
6  
7  
8  
9  
10  
11 package Log::MixedColor;
12 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use MooseX::Aliases;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Module::Load::Conditional qw[ check_install ];
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use constant DEBUG_MSG => "debug";
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use constant ERROR_MSG => "error";
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # We only need to activate storage when someone using us has already installed the module
23 # otherwise the following code can silently fail
24 1 ▶if( check_install( module => 'MooseX::Storage' )){
Branch: 1 branch
BranchTrueFalseTotal
if (check_install("module", "MooseX::Storage")) 1 0 1
25 1 require MooseX::Storage;
26 1 MooseX::Storage->import();
27 1 with Storage(
28 'format' => 'JSON',
29 'io' => 'File',
30 traits => ['DisableCycleDetection']
31 );
32 }
33  
34  
35 1 has 'verbose' => (
36 is => 'rw',
37 isa => 'Bool',
38 default => 0,
39 alias => 'v',
40 );
41  
42  
43 1 has 'debug' => (
44 is => 'rw',
45 isa => 'Bool',
46 default => 0,
47 alias => 'd',
48 );
49  
50  
51 1 ▶sub quote {
Subroutine quote: called CC=1 SCAR=0.0
Pod documented
52 1 my $self = shift;
53 1 return '%%' . shift() . '##';
54 }
55 1 alias q => 'quote';
56  
57  
58 1 has 'quote_start' => (
59 is => 'rw',
60 isa => 'Str',
61 default => '%%',
62 );
63  
64  
65  
66 1 has 'quote_end' => (
67 is => 'rw',
68 isa => 'Str',
69 default => '##',
70 );
71  
72  
73  
74 15 ▶sub info_msg {
Subroutine info_msg: called CC=8 SCAR=20.8
Pod documented
75 15 my $self = shift;
76 15 chomp( my $msg = shift );
77 15 my $level = shift;
78 15 ▶ $level = "" if !$level;
Branch: 1 branch
BranchTrueFalseTotal
unless $level 5 10 15
79 15 my $color = $self->info_color;
80 15 my $var_col = $self->info_quote_color;
81 15 my $prefix = $self->info_prefix;
82 15 ▶ my $say = ( $self->verbose or $self->debug );
Condition100%3/3
$self->verbose || $self->debug
l!l&&r!l&&!r
1032
MC/DC100%2/2
$self->verbose || $self->debug
$self->verbose $self->debug
Truth table100%3/3
A || B
A= $self->verboseB= $self->debug
ABresult
1X1
011
000
83 15 my $extra_nl = "";
84 15 my $excl = "";
85 15 my $fh = *STDOUT;
86 15 ▶ if( $level eq DEBUG_MSG ){
Branch: 2 branches
BranchTrueFalseTotal
if ($level eq 'debug') { } 3 12 15
elsif ($level eq 'error') { } 7 5 12
87 3 $color = $self->debug_color;
88 3 $var_col = $self->debug_quote_color;
89 3 $prefix = $self->debug_prefix;
90 3 $say = $self->debug;
91 } elsif( $level eq ERROR_MSG ){
92 7 $color = $self->err_color;
93 7 $var_col = $self->err_quote_color;
94 7 $prefix = $self->err_prefix;
95 7 $say = 1;
96 7 $extra_nl = "\n";
97 7 $excl = "!";
98 7 $fh = *STDERR;
99 }
100 15 for( $msg ){
101 15 my $code1 = color("reset") . color($var_col);
102 15 my $code2 = color("reset") . color($color);
103 15 s/%%/$code1/g;
104 15 s/##/$code2/g;
105 }
106 15 ▶ $prefix = $prefix . " " if $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 15 0 15
107 15 ▶ say $fh $extra_nl
Branch: 1 branch
BranchTrueFalseTotal
if $say 14 1 15
108 . color($color)
109 . $prefix
110 . $msg
111 . $excl
112 . color("reset")
113 . $extra_nl
114 if $say;
115 }
116 1 alias info => 'info_msg';
117  
118  
119 5 ▶sub debug_msg {
Subroutine debug_msg: called CC=2 SCAR=6.9
Pod documented
120 5 my $self = shift;
121 5 my $msg = shift;
122 5 ▶ $self->info_msg( $msg, DEBUG_MSG ) if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 3 2 5
123 }
124 1 alias dmsg => 'debug_msg';
125  
126  
127 5 ▶sub err_msg {
Subroutine err_msg: called CC=1 SCAR=0.0
Pod documented
128 5 my $self = shift;
129 5 my $msg = shift;
130 5 $self->info_msg( $msg, ERROR_MSG );
131 }
132 1 alias err => 'err_msg';
133 1 alias warn => 'err_msg';
134  
135  
136 2 ▶sub fatal_err {
Subroutine fatal_err: called CC=3 SCAR=11.0
Pod documented
137 2 my $self = shift;
138 2 my $msg = shift;
139 2 my $val = shift;
140 2 ▶ $val = 1 unless defined( $val );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 1 2
141 2 $self->info_msg( $msg, ERROR_MSG );
142 2 ▶ exit $val if $self->fatal_is_fatal;
Branch: 1 branch
BranchTrueFalseTotal
if $self->fatal_is_fatal 2 0 2
143 }
144 1 alias fatal => 'fatal_err';
145  
146  
147 1 has 'fatal_is_fatal' => (
148 is => 'rw',
149 isa => 'Bool',
150 default => 1,
151 );
152  
153  
154 1 has 'info_color' => (
155 is => 'rw',
156 isa => 'Str',
157 default => 'green',
158 );
159  
160 1 has 'debug_color' => (
161 is => 'rw',
162 isa => 'Str',
163 default => 'magenta',
164 );
165  
166 1 has 'err_color' => (
167 is => 'rw',
168 isa => 'Str',
169 default => 'red',
170 );
171  
172 1 has 'info_quote_color' => (
173 is => 'rw',
174 isa => 'Str',
175 default => 'black on_white',
176 );
177  
178 1 has 'debug_quote_color' => (
179 is => 'rw',
180 isa => 'Str',
181 default => 'blue',
182 );
183  
184 1 has 'err_quote_color' => (
185 is => 'rw',
186 isa => 'Str',
187 default => 'yellow',
188 );
189  
190 1 has 'info_prefix' => (
191 is => 'rw',
192 isa => 'Str',
193 default => 'Info:',
194 );
195  
196 1 has 'debug_prefix' => (
197 is => 'rw',
198 isa => 'Str',
199 default => 'Debug:',
200 );
201  
202 1 has 'err_prefix' => (
203 is => 'rw',
204 isa => 'Str',
205 default => 'Error:',
206 );
207  
208  
209  
210  
211 1 1;
212  
213 __END__
↑ Summary