File Coverage

blib/lib/Log/Minimal.pm
Criterion Covered Total %
statement 122 122 100.0
branch 36 40 90.0
condition 17 26 65.3
subroutine 28 28 100.0
pod 11 11 100.0
total 214 227 94.2


line stmt bran cond sub pod time code
1             package Log::Minimal;
2              
3 5     5   201896 use strict;
  5         16  
  5         267  
4 5     5   28 use warnings;
  5         9  
  5         233  
5 5     5   6548 use Term::ANSIColor qw//;
  5         82498  
  5         3580  
6              
7             our $VERSION = '0.19';
8             our @EXPORT = map { ($_.'f', $_.'ff') } qw/crit warn info debug croak/;
9             push @EXPORT, 'ddf';
10              
11             our $PRINT = sub {
12             my ( $time, $type, $message, $trace, $raw_message) = @_;
13             warn "$time [$type] $message at $trace\n";
14             };
15              
16             our $DIE = sub {
17             my ( $time, $type, $message, $trace, $raw_message) = @_;
18             die "$time [$type] $message at $trace\n";
19             };
20              
21             our $DEFAULT_COLOR = {
22             info => { text => 'green', },
23             debug => {
24             text => 'red',
25             background => 'white',
26             },
27             'warn' => {
28             text => 'black',
29             background => 'yellow',
30             },
31             'critical' => {
32             text => 'black',
33             background => 'red'
34             },
35             'error' => {
36             text => 'red',
37             background => 'black'
38             }
39             };
40              
41             if ($ENV{LM_DEFAULT_COLOR}) {
42             # LEVEL=FG;BG:LEVEL=FG;BG:...
43             for my $level_color (split /:/, $ENV{LM_DEFAULT_COLOR}) {
44             my($level, $color) = split /=/, $level_color, 2;
45             my($fg, $bg) = split /;/, $color, 2;
46             $Log::Minimal::DEFAULT_COLOR->{$level} = {
47             $fg ? (text => $fg) : (),
48             $bg ? (background => $bg) : (),
49             };
50             }
51             }
52              
53             our $ENV_DEBUG = "LM_DEBUG";
54             our $AUTODUMP = 0;
55             our $LOG_LEVEL = 'DEBUG';
56             our $TRACE_LEVEL = 0;
57             our $COLOR = $ENV{LM_COLOR} || 0;
58             our $ESCAPE_WHITESPACE = 1;
59              
60             my %log_level_map = (
61             DEBUG => 1,
62             INFO => 2,
63             WARN => 3,
64             CRITICAL => 4,
65             MUTE => 0,
66             ERROR => 99,
67             );
68              
69             sub import {
70 8     8   2206 my $class = shift;
71 8         21 my $package = caller(0);
72 8         19 my @args = @_;
73              
74 8         11 my %want_export;
75             my $env_debug;
76 8         40 while ( my $arg = shift @args ) {
77 2 100       5 if ( $arg eq 'env_debug' ) {
78 1         3 $env_debug = shift @args;
79             }
80             else {
81 1         3 $want_export{$arg} = 1;
82             }
83             }
84              
85 8 100       30 if ( ! keys %want_export ) {
86             #all
87 7         186 $want_export{$_} = 1 for @EXPORT;
88             }
89              
90 5     5   64 no strict 'refs';
  5         10  
  5         7433  
91 8         73 for my $f (grep !/^debug/, @EXPORT) {
92 72 100       191 if ( $want_export{$f} ) {
93 63         117 *{"$package\::$f"} = \&$f;
  63         258  
94             }
95             }
96              
97 8         18 for my $f (map { ($_.'f', $_.'ff') } qw/debug/) {
  8         30  
98 16 100       1813 if ( $want_export{$f} ) {
99 15 100       34 if ( $env_debug ) {
100 1         6 *{"$package\::$f"} = sub {
101 4     4   859 local $TRACE_LEVEL = $TRACE_LEVEL + 1;
102 4         7 local $ENV_DEBUG = $env_debug;
103 4         9 $f->(@_);
104 1         4 };
105             }
106             else {
107 14         31 *{"$package\::$f"} = \&$f;
  14         8736  
108             }
109             }
110             }
111              
112             }
113              
114             sub critf {
115 6     6 1 1088 _log( "CRITICAL", 0, @_ );
116             }
117              
118             sub warnf {
119 16     16 1 10549 _log( "WARN", 0, @_ );
120             }
121              
122             sub infof {
123 2     2 1 987 _log( "INFO", 0, @_ );
124             }
125              
126             sub debugf {
127 17 100 100 17 1 3110 return if !$ENV{$ENV_DEBUG} || $log_level_map{DEBUG} < $log_level_map{uc $LOG_LEVEL};
128 8         54 _log( "DEBUG", 0, @_ );
129             }
130              
131             sub critff {
132 1     1 1 553 _log( "CRITICAL", 1, @_ );
133             }
134              
135             sub warnff {
136 1     1 1 522 _log( "WARN", 1, @_ );
137             }
138              
139             sub infoff {
140 1     1 1 499 _log( "INFO", 1, @_ );
141             }
142              
143             sub debugff {
144 2 100 66 2 1 519 return if !$ENV{$ENV_DEBUG} || $log_level_map{DEBUG} < $log_level_map{uc $LOG_LEVEL};
145 1         4 _log( "DEBUG", 1, @_ );
146             }
147              
148             sub croakf {
149 4     4 1 8257 local $PRINT = $DIE;
150 4         141 local $LOG_LEVEL = 'DEBUG';
151 4         15 _log( "ERROR", 0, @_ );
152             }
153              
154             sub croakff {
155 2     2 1 1668 local $PRINT = $DIE;
156 2         3 local $LOG_LEVEL = 'DEBUG';
157 2         7 _log( "ERROR", 1, @_ );
158             }
159              
160             sub _log {
161 42     42   79 my $tag = shift;
162 42         52 my $full = shift;
163              
164 42   100     147 my $_log_level = $log_level_map{uc $LOG_LEVEL} || return;
165 40 100       112 return unless $log_level_map{$tag} >= $_log_level;
166              
167 39         1848 my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
168             localtime(time);
169 39         263 my $time = sprintf(
170             "%04d-%02d-%02dT%02d:%02d:%02d",
171             $year + 1900,
172             $mon + 1, $mday, $hour, $min, $sec
173             );
174              
175 39         52 my $trace;
176 39 100       93 if ( $full ) {
177 6         10 my $i=$TRACE_LEVEL+1;
178 6         11 my @stack;
179 6         53 while ( my @caller = caller($i) ) {
180 8         23 push @stack, "$caller[1] line $caller[2]";
181 8         43 $i++;
182             }
183 6         21 $trace = join ", ", @stack
184             }
185             else {
186 33         227 my @caller = caller($TRACE_LEVEL+1);
187 33         136 $trace = "$caller[1] line $caller[2]";
188             }
189              
190 39         55 my $messages = '';
191 39 100 66     573 if ( @_ == 1 && defined $_[0]) {
    50          
192 31 100       87 $messages = $AUTODUMP ? ''.Log::Minimal::Dumper->new($_[0]) : $_[0];
193             }
194             elsif ( @_ >= 2 ) {
195 8 100       17 $messages = sprintf(shift, map { $AUTODUMP ? Log::Minimal::Dumper->new($_) : $_ } @_);
  8         38  
196             }
197              
198 39 100       102 if ($ESCAPE_WHITESPACE) {
199 38         75 $messages =~ s/\x0d/\\r/g;
200 38         64 $messages =~ s/\x0a/\\n/g;
201 38         53 $messages =~ s/\x09/\\t/g;
202             }
203              
204 39         51 my $raw_message = $messages;
205 39 100       194 if ( $COLOR ) {
206 6 50       566 $messages = Term::ANSIColor::color($DEFAULT_COLOR->{lc($tag)}->{text})
207             . $messages . Term::ANSIColor::color("reset")
208             if $DEFAULT_COLOR->{lc($tag)}->{text};
209 6 50       255 $messages = Term::ANSIColor::color("on_".$DEFAULT_COLOR->{lc($tag)}->{background})
210             . $messages . Term::ANSIColor::color("reset")
211             if $DEFAULT_COLOR->{lc($tag)}->{background};
212             }
213              
214 39         285 $PRINT->(
215             $time,
216             $tag,
217             $messages,
218             $trace,
219             $raw_message
220             );
221             }
222              
223             sub ddf {
224 3     3 1 454 my $value = shift;
225 3         9 Log::Minimal::Dumper::dumper($value);
226             }
227              
228             1;
229              
230             package
231             Log::Minimal::Dumper;
232              
233 5     5   39 use strict;
  5         11  
  5         178  
