Netdata/Util.pm

filter: Statementstmt 100% 31 / 31 Branchbran 100% 8 / 8 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 94.0% 63 / 67 CC 9 SCAR 17.6
CC
9
Cov
96%
logf
9.0
BEGIN
1.0
BEGIN
1.0
CRAP 5.8
« Netdata/Collector/HTTP.pm ↑ Summary Netdata.pm »
1 package Mojo::Netdata::Util;
2 5 ▶use Mojo::Base -strict, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 5 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Mojo::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Mojo::JSON qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our @EXPORT_OK = qw(logf safe_id);
10 5 our $STDERR = \*STDERR; # useful for testing
11  
12 28 ▶sub logf ($level, $format, @args) {
Subroutine logf: called CC=9 SCAR=22.0
Pod documented
13 28 ▶ return 1 if $ENV{HARNESS_ACTIVE} and !$ENV{HARNESS_IS_VERBOSE};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"HARNESS_ACTIVE"} and not $ENV{"HARNESS_IS_VERBOSE"} 22 6 28
Condition66%2/3
$ENV{"HARNESS_ACTIVE"} and not $ENV{"HARNESS_IS_VERBOSE"}
!ll&&!rl&&r
6022
MC/DC50%1/2
$ENV{"HARNESS_ACTIVE"} and not $ENV{"HARNESS_IS_VERBOSE"}
$ENV{"HARNESS_ACTIVE"} not $ENV{"HARNESS_IS_VERBOSE"}
Truth table66%2/3
A and not(B)
A= $ENV{"HARNESS_ACTIVE"}B= not $ENV{"HARNESS_IS_VERBOSE"}
ABresult
0X0
100
111
14 6 ▶ return 1 if $level eq 'debug' and !($ENV{NETDATA_DEBUG} || $ENV{HARNESS_IS_VERBOSE});
Branch: 1 branch
BranchTrueFalseTotal
if $level eq "debug" and not $ENV{"NETDATA_DEBUG"} || $ENV{"HARNESS_IS_VERBOSE"} 1 5 6
Condition83%5/6
$level eq "debug" and not $ENV{"NETDATA_DEBUG"} || $ENV{"HARNESS_IS_VERBOSE"}
!ll&&!rl&&r
411
$ENV{'NETDATA_DEBUG'} || $ENV{'HARNESS_IS_VERBOSE'}
l!l&&r!l&&!r
101
MC/DC66%2/3
$level eq "debug" and not $ENV{"NETDATA_DEBUG"} || $ENV{"HARNESS_IS_VERBOSE"}
$level eq "debug" $ENV{'NETDATA_DEBUG'} $ENV{'HARNESS_IS_VERBOSE'}
Truth table75%3/4
A and not(B || C)
A= $level eq "debug"B= $ENV{'NETDATA_DEBUG'}C= $ENV{'HARNESS_IS_VERBOSE'}
ABCresult
0XX0
11X0
1010
1001
15  
16 5 my $module_name = caller;
17 5 my ($s, $m, $h, $day, $month, $year) = localtime time;
18  
19 5 state $program_name = Mojo::File->new($0)->basename;
20 5 printf {$STDERR} "%s-%02s-%02s %02s:%02s:%02s: %s: %s: %s: $format\n", $year + 1900, $month + 1,
21 $day, $h, $m, $s, $program_name, uc $level, $module_name,
22 5 ▶ map { overload::Method($_, q("")) ? "$_" : !defined $_ || ref $_ ? encode_json $_ : $_ } @args;
Branch: 2 branches
BranchTrueFalseTotal
overload::Method($_, '""') ? : 1 4 5
!defined($_) || ref $_ ? : 2 2 4
Condition100%3/3
not defined $_ or ref $_
l!l&&r!l&&!r
112
MC/DC100%2/2
not defined $_ or ref $_
not defined $_ ref $_
Truth table100%3/3
not(A) or B
A= not defined $_B= ref $_
ABresult
1X1
011
000
23 5 return 1;
24 }
25  
26 88 ▶sub safe_id ($str) {
Subroutine safe_id: called CC=1 SCAR=0.0
Pod documented
27 88 $str =~ s![^A-Za-z0-9]!_!g;
28 88 $str =~ s!_+$!!g;
29 88 $str =~ s!^_+!!g;
30 88 return $str;
31 }
32  
33 5 1;
34  
35 =encoding utf8
36  
37 =head1 NAME
38  
39 Mojo::Netdata::Util - Utility functions for Mojo::Netdata
40  
41 =head1 SYNOPSIS
42  
43 use Mojo::Netdata::Util qw(safe_id);
44 print safe_id 'Not%co.ol';
45  
46 =head1 DESCRIPTION
47  
48 L<Mojo::Netdata::Util> as functions that can be useful when working with
49 L<Mojo::Netdata::Collector> classes.
50  
51 =head1 EXPORTED FUNCTIONS
52  
53 =head2 logf
54  
55 logf $level, $format, @args;
56  
57 Used to log messages to STDERR. C<$level> can be "debug", "info", "warnings",
58 "error", "fatal". Any references and undefined values in C<@args> will be
59 serialized using L<Mojo::JSON/encode_json>.
60  
61 =head2 safe_id
62  
63 $str = safe_id $str;
64  
65 Turns an "unsafe" string into a string you can use for things like "id" or
66 "type". This is called by L<Mojo::Netdata::Chart/to_string> and
67 L<Mojo::Netdata::Chart/data_to_string> to make sure the output strings are
68 safe.
69  
70 =head1 SEE ALSO
71  
72 L<Mojo::Netdata>.
73  
74 =cut
« Netdata/Collector/HTTP.pm ↑ Summary Netdata.pm »