| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mojo::Netdata::Util; | 
| 2 | 5 |  |  | 5 |  | 200632 | use Mojo::Base -strict, -signatures; | 
|  | 5 |  |  |  |  | 141473 |  | 
|  | 5 |  |  |  |  | 61 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 5 |  |  | 5 |  | 4233 | use overload (); | 
|  | 5 |  |  |  |  | 12 |  | 
|  | 5 |  |  |  |  | 83 |  | 
| 5 | 5 |  |  | 5 |  | 24 | use Exporter qw(import); | 
|  | 5 |  |  |  |  | 6 |  | 
|  | 5 |  |  |  |  | 165 |  | 
| 6 | 5 |  |  | 5 |  | 1453 | use Mojo::File; | 
|  | 5 |  |  |  |  | 90340 |  | 
|  | 5 |  |  |  |  | 262 |  | 
| 7 | 5 |  |  | 5 |  | 2397 | use Mojo::JSON qw(encode_json); | 
|  | 5 |  |  |  |  | 99920 |  | 
|  | 5 |  |  |  |  | 1950 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | our @EXPORT_OK = qw(logf safe_id); | 
| 10 |  |  |  |  |  |  | our $STDERR    = \*STDERR;           # useful for testing | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 24 |  |  | 24 | 1 | 6576 | sub logf ($level, $format, @args) { | 
|  | 24 |  |  |  |  | 47 |  | 
|  | 24 |  |  |  |  | 39 |  | 
|  | 24 |  |  |  |  | 45 |  | 
|  | 24 |  |  |  |  | 28 |  | 
| 13 | 24 | 100 | 66 |  |  | 130 | return 1 if $ENV{HARNESS_ACTIVE} and !$ENV{HARNESS_IS_VERBOSE}; | 
| 14 | 6 | 100 | 66 |  |  | 24 | return 1 if $level eq 'debug' and !$ENV{HARNESS_IS_VERBOSE} and !$ENV{NETDATA_DEBUG_FLAGS}; | 
|  |  |  | 100 |  |  |  |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 | 5 |  |  |  |  | 9 | my $module_name = caller; | 
| 17 | 5 |  |  |  |  | 117 | my ($s, $m, $h, $day, $month, $year) = localtime time; | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 5 |  |  |  |  | 27 | state $program_name = Mojo::File->new($0)->basename; | 
| 20 | 5 |  |  |  |  | 28 | 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 | 100 | 100 |  |  | 107 | map { overload::Method($_, q("")) ? "$_" : !defined $_ || ref $_ ? encode_json $_ : $_ } @args; | 
|  | 5 | 100 |  |  |  | 46 |  | 
| 23 | 5 |  |  |  |  | 902 | return 1; | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 125 |  |  | 125 | 1 | 3375 | sub safe_id ($str) { | 
|  | 125 |  |  |  |  | 149 |  | 
|  | 125 |  |  |  |  | 139 |  | 
| 27 | 125 |  |  |  |  | 399 | $str =~ s![^A-Za-z0-9]!_!g; | 
| 28 | 125 |  |  |  |  | 265 | $str =~ s!_+$!!g; | 
| 29 | 125 |  |  |  |  | 170 | $str =~ s!^_+!!g; | 
| 30 | 125 |  |  |  |  | 338 | return $str; | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | 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 as functions that can be useful when working with | 
| 49 |  |  |  |  |  |  | L 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. | 
| 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 and | 
| 67 |  |  |  |  |  |  | L to make sure the output strings are | 
| 68 |  |  |  |  |  |  | safe. | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | L. | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =cut |