234 5     5   28 use warnings;
  5         10  
  5         231  
235 5     5   37 use base qw/Exporter/;
  5         9  
  5         694  
236 5     5   9089 use Data::Dumper;
  5         106959  
  5         491  
237 5     5   86 use Scalar::Util qw/blessed/;
  5         10  
  5         687  
238              
239             use overload
240 5         49 '""' => \&stringfy,
241             '0+' => \&numeric,
242 5     5   77 fallback => 1;
  5         9  
243              
244             sub new {
245 4     4   7 my ($class, $value) = @_;
246 4         81 bless \$value, $class;
247             }
248              
249             sub stringfy {
250 3     3   6 my $self = shift;
251 3         8 my $value = $$self;
252 3 100 66     24 if ( blessed($value) && (my $stringify = overload::Method( $value, '""' ) || overload::Method( $value, '0+' )) ) {
      66        
253 2         2644 $value = $stringify->($value);
254             }
255 3         142 dumper($value);
256             }
257              
258             sub numeric {
259 1     1   3 my $self = shift;
260 1         2 my $value = $$self;
261 1 50 33     10 if ( blessed($value) && (my $numeric = overload::Method( $value, '0+' ) || overload::Method( $value, '""' )) ) {
      33        
262 1         42 $value = $numeric->($value);
263             }
264 1         52 $value;
265             }
266              
267             sub dumper {
268 6     6   8 my $value = shift;
269 6 100 66     74 if ( defined $value && ref($value) ) {
270 3         5 local $Data::Dumper::Terse = 1;
271 3         6 local $Data::Dumper::Indent = 0;
272 3         5 local $Data::Dumper::Sortkeys = 1;
273 3         16 $value = Data::Dumper::Dumper($value);
274             }
275 6         251 $value;
276             }
277              
278              
279             1;
280             __END__