File Coverage

blib/lib/Log/Agent/Tag/Priority.pm
Criterion Covered Total %
statement 26 26 100.0
branch 2 2 100.0
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 32 33 96.9


line stmt bran cond sub pod time code
1             ###########################################################################
2             #
3             # Priority.pm
4             #
5             # Copyright (C) 1999 Raphael Manfredi.
6             # Copyright (C) 2002-2015 Mark Rogaski, mrogaski@cpan.org;
7             # all rights reserved.
8             #
9             # See the README file included with the
10             # distribution for license information.
11             #
12             ##########################################################################
13              
14 1     1   5 use strict;
  1         2  
  1         44  
15              
16             ########################################################################
17             package Log::Agent::Tag::Priority;
18              
19             require Log::Agent::Tag::String;
20 1     1   4 use vars qw(@ISA);
  1         2  
  1         50  
21             @ISA = qw(Log::Agent::Tag::String);
22              
23 1     1   4 use Log::Agent::Priorities qw(level_from_prio prio_from_level);
  1         3  
  1         314  
24              
25             #
26             # ->make
27             #
28             # Creation routine.
29             #
30             # Calling arguments: a hash table list.
31             #
32             # The keyed argument list may contain:
33             # -POSTFIX whether to postfix log message or prefix it.
34             # -SEPARATOR separator string to use between tag and message
35             # -DISPLAY a string like '[$priority:$level])'
36             # -PRIORITY the log priority string, e.g. "warning".
37             # -LEVEL the log level value, e.g. 4.
38             #
39             # Attributes:
40             # none, besides the inherited ones
41             #
42             sub make {
43 4     4 0 7 my $type = shift;
44 4         46 my (%args) = @_;
45 4         7 my $separator = " ";
46 4         7 my $postfix = 0;
47 4         6 my ($display, $priority, $level);
48              
49 4         22 my %set = (
50             -display => \$display,
51             -postfix => \$postfix,
52             -separator => \$separator,
53             -priority => \$priority,
54             -level => \$level,
55             );
56              
57 4         50 while (my ($arg, $val) = each %args) {
58 16         25 my $vset = $set{lc($arg)};
59 16 100       51 next unless ref $vset;
60 12         43 $$vset = $val;
61             }
62              
63             #
64             # Normalize $priority to the full name (e.g. "err" -> "error")
65             #
66              
67 4         95 $priority = prio_from_level level_from_prio $priority;
68              
69             #
70             # Format according to -display specs.
71             #
72             # Since priority and level are fixed for this object, the resulting
73             # string need only be computed once, i.e. now.
74             #
75             # The following variables are recognized:
76             #
77             # $priority priority name (e.g. "warning")
78             # $level logging level
79             #
80             # We recognize both $level and ${level}.
81             #
82              
83 4         16 $display =~ s/\$priority\b/$priority/g;
84 4         7 $display =~ s/\$\{priority}/$priority/g;
85 4         11 $display =~ s/\$level\b/$level/g;
86 4         8 $display =~ s/\$\{level}/$level/g;
87              
88             #
89             # Now create the constant tag string.
90             #
91              
92 4         26 my $self = Log::Agent::Tag::String->make(
93             -name => "priority",
94             -value => $display,
95             -postfix => $postfix,
96             -separator => $separator,
97             );
98              
99 4         103 return bless $self, $type; # re-blessed in our package
100             }
101              
102             1; # for "require"
103             __END__