Mercurial > pub > Impl
diff lib/IMPL/Debug.pm @ 422:b0481c071bea ref20150831
IMPL::Config::Container tests, YAMLConfiguration now works and tested
author | cin |
---|---|
date | Sun, 20 Aug 2017 00:20:41 +0300 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/IMPL/Debug.pm Sun Aug 20 00:20:41 2017 +0300 @@ -0,0 +1,66 @@ +package IMPL::Debug; +use strict; +use warnings; + +our $ENABLE = 0; +our %ENABLE; + +my %subscriptions; +my @subscriptions; + +sub stub { } + +sub import { + my ( $self, @args ) = @_; + + my $caller = caller; + no strict 'refs'; + + my $enabled = exists $ENABLE{$caller} ? $ENABLE{$caller} : $ENABLE; + + *{"${caller}::dbg_log"} = $enabled + ? sub { + $self->log( $caller, @_ ); + } + : \&stub; + + *{"${caller}::dbg_error"} = $enabled + ? sub { + $self->log( $caller, @_ ); + } + : \&stub; + + *{"${caller}::dbg_warn"} = $enabled + ? sub { + $self->log( $caller, @_ ); + } + : \&stub; +} + +sub log { + my $self = shift; + my $channel = shift; + $_->(@_) foreach @{ $subscriptions{$channel} || [] }; + $_->(@_) foreach @subscriptions; +} + +sub subscribe { + my ( $self, $channel, $callback ) = @_; + + if ( @_ == 2 ) { + $callback = $channel; + $channel = undef; + } + + die IMPL::InvalidArgumentException->new('callback') + unless ref $callback eq 'CODE'; + + if ($channel) { + push @{ $subscriptions{$channel} }, $callback; + } + else { + push @subscriptions, $callback; + } +} + +1;