File Coverage

blib/lib/Log/Log4Cli.pm
Criterion Covered Total %
statement 36 38 94.7
branch 23 28 82.1
condition 9 9 100.0
subroutine 19 19 100.0
pod 11 11 100.0
total 98 105 93.3


line stmt bran cond sub pod time code
1             package Log::Log4Cli;
2              
3 6     6   206407 use 5.006;
  6         67  
4 6     6   28 use strict;
  6         11  
  6         99  
5 6     6   22 use warnings;
  6         8  
  6         178  
6 6     6   286 use parent qw(Exporter);
  6         216  
  6         33  
7              
8 6     6   306 use Carp qw(croak);
  6         10  
  6         262  
9 6     6   2230 use Term::ANSIColor qw(colored);
  6         39317  
  6         8038  
10              
11             our $VERSION = '0.19'; # Don't forget to change in pod below
12              
13             our @EXPORT = qw(
14             die_fatal
15             die_info
16             die_alert
17             die_notice
18              
19             log_fd
20              
21             log_fatal
22             log_error
23             log_alert
24             log_notice
25             log_warn
26             log_info
27             log_debug
28             log_trace
29             );
30              
31             our $COLORS = {
32             FATAL => 'bold red',
33             ERROR => 'red',
34             ALERT => 'bold yellow',
35             WARN => 'yellow',
36             INFO => 'cyan',
37             DEBUG => 'blue',
38             TRACE => 'magenta'
39             };
40             $COLORS->{NOTICE} = $COLORS->{ALERT}; # Deprecated
41              
42             our $LEVEL = 0;
43             our $POSITIONS = undef;
44             my $FD = \*STDERR; # descriptor
45             our $COLOR = -t $FD; # color on/off switcher
46              
47             sub _die($$$$) {
48 6 50   6   18 if ($^S) {
49             # inside eval block
50 6 100       614 croak defined $_[3] ? "$_[3]" : "Died";
51             } else {
52 0 0       0 print $FD $_[2] . (defined $_[3] ? "$_[3]. " : "") .
    0          
53             "Exit $_[0], ET " . (time - $^T) . "s\n" if ($_[1]);
54 0         0 exit $_[0];
55             }
56             }
57              
58             sub _pfx($) {
59 49     49   1152 my ($S, $M, $H, $d, $m, $y) = localtime(time);
60 49         462 my $pfx = sprintf "[%04i-%02i-%02i %02i:%02i:%02i %i %5s] ",
61             $y + 1900, $m + 1, $d, $H, $M, $S, $$, $_[0];
62 49 100 100     919 return ($COLOR ? colored($pfx, $COLORS->{$_[0]}) : $pfx) .
    100          
63             (($POSITIONS or $LEVEL > 4) ? join(":", (caller(1))[1,2]) . " " : "");
64             }
65              
66 2   100 2 1 127392 sub die_fatal(;$;$) { _die $_[1] || 127, $LEVEL > -2, _pfx('FATAL'), $_[0] }
67 2   100 2 1 900 sub die_alert(;$;$) { _die $_[1] || 0, $LEVEL > -1, _pfx('ALERT'), $_[0] }
68             *die_notice = \&die_alert; # Deprecated
69 2   100 2 1 1043 sub die_info(;$;$) { _die $_[1] || 0, $LEVEL > 1, _pfx('INFO'), $_[0] }
70              
71 11 100   11 1 551897 sub log_fatal(&) { print $FD _pfx('FATAL') . $_[0]->($_) . "\n" if $LEVEL > -2 }
72 9 100   9 1 23524 sub log_error(&) { print $FD _pfx('ERROR') . $_[0]->($_) . "\n" if $LEVEL > -1 }
73 10 100   10 1 23682 sub log_alert(&) { print $FD _pfx('ALERT') . $_[0]->($_) . "\n" if $LEVEL > -1 }
74             *log_notice = \&log_alert; # Deprecated
75 9 100   9 1 23925 sub log_warn(&) { print $FD _pfx('WARN') . $_[0]->($_) . "\n" if $LEVEL > 0 }
76 9 100   9 1 21782 sub log_info(&) { print $FD _pfx('INFO') . $_[0]->($_) . "\n" if $LEVEL > 1 }
77 9 100   9 1 26213 sub log_debug(&) { print $FD _pfx('DEBUG') . $_[0]->($_) . "\n" if $LEVEL > 2 }
78 9 100   9 1 22751 sub log_trace(&) { print $FD _pfx('TRACE') . $_[0]->($_) . "\n" if $LEVEL > 3 }
79              
80             sub log_fd(;$) {
81 2 100   2 1 71 if (@_) {
82 1         3 $FD = shift;
83 1         5 $COLOR = -t $FD;
84             }
85 2         14 return $FD;
86             }
87              
88             1;
89              
90             __END__