File Coverage

blib/lib/Lab/Connection/Log.pm
Criterion Covered Total %
statement 26 32 81.2
branch n/a
condition n/a
subroutine 9 10 90.0
pod 0 1 0.0
total 35 43 81.4


line stmt bran cond sub pod time code
1             package Lab::Connection::Log;
2             #ABSTRACT: Role adding logging capability to connections
3             $Lab::Connection::Log::VERSION = '3.880';
4 1     1   1891 use v5.20;
  1         5  
5              
6 1     1   6 use warnings;
  1         3  
  1         27  
7 1     1   5 use strict;
  1         3  
  1         21  
8              
9 1     1   5 use Role::Tiny;
  1         2  
  1         10  
10              
11 1     1   198 use YAML::XS;
  1         2  
  1         69  
12 1     1   8 use Data::Dumper;
  1         3  
  1         61  
13 1     1   603 use autodie;
  1         14564  
  1         6  
14 1     1   7454 use Carp;
  1         3  
  1         80  
15              
16 1     1   8 use Lab::Connection::LogMethodCall qw(dump_method_call);
  1         3  
  1         488  
17              
18             around 'new' => sub {
19             my $orig = shift;
20             my $proto = shift;
21             my $class = ref($proto) || $proto;
22             my $twin = undef;
23              
24             # getting fields and _permitted from parent class
25             my $self = $class->$orig(@_);
26              
27             $self->_construct($class);
28              
29             # open the log file
30             my $logfile = $self->logfile();
31             if ( not defined $logfile ) {
32             croak 'missing "logfile" parameter in connection';
33             }
34              
35             # FIXME: Currently it's not possible to have a filehandle in %fields, as
36             # this breaks the dclone used in Sweep.pm.
37             open my $fh, '>', $self->logfile();
38             close $fh;
39              
40             return $self;
41              
42             };
43              
44             sub dump_ref {
45 0     0 0   my $self = shift;
46 0           my $ref = shift;
47 0           open my $fh, '>>', $self->logfile();
48 0           print {$fh} Dump($ref);
  0            
49 0           close $fh;
50             }
51              
52             for my $method (
53             qw/Clear Write Read Query BrutalRead LongQuery BrutalQuery
54             timeout block_connection unblock_connection is_blocked/
55             ) {
56             around $method => sub {
57             my $orig = shift;
58             my $self = shift;
59             my $retval = $self->$orig(@_);
60              
61             # Inside the around modifier, we need to skip 2 levels to get to the
62             # true caller.
63             my $caller = caller(2);
64             if ( $caller !~ /Lab::Connection.*/ ) {
65              
66             my $index = $self->log_index();
67              
68             my $log = dump_method_call( $index, $method, @_ );
69              
70             $log->{retval} = $retval;
71             $self->dump_ref($log);
72              
73             $self->log_index( ++$index );
74             }
75             return $retval;
76             };
77             }
78              
79             1;
80              
81             __END__
82              
83             =pod
84              
85             =encoding UTF-8
86              
87             =head1 NAME
88              
89             Lab::Connection::Log - Role adding logging capability to connections
90              
91             =head1 VERSION
92              
93             version 3.880
94              
95             =head1 COPYRIGHT AND LICENSE
96              
97             This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
98              
99             Copyright 2016 Simon Reinhardt
100             2017 Andreas K. Huettel
101             2020 Andreas K. Huettel
102              
103              
104             This is free software; you can redistribute it and/or modify it under
105             the same terms as the Perl 5 programming language system itself.
106              
107             =cut