File Coverage

blib/lib/Devel/Probe.pm
Criterion Covered Total %
statement 63 63 100.0
branch 19 24 79.1
condition 9 11 81.8
subroutine 10 10 100.0
pod 0 3 0.0
total 101 111 90.9


line stmt bran cond sub pod time code
1             package Devel::Probe;
2 8     8   593461 use strict;
  8         85  
  8         224  
3 8     8   38 use warnings;
  8         14  
  8         232  
4              
5 8     8   38 use XSLoader;
  8         11  
  8         176  
6 8     8   5595 use Path::Tiny ();
  8         91781  
  8         196  
7 8     8   4987 use JSON::XS ();
  8         33368  
  8         271  
8              
9             use constant {
10 8         4286 PROBE_CONFIG_NAME => '/tmp/devel-probe-config.json',
11             PROBE_SIGNAL_NAME => 'HUP',
12 8     8   55 };
  8         14  
13              
14             our $VERSION = '0.000003';
15             XSLoader::load( 'Devel::Probe', $VERSION );
16              
17             my %known_options = map +( $_ => 1 ), qw/
18             signal_name
19             skip_install
20             config_file
21             check_config_file
22             /;
23              
24             my $config_file;
25             my $signal_name;
26              
27             sub import {
28 8     8   69 my ($class, @opts) = @_;
29              
30 8         19 my %options = @opts;
31 8         30 foreach my $option (keys %options) {
32 8 50       33 die "Unrecognized option $option" unless exists $known_options{$option};
33             }
34              
35 8         38 set_config_name($options{config_file});
36 8         34 set_signal_name($options{signal_name});
37              
38 8 100       1520 return if $options{skip_install};
39 7         32 Devel::Probe::install();
40              
41 7 100       6067 return unless $options{check_config_file};
42 2         6 check_config_file("import");
43             }
44              
45             sub set_config_name {
46 9     9 0 16229 my ($name) = @_;
47              
48 9   100     74 $config_file = $name // PROBE_CONFIG_NAME;
49             }
50              
51             sub set_signal_name {
52 8     8 0 36 my ($name) = @_;
53              
54 8   100     47 $signal_name = $name // PROBE_SIGNAL_NAME;
55 8         211 $SIG{$signal_name} = \&Devel::Probe::check_config_file;
56             }
57              
58             sub check_config_file {
59 3     3 0 1018 my ($reason) = @_;
60              
61 3   100     13 $reason //= 'manual';
62 3         28 Devel::Probe::disable();
63              
64 3         14 my $path = Path::Tiny::path($config_file);
65 3 100 66     217 return unless $path && $path->is_file();
66 1         25 my $contents = $path->slurp_utf8();
67 1 50       1255 return unless $contents;
68 1         26 my $data = JSON::XS->new->utf8->decode($contents);
69 1 50       5 return unless $data;
70              
71 1         3 foreach my $action (@{ $data->{actions} }) {
  1         3  
72 5 100       10 if ($action->{action} eq 'enable') {
73 1         4 Devel::Probe::enable();
74 1         8 next;
75             }
76 4 100       9 if ($action->{action} eq 'disable') {
77 1         3 Devel::Probe::disable();
78 1         3 next;
79             }
80 3 100       7 if ($action->{action} eq 'dump') {
81 1         53 Devel::Probe::dump();
82 1         4 next;
83             }
84 2 100       5 if ($action->{action} eq 'clear') {
85 1         5 Devel::Probe::clear();
86 1         2 next;
87             }
88 1 50       4 if ($action->{action} eq 'define') {
89 1         3 my $file = $action->{file};
90 1 50       3 next unless $file;
91 1   50     2 foreach my $line (@{ $action->{lines} // [] }) {
  1         3  
92 2         11 Devel::Probe::add_probe($file, $line);
93             }
94 1         2 next;
95             }
96             }
97             }
98              
99              
100             1;
101             __END__