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 SCAR 26.7
CC
7
Cov
46%
__ANON__
16.6
log_build_fh
6.0
_log_retval
6.0
CRAP 14.5
« Moose/Stabilizer.pm ↑ Summary Moose/Connection/Socket.pm »
1 package Lab::Moose::Instrument::Log;
2 30 $Lab::Moose::Instrument::Log::VERSION = '3.881';
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 sub log_build_fh {
39 0 ▶ my $self = shift;
Subroutine log_build_fh: not called CC=2 SCAR=17.9
Pod undocumented
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 sub _log_retval {
48 0 ▶ my ( $arg_ref, $retval ) = @_;
Subroutine _log_retval: not called CC=2 SCAR=17.9
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 around $method => sub {
61 735 ▶ my $orig = shift;
Subroutine __ANON__: called CC=5 SCAR=28.1
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__
106  
107 =pod
108  
109 =encoding UTF-8
110  
111 =head1 NAME
112  
113 Lab::Moose::Instrument::Log - Role for Lab::Moose::Instrument connection logging.
114  
115 =head1 VERSION
116  
117 version 3.881
118  
119 =head1 SYNOPSIS
120  
121 use Lab::Moose 'instrument';
122 my $instr = instrument(
123 type => '...',
124 connection_type => '...',
125 connection_options => {...},
126 # write into newly created logfile:
127 log_file => '/tmp/instr.log',
128 # alternative: write into filehandle:
129 log_fh => $filehandle,
130 );
131  
132 =head1 DESCRIPTION
133  
134 Log all of the instrument's C<read, write, query, clear> function calls into a
135 logfile or an existing filehandle.
136  
137 =head1 COPYRIGHT AND LICENSE
138  
139 This software is copyright (c) 2023 by the Lab::Measurement team; in detail:
140  
141 Copyright 2016 Simon Reinhardt
142 2017 Andreas K. Huettel, Simon Reinhardt
143 2020 Andreas K. Huettel
144  
145  
146 This is free software; you can redistribute it and/or modify it under
147 the same terms as the Perl 5 programming language system itself.
148  
149 =cut
« Moose/Stabilizer.pm ↑ Summary Moose/Connection/Socket.pm »