File Coverage

blib/lib/Log/Easy/Filter.pm
Criterion Covered Total %
statement 40 111 36.0
branch 15 90 16.6
condition 3 15 20.0
subroutine 4 7 57.1
pod 0 3 0.0
total 62 226 27.4


line stmt bran cond sub pod time code
1             package Log::Easy::Filter;
2             our $this_package;
3             our ($FILTER_REGEX, $NOT_FILTER_REGEX, $FILTER_ALL_REGEX, $MATCH_LOG_LEVEL_REGEX, $FILTER, $NOT_FILTER, $FILTER_CNT );
4             # if any $(.*)log->write(...)'s are in the calling code, and the log level is specified with one of the followin prepended with a '$'
5 1     1   5 use constant DEFAULT_FILTER => (qw( mll lll cll qll ell all wll nll ill dll tll sll ), map { "dl$_" } ( 0 .. 9, 99 ));
  1         3  
  1         2  
  11         332  
6 28         1636 use constant LOG_LEVELS => ( map { $_ => "D_$_"; } qw( MESSAGE LOUD CLEAN QUIT EXIT
  11         20  
7             EMERG ALERT CRIT FATAL FAIL ERROR WARN NOTICE INFO DEBUG
8 1         14 TRACE SPEW ), map { "DEBUG$_"} ( 0 .. 9, 99 )
9 1     1   5 );
  1         2  
