log4perl icon indicating copy to clipboard operation
log4perl copied to clipboard

Would Like Auto-Instrumentation of Subroutines

Open dwmarshall opened this issue 11 years ago • 2 comments

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.

dwmarshall avatar Jul 01 '14 18:07 dwmarshall

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! :)

mschilli avatar Jul 02 '14 05:07 mschilli

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.

mohawk2 avatar Sep 06 '20 22:09 mohawk2