File Coverage

blib/lib/Log/Sprintf.pm
Criterion Covered Total %
statement 61 61 100.0
branch 16 16 100.0
condition 6 7 85.7
subroutine 17 17 100.0
pod 6 9 66.6
total 106 110 96.3


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