10              
11             BEGIN {
12             sub space {
13 0     0 0   my $sp = 'space';
14             # this is a stupid little subroutine to nicely display stuff
15             # could probably use a format specifier or sprintf better, but what the hell
16             # I mostly use this for aligning output nicely
17 0           my $piece = shift;
18 0           my $max = shift;
19 0 0         defined $max or $max = 27;
20 0           my $separator = shift;
21 0 0 0       unless( defined $separator and length $separator > 0 ) {
22 0           $separator = ' ';
23             }
24 0 0         my $lp = defined $piece ? length $piece : 0;
25 0           my $ls = length $separator;
26 0 0         my $multiplier = $lp < $max ? int (( $max - $lp )/$ls ) : 0;
27 0           my $spacer = $separator x $multiplier;
28 0 0         my $return = $sp eq 'space' ? $piece . $spacer : $spacer . $piece;
29 0           my $lr = length $return;
30 0 0         my $finisher = $lr < $max ? ( ' ' x ( $max - $lr )) : '';
31 0 0         $return = $sp eq 'space' ? ( $return . $finisher ) : ( $finisher . $return );
32 0           return $return;
33            
34             }
35             sub pad {
36 0     0 0   my $sp = 'pad';
37             # this is a stupid little subroutine to nicely display stuff
38             # could probably use a format specifier or sprintf better, but what the hell
39             # I mostly use this for aligning output nicely
40 0           my $piece = shift;
41 0           my $max = shift;
42 0 0         defined $max or $max = 27;
43 0           my $separator = shift;
44 0 0 0       unless( defined $separator and length $separator > 0 ) {
45 0           $separator = ' ';
46             }
47 0 0         my $lp = defined $piece ? length $piece : 0;
48 0           my $ls = length $separator;
49 0 0         my $multiplier = $lp < $max ? int (( $max - $lp )/$ls ) : 0;
50 0           my $spacer = $separator x $multiplier;
51 0 0         my $return = $sp eq 'space' ? $piece . $spacer : $spacer . $piece;
52 0           my $lr = length $return;
53 0 0         my $finisher = $lr < $max ? ( ' ' x ( $max - $lr )) : '';
54 0 0         $return = $sp eq 'space' ? ( $return . $finisher ) : ( $finisher . $return );
55 0           return $return;
56            
57             }
58             sub space_pad {
59 0     0 0   my $sp = pop;
60             # this is a stupid little subroutine to nicely display stuff
61             # could probably use a format specifier or sprintf better, but what the hell
62             # I mostly use this for aligning output nicely
63 0           my $piece = shift;
64 0           my $max = shift;
65 0 0         defined $max or $max = 27;
66 0           my $separator = shift;
67 0 0 0       unless( defined $separator and length $separator > 0 ) {
68 0           $separator = ' ';
69             }
70 0 0         my $lp = defined $piece ? length $piece : 0;
71 0           my $ls = length $separator;
72 0 0         my $multiplier = $lp < $max ? int (( $max - $lp )/$ls ) : 0;
73 0           my $spacer = $separator x $multiplier;
74 0 0         my $return = $sp eq 'space' ? $piece . $spacer : $spacer . $piece;
75 0           my $lr = length $return;
76 0 0         my $finisher = $lr < $max ? ( ' ' x ( $max - $lr )) : '';
77 0 0         $return = $sp eq 'space' ? ( $return . $finisher ) : ( $finisher . $return );
78 0           return $return;
79            
80             }
81            
82             # sub space {
83             # space_pad( @_, 'space');
84             # }
85             # sub pad {
86             # space_pad( @_, 'pad');
87             # }
88              
89              
90            
91             #die;
92 1     1   3 $this_package = __PACKAGE__;
93 1   50     22 $ENV{LOG_PACKAGES_DEBUG} ||= 0;
94 1 50       7 $ENV{LOG_FILTER_DEBUG} = exists $ENV{LOG_FILTER_DEBUG} ? $ENV{LOG_FILTER_DEBUG} : 0;
95 1 50       5 $ENV{LOG_FILTER_PACKAGES_DEBUG} = exists $ENV{LOG_FILTER_PACKAGES_DEBUG} ? $ENV{LOG_FILTER_PACKAGES_DEBUG} : 0; # want to make it so you can see what the filter is doing for specified packages only
96 1   50     8 $ENV{LOG_FILTER} ||= 'ON';
97 1   50     7 $ENV{LOG_INTERNAL_DEBUG} ||= 0;
98 1 50       4 print STDERR "THIS_PACKAGE=$this_package\n" if $ENV{LOG_FILTER_DEBUG};
99 1         10 my @DEFAULT_FILTER= DEFAULT_FILTER();
100 1 50       4 unless ( defined $FILTER_REGEX ) {
101 1         2 my $FILTER;
102 1 50       8 if( $ENV{LOG_FILTER} =~ /^off$/i ) {
    50          
103 0 0       0 print STDERR __PACKAGE__, ":", __LINE__, ": ", "FILTER: IS OFF\n" if $ENV{LOG_FILTER_DEBUG};
104 0         0 $FILTER = [];
105 0         0 $NOT_FILTER = [ @DEFAULT_FILTER ];
106             } elsif( $ENV{LOG_FILTER} =~ /^(on|\d+)$/i ) {
107 1 50       4 print STDERR __PACKAGE__, ":", __LINE__, ": ", "FILTER: IS ON\n" if $ENV{LOG_FILTER_DEBUG};
108 1         5 $FILTER = [ @DEFAULT_FILTER ];
109 1         2 $NOT_FILTER = [];
110             } else {
111 0 0       0 print STDERR __PACKAGE__, ":", __LINE__, ": ", "FILTER: IS SPECIAL FILTER=$ENV{LOG_FILTER}\n" if $ENV{LOG_FILTER_DEBUG};
112 0         0 my %not_filter = ();
113 0         0 my %filter = ();
114 0         0 foreach my $piece ( split( /:/, $ENV{LOG_FILTER} )) {
115 0 0       0 if ( $piece =~ /^\!/ ) {
116 0         0 $piece =~ s/^\!//;
117 0         0 $not_filter{$piece} = $piece;
118             } else {
119 0         0 $filter{$piece} = $piece;
120             }
121             }
122 0 0       0 print STDERR __PACKAGE__, ":", __LINE__, ": ", "\%filter: ", scalar keys %filter , ":", join( ', ', keys %filter ), "\n" if $ENV{LOG_FILTER_DEBUG};
123 0 0       0 print STDERR __PACKAGE__, ":", __LINE__, ": ", "\%not_filter: ", scalar keys %not_filter, ":", join( ', ', keys %not_filter ), "\n" if $ENV{LOG_FILTER_DEBUG};
124 0 0       0 if ( scalar keys %filter ) {
125 0         0 $FILTER = [ keys %filter ];
126 0         0 $NOT_FILTER = [];
127             } else {
128             # $FILTER = [ map { ($not_filter{$_} and $_ =~ /$not_filter{$_}/ )? () : $_; } ( @DEFAULT_FILTER ) ];
129             # $NOT_FILTER = [ map { ($not_filter{$_} and $_ =~ /$not_filter{$_}/ )? $_ : (); } ( @DEFAULT_FILTER ) ];
130 0         0 my $not_filter_rx = join('|', values %not_filter);
131 0         0 $not_filter_rx = qr/$not_filter_rx/;
132 0 0       0 print STDERR __PACKAGE__, ":", __LINE__, ": ", "not_filter_rx : '$not_filter_rx'\n" if $ENV{LOG_FILTER_DEBUG};
133 0 0       0 $FILTER = [ map { $_ =~ m/$not_filter_rx/ ? () : $_; } ( @DEFAULT_FILTER ) ];
  0         0  
134 0 0       0 $NOT_FILTER = [ map { $_ =~ m/$not_filter_rx/ ? $_ : (); } ( @DEFAULT_FILTER ) ];
  0         0  
135             }
136             }
137 1 50       3 print STDERR __PACKAGE__, ":", __LINE__, ": ", "FILTER: ", join('|', @$FILTER ), "\n" if $ENV{LOG_FILTER_DEBUG};
138             #$FILTER_REGEX = '\$[_a-zA-Z]*[_a-zA-Z0-9]*log->write\(.*?\$(' . join('|', @$FILTER) . '),.*?\);';
139 1         5 $FILTER_REGEX = '\$[_a-zA-Z]+[_a-zA-Z0-9]*->write\(.*?\$(' . join('|', @$FILTER) . '),.*?\);';
140 1 50       4 print STDERR __PACKAGE__, ":", __LINE__, ": ", "FILTER_REGEX : $FILTER_REGEX\n" if $ENV{LOG_FILTER_DEBUG};
141 1 50       3 print STDERR __PACKAGE__, ":", __LINE__, ": ", "NOT_FILTER: ", join('|', @$NOT_FILTER ), "\n" if $ENV{LOG_FILTER_DEBUG};
142             #$NOT_FILTER_REGEX = '\$[_a-zA-Z]*[_a-zA-Z0-9]*log->write\(.*?\$(' . join('|', @$NOT_FILTER) . ')(\,|\ \,|\,\ ).*?\);';
143 1         2 $NOT_FILTER_REGEX = '\$[_a-zA-Z]+[_a-zA-Z0-9]*->write\(.*?\$(' . join('|', @$NOT_FILTER) . ')(\,|\ \,|\,\ ).*?\);';
144 1 50       7 print STDERR __PACKAGE__, ":", __LINE__, ": ", "NOT_FILTER_REGEX : $NOT_FILTER_REGEX\n" if $ENV{LOG_FILTER_DEBUG};
145             }
146              
147 1 50       3 unless ( defined $FILTER_ALL_REGEX ) {
148 1         12 my $FILTER = [ @DEFAULT_FILTER ];
149 1         5 $FILTER_ALL_REGEX = '(\$[_a-zA-Z]*[_a-zA-Z0-9]*log->write\(.*?)(' . join('|', @$FILTER) . ')(\,|\ \,|\,\ )(.*?\);)';
150 1 50       4 print STDERR __PACKAGE__, ":", __LINE__, ": ", "FILTER_ALL_REGEX: $FILTER_ALL_REGEX\n" if $ENV{LOG_FILTER_DEBUG};
151             }
152              
153 1 50       3 unless ( defined $MATCH_LOG_LEVEL_REGEX ) {
154 1         12 my $FILTER = [ LOG_LEVELS() ];
155 1         8 $MATCH_LOG_LEVEL_REGEX = '(\$[_a-zA-Z]*[_a-zA-Z0-9]*log->write\(.*?)(' . join('|', @$FILTER) . ')(\,|\ \,|\,\ )(.*?\);)';
156 1 50       28 print STDERR __PACKAGE__, ":", __LINE__, ": ", "MATCH_LOG_LEVEL_REGEX: $MATCH_LOG_LEVEL_REGEX\n" if $ENV{LOG_FILTER_DEBUG};
157             }
158             }
159              
160 1     1   1718 use Filter::Simple;
  1         44269  
  1         9  
