| 1 | 7 | ▶ | use v5.20; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 3 | package Log::Fmt 3.101; | ||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: a little parser and emitter of structured log lines | ||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use experimental 'postderef'; # Not dangerous. Is accepted without changed. | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||
| 8 | 7 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Params::Util qw(_ARRAY0 _HASH0 _CODELIKE); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use Scalar::Util qw(refaddr); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use String::Flogger (); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||
| 13 | #pod =head1 OVERVIEW | ||||||||||||||||||||||||||||||||||||||||
| 14 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 15 | #pod This library primarily exists to service L<Log::Dispatchouli>'s C<log_event> | ||||||||||||||||||||||||||||||||||||||||
| 16 | #pod methods. It converts an arrayref of key/value pairs to a string that a human | ||||||||||||||||||||||||||||||||||||||||
| 17 | #pod can scan tolerably well, and which a machine can parse about as well. It can | ||||||||||||||||||||||||||||||||||||||||
| 18 | #pod also do that tolerably-okay parsing for you. | ||||||||||||||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 20 | #pod =head1 SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||
| 21 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 22 | #pod =head2 The logfmt text format | ||||||||||||||||||||||||||||||||||||||||
| 23 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 24 | #pod Although quite a few tools exist for managing C<logfmt>, there is no spec-like | ||||||||||||||||||||||||||||||||||||||||
| 25 | #pod document for it. Because you may require multiple implementations, a | ||||||||||||||||||||||||||||||||||||||||
| 26 | #pod specification can be helpful. | ||||||||||||||||||||||||||||||||||||||||
| 27 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 28 | #pod Every logfmt event is a sequence of pairs in the form C<key=value>. Pairs are | ||||||||||||||||||||||||||||||||||||||||
| 29 | #pod separated by a single space. | ||||||||||||||||||||||||||||||||||||||||
| 30 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 31 | #pod event = pair *(WSP pair) | ||||||||||||||||||||||||||||||||||||||||
| 32 | #pod pair = key "=" value | ||||||||||||||||||||||||||||||||||||||||
| 33 | #pod okchr = %x21 / %x23-3c / %x3e-5b / %x5d-7e ; VCHAR minus \ and " and = | ||||||||||||||||||||||||||||||||||||||||
| 34 | #pod key = 1*(okchr) | ||||||||||||||||||||||||||||||||||||||||
| 35 | #pod value = key / quoted | ||||||||||||||||||||||||||||||||||||||||
| 36 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 37 | #pod quoted = DQUOTE *( escaped / quoted-ok / okchr / eightbit ) DQUOTE | ||||||||||||||||||||||||||||||||||||||||
| 38 | #pod escaped = escaped-special / escaped-hex | ||||||||||||||||||||||||||||||||||||||||
| 39 | #pod escaped-special = "\\" / "\n" / "\r" / "\t" / ("\" DQUOTE) | ||||||||||||||||||||||||||||||||||||||||
| 40 | #pod escaped-hex = "\x{" 2HEXDIG "}" ; lowercase forms okay also | ||||||||||||||||||||||||||||||||||||||||
| 41 | #pod quoted-ok = SP / "=" | ||||||||||||||||||||||||||||||||||||||||
| 42 | #pod eightbit = %x80-ff | ||||||||||||||||||||||||||||||||||||||||
| 43 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 44 | #pod When formatting a value, if a value is already a valid C<key> token, use it | ||||||||||||||||||||||||||||||||||||||||
| 45 | #pod without further quoting. | ||||||||||||||||||||||||||||||||||||||||
| 46 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 47 | #pod =head2 Quoting a Unicode string | ||||||||||||||||||||||||||||||||||||||||
| 48 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 49 | #pod It is preferable to build quoted values from a Unicode string, because it's | ||||||||||||||||||||||||||||||||||||||||
| 50 | #pod possible to know whether a given codepoint is a non-ASCII unsafe character, | ||||||||||||||||||||||||||||||||||||||||
| 51 | #pod like C<LINE SEPARATOR>. Safe non-ASCII characters can be directly UTF-8 | ||||||||||||||||||||||||||||||||||||||||
| 52 | #pod encoded, rather than quoted with C<\x{...}>. In that way, viewing logfmt events | ||||||||||||||||||||||||||||||||||||||||
| 53 | #pod with a standard terminal can show something like: | ||||||||||||||||||||||||||||||||||||||||
| 54 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 55 | #pod user.name="Jürgen" | ||||||||||||||||||||||||||||||||||||||||
| 56 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 57 | #pod To generate a C<quoted> from a Unicode string, for each codepoint: | ||||||||||||||||||||||||||||||||||||||||
| 58 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 59 | #pod =begin :list | ||||||||||||||||||||||||||||||||||||||||
| 60 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 61 | #pod * convert C<\> to C<\\> | ||||||||||||||||||||||||||||||||||||||||
| 62 | #pod * convert C<"> to C<\"> | ||||||||||||||||||||||||||||||||||||||||
| 63 | #pod * convert a newline (U+000A) to C<\n> | ||||||||||||||||||||||||||||||||||||||||
| 64 | #pod * convert a carriage return (U+000D) to C<\r> | ||||||||||||||||||||||||||||||||||||||||
| 65 | #pod * convert a character tabulation (U+0009) to C<\t> | ||||||||||||||||||||||||||||||||||||||||
| 66 | #pod * for any control character (by general category) or vertical newline: | ||||||||||||||||||||||||||||||||||||||||
| 67 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 68 | #pod =begin :list | ||||||||||||||||||||||||||||||||||||||||
| 69 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 70 | #pod * encode the character into a UTF-8 bytestring | ||||||||||||||||||||||||||||||||||||||||
| 71 | #pod * convert each byte in the bytestring into C<\x{...}> form | ||||||||||||||||||||||||||||||||||||||||
| 72 | #pod * use that sequence of C<\x{...}> codes in place of the replaced character | ||||||||||||||||||||||||||||||||||||||||
| 73 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 74 | #pod =end :list | ||||||||||||||||||||||||||||||||||||||||
| 75 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 76 | #pod =end :list | ||||||||||||||||||||||||||||||||||||||||
| 77 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 78 | #pod Finally, UTF-8 encode the entire string and wrap it in double qoutes. | ||||||||||||||||||||||||||||||||||||||||
| 79 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 80 | #pod B<This Perl implementation assumes that all string values to be encoded are | ||||||||||||||||||||||||||||||||||||||||
| 81 | #pod character strings!> | ||||||||||||||||||||||||||||||||||||||||
| 82 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 83 | #pod =head3 Quoting a bytestring | ||||||||||||||||||||||||||||||||||||||||
| 84 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 85 | #pod Encoding a Unicode string is preferable, but may not be practical. In those | ||||||||||||||||||||||||||||||||||||||||
| 86 | #pod cases when you have only a byte sequence, apply these steps. | ||||||||||||||||||||||||||||||||||||||||
| 87 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 88 | #pod For each byte (using ASCII conventions): | ||||||||||||||||||||||||||||||||||||||||
| 89 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 90 | #pod =for :list | ||||||||||||||||||||||||||||||||||||||||
| 91 | #pod * convert C<\> to C<\\> | ||||||||||||||||||||||||||||||||||||||||
| 92 | #pod * convert C<"> to C<\"> | ||||||||||||||||||||||||||||||||||||||||
| 93 | #pod * convert a newline (C<%0a>) to C<\n> | ||||||||||||||||||||||||||||||||||||||||
| 94 | #pod * convert a carriage return (C<%0d>) to C<\r> | ||||||||||||||||||||||||||||||||||||||||
| 95 | #pod * convert a character tabulation (C<%x09>) to C<\t> | ||||||||||||||||||||||||||||||||||||||||
| 96 | #pod * convert any control character (C<%x00-1f / %x7f>) to the C<\x{...}> form | ||||||||||||||||||||||||||||||||||||||||
| 97 | #pod * convert any non-ASCII byte (C<%x80-ff>) to the C<\x{...}> form | ||||||||||||||||||||||||||||||||||||||||
| 98 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 99 | #pod Finally, wrap the string in double quotes. | ||||||||||||||||||||||||||||||||||||||||
| 100 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 101 | #pod =head3 Special values | ||||||||||||||||||||||||||||||||||||||||
| 102 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 103 | #pod There are no reserved or sentinel values for undefined values, null pointers, | ||||||||||||||||||||||||||||||||||||||||
| 104 | #pod or the like. Convention is to use a single identifier surrounded by tildes to | ||||||||||||||||||||||||||||||||||||||||
| 105 | #pod indicate such a value. For example, given a null pointer for the C<user.name> | ||||||||||||||||||||||||||||||||||||||||
| 106 | #pod value, a system might log: | ||||||||||||||||||||||||||||||||||||||||
| 107 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 108 | #pod user.name=~null~ | ||||||||||||||||||||||||||||||||||||||||
| 109 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 110 | #pod If C<~null~> was a valid username, there would be ambiguity here. The choice | ||||||||||||||||||||||||||||||||||||||||
| 111 | #pod of exact sentinels is left to the implementing system. | ||||||||||||||||||||||||||||||||||||||||
| 112 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 113 | #pod =method format_event_string | ||||||||||||||||||||||||||||||||||||||||
| 114 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 115 | #pod my $octets = Log::Fmt->format_event_string([ | ||||||||||||||||||||||||||||||||||||||||
| 116 | #pod key1 => $value1, | ||||||||||||||||||||||||||||||||||||||||
| 117 | #pod key2 => $value2, | ||||||||||||||||||||||||||||||||||||||||
| 118 | #pod ]); | ||||||||||||||||||||||||||||||||||||||||
| 119 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 120 | #pod Note especially that if any value to encode is a reference I<to a reference>, | ||||||||||||||||||||||||||||||||||||||||
| 121 | #pod then String::Flogger is used to encode the referenced value. This means you | ||||||||||||||||||||||||||||||||||||||||
| 122 | #pod can embed, in your logfmt, a JSON dump of a structure by passing a reference to | ||||||||||||||||||||||||||||||||||||||||
| 123 | #pod the structure, instead of passing the structure itself. | ||||||||||||||||||||||||||||||||||||||||
| 124 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 125 | #pod String values are assumed to be character strings, and will be UTF-8 encoded as | ||||||||||||||||||||||||||||||||||||||||
| 126 | #pod part of the formatting process. | ||||||||||||||||||||||||||||||||||||||||
| 127 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 128 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||
| 130 | # okchr = %x21 / %x23-3c / %x3e-5b / %x5d-7e ; VCHAR minus \ and " and = | ||||||||||||||||||||||||||||||||||||||||
| 131 | # key = 1*(okchr) | ||||||||||||||||||||||||||||||||||||||||
| 132 | # value = key / quoted | ||||||||||||||||||||||||||||||||||||||||
| 133 | # | ||||||||||||||||||||||||||||||||||||||||
| 134 | # Remember to update the tr/// expression in _pairs_to_kvstr_aref if this | ||||||||||||||||||||||||||||||||||||||||
| 135 | # changes! | ||||||||||||||||||||||||||||||||||||||||
| 136 | 7 | my $KEY_RE = qr{[\x21\x23-\x3c\x3e-\x5b\x5d-\x7e]+}; | |||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||
| 138 | 4 | ▶ | sub _escape_unprintable { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_escape_unprintable: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 139 | 4 | my ($chr) = @_; | |||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||
| 141 | return join q{}, | ||||||||||||||||||||||||||||||||||||||||
| 142 | 4 | map {; sprintf '\\x{%02x}', ord } | |||||||||||||||||||||||||||||||||||||||
| 143 | split //, Encode::encode('utf-8', $chr, Encode::FB_DEFAULT); | ||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||
| 146 | 24 | ▶ | sub _quote_string { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_quote_string: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 147 | 24 | my ($string) = @_; | |||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||
| 149 | 24 | $string =~ s{\\}{\\\\}g; | |||||||||||||||||||||||||||||||||||||||
| 150 | 24 | $string =~ s{"}{\\"}g; | |||||||||||||||||||||||||||||||||||||||
| 151 | 24 | $string =~ s{\x09}{\\t}g; | |||||||||||||||||||||||||||||||||||||||
| 152 | 24 | $string =~ s{\x0A}{\\n}g; | |||||||||||||||||||||||||||||||||||||||
| 153 | 24 | $string =~ s{\x0D}{\\r}g; | |||||||||||||||||||||||||||||||||||||||
| 154 | 24 | $string =~ s{([\pC\v])}{_escape_unprintable($1)}ge; | |||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||
| 156 | 24 | $string = Encode::encode('utf-8', $string, Encode::FB_DEFAULT); | |||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||
| 158 | 24 | return qq{"$string"}; | |||||||||||||||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||
| 161 | 3 | ▶ | sub string_flogger { 'String::Flogger' } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
string_flogger: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||
| 163 | 86 | ▶ | sub _pairs_to_kvstr_aref { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pairs_to_kvstr_aref: called CC=14 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||
| 164 | 86 | my ($self, $aref, $seen, $prefix) = @_; | |||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||
| 166 | 86 | ▶ | $seen //= {}; | ||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$seen //= {}
MC/DC100%1/1
$seen //= {}
$seen
Truth table100%2/2
A
A= $seen
| |||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||
| 168 | 86 | my @kvstrs; | |||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||
| 170 | 86 | ▶ | KEY: for (my $i = 0; $i < @$aref; $i += 2) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 171 | # replace non-ident-safe chars with ? | ||||||||||||||||||||||||||||||||||||||||
| 172 | 148 | ▶ | my $key = length $aref->[$i] ? "$aref->[$i]" : '~'; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 173 | 148 | $key =~ tr{\x21\x23-\x3c\x3e-\x5b\x5d-\x7e}{?}c; | |||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||
| 175 | # If the prefix is "" you can end up with a pair like ".foo=1" which is | ||||||||||||||||||||||||||||||||||||||||
| 176 | # weird but probably best. And that means you could end up with | ||||||||||||||||||||||||||||||||||||||||
| 177 | # "foo..bar=1" which is also weird, but still probably for the best. | ||||||||||||||||||||||||||||||||||||||||
| 178 | 148 | ▶ | $key = "$prefix.$key" if defined $prefix; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||
| 180 | 148 | my $value = $aref->[$i+1]; | |||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||
| 182 | 148 | ▶ | if (_CODELIKE $value) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 183 | 5 | $value = $value->(); | |||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||
| 186 | 148 | ▶ | if (ref $value && ref $value eq 'REF') { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $value and ref $value eq "REF"
MC/DC100%2/2
ref $value and ref $value eq "REF"
ref $value
ref $value eq "REF"
Truth table100%3/3
A and B
A= ref $valueB= ref $value eq "REF"
| |||||||||||||||||||||||||||||||||||||||||
| 187 | 3 | $value = $self->string_flogger->flog([ '%s', $$value ]); | |||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||
| 190 | 148 | ▶ | if (! defined $value) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||
| 191 | 1 | $value = '~missing~'; | |||||||||||||||||||||||||||||||||||||||
| 192 | } elsif (ref $value) { | ||||||||||||||||||||||||||||||||||||||||
| 193 | 34 | my $refaddr = refaddr $value; | |||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||
| 195 | 34 | ▶ | if ($seen->{ $refaddr }) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||
| 196 | 2 | $value = $seen->{ $refaddr }; | |||||||||||||||||||||||||||||||||||||||
| 197 | } elsif (_ARRAY0($value)) { | ||||||||||||||||||||||||||||||||||||||||
| 198 | 13 | $seen->{ $refaddr } = "&$key"; | |||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||
| 200 | push @kvstrs, $self->_pairs_to_kvstr_aref( | ||||||||||||||||||||||||||||||||||||||||
| 201 | 13 | [ map {; $_ => $value->[$_] } (0 .. $#$value) ], | |||||||||||||||||||||||||||||||||||||||
| 202 | $seen, | ||||||||||||||||||||||||||||||||||||||||
| 203 | $key, | ||||||||||||||||||||||||||||||||||||||||
| 204 | )->@*; | ||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||
| 206 | 13 | next KEY; | |||||||||||||||||||||||||||||||||||||||
| 207 | } elsif (_HASH0($value)) { | ||||||||||||||||||||||||||||||||||||||||
| 208 | 11 | $seen->{ $refaddr } = "&$key"; | |||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||
| 210 | push @kvstrs, $self->_pairs_to_kvstr_aref( | ||||||||||||||||||||||||||||||||||||||||
| 211 | 11 | [ $value->%{ sort keys %$value } ], | |||||||||||||||||||||||||||||||||||||||
| 212 | $seen, | ||||||||||||||||||||||||||||||||||||||||
| 213 | $key, | ||||||||||||||||||||||||||||||||||||||||
| 214 | )->@*; | ||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||
| 216 | 11 | next KEY; | |||||||||||||||||||||||||||||||||||||||
| 217 | } else { | ||||||||||||||||||||||||||||||||||||||||
| 218 | 8 | $value = "$value"; # Meh. | |||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||
| 222 | 124 | ▶ | my $str = "$key=" | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 223 | . ($value =~ /\A$KEY_RE\z/ | ||||||||||||||||||||||||||||||||||||||||
| 224 | ? "$value" | ||||||||||||||||||||||||||||||||||||||||
| 225 | : _quote_string($value)); | ||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||
| 227 | 124 | push @kvstrs, $str; | |||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||
| 230 | 86 | return \@kvstrs; | |||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||
| 233 | 5 | ▶ | sub format_event_string { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_event_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 234 | 5 | my ($self, $aref) = @_; | |||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||
| 236 | 5 | return join q{ }, $self->_pairs_to_kvstr_aref($aref)->@*; | |||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||
| 239 | #pod =method parse_event_string | ||||||||||||||||||||||||||||||||||||||||
| 240 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 241 | #pod my $kv_pairs = Log::Fmt->parse_event_string($octets); | ||||||||||||||||||||||||||||||||||||||||
| 242 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 243 | #pod Given the kind of (byte) string emitted by C<format_event_string>, this method | ||||||||||||||||||||||||||||||||||||||||
| 244 | #pod returns a reference to an array of key/value pairs. After being unquoted, | ||||||||||||||||||||||||||||||||||||||||
| 245 | #pod value strings will be UTF-8 decoded into character strings. | ||||||||||||||||||||||||||||||||||||||||
| 246 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 247 | #pod This isn't exactly a round trip. First off, the formatting can change illegal | ||||||||||||||||||||||||||||||||||||||||
| 248 | #pod keys by replacing characters with question marks, or replacing empty strings | ||||||||||||||||||||||||||||||||||||||||
| 249 | #pod with tildes. Secondly, the formatter will expand some values like arrayrefs | ||||||||||||||||||||||||||||||||||||||||
| 250 | #pod and hashrefs into multiple keys, but the parser will not recombined those keys | ||||||||||||||||||||||||||||||||||||||||
| 251 | #pod into structures. Also, there might be other asymmetric conversions. That | ||||||||||||||||||||||||||||||||||||||||
| 252 | #pod said, the string escaping done by the formatter should correctly reverse. | ||||||||||||||||||||||||||||||||||||||||
| 253 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 254 | #pod If the input string is badly formed, hunks that don't appear to be value | ||||||||||||||||||||||||||||||||||||||||
| 255 | #pod key/value pairs will be presented as values for the key C<junk>. | ||||||||||||||||||||||||||||||||||||||||
| 256 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 257 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||
| 259 | 14 | ▶ | sub parse_event_string { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_event_string: called CC=12 SCAR=25.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 260 | 14 | my ($self, $octets) = @_; | |||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||
| 262 | 14 | my @result; | |||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||
| 264 | 14 | ▶ | HUNK: while (length $octets) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 265 | 33 | ▶ | if ($octets =~ s/\A($KEY_RE)=($KEY_RE)(?:\s+|\z)//) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 266 | 19 | push @result, $1, $2; | |||||||||||||||||||||||||||||||||||||||
| 267 | 19 | next HUNK; | |||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||
| 270 | 14 | ▶ | if ($octets =~ s/\A($KEY_RE)="((\\\\|\\"|[^"])*?)"(?:\s+|\z)//) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 271 | 10 | my $key = $1; | |||||||||||||||||||||||||||||||||||||||
| 272 | 10 | my $qstring = $2; | |||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||
| 274 | 10 | $qstring =~ s{ | |||||||||||||||||||||||||||||||||||||||
| 275 | ( \\\\ | \\["nrt] | (\\x)\{([[:xdigit:]]{2})\} ) | ||||||||||||||||||||||||||||||||||||||||
| 276 | } | ||||||||||||||||||||||||||||||||||||||||
| 277 | { | ||||||||||||||||||||||||||||||||||||||||
| 278 | 19 | ▶ | $1 eq "\\\\" ? "\\" | ||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
Condition50%1/2
$2 // ""
MC/DC0%0/1
$2 // ""
$2
Truth table50%1/2
A
A= $2
| |||||||||||||||||||||||||||||||||||||||||
| 279 | : $1 eq "\\\"" ? q{"} | ||||||||||||||||||||||||||||||||||||||||
| 280 | : $1 eq "\\n" ? qq{\n} | ||||||||||||||||||||||||||||||||||||||||
| 281 | : $1 eq "\\r" ? qq{\r} | ||||||||||||||||||||||||||||||||||||||||
| 282 | : $1 eq "\\t" ? qq{\t} | ||||||||||||||||||||||||||||||||||||||||
| 283 | : ($2//'') eq "\\x" ? chr(hex("0x$3")) | ||||||||||||||||||||||||||||||||||||||||
| 284 | : $1 | ||||||||||||||||||||||||||||||||||||||||
| 285 | }gex; | ||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||
| 287 | 10 | push @result, $key, Encode::decode('utf-8', $qstring, Encode::FB_DEFAULT); | |||||||||||||||||||||||||||||||||||||||
| 288 | 10 | next HUNK; | |||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||
| 291 | 4 | ▶ | if ($octets =~ s/\A(\S+)(?:\s+|\z)//) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 292 | 4 | push @result, 'junk', $1; | |||||||||||||||||||||||||||||||||||||||
| 293 | 4 | next HUNK; | |||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||
| 296 | # I hope this is unreachable. -- rjbs, 2022-11-03 | ||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | push (@result, 'junk', $octets, aborted => 1); | |||||||||||||||||||||||||||||||||||||||
| 298 | 0 | last HUNK; | |||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||
| 301 | 14 | return \@result; | |||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||
| 304 | #pod =method parse_event_string_as_hash | ||||||||||||||||||||||||||||||||||||||||
| 305 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 306 | #pod my $hashref = Log::Fmt->parse_event_string_as_hash($line); | ||||||||||||||||||||||||||||||||||||||||
| 307 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 308 | #pod This parses the given line as logfmt, then puts the key/value pairs into a hash | ||||||||||||||||||||||||||||||||||||||||
| 309 | #pod and returns a reference to it. | ||||||||||||||||||||||||||||||||||||||||
| 310 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 311 | #pod Because nothing prevents a single key from appearing more than once, you should | ||||||||||||||||||||||||||||||||||||||||
| 312 | #pod use this with the understanding that data could be lost. No guarantee is made | ||||||||||||||||||||||||||||||||||||||||
| 313 | #pod of which value will be preserved. | ||||||||||||||||||||||||||||||||||||||||
| 314 | #pod | ||||||||||||||||||||||||||||||||||||||||
| 315 | #pod =cut | ||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||
| 317 | 1 | ▶ | sub parse_event_string_as_hash { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_event_string_as_hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 318 | 1 | my ($self, $octets) = @_; | |||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||
| 320 | 1 | return { $self->parse_event_string($octets)->@* }; | |||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||
| 323 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||
| 325 | __END__ | ||||||||||||||||||||||||||||||||||||||||