| 1 | package Log::Log4perl::Resurrector; | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | |||
| 5 | # [rt.cpan.org #84818] | ||
| 6 | use if $^O eq "MSWin32", "Win32"; | ||
| 7 | |||
| 8 | use File::Temp qw(tempfile); | ||
| 9 | use File::Spec; | ||
| 10 | |||
| 11 | use constant INTERNAL_DEBUG => 0; | ||
| 12 | |||
| 13 | our $resurrecting = ''; | ||
| 14 | |||
| 15 | ########################################### | ||
| 16 | sub import { | ||
| 17 | ########################################### | ||
| 18 | resurrector_init(); | ||
| 19 | } | ||
| 20 | |||
| 21 | ################################################## | ||
| 22 | sub resurrector_fh { | ||
| 23 | ################################################## | ||
| 24 | my($file) = @_; | ||
| 25 | |||
| 26 | local($/) = undef; | ||
| 27 | open FILE, "<$file" or die "Cannot open $file"; | ||
| 28 | my $text = <FILE>; | ||
| 29 | close FILE; | ||
| 30 | |||
| 31 | print "Read ", length($text), " bytes from $file\n" if INTERNAL_DEBUG; | ||
| 32 | |||
| 33 | my($tmp_fh, $tmpfile) = tempfile( UNLINK => 1 ); | ||
| 34 | print "Opened tmpfile $tmpfile\n" if INTERNAL_DEBUG; | ||
| 35 | |||
| 36 | $text =~ s/^\s*###l4p//mg; | ||
| 37 | |||
| 38 | print "Text=[$text]\n" if INTERNAL_DEBUG; | ||
| 39 | |||
| 40 | print $tmp_fh $text; | ||
| 41 | seek $tmp_fh, 0, 0; | ||
| 42 | |||
| 43 | return $tmp_fh; | ||
| 44 | } | ||
| 45 | |||
| 46 | ########################################### | ||
| 47 | sub resurrector_loader { | ||
| 48 | ########################################### | ||
| 49 | my ($code, $module) = @_; | ||
| 50 | |||
| 51 | print "resurrector_loader called with $module\n" if INTERNAL_DEBUG; | ||
| 52 | |||
| 53 | # Avoid recursion | ||
| 54 | if($resurrecting eq $module) { | ||
| 55 | print "ignoring $module (recursion)\n" if INTERNAL_DEBUG; | ||
| 56 | return undef; | ||
| 57 | } | ||
| 58 | |||
| 59 | local $resurrecting = $module; | ||
| 60 | |||
| 61 | |||
| 62 | # Skip Log4perl appenders | ||
| 63 | if($module =~ m#^Log/Log4perl/Appender#) { | ||
| 64 | print "Ignoring $module (Log4perl-internal)\n" if INTERNAL_DEBUG; | ||
| 65 | return undef; | ||
| 66 | } | ||
| 67 | |||
| 68 | my $path = $module; | ||
| 69 | |||
| 70 | # Skip unknown files | ||
| 71 | if(!-f $module) { | ||
| 72 | # We might have a 'use lib' statement that modified the | ||
| 73 | # INC path, search again. | ||
| 74 | $path = pm_search($module); | ||
| 75 | if(! defined $path) { | ||
| 76 | print "File $module not found\n" if INTERNAL_DEBUG; | ||
| 77 | return undef; | ||
| 78 | } | ||
| 79 | print "File $module found in $path\n" if INTERNAL_DEBUG; | ||
| 80 | } | ||
| 81 | |||
| 82 | print "Resurrecting module $path\n" if INTERNAL_DEBUG; | ||
| 83 | |||
| 84 | my $fh = resurrector_fh($path); | ||
| 85 | |||
| 86 | my $abs_path = File::Spec->rel2abs( $path ); | ||
| 87 | print "Setting %INC entry of $module to $abs_path\n" if INTERNAL_DEBUG; | ||
| 88 | $INC{$module} = $abs_path; | ||
| 89 | |||
| 90 | return $fh; | ||
| 91 | } | ||
| 92 | |||
| 93 | ########################################### | ||
| 94 | sub pm_search { | ||
| 95 | ########################################### | ||
| 96 | my($pmfile) = @_; | ||
| 97 | |||
| 98 | for(@INC) { | ||
| 99 | # Skip subrefs | ||
| 100 | next if ref($_); | ||
| 101 | my $path = File::Spec->catfile($_, $pmfile); | ||
| 102 | return $path if -f $path; | ||
| 103 | } | ||
| 104 | |||
| 105 | return undef; | ||
| 106 | } | ||
| 107 | |||
| 108 | ########################################### | ||
| 109 | sub resurrector_init { | ||
| 110 | ########################################### | ||
| 111 | unshift @INC, \&resurrector_loader; | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; | ||
| 115 | |||
| 116 | __END__ |