161             our $replace = '1;';
162             FILTER { # this filters out unwanted log messages from source code BEFORE COMPILATION
163             # proves to be a great boon to performance
164             $FILTER_CNT++;
165             ##print STDERR __LINE__, ": \$ENV{LOG_FILTER} = $ENV{LOG_FILTER}\n";
166             return if ( $ENV{LOG_FILTER} and $ENV{LOG_FILTER} =~ /^(OFF|)$/i);
167             #return if ( $before =~ /\s*/s );
168             my @caller = caller(1);
169             $ENV{LOG_FILTER_DEBUG} ||= 0;
170             print STDERR __PACKAGE__, ":", __LINE__, ": ", "CALLER: \n\t", join("\t\n", map { (defined $_ ? $_ : '')} @caller ), "\n" if ($ENV{LOG_FILTER_DEBUG} > 6);
171             my $package = $caller[0];
172             my $file = $caller[1];
173             my $calline = $caller[2];
174             #print STDERR "." if $ENV{LOG_FILTER_DEBUG};
175             my $debug_this_package = $ENV{LOG_FILTER_PACKAGES_DEBUG} ? $file =~ /$ENV{LOG_FILTER_PACKAGES_DEBUG}/ : 1;
176             print STDERR __PACKAGE__, ":", __LINE__, ": ", "DEBUG_THIS_PACKAGE=$debug_this_package ... CALLED FROM FILE: '$file' ($ENV{LOG_FILTER_PACKAGES_DEBUG})\n" if $ENV{LOG_FILTER_DEBUG};
177             my $not_filtered = $ENV{LOG_FILTER_DEBUG} ? "' ### LOG MESSAGE UN-FILTERABLE ### '" : '';
178             my @match;
179             my @before = split("\n", $_ );
180             my @after = ();
181             my $linenum = $calline;
182             my $totallines = scalar @before;
183             my $filtered = '';
184             my $filtered_status = '';
185             foreach my $line ( @before ) {
186             $linenum++;
187             $filtered = '';
188             $filtered_status = '';
189             if ( $line =~ /$MATCH_LOG_LEVEL_REGEX/ ) {
190             $filtered_status = 'UNTOUCHED';
191             } elsif ( $line =~ s/($FILTER_REGEX)\s*$/$replace/g ) {
192             $filtered_status = 'FILTERED ';
193             $filtered = $1;
194             } elsif ( $line =~ /$NOT_FILTER_REGEX/g ) {
195             $filtered_status = 'NOT-FILTERED';
196             } elsif ( $line =~ /$FILTER_ALL_REGEX/ ) { #and $line !~ /$not_filtered/ ) {
197             print STDERR __PACKAGE__, ":", __LINE__, ": ", "WARNING DEBUG LOG MESSAGE NOT REMOVED: $file : $linenum: $line \n" if ($ENV{WARN_FILTER} or ( $debug_this_package and $ENV{LOG_FILTER_DEBUG}));
198             $line =~ s/$FILTER_ALL_REGEX/${1}${2},${not_filtered}${3}${4}/;
199             $filtered_status = 'CHANGED';
200             }
201             push @after, $line;
202             print STDERR __PACKAGE__, ":", __LINE__, ": ", pad(++$ENV{GLOBAL_LINES_FILTER_EXAMINED},5), ' |', pad($linenum,5), '/', space($totallines,5),': ', space($filtered_status, undef, '.'), '| ', $line, "\n" if ($debug_this_package and $ENV{LOG_FILTER_DEBUG} > 3);
203             print STDERR __PACKAGE__, ":", __LINE__, ": ", pad('',5, 'x'), 'xx', pad('',5,'x'), 'x', space('',5,'x'),'::::: ', "FORMER CONTENTS: $filtered", "\n" if ( $debug_this_package and $ENV{LOG_FILTER_DEBUG} > 3 and $filtered );
204             }
205             $_ = join( "\n", @after) . "\n";
206             };
207              
208             1;