File Coverage

blib/lib/Log/Sprintf.pm
Criterion Covered Total %
statement 55 55 100.0
branch 16 16 100.0
condition 6 7 85.7
subroutine 17 17 100.0
pod 6 9 66.6
total 100 104 96.1


line stmt bran cond sub pod time code
1             package Log::Sprintf;
2             {
3             $Log::Sprintf::VERSION = '0.001002';
4             }
5              
6             # ABSTRACT: Format strings the way Log::log4perl does, without all the weight
7              
8 2     2   65562 use strict;
  2         5  
  2         70  
9 2     2   11 use warnings;
  2         3  
  2         56  
10              
11 2     2   30937 use String::Formatter;
  2         180575  
  2         22  
12 2     2   3508 use syntax 'junction';
  2         64825  
  2         14  
13              
14             my %codes = (
15             C => 'package',
16             c => 'category',
17             d => 'date',
18             F => 'file',
19             H => 'host',
20             L => 'line',
21             l => 'location',
22             M => 'subroutine',
23             m => 'message',
24             n => 'newline',
25             P => 'pid',
26             p => 'priority',
27             r => 'milliseconds_since_start',
28             R => 'milliseconds_since_last_log',
29             T => 'stacktrace',
30             );
31              
32 5   50 5 1 2122 sub new { bless $_[1]||{}, $_[0] }
33              
34             sub _formatter {
35 13     13   16 my $self = shift;
36 13 100       35 unless (defined $self->{formatter}) {
37 5         17 $self->{formatter} = String::Formatter->new({
38             input_processor => 'require_single_input',
39             string_replacer => 'method_replace',
40             codes => $self->_codes,
41             });
42             }
43 13         350 return $self->{formatter}
44             }
45              
46             sub sprintf {
47 13     13 1 2533 my ($self, $args) = @_;
48              
49 13         39 local @{$self}{keys %$args} = values %$args;
  13         41  
50              
51 13         37 $self->_formatter->format($self->{format}, $self);
52             }
53              
54 4     4 0 55 sub codes { +{} }
55              
56 5     5   23 sub _codes { return { %codes, %{$_[0]->codes} } }
  5         15  
57              
58             {
59 2     2   37948 no strict 'refs';
  2         5  
  2         1373  
60             for my $name (
61             grep { $_ eq none(qw( stacktrace date message priority newline location )) }
62             values %codes
63             ) {
64             *{$name} = sub {
65 2 100   2   78 die "you forgot to pass $name" unless exists $_[0]->{$name};
66 1         4 $_[0]->{$name}
67             }
68             }
69             }
70              
71             sub date {
72 2 100   2 0 136 die 'you forgot to pass date' unless exists $_[0]->{date};
73              
74 1         2 my @date = @{$_[0]->{date}};
  1         4  
75 1         4 splice @date, 6, 3;
76 1         3 $date[-1] += 1900;
77 1         2 $date[-2]++;
78 1         12 CORE::sprintf '%04i-%02i-%02i %02i:%02i:%02i', reverse @date
79             }
80              
81             sub message {
82 8 100   8 1 379 die 'you forgot to pass message' unless exists $_[0]->{message};
83 7         19 my $self = shift;
84 7         10 my $chomp = shift;
85 7         10 my $m = $self->{message};
86              
87 7 100 100     28 chomp $m if defined $chomp && $chomp eq 'chomp';
88              
89 7         31 $m
90             }
91              
92             sub priority {
93 3 100   3 1 263 die 'you forgot to pass priority' unless exists $_[0]->{priority};
94 2         3 my $self = shift;
95 2         3 my $skinny = shift;
96 2         3 my $p = $self->{priority};
97              
98 2 100       8 return substr $p, 0, 1 if $skinny;
99 1         12 $p;
100             }
101              
102             sub location {
103             exists $_[0]->{$_} or die "you forgot to pass $_"
104 2   100 2 1 158 for qw(subroutine file line);
105 1         7 "$_[0]->{subroutine} ($_[0]->{file}:$_[0]->{line})"
106             }
107              
108             sub newline() { "\n" }
109              
110             sub stacktrace {
111 2 100   2 0 73 die 'you forgot to pass stacktrace' unless exists $_[0]->{stacktrace};
112 1         2 my $s = $_[0]->{stacktrace};
113 1         16 "$s->[0][1] line $s->[0][2]\n" .
114             join "\n",
115             map "\t$_->[3] called at $_->[1] line $_->[2]",
116 1         5 @{$s}[1..$#$s] # all but the first level
117             }
118              
119 1     1 1 19 sub format { $_[0]->{format} }
120              
121             1;
122              
123             __END__