File Coverage

lib/Log/Shiras/TapWarn.pm
Criterion Covered Total %
statement 52 56 92.8
branch 16 24 66.6
condition 6 12 50.0
subroutine 12 13 92.3
pod 2 2 100.0
total 88 107 82.2


line stmt bran cond sub pod time code
1             package Log::Shiras::TapWarn;
2             our $AUTHORITY = 'cpan:JANDREW';
3 2     2   860 use version; our $VERSION = version->declare("v0.44.0");
  2         2  
  2         11  
4             #~ use lib '../../';
5             #~ use Log::Shiras::Unhide qw( :InternalTaPWarN );
6             ###InternalTaPWarN warn "You uncovered internal logging statements for Log::Shiras::TapWarn-$VERSION" if !$ENV{hide_warn};
7 2     2   161 use 5.010;
  2         5  
8 2     2   7 use utf8;
  2         2  
  2         11  
9 2     2   32 use Moose::Exporter;
  2         2  
  2         14  
10             Moose::Exporter->setup_import_methods(
11             as_is => [ qw( re_route_warn restore_warn ) ],
12             );
13 2     2   88 use MooseX::Types::Moose qw( HashRef );
  2         2  
  2         18  
14 2     2   6289 use Carp 'longmess';
  2         2  
  2         90  
15 2     2   8 use strict;
  2         2  
  2         29  
16 2     2   6 use warnings;
  2         2  
  2         45  
17 2     2   7 use lib '../../../lib';
  2         2  
  2         12  
18 2     2   185 use Log::Shiras::Switchboard;
  2         3  
  2         14  
