File Coverage

blib/lib/IO/Async/Debug.pm
Criterion Covered Total %
statement 5 22 22.7
branch 0 8 0.0
condition 0 5 0.0
subroutine 2 4 50.0
pod 0 2 0.0
total 7 41 17.0


line stmt bran cond sub pod time code
1             # You may distribute under the terms of either the GNU General Public License
2             # or the Artistic License (the same terms as Perl itself)
3             #
4             # (C) Paul Evans, 2015-2024 -- leonerd@leonerd.org.uk
5              
6             package IO::Async::Debug 0.805;
7              
8 85     85   1564 use v5.14;
  85         397  
9 85     85   478 use warnings;
  85         240  
  85         49148  
10              
11             our $DEBUG = $ENV{IO_ASYNC_DEBUG} || 0;
12             our $DEBUG_FD = $ENV{IO_ASYNC_DEBUG_FD};
13             our $DEBUG_FILE = $ENV{IO_ASYNC_DEBUG_FILE};
14             our $DEBUG_FH;
15             our %DEBUG_FLAGS = map { $_ => 1 } split m/,/, $ENV{IO_ASYNC_DEBUG_FLAGS} || "";
16              
17             =head1 NAME
18              
19             C - debugging control and support for L
20              
21             =head1 DESCRIPTION
22              
23             The following methods and behaviours are still experimental and may change or
24             even be removed in future.
25              
26             Debugging support is enabled by an environment variable called
27             C having a true value.
28              
29             When debugging is enabled, the C and C methods
30             on L (and their C variants) are altered such that
31             when the event is fired, a debugging line is printed, using the C
32             method. This identifes the name of the event.
33              
34             By default, the line is only printed if the caller of one of these methods is
35             the same package as the object is blessed into, allowing it to print the
36             events of the most-derived class, without the extra verbosity of the
37             lower-level events of its parent class used to create it. All calls regardless
38             of caller can be printed by setting a number greater than 1 as the value of
39             C.
40              
41             By default the debugging log goes to C, but two other environment
42             variables can redirect it. If C is set, it names a file
43             which will be opened for writing, and logging written into it. Otherwise, if
44             C is set, it gives a file descriptor number that logging
45             should be written to. If opening the named file or file descriptor fails then
46             the log will be written to C as normal.
47              
48             Extra debugging flags can be set in a comma-separated list in an environment
49             variable called C. The presence of these flags can cause
50             extra information to be written to the log. Full details on these flags will
51             be documented by the implementing classes. Typically these flags take the form
52             of one or more capital letters indicating the class, followed by one or more
53             lowercase letters enabling some particular feature within that class.
54              
55             =cut
56              
57             sub logf
58             {
59 0     0 0   my ( $fmt, @args ) = @_;
60              
61 0   0       $DEBUG_FH ||= do {
62 0           my $fh;
63 0 0         if( $DEBUG_FILE ) {
    0          
64 0 0         open $fh, ">", $DEBUG_FILE or undef $fh;
65             }
66             elsif( $DEBUG_FD ) {
67 0           $fh = IO::Handle->new;
68 0 0         $fh->fdopen( $DEBUG_FD, "w" ) or undef $fh;
69             }
70 0   0       $fh ||= \*STDERR;
71 0           $fh->autoflush;
72 0           $fh;
73             };
74              
75 0           printf $DEBUG_FH $fmt, @args;
76             }
77              
78             sub log_hexdump
79             {
80 0     0 0   my ( $bytes ) = @_;
81              
82 0           foreach my $chunk ( $bytes =~ m/(.{1,16})/sg ) {
83 0           my $chunk_hex = join " ", map { sprintf "%02X", ord $_ } split //, $chunk;
  0            
84 0           ( my $chunk_safe = $chunk ) =~ s/[^\x20-\x7e]/./g;
85              
86 0           logf " | %-48s | %-16s |\n", $chunk_hex, $chunk_safe;
87             }
88             }
89              
90             =head1 AUTHOR
91              
92             Paul Evans
93              
94             =cut
95              
96             0x55AA;