File: Debuggable.pm

package info (click to toggle)
libmr-tarantool-perl 0.0.24-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 416 kB
  • sloc: perl: 3,662; makefile: 2
file content (82 lines) | stat: -rw-r--r-- 1,092 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package MR::IProto::Role::Debuggable;

=head1 NAME

MR::IProto::Role::Debuggable - debuggable role

=head1 DESCRIPTION

=over

=cut

use Mouse::Role;

=item debug

Debug level.

=cut

has debug => (
    is  => 'rw',
    isa => 'Int',
    default => 0,
);

=item debug_cb

Callback which is called when debug message is written.

=cut

has debug_cb => (
    is  => 'rw',
    isa => 'CodeRef',
    lazy_build => 1,
);

=item dump_no_ints

Skip print of integers in dump.

=cut

has dump_no_ints => (
    is  => 'ro',
    isa => 'Bool',
);

sub _build_debug_cb {
    my ($self) = @_;
    my $prefix = ref $self;
    return sub {
        my ($msg) = @_;
        chomp $msg;
        warn sprintf "$prefix: $msg\n";
        return;
    };
}

sub _debug {
    $_[0]->debug_cb->($_[1]);
}

sub _debug_dump {
    my ($self, $msg, $datum) = @_;
    unless($self->dump_no_ints) {
        $msg .= join(' ', unpack('L*', $datum));
        $msg .= ' > ';
    }
    $msg .= join(' ', map { sprintf "%02x", $_ } unpack("C*", $datum));
    $self->_debug($msg);
    return;
}

=back

=cut

no Mouse::Role;

1;