File Coverage

blib/lib/Log/Fu/Chomp.pm
Criterion Covered Total %
statement 51 62 82.2
branch 16 28 57.1
condition 8 12 66.6
subroutine 6 7 85.7
pod 0 3 0.0
total 81 112 72.3


line stmt bran cond sub pod time code
1             package Log::Fu::Chomp;
2 5     5   28 use strict;
  5         10  
  5         188  
3 5     5   29 use warnings;
  5         8  
  5         158  
4 5     5   24 use base qw(Exporter);
  5         8  
  5         496  
5              
6             our @EXPORT = qw(fu_chomp);
7              
8 5     5   28 use Log::Fu::Common qw(%Config);
  5         9  
  5         4375  
9              
10             my $HKEY_MORE_CHAR = 'StripMoreIndicator';
11             my $HKEY_STRIP = 'Strip';
12             my $HKEY_MAX_COMPONENTS = 'StripMaxComponents';
13             my $HKEY_STRIP_WIDTH = 'StripCallerWidth';
14             my $HKEY_STRIP_KEEP = 'StripKeepChars';
15             my $HKEY_STRIP_TLNS = 'StripTopLevelNamespace';
16             my $HKEY_STRIP_SUB = 'StripSubBasename';
17              
18              
19             #Filler 'more' char
20             $Config{$HKEY_MORE_CHAR} = '~';
21              
22             #Boolean, whether to strip
23             $Config{$HKEY_STRIP} = 0;
24              
25             #How many intermediate components to keep (max)
26             $Config{$HKEY_MAX_COMPONENTS} = 2;
27              
28             #How small can caller info be to ignore stripping
29             $Config{$HKEY_STRIP_WIDTH} = 10;
30              
31             #How many characters to keep for each shortened component
32             $Config{$HKEY_STRIP_KEEP} = 2;
33              
34             #Whether to strip the top-level namespace
35             $Config{$HKEY_STRIP_TLNS} = 0;
36              
37             #Maximum length for function basname
38             $Config{$HKEY_STRIP_SUB} = 8;
39              
40             my %Handlers = ();
41             my %HandlerCache = ();
42              
43             sub AddHandler {
44 1     1 0 602 my ($match,$code) = @_;
45 1         5 $Handlers{$match} = $code;
46 1         4 %HandlerCache = ();
47             }
48              
49             sub DelHandler {
50 0     0 0 0 my ($match) = @_;
51 0 0       0 if(delete $Handlers{$match}) {
52 0         0 %HandlerCache = ();
53             }
54             }
55              
56             sub fu_chomp {
57 19     19 0 45 my $sub = $_[0];
58            
59             GT_FETCH_CACHE:
60 20 100       62 if(%Handlers) {
61 2         5 my $code = $HandlerCache{$sub};
62 2 50 66     14 if(defined $code && ref $code ne 'CODE') {
    100          
63 0         0 goto GT_WE_PROCESS;
64             } elsif (defined $code) {
65 1         4 goto &$code;
66             } else {
67 1         2 my $module = $sub;
68 1         6 $module =~ s/::[^:]+$//g;
69 1         6 foreach my $key (reverse sort { length($a) <=> length($b) }
  0         0  
70             keys %Handlers) {
71 1 50       5 if(index($module, $key) >= 0) {
72 1         4 $HandlerCache{$sub} = $Handlers{$key};
73 1         10 goto GT_FETCH_CACHE;
74             }
75             }
76 0         0 $HandlerCache{$sub} = 0;
77             }
78             }
79            
80             GT_WE_PROCESS:
81 18 100       84 return $sub unless $Config{$HKEY_STRIP};
82 2 50       7 return $sub if $Log::Fu::NO_STRIP;
83            
84 2         3 my $maxwidth;
85            
86 2 50       7 if( ($maxwidth = $Config{$HKEY_STRIP_WIDTH}) ){
87 2 50       8 return $sub if length($sub) < $maxwidth;
88             }
89            
90 2         10 my @orig_components = split(/::/, $sub);
91 2         4 my @components;
92 2         5 my $sub_basename = pop @orig_components;
93            
94 2         3 my $tlns = shift @orig_components;
95 2 50       7 $tlns .= "::" if $tlns;
96            
97             #Strip top-level component
98 2         8 my $tlns_len = $Config{$HKEY_STRIP_TLNS} + length($Config{$HKEY_MORE_CHAR});
99 2 50 66     16 if($Config{$HKEY_STRIP_TLNS} &&
      66        
100             $tlns_len-1 && length($tlns) > $tlns_len) {
101 1         4 $tlns = substr($tlns, 0, $Config{$HKEY_STRIP_TLNS});
102 1         3 $tlns .= $Config{$HKEY_MORE_CHAR};
103             }
104 2         9 push @components, $tlns;
105            
106 2 50       7 if($Config{$HKEY_MAX_COMPONENTS}) {
107 2         9 while(scalar @orig_components > $Config{$HKEY_MAX_COMPONENTS}) {
108 0         0 shift @orig_components;
109             }
110             }
111            
112             #Strip intermediary components
113 2         7 my $new_len_min = $Config{$HKEY_STRIP_KEEP} + length($Config{$HKEY_MORE_CHAR});
114 2         13 while ( (my $comp = shift @orig_components) ) {
115 0 0       0 if(length($comp) <= $new_len_min) {
116             } else {
117 0         0 $comp = substr($comp, 0, $Config{$HKEY_STRIP_KEEP});
118 0         0 $comp .= $Config{$HKEY_MORE_CHAR};
119             }
120 0         0 push @components, $comp;
121             }
122            
123             #Strip the basename of the sub
124 2         6 my $sub_len_min = $Config{$HKEY_STRIP_SUB} + length($Config{$HKEY_MORE_CHAR});
125 2 100 66     13 if($Config{$HKEY_STRIP_SUB} && length($sub_basename) > $sub_len_min) {
126 1         3 my $offset = length($sub_basename) - $Config{$HKEY_STRIP_SUB};
127 1         3 $sub_basename = $Config{$HKEY_MORE_CHAR} . substr($sub_basename, $offset);
128             #Make sure we have an offset. We wi
129             }
130            
131 2         5 push @components, $sub_basename;
132 2         11 return join("", @components);
133             }
134              
135              
136             1;