File Coverage

lib/Log/Message/Config.pm
Criterion Covered Total %
statement 61 62 98.3
branch 21 26 80.7
condition n/a
subroutine 12 12 100.0
pod 0 1 0.0
total 94 101 93.0


line stmt bran cond sub pod time code
1             package Log::Message::Config;
2 2     2   3727 use if $] > 5.017, 'deprecate';
  2         30  
  2         13  
3 2     2   2226 use strict;
  2         6  
  2         70  
4              
5 2     2   1317 use Params::Check qw[check];
  2         5384  
  2         162  
6 2     2   1961 use Module::Load;
  2         2414  
  2         14  
7 2     2   2192 use FileHandle;
  2         28700  
  2         17  
8 2     2   951 use Locale::Maketext::Simple Style => 'gettext';
  2         4  
  2         20  
9              
10             BEGIN {
11 2     2   1186 use vars qw[$VERSION $AUTOLOAD];
  2         5  
  2         128  
12 2     2   1681 $VERSION = '0.08';
13             }
14              
15             sub new {
16 9     9 0 15 my $class = shift;
17 9         32 my %hash = @_;
18              
19             ### find out if the user specified a config file to use
20             ### and/or a default configuration object
21             ### and remove them from the argument hash
22 9         52 my %special = map { lc, delete $hash{$_} }
  9         39  
23             grep /^config|default$/i, keys %hash;
24              
25             ### allow provided arguments to override the values from the config ###
26 9         84 my $tmpl = {
27             private => { default => undef, },
28             verbose => { default => 1 },
29             tag => { default => 'NONE', },
30             level => { default => 'log', },
31             remove => { default => 0 },
32             chrono => { default => 1 },
33             };
34              
35 9         21 my %lc_hash = map { lc, $hash{$_} } keys %hash;
  11         33  
36              
37 9         15 my $file_conf;
38 9 100       26 if( $special{config} ) {
39 2 50       7 $file_conf = _read_config_file( $special{config} )
40             or ( warn( loc(q[Could not parse config file!]) ), return );
41             }
42              
43 9 100       15 my $def_conf = \%{ $special{default} || {} };
  9         37  
44              
45             ### make sure to only include keys that are actually defined --
46             ### the checker will assign even 'undef' if you have provided that
47             ### as a value
48             ### priorities goes as follows:
49             ### 1: arguments passed
50             ### 2: any config file passed
51             ### 3: any default config passed
52 41         90 my %to_check = map { @$_ }
  54         104  
53 54 100       225 grep { defined $_->[1] }
    100          
    100          
54 9         35 map { [ $_ =>
55             defined $lc_hash{$_} ? $lc_hash{$_} :
56             defined $file_conf->{$_} ? $file_conf->{$_} :
57             defined $def_conf->{$_} ? $def_conf->{$_} :
58             undef
59             ]
60             } keys %$tmpl;
61              
62 9 50       62 my $rv = check( $tmpl, \%to_check, 1 )
63             or ( warn( loc(q[Could not validate arguments!]) ), return );
64              
65 9         1438 return bless $rv, $class;
66             }
67              
68             sub _read_config_file {
69 2 50   2   6 my $file = shift or return;
70              
71 2         3 my $conf = {};
72 2         16 my $FH = new FileHandle;
73 2 50       80 $FH->open("$file", 'r') or (
74             warn(loc(q[Could not open config file '%1': %2],$file,$!)),
75             return {}
76             );
77              
78 2         2159 while(<$FH>) {
79 60 100       224 next if /\s*#/;
80 28 100       99 next unless /\S/;
81              
82 14         24 chomp; s/^\s*//; s/\s*$//;
  14         36  
  14         197  
83              
84 14         61 my ($param,$val) = split /\s*=\s*/;
85              
86 14 100       40 if( (lc $param) eq 'include' ) {
87 2         11 load $val;
88 2         834 next;
89             }
90              
91             ### add these to the config hash ###
92 12         77 $conf->{ lc $param } = $val;
93             }
94 2         24 close $FH;
95              
96 2         14 return $conf;
97             }
98              
99             sub AUTOLOAD {
100 80     80   335 $AUTOLOAD =~ s/.+:://;
101              
102 80         113 my $self = shift;
103              
104 80 50       760 return $self->{ lc $AUTOLOAD } if exists $self->{ lc $AUTOLOAD };
105              
106 0         0 die loc(q[No such accessor '%1' for class '%2'], $AUTOLOAD, ref $self);
107             }
108              
109 14     14   15167 sub DESTROY { 1 }
110              
111             1;
112              
113             __END__