File Coverage

blib/lib/Log/Log4Cli.pm
Criterion Covered Total %
statement 53 53 100.0
branch 30 30 100.0
condition 11 11 100.0
subroutine 25 25 100.0
pod 11 11 100.0
total 130 130 100.0


line stmt bran cond sub pod time code
1             package Log::Log4Cli;
2              
3 6     6   212268 use 5.006;
  6         51  
4 6     6   28 use strict;
  6         6  
  6         111  
5 6     6   24 use warnings;
  6         8  
  6         175  
6 6     6   757 use parent qw(Exporter);
  6         466  
  6         47  
7              
8 6     6   3274 use Term::ANSIColor qw(colored);
  6         41264  
  6         4190  
9              
10             BEGIN {
11             *CORE::GLOBAL::die = sub {
12 3   100 3   20 my $msg = join(' ', grep { defined } @_) || "Died";
13 3         14 $msg .= " at " . join(' line ', (caller)[1,2]);
14 3         7 &die_fatal($msg, 255);
15 6     6   5171 };
16             }
17              
18             our $VERSION = '0.22'; # Don't forget to change in pod below
19              
20             our @EXPORT = qw(
21             die_fatal
22             die_info
23             die_alert
24              
25             log_fd
26              
27             log_fatal
28             log_error
29             log_alert
30             log_warn
31             log_info
32             log_debug
33             log_trace
34             );
35              
36             our $COLORS = {
37             FATAL => 'bold red',
38             ERROR => 'red',
39             ALERT => 'bold yellow',
40             WARN => 'yellow',
41             INFO => 'cyan',
42             DEBUG => 'blue',
43             TRACE => 'magenta'
44             };
45              
46             our $LEVEL = 0;
47             our $POSITIONS = undef;
48             our $COLOR; # color on/off switcher; defined below
49              
50             my $FD; # descriptor to write messages; defined below
51              
52             sub _die($$$$) {
53 19 100   19   76 my $log_msg = $_[2] . (defined $_[3] ? "$_[3]. " : "") .
54             "Exit $_[0], ET " . (time - $^T) . "s\n";
55              
56 19 100       34 if ($^S) {
57             # inside eval block
58 4         19 my ($file, $line) = (caller(1))[1,2];
59 4 100       39 CORE::die bless {
    100          
60             ERR_MESSAGE => ($_[3] ? "$_[3]" : "Died") . " at $file line $line.",
61             EXIT_CODE => $_[0],
62             FILE => $file,
63             LINE => $line,
64             LOG_MESSAGE => $_[1] ? $log_msg : '',
65             }, 'Log::Log4Cli::Exception';
66             } else {
67 15 100       44 print $FD $log_msg if ($_[1]);
68 15         32 exit $_[0];
69             }
70             }
71              
72             sub _pfx($) {
73 61     61   1520 my ($S, $M, $H, $d, $m, $y) = localtime(time);
74 61         510 my $pfx = sprintf "[%04i-%02i-%02i %02i:%02i:%02i %i %5s] ",
75             $y + 1900, $m + 1, $d, $H, $M, $S, $$, $_[0];
76 61 100 100     965 return ($COLOR ? colored($pfx, $COLORS->{$_[0]}) : $pfx) .
    100          
77             (($POSITIONS or $LEVEL > 4) ? join(":", (caller(1))[1,2]) . " " : "");
78             }
79              
80 9   100 9 1 2431 sub die_fatal(;$;$) { _die $_[1] || 127, $LEVEL > -2, _pfx('FATAL'), $_[0] }
81 4   100 4 1 25 sub die_alert(;$;$) { _die $_[1] || 0, $LEVEL > -1, _pfx('ALERT'), $_[0] }
82 6   100 6 1 3076 sub die_info(;$;$) { _die $_[1] || 0, $LEVEL > 1, _pfx('INFO'), $_[0] }
83              
84 11 100   11 1 24959 sub log_fatal(&) { print $FD _pfx('FATAL') . $_[0]->($_) . "\n" if $LEVEL > -2 }
85 9 100   9 1 22110 sub log_error(&) { print $FD _pfx('ERROR') . $_[0]->($_) . "\n" if $LEVEL > -1 }
86 9 100   9 1 21851 sub log_alert(&) { print $FD _pfx('ALERT') . $_[0]->($_) . "\n" if $LEVEL > -1 }
87 9 100   9 1 528500 sub log_warn(&) { print $FD _pfx('WARN') . $_[0]->($_) . "\n" if $LEVEL > 0 }
88 9 100   9 1 22484 sub log_info(&) { print $FD _pfx('INFO') . $_[0]->($_) . "\n" if $LEVEL > 1 }
89 9 100   9 1 23371 sub log_debug(&) { print $FD _pfx('DEBUG') . $_[0]->($_) . "\n" if $LEVEL > 2 }
90 9 100   9 1 21264 sub log_trace(&) { print $FD _pfx('TRACE') . $_[0]->($_) . "\n" if $LEVEL > 3 }
91              
92             sub log_fd(;$) {
93 23 100   23 1 12834 if (@_) {
94 22         36 $FD = shift;
95 22         74 $COLOR = -t $FD;
96             }
97 23         38 return $FD;
98             }
99              
100             log_fd(\*STDERR);
101              
102             1;
103              
104             package Log::Log4Cli::Exception;
105              
106 6     6   120 use 5.006;
  6         17  
107 6     6   30 use strict;
  6         9  
  6         178  
108 6     6   42 use warnings;
  6         12  
  6         260  
109              
110 6     6   2087 use overload '""' => \&as_string;
  6         1522  
  6         50  
111              
112             sub as_string {
113 4     4   2040 $_[0]->{ERR_MESSAGE};
114             }
115              
116             1;
117              
118             __END__