File Coverage

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


line stmt bran cond sub pod time code
1             package Log::Log4Cli;
2              
3 6     6   208978 use 5.006;
  6         51  
4 6     6   26 use strict;
  6         8  
  6         105  
5 6     6   21 use warnings;
  6         16  
  6         169  
6 6     6   715 use parent qw(Exporter);
  6         465  
  6         26  
7              
8 6     6   3162 use Term::ANSIColor qw(colored);
  6         40034  
  6         4049  
9              
10             BEGIN {
11             *CORE::GLOBAL::die = sub {
12 3   100 3   19 my $msg = join(' ', grep { defined } @_) || "Died";
13 3         12 $msg .= " at " . join(' line ', (caller)[1,2]);
14 3         7 &die_fatal($msg, 255);
15 6     6   5368 };
16             }
17              
18             our $VERSION = '0.21'; # 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             our $STATUS = undef; # exit code; deprecated TODO: remove it
50              
51             my $FD; # descriptor to write messages; defined below
52              
53             sub _die($$$$) {
54 19 100   19   72 my $log_msg = $_[2] . (defined $_[3] ? "$_[3]. " : "") .
55             "Exit $_[0], ET " . (time - $^T) . "s\n";
56              
57 19 100       34 if ($^S) {
58             # inside eval block
59 4         5 $STATUS = $_[0]; # deprecated TODO: remove it
60              
61 4         19 my ($file, $line) = (caller(1))[1,2];
62 4 100       41 CORE::die bless {
    100          
63             ERR_MESSAGE => ($_[3] ? "$_[3]" : "Died") . " at $file line $line.",
64             EXIT_CODE => $_[0],
65             FILE => $file,
66             LINE => $line,
67             LOG_MESSAGE => $_[1] ? $log_msg : '',
68             }, 'Log::Log4Cli::Exception';
69             } else {
70 15 100       38 print $FD $log_msg if ($_[1]);
71 15         33 exit $_[0];
72             }
73             }
74              
75             sub _pfx($) {
76 61     61   1422 my ($S, $M, $H, $d, $m, $y) = localtime(time);
77 61         475 my $pfx = sprintf "[%04i-%02i-%02i %02i:%02i:%02i %i %5s] ",
78             $y + 1900, $m + 1, $d, $H, $M, $S, $$, $_[0];
79 61 100 100     954 return ($COLOR ? colored($pfx, $COLORS->{$_[0]}) : $pfx) .
    100          
80             (($POSITIONS or $LEVEL > 4) ? join(":", (caller(1))[1,2]) . " " : "");
81             }
82              
83 9   100 9 1 2367 sub die_fatal(;$;$) { _die $_[1] || 127, $LEVEL > -2, _pfx('FATAL'), $_[0] }
84 4   100 4 1 27 sub die_alert(;$;$) { _die $_[1] || 0, $LEVEL > -1, _pfx('ALERT'), $_[0] }
85 6   100 6 1 2913 sub die_info(;$;$) { _die $_[1] || 0, $LEVEL > 1, _pfx('INFO'), $_[0] }
86              
87 11 100   11 1 25896 sub log_fatal(&) { print $FD _pfx('FATAL') . $_[0]->($_) . "\n" if $LEVEL > -2 }
88 9 100   9 1 21677 sub log_error(&) { print $FD _pfx('ERROR') . $_[0]->($_) . "\n" if $LEVEL > -1 }
89 9 100   9 1 22440 sub log_alert(&) { print $FD _pfx('ALERT') . $_[0]->($_) . "\n" if $LEVEL > -1 }
90 9 100   9 1 21907 sub log_warn(&) { print $FD _pfx('WARN') . $_[0]->($_) . "\n" if $LEVEL > 0 }
91 9 100   9 1 22202 sub log_info(&) { print $FD _pfx('INFO') . $_[0]->($_) . "\n" if $LEVEL > 1 }
92 9 100   9 1 483916 sub log_debug(&) { print $FD _pfx('DEBUG') . $_[0]->($_) . "\n" if $LEVEL > 2 }
93 9 100   9 1 21667 sub log_trace(&) { print $FD _pfx('TRACE') . $_[0]->($_) . "\n" if $LEVEL > 3 }
94              
95             sub log_fd(;$) {
96 23 100   23 1 12247 if (@_) {
97 22         30 $FD = shift;
98 22         73 $COLOR = -t $FD;
99             }
100 23         39 return $FD;
101             }
102              
103             log_fd(\*STDERR);
104              
105             1;
106              
107             package Log::Log4Cli::Exception;
108              
109 6     6   128 use 5.006;
  6         20  
110 6     6   31 use strict;
  6         10  
  6         142  
111 6     6   32 use warnings;
  6         9  
  6         219  
112              
113 6     6   1930 use overload '""' => \&as_string;
  6         1452  
  6         58  
114              
115             sub as_string {
116 4     4   3486 $_[0]->{ERR_MESSAGE};
117             }
118              
119             1;
120              
121             __END__