File Coverage

blib/lib/Log/Any/Adapter/Util.pm
Criterion Covered Total %
statement 58 61 95.0
branch 6 8 75.0
condition 1 3 33.3
subroutine 20 22 90.9
pod 11 11 100.0
total 96 105 91.4


line stmt bran cond sub pod time code
1 25     25   155765 use 5.008001;
  25         102  
2 25     25   131 use strict;
  25         58  
  25         532  
3 25     25   142 use warnings;
  25         118  
  25         1731  
4              
5             package Log::Any::Adapter::Util;
6              
7             # ABSTRACT: Common utility functions for Log::Any
8             our $VERSION = '1.716';
9              
10 25     25   201 use Exporter;
  25         55  
  25         2285  
11             our @ISA = qw/Exporter/;
12              
13             my %LOG_LEVELS;
14             BEGIN {
15 25     25   948 %LOG_LEVELS = (
16             EMERGENCY => 0,
17             ALERT => 1,
18             CRITICAL => 2,
19             ERROR => 3,
20             WARNING => 4,
21             NOTICE => 5,
22             INFO => 6,
23             DEBUG => 7,
24             TRACE => 8,
25             );
26             }
27              
28 25     25   268 use constant \%LOG_LEVELS;
  25         59  
  25         8452  
29              
30             our @EXPORT_OK = qw(
31             cmp_deeply
32             detection_aliases
33             detection_methods
34             dump_one_line
35             log_level_aliases
36             logging_aliases
37             logging_and_detection_methods
38             logging_methods
39             make_method
40             numeric_level
41             read_file
42             require_dynamic
43             );
44              
45             push @EXPORT_OK, keys %LOG_LEVELS;
46              
47             our %EXPORT_TAGS = ( 'levels' => [ keys %LOG_LEVELS ] );
48              
49             my ( %LOG_LEVEL_ALIASES, @logging_methods, @logging_aliases, @detection_methods,
50             @detection_aliases, @logging_and_detection_methods );
51              
52             BEGIN {
53 25     25   205 %LOG_LEVEL_ALIASES = (
54             inform => 'info',
55             warn => 'warning',
56             err => 'error',
57             crit => 'critical',
58             fatal => 'critical'
59             );
60 25         138 @logging_methods =
61             qw(trace debug info notice warning error critical alert emergency);
62 25         111 @logging_aliases = keys(%LOG_LEVEL_ALIASES);
63 25         75 @detection_methods = map { "is_$_" } @logging_methods;
  225         496  
64 25         67 @detection_aliases = map { "is_$_" } @logging_aliases;
  125         240  
65 25         7132 @logging_and_detection_methods = ( @logging_methods, @detection_methods );
66             }
67              
68             #pod =sub logging_methods
69             #pod
70             #pod Returns a list of all logging method. E.g. "trace", "info", etc.
71             #pod
72             #pod =cut
73              
74 80     80 1 21742 sub logging_methods { @logging_methods }
75              
76             #pod =sub detection_methods
77             #pod
78             #pod Returns a list of detection methods. E.g. "is_trace", "is_info", etc.
79             #pod
80             #pod =cut
81              
82 38     38 1 3324 sub detection_methods { @detection_methods }
83              
84             #pod =sub logging_and_detection_methods
85             #pod
86             #pod Returns a list of logging and detection methods (but not aliases).
87             #pod
88             #pod =cut
89              
90 33     33 1 219 sub logging_and_detection_methods { @logging_and_detection_methods }
91              
92             #pod =sub log_level_aliases
93             #pod
94             #pod Returns key/value pairs mapping aliases to "official" names. E.g. "err" maps
95             #pod to "error".
96             #pod
97             #pod =cut
98              
99 50     50 1 308 sub log_level_aliases { %LOG_LEVEL_ALIASES }
100              
101             #pod =sub logging_aliases
102             #pod
103             #pod Returns a list of logging alias names. These are the keys from
104             #pod L.
105             #pod
106             #pod =cut
107              
108 0     0 1 0 sub logging_aliases { @logging_aliases }
109              
110             #pod =sub detection_aliases
111             #pod
112             #pod Returns a list of detection aliases. E.g. "is_err", "is_fatal", etc.
113             #pod
114             #pod =cut
115              
116 0     0 1 0 sub detection_aliases { @detection_aliases }
117              
118             #pod =sub numeric_level
119             #pod
120             #pod Given a level name (or alias), returns the numeric value described above under
121             #pod log level constants. E.g. "err" would return 3.
122             #pod
123             #pod =cut
124              
125             sub numeric_level {
126 508     508 1 927 my ($level) = @_;
127             my $canonical =
128 508 100       1156 exists $LOG_LEVEL_ALIASES{ lc $level } ? $LOG_LEVEL_ALIASES{ lc $level } : $level;
129 508         1220 return $LOG_LEVELS{ uc($canonical) };
130             }
131              
132             #pod =sub dump_one_line
133             #pod
134             #pod Given a reference, returns a one-line L dump with keys sorted.
135             #pod
136             #pod =cut
137              
138             # lazy trampoline to load Data::Dumper only on demand but then not try to
139             # require it pointlessly each time
140             *dump_one_line = sub {
141 10     10   7234 require Data::Dumper;
142              
143             my $dumper = sub {
144 26     26   1025 my ($value) = @_;
145              
146 26         127 return Data::Dumper->new( [$value] )->Indent(0)->Sortkeys(1)->Quotekeys(0)
147             ->Terse(1)->Useqq(1)->Dump();
148 10         68457 };
149              
150 10         47 my $string = $dumper->(@_);
151 25     25   198 no warnings 'redefine';
  25         48  
  25         2810  
152 10         953 *dump_one_line = $dumper;
153 10         59 return $string;
154             };
155              
156             #pod =sub make_method
157             #pod
158             #pod Given a method name, a code reference and a package name, installs the code
159             #pod reference as a method in the package.
160             #pod
161             #pod =cut
162              
163             sub make_method {
164 54     54 1 235 my ( $method, $code, $pkg ) = @_;
165              
166 54   33     248 $pkg ||= caller();
167 25     25   197 no strict 'refs';
  25         70  
  25         7334  
168 54         69 *{ $pkg . "::$method" } = $code;
  54         328  
169             }
170              
171             #pod =sub require_dynamic (DEPRECATED)
172             #pod
173             #pod Given a class name, attempts to load it via require unless the class
174             #pod already has a constructor available. Throws an error on failure. Used
175             #pod internally and may become private in the future.
176             #pod
177             #pod =cut
178              
179             sub require_dynamic {
180 64     64 1 129 my ($class) = @_;
181              
182 64 100       487 return 1 if $class->can('new'); # duck-type that class is loaded
183              
184 10 50       692 unless ( defined( eval "require $class; 1" ) )
185             { ## no critic (ProhibitStringyEval)
186 0         0 die $@;
187             }
188             }
189              
190             #pod =sub read_file (DEPRECATED)
191             #pod
192             #pod Slurp a file. Does *not* apply any layers. Used for testing and may
193             #pod become private in the future.
194             #pod
195             #pod =cut
196              
197             sub read_file {
198 4     4 1 13 my ($file) = @_;
199              
200 4         19 local $/ = undef;
201 4 50       155 open( my $fh, '<:utf8', $file ) ## no critic
202             or die "cannot open '$file': $!";
203 4         138 my $contents = <$fh>;
204 4         98 return $contents;
205             }
206              
207             #pod =sub cmp_deeply (DEPRECATED)
208             #pod
209             #pod Compares L results for two references. Also takes a test
210             #pod label as a third argument. Used for testing and may become private in the
211             #pod future.
212             #pod
213             #pod =cut
214              
215             sub cmp_deeply {
216 4     4 1 77 my ( $ref1, $ref2, $name ) = @_;
217              
218 4         23 my $tb = Test::Builder->new();
219 4         30 $tb->is_eq( dump_one_line($ref1), dump_one_line($ref2), $name );
220             }
221              
222             # 0.XX version loaded Log::Any and some adapters relied on this happening
223             # behind the scenes. Since Log::Any now uses this module, we load Log::Any
224             # via require after compilation to mitigate circularity.
225             require Log::Any;
226              
227             1;
228              
229              
230             # vim: ts=4 sts=4 sw=4 et tw=75:
231              
232             __END__