blib/lib/Log/Colorize/Helper.pm

filter: Statementstmt 14.8% 16 / 108 Branchbran 0.0% 0 / 60 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 71.4% 5 / 7 Podpod 100% 2 / 2 totaltotal 11.1% 23 / 207 CC 37 SCAR 71.0
CC
37
Cov
9%
colorize
1406.0
new
2.0
BEGIN
1.0
CRAP 1210.0
↑ Summary
1 package Log::Colorize::Helper;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'Error::Helper';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use String::ShellQuote;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 Log::Colorize::Helper - Makes searching and colorizing logs trivial with out all the need for piping
12  
13 =head1 VERSION
14  
15 Version 0.1.1
16  
17 =cut
18  
19 1 our $VERSION = '0.1.1';
20  
21  
22 =head1 SYNOPSIS
23  
24 use Log::Colorize::Helper;
25 use Getopt::Std;
26
27 #gets the options
28 my %opts=();
29 getopts('efhtn:g:ivlFGJ', \%opts);
30
31 #init it
32 my $clog=Log::Colorize::Helper->new;
33
34 #set tail/head stuff if needed
35 if ( ! defined( $opts{n} ) ){
36 $opts{n}=10;
37 }
38 if ( $opts{t} ){
39 $opts{t}=$opts{n};
40 }
41 if ( $opts{h} ){
42 $opts{h}=$opts{n};
43 }
44
45 $clog->colorize(
46 {
47 echo=>$opts{e},
48 log=>$ARGV[0],
49 head=>$opts{h},
50 tail=>$opts{t},
51 grep=>$opts{g},
52 less=>$opts{l},
53 follow=>$opts{f},
54 'grep-insensitive'=>$opts{i},
55 'grep-invert'=>$opts{v},
56 'grep-first'=>$opts{F},
57 bzip2=>$opts{J},
58 gzip=>$opts{G},
59 }
60 );
61  
62 This module uses L<Error::Helper> for error reporting.
63  
64 =head1 METHODS
65  
66 =head2 new
67  
68 Creates a new object. This method will never error.
69  
70 my $clog=Log::Colorize::Helper->new;
71  
72 =cut
73  
74 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
75 0 my $self={
76 perror=>undef,
77 error=>undef,
78 errorString=>'',
79 errorExtra=>{
80 flags=>{
81 1=>'noFileSpecified',
82 2=>'doesNotExist',
83 3=>'badCombo',
84 4=>'noGre[',
85 },
86 },
87 };
88 0 bless $self;
89 0 return $self;
90 }
91  
92 =head2 colorize
93  
94 =head3 args hash
95  
96 =head4 bzip2
97  
98 The log is compressed using bzip2.
99  
100 =head4 echo
101  
102 Print the command used.
103  
104 =head4 follow
105  
106 A Perl boolean for if it should follow while tailing.
107  
108 Default is false.
109  
110 If set to true and tail is not specified it is set to 10.
111  
112 =head4 head
113  
114 How many lines to print at the top of the file.
115  
116 The default is 0, false. This means head will not be used.
117  
118 Can't be combined with tail.
119  
120 =head4 grep
121  
122 An optional string to grep for.
123  
124 =head4 grep-first
125  
126 A Perl boolean to run grep infront of the head/tail instead of after.
127  
128 The default is false.
129  
130 =head4 grep-insensitive
131  
132 This is a Perl boolean value for if grep should be case insensitive.
133  
134 The default is false.
135  
136 =head4 grep-invert
137  
138 This is a Perl boolean value for if grep should be inverted or not.
139  
140 The default is false.
141  
142 =head4 gzip
143  
144 The log is compressed using gzip.
145  
146 =head4 less
147  
148 A Perl boolean value for if it should pass it to 'less -R'
149  
150 =head4 log
151  
152 The log file to colorize.
153  
154 =head4 tail
155  
156 How many lines to print at the bottom of the file.
157  
158 The default is 0, false. This means tail will not be used.
159  
160 Can't be combined with head.
161  
162 #gets the options
163 my %opts=();
164 getopts('efhtn:g:ivlFGJ', \%opts);
165
166 #init it
167 my $clog=Log::Colorize::Helper->new;
168
169 #set tail/head stuff if needed
170 if ( ! defined( $opts{n} ) ){
171 $opts{n}=10;
172 }
173 if ( $opts{t} ){
174 $opts{t}=$opts{n};
175 }
176 if ( $opts{h} ){
177 $opts{h}=$opts{n};
178 }
179
180 $clog->colorize(
181 {
182 echo=>$opts{e},
183 log=>$ARGV[0],
184 head=>$opts{h},
185 tail=>$opts{t},
186 grep=>$opts{g},
187 less=>$opts{l},
188 follow=>$opts{f},
189 'grep-insensitive'=>$opts{i},
190 'grep-invert'=>$opts{v},
191 'grep-first'=>$opts{F},
192 bzip2=>$opts{J},
193 gzip=>$opts{G},
194 }
195 );
196  
197  
198 =cut
199  
200 0 ▶sub colorize{
Subroutine colorize: not called CC=37 SCAR=72.5
Pod documented
201 0 my $self=$_[0];
202 0 my %args;
203 0 ▶ if(defined($_[1])){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 0 0 0
204 0 %args= %{$_[1]};
205 }
206  
207 0 $self->errorblank;
208  
209 #set various defaults
210 0 ▶ if ( !defined( $args{head} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"head"}) 0 0 0
211 0 $args{head}=0;
212 }
213 0 ▶ if ( !defined( $args{echo} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"echo"}) 0 0 0
214 0 $args{echo}=0;
215 }
216 0 ▶ if ( !defined( $args{tail} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"tail"}) 0 0 0
217 0 $args{tail}=0;
218 }
219 0 ▶ if ( !defined( $args{less} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"less"}) 0 0 0
220 0 $args{less}=0;
221 }
222 0 ▶ if ( !defined( $args{bzip2} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"bzip2"}) 0 0 0
223 0 $args{bzip2}=0;
224 }
225 0 ▶ if ( !defined( $args{gzip} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"gzip"}) 0 0 0
226 0 $args{gzip}=0;
227 }
228 0 ▶ if ( !defined( $args{'grep-first'} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"grep-first"}) 0 0 0
229 0 $args{'grep-first'}=0;
230 }
231 0 ▶ if ( !defined( $args{'grep-insensitive'} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"grep-insensitive"}) 0 0 0
232 0 $args{'grep-insensitive'}=0;
233 }
234 0 ▶ if ( !defined( $args{'grep-invert'} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"grep-invert"}) 0 0 0
235 0 $args{'grep-invert'}=0;
236 }
237  
238 #set tail to 10 if it is not set and follow is set
239 0 ▶ if ( $args{follow} && ! $args{tail} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"follow"} and not $args{"tail"}) 0 0 0
Condition0%0/3
$args{"follow"} and not $args{"tail"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"follow"} and not $args{"tail"}
$args{"follow"} not $args{"tail"}
Truth table0%0/3
A and not(B)
A= $args{"follow"}B= not $args{"tail"}
ABresult
0X0
100
111
240 0 $args{tail}=10;
241 }
242  
243 #error if grep is not set and follow is true
244 0 ▶ if ( $args{'grep-first'} && ! defined( $args{grep} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"grep-first"} and not defined $args{"grep"}) 0 0 0
Condition0%0/3
$args{"grep-first"} and not defined $args{"grep"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"grep-first"} and not defined $args{"grep"}
$args{"grep-first"} not defined $args{"grep"}
Truth table0%0/3
A and not(B)
A= $args{"grep-first"}B= not defined $args{"grep"}
ABresult
0X0
100
111
245 0 $self->{error}=4;
246 0 $self->{errorString}='grep-first is true, but grep is not set';
247 0 $self->warn;
248 0 return undef;
249 }
250
251 #cam't combine these both tail and head
252 0 ▶ if ( $args{head} & $args{tail} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"head"} & $args{"tail"}) 0 0 0
253 0 $self->{error}=3;
254 0 $self->{errorString}="Can't combine head and tail";
255 0 $self->warn;
256 0 return undef;
257 }
258  
259 #can't combine follow and head
260 0 ▶ if ( $args{head} && $args{follow} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"head"} and $args{"follow"}) 0 0 0
Condition0%0/3
$args{"head"} and $args{"follow"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"head"} and $args{"follow"}
$args{"head"} $args{"follow"}
Truth table0%0/3
A and B
A= $args{"head"}B= $args{"follow"}
ABresult
0X0
100
111
261 0 $self->{error}=3;
262 0 $self->{errorString}="Can't combine head and follow";
263 0 $self->warn;
264 0 return undef;
265 }
266  
267 #can't combine follow and head
268 0 ▶ if ( $args{'grep-first'} && $args{follow} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"grep-first"} and $args{"follow"}) 0 0 0
Condition0%0/3
$args{"grep-first"} and $args{"follow"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"grep-first"} and $args{"follow"}
$args{"grep-first"} $args{"follow"}
Truth table0%0/3
A and B
A= $args{"grep-first"}B= $args{"follow"}
ABresult
0X0
100
111
269 0 $self->{error}=3;
270 0 $self->{errorString}="Can't combine follow and grep first";
271 0 $self->warn;
272 0 return undef;
273 }
274
275 #make sure we have a log specified
276 0 ▶ if ( !defined( $args{log} ) ){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"log"}) 0 0 0
277 0 $self->{error}=1;
278 0 $self->{errorString}='no log file specified';
279 0 $self->warn;
280 0 return undef;
281 };
282  
283 #make sure it exists and is a file
284 0 ▶ if ( ! -f $args{log} ){
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $args{"log"}) 0 0 0
285 0 $self->{error}=2;
286 0 $self->{errorString}='"'.$args{log}.'" is not a file or does not exist';
287 0 $self->warn;
288 0 return undef;
289 }
290  
291 #the command to use, initial assembly
292 0 my $command='cat '.shell_quote( $args{log} ).' ';
293  
294 #unarchive if needed...
295 0 ▶ if ( $args{bzip2} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"bzip2"}) 0 0 0
296 0 $command=$command.'| bunzip2 ';
297 }
298 0 ▶ if ( $args{gzip} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"gzip"}) 0 0 0
299 0 $command=$command.'| gunzip ';
300 }
301
302 #puts grep together
303 0 my $grep='';
304 0 ▶ if ( defined( $args{grep} ) ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"grep"}) 0 0 0
305 0 $grep='| grep';
306  
307 0 ▶ if ( $args{'grep-insensitive'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"grep-insensitive"}) 0 0 0
308 0 $grep=$grep.' -i';
309 }
310 0 ▶ if ( $args{'grep-insensitive'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"grep-insensitive"}) 0 0 0
311 0 $grep=$grep.' -v';
312 }
313  
314 #unarchive if needed
315 0 ▶ if ( $args{'grep-first'} && $args{bzip2} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"grep-first"} and $args{"bzip2"}) 0 0 0
Condition0%0/3
$args{"grep-first"} and $args{"bzip2"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"grep-first"} and $args{"bzip2"}
$args{"grep-first"} $args{"bzip2"}
Truth table0%0/3
A and B
A= $args{"grep-first"}B= $args{"bzip2"}
ABresult
0X0
100
111
316 0 $grep=$grep.' -J';
317 }
318 0 ▶ if ( $args{'grep-first'} && $args{gzip} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"grep-first"} and $args{"gzip"}) 0 0 0
Condition0%0/3
$args{"grep-first"} and $args{"gzip"}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"grep-first"} and $args{"gzip"}
$args{"grep-first"} $args{"gzip"}
Truth table0%0/3
A and B
A= $args{"grep-first"}B= $args{"gzip"}
ABresult
0X0
100
111
319 0 $grep=$grep.' -Z';
320 }
321
322 0 $grep=$grep.' '.$args{grep};
323 }
324  
325 #apply grep if it is first
326 0 ▶ if ( $args{'grep-first'} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"grep-first"}) 0 0 0
327 0 $command=$grep.' '.shell_quote( $args{log} ).' ';
328 0 $grep=''; #blank grep so when we apply it we are not running it twice
329 }
330  
331 #add head if needed
332 0 ▶ if ( $args{head} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"head"}) 0 0 0
333 0 $command=$command.'| head -n '.$args{head}.' ';
334 }
335  
336 #tail if needed
337 0 ▶ if ( $args{tail} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"tail"}) 0 0 0
338 0 ▶ if ( $args{follow} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'follow'}) { } 0 0 0
339 0 $command='tail -f -F -n '.$args{tail}.' '.shell_quote( $args{log} ).' ';
340 }else{
341 0 $command=$command.'| tail -n '.$args{tail};
342 }
343 }
344  
345 #add grep and colorize
346 0 $command=$command.$grep.' | colorize';
347  
348 #add less if desired
349 0 ▶ if ( $args{less} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"less"}) 0 0 0
350 0 $command=$command.' | less -R';
351 }
352  
353 #echo if needed
354 0 ▶ if ( $args{echo} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"echo"}) 0 0 0
355 0 print $command."\n";
356 }
357
358 0 system($command);
359
360 0 return 1;
361 }
362  
363 =head1 ERROR CODES
364  
365 =head2 1/noFileSpecified
366  
367 No log file specified.
368  
369 =head2 2/doesNotExist
370  
371 The log file does not exist.
372  
373 =head2 3/badCombo
374  
375 A bad combination of options.
376  
377 =head2 4/noGrep
378  
379 grep-first is true, but there is no grep.
380  
381 =head1 AUTHOR
382  
383 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
384  
385 =head1 BUGS
386  
387 Please report any bugs or feature requests to C<bug-log-colorize-helper at rt.cpan.org>, or through
388 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Log-Colorize-Helper>. I will be notified, and then you'll
389 automatically be notified of progress on your bug as I make changes.
390  
391  
392  
393  
394 =head1 SUPPORT
395  
396 You can find documentation for this module with the perldoc command.
397  
398 perldoc Log::Colorize::Helper
399  
400  
401 You can also look for information at:
402  
403 =over 4
404  
405 =item * RT: CPAN's request tracker (report bugs here)
406  
407 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Log-Colorize-Helper>
408  
409 =item * AnnoCPAN: Annotated CPAN documentation
410  
411 L<http://annocpan.org/dist/Log-Colorize-Helper>
412  
413 =item * CPAN Ratings
414  
415 L<http://cpanratings.perl.org/d/Log-Colorize-Helper>
416  
417 =item * Search CPAN
418  
419 L<http://search.cpan.org/dist/Log-Colorize-Helper/>
420  
421 =back
422  
423  
424 =head1 ACKNOWLEDGEMENTS
425  
426  
427 =head1 LICENSE AND COPYRIGHT
428  
429 Copyright 2017 Zane C. Bowers-Hadley.
430  
431 This program is distributed under the (Simplified) BSD License:
432 L<http://www.opensource.org/licenses/BSD-2-Clause>
433  
434 Redistribution and use in source and binary forms, with or without
435 modification, are permitted provided that the following conditions
436 are met:
437  
438 * Redistributions of source code must retain the above copyright
439 notice, this list of conditions and the following disclaimer.
440  
441 * Redistributions in binary form must reproduce the above copyright
442 notice, this list of conditions and the following disclaimer in the
443 documentation and/or other materials provided with the distribution.
444  
445 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
446 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
447 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
448 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
449 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
450 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
451 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
452 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
453 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
454 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
455 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
456  
457  
458 =cut
459  
460 1 1; # End of Log::Colorize::Helper
↑ Summary