On Tue, 19 Jun 2001, Doug MacEachern wrote:

> On Wed, 20 Jun 2001, Stas Bekman wrote:
>
> > $ perl -MModPerl::Trace=error,dumper,warning,notice,debug,todo -lwe \
> > 'error "error"; warning "warning"; notice "notice"; debug "debug"; \
> > todo "todo";  dumper [1..3]'
>
> cool.  howbout instead of a dumper function, have the trace
> function(s) dump any args that are ref()'s ?

Can you give me an example of how would you want to use those? Why would
you want to have notify functions have this capability.

Something like this?

my $expand = sub {
  HAS_DUMPER ?
    map { ref $_ ? Data::Dumper::Dumper($_) : $_ } @_ :
    @_;
}

sub c_trace {
     my $level = shift;
     my $fh = \*STDERR; #could configure to something else
     print $fh $colors{$level},
             join("\n", $expand->(@_), ''), $colors{reset};
 }

> > is this OK? Apache-Test becomes dependant on ModPerl::, or was it
> > dependant already?
>
> no, Apache-Test must be independant of Apache-Test/../, but
> modperl-2.0 can depend on Apache-Test. so something like
> Apache::TestTrace would be ok.

I'm thinking about Ken's suggestion to have this code make its way into
Test:: . I think the main problem is back-compatibility. Authors relying
on Test:: will have to require the new version of it, in order for the new
code using these idioms to work. Though I'll ask Michael.

So shell we start with Apache::TestTrace for now?

> a few suggestions that would make it easier for things like supporting
> dumper() of args passed to trace functions, adding new levels, etc..

Yeah, sounds cool, you have beaten me to the code's beauty :)

> package ModPerl::Trace;
>
> use strict;
>
> use Exporter ();
> use vars qw(@Levels @Utils); #vars.pm is still useful after all
>
> BEGIN {
>     @Levels = qw(warning debug notice error todo);
>     @Utils  = qw(dumper);
> }
>
> our @ISA = qw(Exporter);
> our @EXPORT = (@Levels, @Utils);
> our $VERSION = '0.01';
>
> use subs @Levels;
>
> use constant HAS_COLOR => eval {
>     require Term::ANSIColor;
> };
>
> use constant HAS_DUMPER => eval {
>     require Data::Dumper;
> };
>
> my %colors = ();
>
> if (HAS_COLOR) {
>     $Term::ANSIColor::AUTORESET = 1;
>     %colors = (
>         error   => Term::ANSIColor::color('bold red'),
>         warning => Term::ANSIColor::color('blue'),
>         notice  => Term::ANSIColor::color('reset'),
>         debug   => Term::ANSIColor::color('green'),
>         todo    => Term::ANSIColor::color('underline'),
>         reset   => Term::ANSIColor::color('reset'),
>     );
> }
> else {
>     %colors = (
>         error   => '!!!',
>         warning => '***',
>         notice  => '   ',
>         debug   => '==>',
>         todo    => 'todo',
>     );
> }
>
> sub c_trace {
>     my $level = shift;
>     my $fh = \*STDERR; #could configure to something else
>     print $fh $colors{$level}, join("\n", @_, ''), $colors{reset};
> }
>
> sub nc_trace {
>     my $level = shift;
>     my $fh = \*STDERR; #could configure to something else
>     print $fh map { sprintf "%-4s: %s\n", $colors{$level}, $_ } @_;
> }
>
> {
>     my $trace = HAS_COLOR ? \&c_trace : \&nc_trace;
>
>     for my $level (@Levels) {
>         no strict 'refs';
>         *$level = sub { $trace->($level, @_) };
>     }
> }
>
> *dumper = HAS_DUMPER ?
>   sub { print STDERR +Data::Dumper::Dumper(@_) } :
>   sub { error "Install Data::Dumper to use dumper" };
>
> 1;
> __END__
>



_____________________________________________________________________
Stas Bekman              JAm_pH     --   Just Another mod_perl Hacker
http://stason.org/       mod_perl Guide  http://perl.apache.org/guide
mailto:[EMAIL PROTECTED]   http://apachetoday.com http://eXtropia.com/
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to