1 package Log::Message::Config;
4 use Params::Check qw[check];
7 use Locale::Maketext::Simple Style => 'gettext';
10 use vars qw[$VERSION $AUTOLOAD];
18 ### find out if the user specified a config file to use
19 ### and/or a default configuration object
20 ### and remove them from the argument hash
21 my %special = map { lc, delete $hash{$_} }
22 grep /^config|default$/i, keys %hash;
24 ### allow provided arguments to override the values from the config ###
26 private => { default => undef, },
27 verbose => { default => 1 },
28 tag => { default => 'NONE', },
29 level => { default => 'log', },
30 remove => { default => 0 },
31 chrono => { default => 1 },
34 my %lc_hash = map { lc, $hash{$_} } keys %hash;
37 if( $special{config} ) {
38 $file_conf = _read_config_file( $special{config} )
39 or ( warn( loc(q[Could not parse config file!]) ), return );
42 my $def_conf = \%{ $special{default} || {} };
44 ### make sure to only include keys that are actually defined --
45 ### the checker will assign even 'undef' if you have provided that
47 ### priorities goes as follows:
48 ### 1: arguments passed
49 ### 2: any config file passed
50 ### 3: any default config passed
51 my %to_check = map { @$_ }
52 grep { defined $_->[1] }
54 defined $lc_hash{$_} ? $lc_hash{$_} :
55 defined $file_conf->{$_} ? $file_conf->{$_} :
56 defined $def_conf->{$_} ? $def_conf->{$_} :
61 my $rv = check( $tmpl, \%to_check, 1 )
62 or ( warn( loc(q[Could not validate arguments!]) ), return );
64 return bless $rv, $class;
67 sub _read_config_file {
68 my $file = shift or return;
71 my $FH = new FileHandle;
72 $FH->open("$file") or (
73 warn(loc(q[Could not open config file '%1': %2],$file,$!)),
81 chomp; s/^\s*//; s/\s*$//;
83 my ($param,$val) = split /\s*=\s*/;
85 if( (lc $param) eq 'include' ) {
90 ### add these to the config hash ###
91 $conf->{ lc $param } = $val;
99 $AUTOLOAD =~ s/.+:://;
103 return $self->{ lc $AUTOLOAD } if exists $self->{ lc $AUTOLOAD };
105 die loc(q[No such accessor '%1' for class '%2'], $AUTOLOAD, ref $self);
118 Log::Message::Config - Configuration options for Log::Message
122 # This module is implicitly used by Log::Message to create a config
123 # which it uses to log messages.
124 # For the options you can pass, see the C<Log::Message new()> method.
126 # Below is a sample of a config file you could use
128 # comments are denoted by a single '#'
129 # use a shared stack, or have a private instance?
130 # if none provided, set to '0',
136 # default tag to set on new items
137 # if none provided, set to 'NONE'
140 # default level to handle items
141 # if none provided, set to 'log'
144 # extra files to include
145 # if none provided, no files are auto included
147 include = ../my/other/lib.pl
149 # automatically delete items
150 # when you retrieve them from the stack?
151 # if none provided, set to '0'
154 # retrieve errors in chronological order, or not?
155 # if none provided, set to '1'
160 Log::Message::Config provides a standardized config object for
161 Log::Message objects.
163 It can either read options as perl arguments, or as a config file.
164 See the Log::Message manpage for more information about what arguments
165 are valid, and see the Synopsis for an example config file you can use
169 L<Log::Message>, L<Log::Message::Item>, L<Log::Message::Handlers>
174 Jos Boumans E<lt>kane@cpan.orgE<gt>.
176 =head1 Acknowledgements
178 Thanks to Ann Barcomb for her suggestions.
183 copyright (c) 2002 Jos Boumans E<lt>kane@cpan.orgE<gt>.
186 This library is free software;
187 you may redistribute and/or modify it under the same
188 terms as Perl itself.
193 # c-indentation-style: bsd
195 # indent-tabs-mode: nil
197 # vim: expandtab shiftwidth=4: