Moose/Instrument/Log.pm

filter: Statementstmt 53.4% 31 / 58 Branchbran 10.0% 1 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 77.7% 7 / 9 Podpod 0.0% 0 / 1 totaltotal 48.1% 40 / 83 CC 7 SCAR 20.2
CC
7
Cov
46%
__ANON__
16.6
log_build_fh
6.0
_log_retval
6.0
CRAP 7.5
« Moose/Instrument/Bluefors_Temp.pm ↑ Summary Moose/DataFile/Read.pm »
1 package Lab::Moose::Instrument::Log;
2 30 $Lab::Moose::Instrument::Log::VERSION = '3.931';
3 #ABSTRACT: Role for Lab::Moose::Instrument connection logging.
4  
5 30 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 30 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 30 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 30 has log_file => (
14 is => 'ro',
15 isa => 'Str',
16 predicate => 'has_log_file',
17 );
18  
19 30 has log_fh => (
20 is => 'ro',
21 isa => 'FileHandle',
22 builder => 'log_build_fh',
23 predicate => 'has_log_fh',
24 lazy => 1,
25 );
26  
27 30 has log_id => (
28 is => 'ro',
29 isa => 'Int',
30 writer => '_log_id',
31 default => 0,
32 );
33  
34  
35 30 my @wrapped_methods = qw/binary_read write binary_query clear/;
36 30 requires(@wrapped_methods);
37  
38 0 ▶sub log_build_fh {
Subroutine log_build_fh: not called CC=2 SCAR=17.9
Pod undocumented
39 0 my $self = shift;
40 0 my $file = $self->log_file();
41 0 ▶ open my $fh, '>', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or croak("cannot open logfile '${file}': $!") 0 0 0
42 or croak "cannot open logfile '$file': $!";
43 0 $fh->autoflush();
44 0 return $fh;
45 }
46  
47 0 ▶sub _log_retval {
Subroutine _log_retval: not called CC=2 SCAR=17.9
48 0 my ( $arg_ref, $retval ) = @_;
49  
50 0 ▶ if ( $retval !~ /[^[:ascii:]]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $retval =~ /[^[:ascii:]]/u) { } 0 0 0
51 0 $arg_ref->{retval} = $retval;
52 }
53 else {
54 0 $arg_ref->{retval_enc} = 'hex';
55 0 $arg_ref->{retval} = unpack( 'H*', $retval );
56 }
57 }
58  
59 30 for my $method (@wrapped_methods) {
60 735 ▶ around $method => sub {
Subroutine __ANON__: called CC=5 SCAR=28.1
61 735 my $orig = shift;
62 735 my $self = shift;
63 735 my @params = @_;
64  
65 735 ▶ if ( !( $self->has_log_fh() || $self->has_log_file() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_log_fh or $self->has_log_file) 735 0 735
Condition33%1/3
$self->has_log_fh or $self->has_log_file
l!l&&r!l&&!r
00735
MC/DC0%0/2
$self->has_log_fh or $self->has_log_file
$self->has_log_fh $self->has_log_file
Truth table33%1/3
A or B
A= $self->has_log_fhB= $self->has_log_file
ABresult
1X1
011
000
66 735 return $self->$orig(@params);
67 }
68  
69 0 my %arg;
70 0 ▶ if ( ref $params[0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params[0] eq 'HASH') { } 0 0 0
71 0 %arg = %{ $params[0] };
72 }
73 else {
74 0 %arg = @params;
75 }
76  
77 0 my $retval = $self->$orig(@params);
78  
79 0 ▶ if ( $method =~ /read|query/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /read|query/u) 0 0 0
80 0 _log_retval( \%arg, $retval );
81 }
82  
83 0 my %methods = (
84 binary_read => 'Read',
85 write => 'Write',
86 binary_query => 'Query',
87 clear => 'Clear',
88 );
89  
90 0 $arg{method} = $methods{$method};
91  
92 0 my $id = $self->log_id();
93 0 $arg{id} = $id;
94 0 $self->_log_id( ++$id );
95  
96 0 my $fh = $self->log_fh();
97 0 print {$fh} Dump( \%arg );
98  
99 0 return $retval;
100 }
101 120 }
102  
103 30 1;
104  
105 __END__
« Moose/Instrument/Bluefors_Temp.pm ↑ Summary Moose/DataFile/Read.pm »