NAME
Log::Shiras::TapPrint - Reroute print to Log::Shiras::Switchboard
SYNOPSIS
use Modern::Perl;
#~ use Log::Shiras::Unhide qw( :InternalTaPPrinT );
$ENV{hide_warn} = 0;
use Log::Shiras::Switchboard;
use Log::Shiras::TapPrint 're_route_print';
my $ella_peterson = Log::Shiras::Switchboard->get_operator(
name_space_bounds =>{
UNBLOCK =>{
log_file => 'debug',
},
main =>{
27 =>{
UNBLOCK =>{
log_file => 'info',
},
},
},
},
reports =>{ log_file =>[ Print::Log->new ] },
);
re_route_print(
fail_over => 0,
level => 'debug',
report => 'log_file',
);
print "Hello World 1\n";
print "Hello World 2\n";
print STDOUT "Hello World 3\n";
restore_print;
print "Hello World 4\n";
package Print::Log;
use Data::Dumper;
sub new{
bless {}, shift;
}
sub add_line{
shift;
my @input = ( ref $_[0]->{message} eq 'ARRAY' ) ?
@{$_[0]->{message}} : $_[0]->{message};
my ( @print_list, @initial_list );
no warnings 'uninitialized';
for my $value ( @input ){
push @initial_list, (( ref $value ) ? Dumper( $value ) : $value );
}
for my $line ( @initial_list ){
$line =~ s/\n$//;
$line =~ s/\n/\n\t\t/g;
push @print_list, $line;
}
my $output = sprintf( "| level - %-6s | name_space - %-s\n| line - %04d | file_name - %-s\n\t:(\t%s ):\n",
$_[0]->{level}, $_[0]->{name_space},
$_[0]->{line}, $_[0]->{filename},
join( "\n\t\t", @print_list ) );
print STDOUT $output;
use warnings 'uninitialized';
}
1;
#######################################################################################
# Synopsis Screen Output
# 01: Re-routing print statements at ../lib/Log/Shiras/TapPrint.pm line 22, <DATA> line 1.
# 02: | level - debug | name_space - main::26
# 03: | line - 0026 | file_name - log_shiras_tapprint.pl
# 04: :( Hello World 1 ):
# 05: Hello World 3
# 06: Hello World 4
#######################################################################################
DESCRIPTION
This package allows Log::Shiras to be used for code previously written with print statement outputs. It will re-direct the print statements using the select command with IO::Callback. Using this mechanisim means that a call to;
print STDOUT "Print some line\n";
Will still do as expected but;
print "Capture this line\n";
Will be routed to Log::Shiras::Switchboard.
This class is used to import functions into the script. These are not object methods and there is no reason to call ->new. Uncomment line 2 of the SYNOPSIS to watch the inner workings.
Output Explanation
01: The method re_route_print will throw a warning statement whenever $ENV{hide_warn} is not set and the method is called.
02-04: Line 26 of the code has been captured (meta data appended) and then sent to the Print::Log class for reporting.
05: Line 27 of the script did not print since that line requires a different urgency than the urgency provided by the re_route_print call in the SYNOPSIS. Line 28 is not re-routed and does print normally since it is explicitly sent to STDOUT.
06: Line 29 of the script turns off re-routing so Line 30 of the script prints normally with no shenanigans.
Functions
These functions are used to change the routing of general print statements.
re_route_print( %args )
This is the function used to re_route generic print statements to Log::Shiras::Switchboard for processing. There are several settings adjustments that affect the routing of these statements. Since print statments are intended to be captured in-place, with no modification, all these settings must be fixed when the re-routing is implemented. Fine grained control of which print statements are used is done by line number (See the SYNOPSIS for an example). This function accepts all of the possible settings, minimally scrubs the data as needed, builds the needed anonymous subroutine, and then redirects generic print statements to that subroutine. Each set of content from generic print statements will then be packaged by the anonymous subroutine and sent to "master_talk( $args_ref )" in Log::Shiras::Switchboard. Since print statements are generally scattered throughout pre-existing code the name-space is either 'main::line_number' for scripts or the subroutine block name within which the print statement occurs with the line number. For example the name_space 'main::my_sub::32' would be applied to a print statement executed on line 32 within the sub block named 'my_sub' in the 'main' script.
Accepts:
The following keys in a hash or hashref which are passed directly to "master_talk( $args_ref )" in Log::Shiras::Switchboard - see the documentation there to understand how they are used by the switchboard. All values that are passed remain in force until a new re_route_print call is made or the restore_print call is made.
report - default = 'log_file'
level - default = 2 (info)
fail_over - default = 0
carp_stack - default = 0
Returns: 1
restore_print
This sends all generic print statements to STDOUT using select .
Accepts: Nothing
Returns: 1
SUPPORT
GLOBAL VARIABLES
- $ENV{hide_warn}
-
The module will warn when re-routing print statements are turned on. It will also warn when internal debug lines are 'Unhide'n. In the case where the you don't want these warnings then set this environmental variable to true.
TODO
1. Nothing currently
AUTHOR
COPYRIGHT
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the LICENSE file included with this module.
DEPENDANCIES
5.010 (for use of defined or //)
Carp - longmess
SEE ALSO
Capture::Tiny - capture_stdout