19             our $switchboard = Log::Shiras::Switchboard->instance;
20             my $warn_store;
21              
22             #########1 Public Functions 3#########4#########5#########6#########7#########8#########9
23              
24             sub re_route_warn{
25 3 100   3 1 2354 warn "Re-routing warn statements" if !$ENV{hide_warn};# called at " . (caller( 0 ))[1] . ' line ' . (caller( 0 ))[2] . "\n"
26             ###InternalTaPWarN $switchboard->master_talk( { report => 'log_file', level => 2,
27             ###InternalTaPWarN name_space => 'Log::Shiras::TapWarn::re_route_warn',
28             ###InternalTaPWarN message =>[ "Re-routing warn statements", ], } );
29 3         571 my ( @passed ) = @_;
30             my $data_ref =
31             ( exists $passed[0] and
32             is_HashRef( $passed[0] ) and
33             ( exists $passed[0]->{report} or
34             exists $passed[0]->{level} or
35             exists $passed[0]->{carp_stack} or
36             exists $passed[0]->{fail_over} ) ) ?
37             $passed[0] :
38             ( @passed % 2 == 0 and
39             ( exists {@passed}->{report} or
40             exists {@passed}->{level} or
41             exists {@passed}->{carp_stack} or
42 3 50 33     17 exists {@passed}->{fail_over} ) ) ?
    50 33        
43             {@passed} :
44             { level => $_[0] };
45             ###InternalTaPWarN $switchboard->master_talk( { report => 'log_file', level => 0,
46             ###InternalTaPWarN name_space => 'Log::Shiras::TapWarn::re_route_warn',
47             ###InternalTaPWarN message =>[ "With settings: ", $data_ref ], } );
48              
49             # set common report
50 3 50       412 if( !$data_ref->{report} ){
51 0         0 $data_ref->{report} = 'log_file';
52             ###InternalTaPWarN $switchboard->master_talk( { report => 'log_file', level => 3,
53             ###InternalTaPWarN name_space => 'Log::Shiras::TapWarn::re_route_warn',
54             ###InternalTaPWarN message =>[ "No report was passed to 're_route_warn' so the " .
55             ###InternalTaPWarN "target report for print is set to: 'log_file'" ], } );
56             }
57              
58             # set common urgency level
59 3 50       8 if( !$data_ref->{level} ){
60 3         8 $data_ref->{level} = 3;
61             ###InternalTaPWarN $switchboard->master_talk( { report => 'log_file', level => 3,
62             ###InternalTaPWarN name_space => 'Log::Shiras::TapWarn::re_route_warn',
63             ###InternalTaPWarN message =>[ "No urgency level was defined in the 're_route_warn' method " .
64             ###InternalTaPWarN "call so future 'print' messages will be sent at: 2 (These go to 11)" ], } );
65             }
66              
67             # set failover
68 3 100       7 if( !$data_ref->{fail_over} ){
69 2         4 $data_ref->{fail_over} = 0;
70             ###InternalTaPWarN $switchboard->master_talk( { report => 'log_file', level => 3,
71             ###InternalTaPWarN name_space => 'Log::Shiras::TapWarn::re_route_warn',
72             ###InternalTaPWarN message =>[ "fail_over was not set - setting it to: 0" ], } );
73             }
74              
75             # set (add) carp stack
76 3 50       9 if( !$data_ref->{carp_stack} ){
77 3         4 $data_ref->{carp_stack} = 0;
78             ###InternalTaPWarN $switchboard->master_talk( { report => 'log_file', level => 3,
79             ###InternalTaPWarN name_space => 'Log::Shiras::TapWarn::re_route_warn',
80             ###InternalTaPWarN message =>[ "carp_stack was not set - setting it to: 0" ], } );
81             }
82              
83             # Set the source_sub (Fixed for this class)
84 3         6 $data_ref->{source_sub} = 'Log::Shiras::TapWarn::__ANON__';
85             ###InternalTaPWarN $switchboard->master_talk( { report => 'log_file', level => 1,
86             ###InternalTaPWarN name_space => 'Log::Shiras::TapWarn::re_route_warn',
87             ###InternalTaPWarN message =>[ "Adjusting the warn sig handler with the data ref: ", $data_ref, ], } );
88              
89 3 100       11 $warn_store = $SIG{__WARN__} if $SIG{__WARN__};
90             $SIG{__WARN__} = sub{
91 10     10   2817 $data_ref->{message} = [ @_ ];
92 10         16 chomp @{$data_ref->{message}};
  10         19  
93             ###InternalTaPWarN $switchboard->master_talk( { report => 'log_file', level => 2,
94             ###InternalTaPWarN name_space => 'Log::Shiras::TapWarn::warn',
95             ###InternalTaPWarN message =>[ "Inbound warn statement: ", $data_ref->{message},], } );# caller( 0 ), '-----', caller( 1 ), '-----', caller( 2 ), '-----', caller( 3 ), '-----', caller( 4 ),
96 10         40 my $line = (caller( 0 ))[2];
97 10 100 66     115 $data_ref->{name_space} = ((caller( 1 ))[3] and (caller( 1 ))[3] !~ /__ANON__/) ? (caller( 1 ))[3] : (caller( 0 ))[0];
98 10         22 $data_ref->{name_space} .= "::$line";
99             ###InternalTaPWarN $switchboard->master_talk( { report => 'log_file', level => 1,
100             ###InternalTaPWarN name_space => 'Log::Shiras::TapWarn::warn',
101             ###InternalTaPWarN message =>[ "Added name_space: ", $data_ref->{name_space}, ], } );
102              
103             # Dispatch the message
104 10         36 my $report_count = $switchboard->master_talk( $data_ref );
105             ###InternalTaPWarN $switchboard->master_talk( { report => 'log_file', level => 2,
106             ###InternalTaPWarN name_space => 'Log::Shiras::TapWarn::warn',
107             ###InternalTaPWarN message =>[ "Message reported |$report_count| times"], } );
108              
109             # Handle fail_over
110 10 100 66     76 if( $report_count == 0 and $data_ref->{fail_over} ){
111             ###InternalTelephonE $switchboard->master_talk( { report => 'log_file', level => 4,
112             ###InternalTelephonE name_space => 'Log::Shiras::TapWarn::warn',
113             ###InternalTelephonE message => [ "Message allowed but found no destination!", $data_ref->{message} ], } );
114 1         236 warn longmess( "This message sent to the report -$data_ref->{report}- was approved but found no destination objects to use" ), @_;
115             }
116 3 50       199 } or die "Couldn't redirect __WARN__: $!";
117             ###InternalTaPWarN $switchboard->master_talk( { report => 'log_file', level => 0,
118             ###InternalTaPWarN name_space => 'Log::Shiras::TapWarn::re_route_warn',
119             ###InternalTaPWarN message =>[ "Finished re_routing warn statements" ], } );
120 3         15 return 1;
121             }
122              
123             sub restore_warn{
124 0 0   0 1   $SIG{__WARN__} = $warn_store ? $warn_store : undef;
125 0           $warn_store = undef;
126             ###InternalTaPWarN $switchboard->master_talk( { report => 'log_file', level => 0,
127             ###InternalTaPWarN name_space => 'Log::Shiras::TapWarn::restore_warn',
128             ###InternalTaPWarN message =>[ "Log::Shiras is no longer tapping into warnings!" ], } );
129 0           return 1;
130             }
131              
132             #########1 Phinish 3#########4#########5#########6#########7#########8#########9
133              
134             1;
135              
136             #########1 main pod docs 3#########4#########5#########6#########7#########8#########9
137             __END__
138              
139             =head1 NAME
140              
141             Log::Shiras::TapWarn - Reroute warn to Log::Shiras::Switchboard
142              
143             =head1 SYNOPSIS
144              
145             use Modern::Perl;
146             #~ use Log::Shiras::Unhide qw( :InternalTaPWarN );# :InternalSwitchboarD
147             $ENV{hide_warn} = 0;
148             use Log::Shiras::Switchboard;
149             use Log::Shiras::TapWarn qw( re_route_warn restore_warn );
150             my $ella_peterson = Log::Shiras::Switchboard->get_operator(
151             name_space_bounds =>{
152             UNBLOCK =>{
153             log_file => 'trace',
154             },
155             main =>{
156             32 =>{
157             UNBLOCK =>{
158             log_file => 'fatal',
159             },
160             },
161             34 =>{
162             UNBLOCK =>{
163             log_file => 'fatal',
164             },
165             },
166             },
167             },
168             reports =>{ log_file =>[ Print::Log->new ] },
169             );
170             re_route_warn(
171             fail_over => 0,
172             level => 'debug',
173             report => 'log_file',
174             );
175             warn "Hello World 1";
176             warn "Hello World 2";
177             restore_warn;
178             warn "Hello World 3";
179              
180             package Print::Log;
181             use Data::Dumper;
182             sub new{
183             bless {}, shift;
184             }
185             sub add_line{
186             shift;
187             my @input = ( ref $_[0]->{message} eq 'ARRAY' ) ?
188             @{$_[0]->{message}} : $_[0]->{message};
189             my ( @print_list, @initial_list );
190             no warnings 'uninitialized';
191             for my $value ( @input ){
192             push @initial_list, (( ref $value ) ? Dumper( $value ) : $value );
193             }
194             for my $line ( @initial_list ){
195             $line =~ s/\n$//;
196             $line =~ s/\n/\n\t\t/g;
197             push @print_list, $line;
198             }
199             my $output = sprintf( "| level - %-6s | name_space - %-s\n| line - %04d | file_name - %-s\n\t:(\t%s ):\n",
200             $_[0]->{level}, $_[0]->{name_space},
201             $_[0]->{line}, $_[0]->{filename},
202             join( "\n\t\t", @print_list ) );
203             print $output;
204             use warnings 'uninitialized';
205             }
206              
207             1;
208              
209             #######################################################################################
210             # Synopsis Screen Output
211             # 01: Re-routing warn statements at ../lib/Log/Shiras/TapWarn.pm line 22, <DATA> line 1.
212             # 02: | level - debug | name_space - main::33
213             # 03: | line - 0033 | file_name - log_shiras_tapwarn.pl
214             # 04: :( Hello World 1 at log_shiras_tapwarn.pl line 33, <DATA> line 1. ):
215             # 05: Hello World 3 at log_shiras_tapwarn.pl line 36, <DATA> line 1.
216             #######################################################################################
217              
218             =head1 DESCRIPTION
219              
220             This package allows Log::Shiras to be used for code previously written with warn statement
221             outputs. It will re-direct the warn statements using the L<$SIG{__WARN__} (%SIG)
222             |http://perldoc.perl.org/perlvar.html> handler. Using this
223             mechanisim means that the string in;
224              
225             warn "Print some line";
226              
227             Will be routed to L<Log::Shiras::Switchboard> after the method call L<re_route_warn
228             |/re_route_warn( %args )>
229              
230             This class is used to import functions into the script. These are not object methods and
231             there is no reason to call ->new. Uncomment line 2 of the SYNOPSIS to watch the inner
232             workings.
233              
234             =head2 Output Explanation
235              
236             B<01:> The method re_route_warn will throw a warning statement whenever
237             $ENV{hide_warn} is not set and the method is called.
238              
239             B<02-04:> Line 31 of the code has been captured (meta data appended) and then sent to the
240             Print::Log class for reporting.
241              
242             B<05:> Line 32 of the script did not print since that line has a higher required urgency
243             than the standard 'warn' level provided by the L<re_route_warn|/re_route_warn( %args )>
244             call in the SYNOPSIS.
245              
246             B<06:> Line 33 of the script turns off re-routing so Line 34 of the script prints normally
247             with no shenanigans. (Even though it is also blocked by line number)
248              
249             =head2 Functions
250              
251             These functions are used to change the routing of warn statements.
252              
253             =head3 re_route_warn( %args )
254              
255             This is the function used to re_route warnings to L<Log::Shiras::Switchboard> for
256             processing. There are several settings adjustments that affect the routing of warnings.
257             Since warnings are intended to be captured in-place, with no modification, all these
258             settings must be fixed when the re-routing is implemented. Fine grained control of which
259             warnings are processed is done by line number (See the SYNOPSIS for an example). This
260             function accepts all of the possible settings, minimally scrubs the settings as needed,
261             builds the needed anonymous subroutine, and then redirects (runtime) future warnings to
262             that subroutine. Each set of content from a warning statement will then be packaged by
263             the anonymous subroutine and sent to L<Log::Shiras::Switchboard/master_talk( $args_ref )>.
264             Since warnings are generally scattered throughout pre-existing code the auto assigned
265             name-space is either 'main::line_number' for top level scripts or the subroutine block
266             name and warning line number within the block. For example the name_space
267             'main::my_sub::32' would be applied to a warning executed on line 32 within the sub
268             block named 'my_sub' in the 'main' script.
269              
270             =over
271              
272             B<Accepts>
273              
274             The following keys in a hash or hashref which are passed directly to
275             L<Log::Shiras::Switchboard/master_talk( $args_ref )> - see the documentation there to
276             understand how they are used by the switchboard. All values that are passed remain in force
277             until a new re_route_warn call is made or the L<restore_warn|/restore_warn> call is made.
278              
279             =over
280              
281             report - I<default = 'log_file'>
282              
283             level - I<default = 3 (warn)>
284              
285             fail_over - I<default = 0>
286              
287             carp_stack - I<default = 0>
288              
289             =back
290              
291             B<Returns> 1
292              
293             =back
294              
295             =head3 restore_warn
296              
297             This returns the $SIG{__WARN__} settings to what they were before or undef.
298             The result is warn statements will start to be processed as they were prior to the
299             're_route_warn' call.
300              
301             =over
302              
303             B<Accepts:> Nothing
304              
305             B<Returns:> 1
306              
307             =back
308              
309             =head1 SUPPORT
310              
311             =over
312              
313             L<github Log-Shiras/issues|https://github.com/jandrew/Log-Shiras/issues>
314              
315             =back
316              
317             =head1 GLOBAL VARIABLES
318              
319             =over
320              
321             =item B<$ENV{hide_warn}>
322              
323             The module will warn when re-routing warn statements are turned on. It
324             will also warn when internal debug lines are 'Unhide'n. In
325             the case where the you don't want these warnings then set this
326             environmental variable to true.
327              
328             =back
329              
330             =head1 TODO
331              
332             =over
333              
334             B<1.> Nothing currently
335              
336             =back
337              
338             =head1 AUTHOR
339              
340             =over
341              
342             =item Jed Lund
343              
344             =item jandrew@cpan.org
345              
346             =back
347              
348             =head1 COPYRIGHT
349              
350             This program is free software; you can redistribute
351             it and/or modify it under the same terms as Perl itself.
352              
353             The full text of the license can be found in the
354             LICENSE file included with this module.
355              
356             =head1 DEPENDANCIES
357              
358             =over
359              
360             L<version>
361              
362             L<5.010|http://perldoc.perl.org/perl5100delta.html> (for use of
363             L<defined or|http://perldoc.perl.org/perlop.html#Logical-Defined-Or> //)
364              
365             L<utf8>
366              
367             L<Moose::Exporter>
368              
369             L<MooseX::Types::Moose>
370              
371             L<Carp> - longmess
372              
373             L<Log::Shiras::Switchboard>
374              
375             =back
376              
377             =head1 SEE ALSO
378              
379             =over
380              
381             L<Capture::Tiny> - capture_stderr
382              
383             =back
384              
385             =cut
386              
387             #########1 main pod docs end 3#########4#########5#########6#########7#########8#########9