| 1 | package FindApp::Utils::Debug; | ||
| 2 | |||
| 3 | use v5.10; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | |||
| 8 | use Carp qw(carp croak cluck confess); | ||
| 9 | use File::Spec qw(); | ||
| 10 | |||
| 11 | use FindApp::Vars qw( :all ); | ||
| 12 | use FindApp::Utils::Assert qw( :all ); | ||
| 13 | use FindApp::Utils::Syntax qw( function ); | ||
| 14 | use FindApp::Utils::Foreign qw( abs2rel ); | ||
| 15 | use FindApp::Utils::Objects qw( class_prune ); | ||
| 16 | |||
| 17 | ################################################################# | ||
| 18 | |||
| 19 | use Exporter qw(import); | ||
| 20 | our $VERSION = 1.0; | ||
| 21 | our @EXPORT_OK = ( | ||
| 22 | <ENTER_TRACE{,_{1,2,3,4,5}}>, | ||
| 23 | qw( | ||
| 24 | debug | ||
| 25 | debugging | ||
| 26 | tracing | ||
| 27 | ), | ||
| 28 | ); | ||
| 29 | our %EXPORT_TAGS = ( | ||
| 30 | all => \@EXPORT_OK, | ||
| 31 | ); | ||
| 32 | |||
| 33 | ################################################################# | ||
| 34 | |||
| 35 | sub debug ; | ||
| 36 | sub debugging ; | ||
| 37 | sub hail_mary ; | ||
| 38 | sub safety_net ; | ||
| 39 | sub tracing ; | ||
| 40 | |||
| 41 | sub ENTER_TRACE ; | ||
| 42 | sub ENTER_TRACE_1 ; | ||
| 43 | sub ENTER_TRACE_2 ; | ||
| 44 | sub ENTER_TRACE_3 ; | ||
| 45 | sub ENTER_TRACE_4 ; | ||
| 46 | sub ENTER_TRACE_5 ; | ||
| 47 | |||
| 48 | ################################################################# | ||
| 49 | |||
| 50 | sub tracing { | ||
| 51 | my $self = shift; | ||
| 52 | if (@_) { | ||
| 53 | $Tracing = shift; | ||
| 54 | safety_net($Debugging || $Tracing); | ||
| 55 | } | ||
| 56 | return $Tracing; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub debug { | ||
| 60 | return unless $Debugging; | ||
| 61 | my($pack, $file, $line, $sub) = caller(0); | ||
| 62 | $file = abs2rel($file); | ||
| 63 | no overloading; | ||
| 64 | my $suffix = $Debugging > 1 && " at $file line $line"; | ||
| 65 | local @SIG{<__{WARN,DIE}__>}; | ||
| 66 | print STDERR "@_$suffix\n"; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub safety_net { | ||
| 70 | my($enable) = @_; | ||
| 71 | $SIG{__DIE__} = $enable ? \&hail_mary : undef; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub debugging { | ||
| 75 | my $self = shift; | ||
| 76 | if (@_) { | ||
| 77 | $Debugging = shift; | ||
| 78 | safety_net($Debugging || $Tracing); | ||
| 79 | } | ||
| 80 | return $Debugging; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub hail_mary { | ||
| 84 | return if $^S; | ||
| 85 | local @SIG{<__{WARN,DIE}__>}; | ||
| 86 | confess "caught exception: @_"; | ||
| 87 | } | ||
| 88 | |||
| 89 | ################################################################# | ||
| 90 | |||
| 91 | for my $func (<debugging tracing>) { | ||
| 92 | no strict "refs"; | ||
| 93 | my $var = ucfirst $func; | ||
| 94 | $func->($$var) if $$var; | ||
| 95 | } | ||
| 96 | |||
| 97 | for my $LEVEL (1 .. 5) { | ||
| 98 | function "ENTER_TRACE_${LEVEL}" => sub { | ||
| 99 | return unless $Tracing && $Tracing >= $LEVEL; | ||
| 100 | my($pack, $file, $line, $sub) = caller(1); | ||
| 101 | $file = abs2rel($file); | ||
| 102 | no overloading; | ||
| 103 | local @SIG{<__{WARN,DIE}__>}; | ||
| 104 | my @safe_args = map { defined() ? $_ : "(undef)" } @_; | ||
| 105 | print STDERR class_prune "$sub @safe_args\n"; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | *ENTER_TRACE = \&ENTER_TRACE_1; | ||
| 110 | |||
| 111 | 1; | ||
| 112 | |||
| 113 | =encoding utf8 | ||
| 114 | |||
| 115 | =head1 NAME | ||
| 116 | |||
| 117 | FindApp::Utils::Debug - FIXME | ||
| 118 | |||
| 119 | =head1 SYNOPSIS | ||
| 120 | |||
| 121 | use FindApp::Utils::Debug; | ||
| 122 | |||
| 123 | =head1 DESCRIPTION | ||
| 124 | |||
| 125 | =head2 Exports | ||
| 126 | |||
| 127 | =over | ||
| 128 | |||
| 129 | =item ENTER_TRACE | ||
| 130 | |||
| 131 | =item ENTER_TRACE_1 | ||
| 132 | |||
| 133 | =item ENTER_TRACE_2 | ||
| 134 | |||
| 135 | =item ENTER_TRACE_3 | ||
| 136 | |||
| 137 | =item ENTER_TRACE_4 | ||
| 138 | |||
| 139 | =item ENTER_TRACE_5 | ||
| 140 | |||
| 141 | =item debug | ||
| 142 | |||
| 143 | =item debugging | ||
| 144 | |||
| 145 | =item hail_mary | ||
| 146 | |||
| 147 | =item safety_net | ||
| 148 | |||
| 149 | =item tracing | ||
| 150 | |||
| 151 | =back | ||
| 152 | |||
| 153 | =head1 EXAMPLES | ||
| 154 | |||
| 155 | =head1 ENVIRONMENT | ||
| 156 | |||
| 157 | =head1 SEE ALSO | ||
| 158 | |||
| 159 | =over | ||
| 160 | |||
| 161 | =item L<FindApp> | ||
| 162 | |||
| 163 | =back | ||
| 164 | |||
| 165 | =head1 CAVEATS AND PROVISOS | ||
| 166 | |||
| 167 | =head1 BUGS AND LIMITATIONS | ||
| 168 | |||
| 169 | =head1 HISTORY | ||
| 170 | |||
| 171 | =head1 AUTHOR | ||
| 172 | |||
| 173 | Tom Christiansen << <tchrist@perl.com> >> | ||
| 174 | |||
| 175 | =head1 LICENCE AND COPYRIGHT | ||
| 176 | |||
| 177 | Copyright (c) 2016, Tom Christiansen C<< <tchrist@perl.com> >>. | ||
| 178 | All Rights Reserved. | ||
| 179 | |||
| 180 | This module is free software; you can redistribute it and/or | ||
| 181 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 182 | |||
| 183 | =cut | ||
| 184 |