File Coverage

blib/lib/Lab/Moose/Instrument/Log.pm
Criterion Covered Total %
statement 17 27 62.9
branch 0 4 0.0
condition n/a
subroutine 6 8 75.0
pod 0 1 0.0
total 23 40 57.5


line stmt bran cond sub pod time code
1             package Lab::Moose::Instrument::Log;
2             $Lab::Moose::Instrument::Log::VERSION = '3.900';
3             #ABSTRACT: Role for Lab::Moose::Instrument connection logging.
4              
5 30     30   20220 use v5.20;
  30         115  
6              
7 30     30   193 use Moose::Role;
  30         75  
  30         300  
8 30     30   164909 use Carp;
  30         82  
  30         2129  
9 30     30   244 use namespace::autoclean;
  30         83  
  30         245  
10 30     30   8371 use YAML::XS;
  30         38642  
  30         1930  
11 30     30   1288 use IO::Handle;
  30         12503  
  30         18039  
12              
13             has log_file => (
14             is => 'ro',
15             isa => 'Str',
16             predicate => 'has_log_file',
17             );
18              
19             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             has log_id => (
28             is => 'ro',
29             isa => 'Int',
30             writer => '_log_id',
31             default => 0,
32             );
33              
34              
35             my @wrapped_methods = qw/binary_read write binary_query clear/;
36             requires(@wrapped_methods);
37              
38             sub log_build_fh {
39 0     0 0   my $self = shift;
40 0           my $file = $self->log_file();
41 0 0         open my $fh, '>', $file
42             or croak "cannot open logfile '$file': $!";
43 0           $fh->autoflush();
44 0           return $fh;
45             }
46              
47             sub _log_retval {
48 0     0     my ( $arg_ref, $retval ) = @_;
49              
50 0 0         if ( $retval !~ /[^[:ascii:]]/ ) {
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             for my $method (@wrapped_methods) {
60             around $method => sub {
61             my $orig = shift;
62             my $self = shift;
63             my @params = @_;
64              
65             if ( !( $self->has_log_fh() || $self->has_log_file() ) ) {
66             return $self->$orig(@params);
67             }
68              
69             my %arg;
70             if ( ref $params[0] eq 'HASH' ) {
71             %arg = %{ $params[0] };
72             }
73             else {
74             %arg = @params;
75             }
76              
77             my $retval = $self->$orig(@params);
78              
79             if ( $method =~ /read|query/ ) {
80             _log_retval( \%arg, $retval );
81             }
82              
83             my %methods = (
84             binary_read => 'Read',
85             write => 'Write',
86             binary_query => 'Query',
87             clear => 'Clear',
88             );
89              
90             $arg{method} = $methods{$method};
91              
92             my $id = $self->log_id();
93             $arg{id} = $id;
94             $self->_log_id( ++$id );
95              
96             my $fh = $self->log_fh();
97             print {$fh} Dump( \%arg );
98              
99             return $retval;
100             }
101             }
102              
103             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.900
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