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