| 1 | package Log::Log4perl::InternalDebug; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | use File::Temp qw(tempfile); | ||
| 6 | use File::Spec; | ||
| 7 | |||
| 8 | require Log::Log4perl::Resurrector; | ||
| 9 | |||
| 10 | ########################################### | ||
| 11 | sub enable { | ||
| 12 | ########################################### | ||
| 13 | unshift @INC, \&internal_debug_loader; | ||
| 14 | } | ||
| 15 | |||
| 16 | ################################################## | ||
| 17 | sub internal_debug_fh { | ||
| 18 | ################################################## | ||
| 19 | my($file) = @_; | ||
| 20 | |||
| 21 | local($/) = undef; | ||
| 22 | open FILE, "<$file" or die "Cannot open $file"; | ||
| 23 | my $text = <FILE>; | ||
| 24 | close FILE; | ||
| 25 | |||
| 26 | my($tmp_fh, $tmpfile) = tempfile( UNLINK => 1 ); | ||
| 27 | |||
| 28 | $text =~ s/_INTERNAL_DEBUG(?!\s*=>)/1/g; | ||
| 29 | |||
| 30 | print $tmp_fh $text; | ||
| 31 | seek $tmp_fh, 0, 0; | ||
| 32 | |||
| 33 | return $tmp_fh; | ||
| 34 | } | ||
| 35 | |||
| 36 | ########################################### | ||
| 37 | sub internal_debug_loader { | ||
| 38 | ########################################### | ||
| 39 | my ($code, $module) = @_; | ||
| 40 | |||
| 41 | # Skip non-Log4perl modules | ||
| 42 | if($module !~ m#^Log/Log4perl#) { | ||
| 43 | return undef; | ||
| 44 | } | ||
| 45 | |||
| 46 | my $path = $module; | ||
| 47 | if(!-f $path) { | ||
| 48 | $path = Log::Log4perl::Resurrector::pm_search( $module ); | ||
| 49 | } | ||
| 50 | |||
| 51 | my $fh = internal_debug_fh($path); | ||
| 52 | |||
| 53 | my $abs_path = File::Spec->rel2abs( $path ); | ||
| 54 | $INC{$module} = $abs_path; | ||
| 55 | |||
| 56 | return $fh; | ||
| 57 | } | ||
| 58 | |||
| 59 | ########################################### | ||
| 60 | sub resurrector_init { | ||
| 61 | ########################################### | ||
| 62 | unshift @INC, \&resurrector_loader; | ||
| 63 | } | ||
| 64 | |||
| 65 | ########################################### | ||
| 66 | sub import { | ||
| 67 | ########################################### | ||
| 68 | # enable it on import | ||
| 69 | enable(); | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; | ||
| 73 | |||
| 74 | __END__ |