From: Arthur Axel 'fREW' Schmidt Date: Thu, 4 Aug 2011 02:56:08 +0000 (-0500) Subject: Use Exporter::Declare for importing X-Git-Tag: v0.004000~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=675503c74661f1c5ee6740acece40fcdd6b4d260;p=p5sagit%2FLog-Contextual.git Use Exporter::Declare for importing --- diff --git a/Makefile.PL b/Makefile.PL index 4b0cf68..53118e3 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -6,5 +6,6 @@ use warnings FATAL => 'all'; perl_version '5.006'; all_from 'lib/Log/Contextual.pm'; requires 'Data::Dumper::Concise'; +requires 'Exporter::Declare'; WriteAll; diff --git a/lib/Log/Contextual.pm b/lib/Log/Contextual.pm index 23c7aff..550baae 100644 --- a/lib/Log/Contextual.pm +++ b/lib/Log/Contextual.pm @@ -7,12 +7,11 @@ our $VERSION = '0.00305'; my @levels = qw(debug trace warn info error fatal); -require Exporter; +use Exporter::Declare; +use Exporter::Declare::Export::Generator; use Data::Dumper::Concise; use Scalar::Util 'blessed'; -BEGIN { our @ISA = qw(Exporter) } - my @dlog = ((map "Dlog_$_", @levels), (map "DlogS_$_", @levels)); my @log = ((map "log_$_", @levels), (map "logS_$_", @levels)); @@ -23,36 +22,64 @@ eval { Log::Log4perl->wrapper_register(__PACKAGE__) }; -our @EXPORT_OK = ( +exports ( @dlog, @log, qw( set_logger with_logger ) ); -our %EXPORT_TAGS = ( - dlog => \@dlog, - log => \@log, - all => [@dlog, @log], -); +export_tag dlog => @dlog; +export_tag log => @log; +import_arguments qw(logger package_logger default_logger); + +sub before_import { + my ($class, $importer, $spec) = @_; -sub import { - my $package = shift; die 'Log::Contextual does not have a default import list' - unless @_; - - for my $idx ( 0 .. $#_ ) { - my $val = $_[$idx]; - if ( defined $val && $val eq '-logger' ) { - set_logger($_[$idx + 1]); - splice @_, $idx, 2; - } elsif ( defined $val && $val eq '-package_logger' ) { - _set_package_logger_for(scalar caller, $_[$idx + 1]); - splice @_, $idx, 2; - } elsif ( defined $val && $val eq '-default_logger' ) { - _set_default_logger_for(scalar caller, $_[$idx + 1]); - splice @_, $idx, 2; + if $spec->config->{default}; + + my @levels = qw(debug trace warn info error fatal); + if ( my $levels = $spec->config->{levels} ) { + @levels = @$levels + } + for my $level (@levels) { + if ($spec->config->{log}) { + $spec->add_export("&log_$level", sub (&@) { + _do_log( $level => _get_logger( caller ), shift @_, @_) + }); + $spec->add_export("&logS_$level", sub (&@) { + _do_logS( $level => _get_logger( caller ), $_[0], $_[1]) + }); + } + if ($spec->config->{dlog}) { + $spec->add_export("&Dlog_$level", sub (&@) { + my ($code, @args) = @_; + return _do_log( $level => _get_logger( caller ), sub { + local $_ = (@args?Data::Dumper::Concise::Dumper @args:'()'); + $code->(@_) + }, @args ); + }); + $spec->add_export("&DlogS_$level", sub (&$) { + my ($code, $ref) = @_; + _do_logS( $level => _get_logger( caller ), sub { + local $_ = Data::Dumper::Concise::Dumper $ref; + $code->($ref) + }, $ref ) + }); } } - $package->export_to_level(1, $package, @_); +} + +sub after_import { + my ($class, $importer, $specs) = @_; + + set_logger( $specs->config->{logger} ) + if $specs->config->{logger}; + + _set_package_logger_for( $importer, $specs->config->{package_logger} ) + if $specs->config->{package_logger}; + + _set_default_logger_for( $importer, $specs->config->{default_logger} ) + if $specs->config->{default_logger}; } our $Get_Logger; @@ -135,34 +162,6 @@ sub _do_logS { $value } -for my $level (@levels) { - no strict 'refs'; - - *{"log_$level"} = sub (&@) { - _do_log( $level => _get_logger( caller ), shift @_, @_) - }; - - *{"logS_$level"} = sub (&$) { - _do_logS( $level => _get_logger( caller ), $_[0], $_[1]) - }; - - *{"Dlog_$level"} = sub (&@) { - my ($code, @args) = @_; - return _do_log( $level => _get_logger( caller ), sub { - local $_ = (@args?Data::Dumper::Concise::Dumper @args:'()'); - $code->(@_) - }, @args ); - }; - - *{"DlogS_$level"} = sub (&$) { - my ($code, $ref) = @_; - _do_logS( $level => _get_logger( caller ), sub { - local $_ = Data::Dumper::Concise::Dumper $ref; - $code->($ref) - }, $ref ) - }; -} - 1; __END__