File Coverage

blib/lib/Protocol/TLS/Trace.pm
Criterion Covered Total %
statement 25 46 54.3
branch 5 8 62.5
condition n/a
subroutine 9 16 56.2
pod 0 10 0.0
total 39 80 48.7


line stmt bran cond sub pod time code
1             package Protocol::TLS::Trace;
2 2     2   10 use strict;
  2         4  
  2         48  
3 2     2   10 use warnings;
  2         3  
  2         53  
4 2     2   841 use Time::HiRes qw(time);
  2         1470  
  2         25  
5              
6 2     2   370 use Exporter qw(import);
  2         4  
  2         1351  
7             our @EXPORT_OK = qw(tracer bin2hex);
8              
9             my %levels = (
10             debug => 0,
11             info => 1,
12             notice => 2,
13             warning => 3,
14             error => 4,
15             critical => 5,
16             alert => 6,
17             emergency => 7,
18             );
19              
20             my $tracer_sngl = Protocol::TLS::Trace->_new(
21             min_level => ( exists $ENV{TLS_DEBUG} && exists $levels{ $ENV{TLS_DEBUG} } )
22             ? $levels{ $ENV{TLS_DEBUG} }
23             : $levels{error}
24             );
25             my $start_time = 0;
26              
27             sub tracer {
28 116     116 0 651 $tracer_sngl;
29             }
30              
31             sub _new {
32 2     2   8 my ( $class, %opts ) = @_;
33 2         11 bless {%opts}, $class;
34             }
35              
36             sub _log {
37 116     116   219 my ( $self, $level, $message ) = @_;
38 116 50       560 if ( $level >= $self->{min_level} ) {
39 0         0 chomp($message);
40 0         0 my @caller = map { s/Protocol::TLS:://; $_ }
  0         0  
  0         0  
41             ( ( caller(2) )[3], ( caller(1) )[2] );
42 0         0 my $now = time;
43 0 0       0 if ( $now - $start_time < 60 ) {
44 0         0 $message =~ s/\n/\n /g;
45 0         0 printf "[%05.3f] [%s:%s] %s\n", $now - $start_time, @caller,
46             $message;
47             }
48             else {
49 0         0 my @t = ( localtime() )[ 5, 4, 3, 2, 1, 0 ];
50 0         0 $t[0] += 1900;
51 0         0 $t[1]++;
52 0         0 $message =~ s/\n/\n /g;
53 0         0 printf "[%4d-%02d-%02d %02d:%02d:%02d] [%s:%s] %s\n", @t,
54             @caller, $message;
55 0         0 $start_time = $now;
56             }
57             }
58             }
59              
60             sub debug {
61 116     116 0 334 shift->_log( 0, @_ );
62             }
63              
64             sub info {
65 0     0 0 0 shift->_log( 1, @_ );
66             }
67              
68             sub notice {
69 0     0 0 0 shift->_log( 2, @_ );
70             }
71              
72             sub warning {
73 0     0 0 0 shift->_log( 3, @_ );
74             }
75              
76             sub error {
77 0     0 0 0 shift->_log( 4, @_ );
78             }
79              
80             sub critical {
81 0     0 0 0 shift->_log( 5, @_ );
82             }
83              
84             sub alert {
85 0     0 0 0 shift->_log( 6, @_ );
86             }
87              
88             sub emergency {
89 0     0 0 0 shift->_log( 7, @_ );
90             }
91              
92             sub bin2hex {
93 10     10 0 18 my $bin = shift;
94 10         18 my $c = 0;
95 10         18 my $s;
96              
97             join "", map {
98 10         83 $c++;
  200         246  
99 200 100       480 $s = !( $c % 16 ) ? "\n" : ( $c % 2 ) ? "" : " ";
    100          
100 200         456 $_ . $s
101             } unpack( "(H2)*", $bin );
102             }
103              
104             1