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 ?
 
> 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.
a few suggestions that would make it easier for things like supporting
dumper() of args passed to trace functions, adding new levels, etc..
 
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__


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

Reply via email to