mschilli / log4perl

Log4j Implementation For Perl
http://log4perl.com
Other
116 stars 67 forks source link

Would Like Auto-Instrumentation of Subroutines #47

Open dwmarshall opened 10 years ago

dwmarshall commented 10 years ago

Particularly when I am debugging a module that I haven't maintained before, I'd like to automatically instrument all the subroutines. Here's what I'd like to do:

package Foo::Bar;

use Log::Log4perl qw(:easy);

sub A {}

sub B {}

Log::Log4perl::auto_instrument();
1;

This would look through the symbol table for Foo::Bar and replace each subroutine something like this:

*new_A = \&A;
sub A {
   DEBUG "entering A, arguments are : " . join(' ', @_);
  new_A(@_);
  DEBUG "leaving A";
}

Mike, I wanted to get your thoughts before contributing any code changes.

mschilli commented 10 years ago

Sounds like a great idea! Here's a few things you might find useful:

First off, there's a little known feature in the built-in perl debugger that lets you do something similar, so if you have something like

for my $i (1..3) {
    foo($i);
}

sub foo {
    print "$_[0]\n";
}

and run the script via

PERLDB_OPTS="NonStop=1 AutoTrace=1 frame=4" perl -dS scriptname

you'll get

8:      for my $i (1..3) {
9:          foo($i);
in  .=main::foo(1) from ./t:9
 13:        print "$_[0]\n";
1
9:          foo($i);
in  .=main::foo(2) from ./t:9
 13:        print "$_[0]\n";
2
9:          foo($i);
in  .=main::foo(3) from ./t:9
 13:        print "$_[0]\n";
3

but unfortunately, that doesn't provide the convenience Log4perl users take for granted :). As for Log4perl, many, many years ago (11 to be precise), someone on CPAN set out to create a Log4perl extension to accomplish something similar:

http://search.cpan.org/~jcromie/Log-Log4perl-AutoCategorize-0.03/

Unfortunately, this module didn't stand the test of time and looks broken today, but you might be able to extract some ideas.

Keep the pull request coming! :)

mohawk2 commented 4 years ago

Another bit of existing art that leverages the Perl debugger is https://metacpan.org/pod/Devel::TraceRun. You might request a takeover of the AutoCategorize module, and then get it back to working.