include/punk/punk_log.h

filter: Statementstmt 99.0% 298 / 303 (2 uncoverable) Branchbran 78.8% 280 / 382 (26 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.7% 578 / 685 (28 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_frozen.h ↑ Summary include/punk/punk_sessionstore.h »
1 /* punk_log.h - a level-based logger, in C.
2 *
3 * $c->log and $app->log return a Punk::Logger (a blessed hash { level, format,
4 * to, ctx }); its debug/info/warn/error/fatal methods format a line and emit it
5 * to the server's psgix.logger when there is one, else STDERR (a `to` coderef
6 * or filehandle overrides). A request logger (ctx set) adds the method/path.
7 * Below-threshold calls return before any formatting.
8 *
9 * A lone unblessed hashref is a record rather than a message: its `message`
10 * key is the message and the rest are fields, merged into the object under
11 * `format => 'json'` and rendered as sorted logfmt pairs otherwise. The six
12 * names the house owns (pl_reserved) cannot be taken by a field.
13 *
14 * Must be included after punk_context.h (pcx_* / PCX_* / frj).
15 */
16  
17 #ifndef PUNK_LOG_H
18 #define PUNK_LOG_H
19  
20 #include <time.h>
21  
22 enum { PL_DEBUG = 0, PL_INFO = 1, PL_WARN = 2, PL_ERROR = 3, PL_FATAL = 4 };
23 static const char *const PL_NAMES[] = { "debug", "info", "warn", "error", "fatal" };
24  
25 23 static int pl_level_num(pTHX_ SV *sv) {
26 /*
27 # uncoverable branch true
28 # uncoverable condition left count:1 class:ignore_covered_err */
29 23 ▶ if (!SvOK(sv)) return PL_INFO;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) return PL_INFO; 0 23 23
30 23 ▶ if (SvIOK(sv) && !SvPOK(sv)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvIOK(sv) && !SvPOK(sv)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 20 23
if (SvIOK(sv) && !SvPOK(sv)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 3 0 3
31 3 IV n = SvIV(sv);
32 3 ▶ return n < 0 ? 0 : n > 4 ? 4 : (int)n;
Branch: 2 branches
BranchTrueFalseTotal
return n < 0 ? 0 : n > 4 ? 4 : (int)n; 2 1 3
return n < 0 ? 0 : n > 4 ? 4 : (int)n; 1 1 2
33 }
34 {
35 20 STRLEN l; const char *s = SvPV_const(sv, l);
36 int i;
37 55 ▶ for (i = 0; i < 5; i++) if (strEQ(s, PL_NAMES[i])) return i;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < 5; i++) if (strEQ(s, PL_NAMES[i])) return i; 19 35 54
for (i = 0; i < 5; i++) if (strEQ(s, PL_NAMES[i])) return i; 54 1 55
38 }
39 1 return PL_INFO;
40 }
41  
42 121 static void pl_iso_time(char *out, size_t n) {
43 struct tm tm;
44 121 time_t t = time(NULL);
45 #ifdef HAS_GMTIME_R
46 gmtime_r(&t, &tm);
47 #else
48 121 tm = *gmtime(&t);
49 #endif
50 121 my_snprintf(out, n, "%04d-%02d-%02dT%02d:%02d:%02dZ",
51 121 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
52 tm.tm_hour, tm.tm_min, tm.tm_sec);
53 121 }
54  
55 260 static HV *pl_hv(pTHX_ SV *self) {
56 260 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */ 259 1 260
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 259 259
57 1 croak("Punk::Logger: not a logger");
58 259 return (HV *)SvRV(self);
59 }
60  
61 /* The logger's configured level. Read before anything is formatted: a call
62 * below it must cost nothing, which is what Punk::Logger has always promised
63 * and, until this was hoisted out of pl_emit, did not do for the sprintf
64 * form. */
65 132 static int pl_threshold(pTHX_ SV *self) {
66 132 SV **lev = hv_fetchs(pl_hv(aTHX_ self), "level", 0);
67 /*
68 # uncoverable condition right count:1 class:ignore_covered_err
69 # uncoverable condition right count:2 class:ignore_covered_err */
70 131 ▶ return (lev && *lev) ? (int)SvIV(*lev) : PL_INFO;
Branch: 2 branches
BranchTrueFalseTotal
return (lev && *lev) ? (int)SvIV(*lev) : PL_INFO; 131 0 131
return (lev && *lev) ? (int)SvIV(*lev) : PL_INFO; 131 0 131
71 }
72  
73 /* The keys the house owns. A field carrying one of these names is dropped
74 * rather than merged, in every format - a field called `level` must not be
75 * able to forge a line's severity, and a reader must be able to trust that
76 * they mean what the logger says they mean.
77 *
78 * `trace_id` and `span_id` are here for the same reason as the rest: a
79 * telemetry layer writes them from the active span, and an application field
80 * of the same name would forge a correlation, pointing a reader at somebody
81 * else's trace. */
82 172 static int pl_reserved(const char *k, STRLEN l) {
83 172 switch (l) {
84 21 ▶ case 4: return memEQ(k, "time", 4) || memEQ(k, "path", 4);
Branch: 2 branches
BranchTrueFalseTotal
case 4: return memEQ(k, "time", 4) || memEQ(k, "path", 4); 18 3 21
case 4: return memEQ(k, "time", 4) || memEQ(k, "path", 4); 2 16 18
85 26 case 5: return memEQ(k, "level", 5);
86 5 case 6: return memEQ(k, "method", 6);
87 62 case 7: return memEQ(k, "message", 7)
88 62 ▶ || memEQ(k, "span_id", 7);
Branch: 2 branches
BranchTrueFalseTotal
|| memEQ(k, "span_id", 7); 3 59 62
|| memEQ(k, "span_id", 7); 2 1 3
89 3 case 8: return memEQ(k, "trace_id", 8);
90 2 case 10: return memEQ(k, "request_id", 10);
91 53 default: return 0;
92 }
93 }
94  
95  
96 /* Exactly what File::Raw::JSON refuses to encode: it croaks on a CODE, GLOB
97 * or Regexp reference, and takes everything else - hashes and arrays, blessed
98 * or not, and a scalar reference as a boolean. SvRXOK is frj's own regexp
99 * test, so the two agree by construction. */
100 26 static int pl_reject(pTHX_ SV *v, SV *rv) {
101 #ifdef SvRXOK
102 26 ▶ if (SvRXOK(v) || SvRXOK(rv)) return 1; /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvRXOK(v) || SvRXOK(rv)) return 1; /* # uncoverable condition left count:2 class:ignore_covered_err */ 22 4 26
if (SvRXOK(v) || SvRXOK(rv)) return 1; /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 22 22
103 #endif
104 22 ▶ return SvTYPE(rv) == SVt_PVCV || SvTYPE(rv) == SVt_PVGV;
Branch: 2 branches
BranchTrueFalseTotal
return SvTYPE(rv) == SVt_PVCV || SvTYPE(rv) == SVt_PVGV; 18 4 22
return SvTYPE(rv) == SVt_PVCV || SvTYPE(rv) == SVt_PVGV; 4 14 18
105 }
106  
107 #define PL_SCRUB_MAX 32 /* deeper than any log record; also the cycle stop */
108  
109 /* Does v hold something frj would croak on, anywhere inside it? Allocates
110 * nothing, and is the only walk the ordinary all-scalars record pays for. */
111 136 static int pl_unsafe(pTHX_ SV *v, int depth) {
112 SV *rv;
113 /*
114 # uncoverable condition left count:1 class:ignore_covered_err
115 # uncoverable condition left count:3 class:ignore_covered_err */
116 136 ▶ if (!v || !SvROK(v) || depth > PL_SCRUB_MAX) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!v || !SvROK(v) || depth > PL_SCRUB_MAX) return 0; 136 0 136
if (!v || !SvROK(v) || depth > PL_SCRUB_MAX) return 0; 15 121 136
if (!v || !SvROK(v) || depth > PL_SCRUB_MAX) return 0; 0 15 15
117 15 rv = SvRV(v);
118 15 ▶ if (pl_reject(aTHX_ v, rv)) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pl_reject(aTHX_ v, rv)) return 1; 5 10 15
119 10 ▶ if (SvTYPE(rv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVHV) { 6 4 10
120 6 HV *h = (HV *)rv;
121 HE *he;
122 6 hv_iterinit(h);
123 10 ▶ while ((he = hv_iternext(h))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(h))) { 6 4 10
124 6 ▶ if (pl_unsafe(aTHX_ HeVAL(he), depth + 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pl_unsafe(aTHX_ HeVAL(he), depth + 1)) { 2 4 6
125 2 hv_iterinit(h); /* do not leave the shared iterator
126 * parked half way through the hash */
127 2 return 1;
128 }
129 }
130 4 return 0;
131 }
132 /*
133 # uncoverable branch false
134 # uncoverable condition right count:1 class:ignore_covered_err */
135 4 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 4 0 4
136 4 AV *a = (AV *)rv;
137 4 SSize_t i, n = av_len(a) + 1;
138 12 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 8 4 12
139 8 SV **e = av_fetch(a, i, 0);
140 /*
141 # uncoverable branch true class:ignore_covered_err
142 # uncoverable condition left count:1 class:ignore_covered_err
143 # uncoverable condition right count:1 class:ignore_covered_err
144 # uncoverable condition left count:2 class:ignore_covered_err
145 # uncoverable condition right count:2 class:ignore_covered_err
146 # uncoverable condition left count:3 class:ignore_covered_err */
147 8 ▶ if (e && *e && pl_unsafe(aTHX_ *e, depth + 1)) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (e && *e && pl_unsafe(aTHX_ *e, depth + 1)) return 1; 8 0 8
if (e && *e && pl_unsafe(aTHX_ *e, depth + 1)) return 1; 8 0 8
if (e && *e && pl_unsafe(aTHX_ *e, depth + 1)) return 1; 0 8 8
148 }
149 }
150 4 return 0;
151 }
152  
153 /* A copy of v with every value frj rejects replaced by its stringification.
154 * Only ever called when pl_unsafe said so, so the cost lands on the record
155 * that earned it. The copy is plain - a blessed hash loses its class - which
156 * changes no output, because frj encodes a blessed hash as a plain object
157 * anyway. Mortal. */
158 11 static SV *pl_scrubbed(pTHX_ SV *v, int depth) {
159 SV *rv;
160 /*
161 # uncoverable branch true
162 # uncoverable condition left count:1 class:ignore_covered_err */
163 11 ▶ if (!v) return &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return &PL_sv_undef; 0 11 11
164 /*
165 # uncoverable branch true
166 # uncoverable condition left count:1 class:ignore_covered_err
167 # uncoverable condition left count:2 class:ignore_covered_err */
168 11 ▶ if (!SvROK(v) || depth > PL_SCRUB_MAX) return v;
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(v) || depth > PL_SCRUB_MAX) return v; 11 0 11
if (!SvROK(v) || depth > PL_SCRUB_MAX) return v; 0 11 11
169 11 rv = SvRV(v);
170 11 ▶ if (pl_reject(aTHX_ v, rv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pl_reject(aTHX_ v, rv)) { 7 4 11
171 STRLEN l;
172 7 const char *s = SvPV_const(v, l);
173 7 return sv_2mortal(newSVpvn(s, l));
174 }
175 4 ▶ if (SvTYPE(rv) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVHV) { 2 2 4
176 2 HV *src = (HV *)rv, *dst = newHV();
177 HE *he;
178 2 hv_iterinit(src);
179 6 ▶ while ((he = hv_iternext(src)))
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) 4 2 6
180 4 (void)hv_store_ent(dst, hv_iterkeysv(he),
181 newSVsv(pl_scrubbed(aTHX_ HeVAL(he), depth + 1)), 0);
182 2 return sv_2mortal(newRV_noinc((SV *)dst));
183 }
184 /*
185 # uncoverable branch false
186 # uncoverable condition right count:1 class:ignore_covered_err */
187 2 ▶ if (SvTYPE(rv) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(rv) == SVt_PVAV) { 2 0 2
188 2 AV *src = (AV *)rv, *dst = newAV();
189 2 SSize_t i, n = av_len(src) + 1;
190 4 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 2 2 4
191 2 SV **e = av_fetch(src, i, 0);
192 /*
193 # uncoverable condition right count:1 class:ignore_covered_err
194 # uncoverable condition right count:2 class:ignore_covered_err */
195 2 ▶ av_push(dst, newSVsv((e && *e) ? pl_scrubbed(aTHX_ *e, depth + 1)
Branch: 2 branches
BranchTrueFalseTotal
av_push(dst, newSVsv((e && *e) ? pl_scrubbed(aTHX_ *e, depth + 1) 2 0 2
av_push(dst, newSVsv((e && *e) ? pl_scrubbed(aTHX_ *e, depth + 1) 2 0 2
196 : &PL_sv_undef));
197 }
198 2 return sv_2mortal(newRV_noinc((SV *)dst));
199 }
200 0 return v; /* # uncoverable statement */
201 }
202  
203 /* A field value made safe to hand to frj: v itself when it already is. */
204 122 static SV *pl_safe(pTHX_ SV *v) {
205 /*
206 # uncoverable branch true
207 # uncoverable condition left count:1 class:ignore_covered_err */
208 122 ▶ if (!v) return &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return &PL_sv_undef; 0 122 122
209 122 ▶ return pl_unsafe(aTHX_ v, 0) ? pl_scrubbed(aTHX_ v, 0) : v;
Branch: 1 branch
BranchTrueFalseTotal
return pl_unsafe(aTHX_ v, 0) ? pl_scrubbed(aTHX_ v, 0) : v; 5 117 122
210 }
211  
212 /* A logfmt key. Anything that would break the line apart - a space, an `=`, a
213 * quote, a control character - becomes an underscore, because a field name is
214 * not always a literal in the source: `{ %$from_the_client }` is an ordinary
215 * thing to write, and a key holding a newline would otherwise forge a line. */
216 90 static void pl_logfmt_key(pTHX_ SV *out, SV *k) {
217 STRLEN l, i;
218 90 const char *s = SvPV_const(k, l);
219 816 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 726 90 816
220 726 unsigned char c = (unsigned char)s[i];
221 /* 0x7f as well as the C0 range: DEL is not printable and is not
222 * caught by `c <= ' '`. */
223 /*
224 # uncoverable condition left count:2 class:ignore_covered_err
225 # uncoverable condition left count:5 class:ignore_covered_err */
226 726 ▶ if (c <= ' ' || c == 0x7f || c == '=' || c == '"' || c == '\\')
Branch: 5 branches
BranchTrueFalseTotal
if (c <= ' ' || c == 0x7f || c == '=' || c == '"' || c == '\\') 724 2 726
if (c <= ' ' || c == 0x7f || c == '=' || c == '"' || c == '\\') 724 0 724
if (c <= ' ' || c == 0x7f || c == '=' || c == '"' || c == '\\') 723 1 724
if (c <= ' ' || c == 0x7f || c == '=' || c == '"' || c == '\\') 722 1 723
if (c <= ' ' || c == 0x7f || c == '=' || c == '"' || c == '\\') 0 722 722
227 4 sv_catpvs(out, "_");
228 722 else sv_catpvn(out, s + i, 1);
229 }
230 90 }
231  
232 /* A logfmt value. An undef field renders as a bare `key=`; a reference is
233 * encoded as compact JSON first. Quoted whenever it is empty or holds
234 * anything that would break a space-split reader, and a newline inside a
235 * value is escaped rather than passed through - a log line has to stay one
236 * line whatever it was handed. */
237 111 static void pl_logfmt_val(pTHX_ SV *out, SV *v) {
238 const char *s;
239 STRLEN l, i;
240 111 int quote = 0;
241 190 ▶ if (!v || !SvOK(v)) return; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !SvOK(v)) return; /* # uncoverable condition left count:1 class:ignore_covered_err */ 111 0 111
if (!v || !SvOK(v)) return; /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 110 111
242 110 v = pl_safe(aTHX_ v); /* first: this may have turned a reference
243 * frj cannot take into a plain string, and
244 * that string wants rendering as a string
245 * and not as a JSON-encoded one */
246 110 ▶ if (SvROK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(v)) { 4 106 110
247 4 SV *enc = sv_2mortal(punk_frj(aTHX)->encode(aTHX_ v, NULL));
248 4 s = SvPV_const(enc, l);
249 }
250 106 else s = SvPV_const(v, l);
251 110 ▶ if (l == 0) quote = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (l == 0) quote = 1; 1 109 110
252 1241 ▶ for (i = 0; i < l && !quote; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < l && !quote; i++) { 1161 80 1241
for (i = 0; i < l && !quote; i++) { 1131 30 1161
253 1131 unsigned char c = (unsigned char)s[i];
254 1131 ▶ if (c <= ' ' || c == 0x7f || c == '"' || c == '=' || c == '\\')
Branch: 5 branches
BranchTrueFalseTotal
if (c <= ' ' || c == 0x7f || c == '"' || c == '=' || c == '\\') 1109 22 1131
if (c <= ' ' || c == 0x7f || c == '"' || c == '=' || c == '\\') 1108 1 1109
if (c <= ' ' || c == 0x7f || c == '"' || c == '=' || c == '\\') 1103 5 1108
if (c <= ' ' || c == 0x7f || c == '"' || c == '=' || c == '\\') 1102 1 1103
if (c <= ' ' || c == 0x7f || c == '"' || c == '=' || c == '\\') 1 1101 1102
255 30 quote = 1;
256 }
257 110 ▶ if (!quote) { sv_catpvn(out, s, l); return; }
Branch: 1 branch
BranchTrueFalseTotal
if (!quote) { sv_catpvn(out, s, l); return; } 79 31 110
258 31 sv_catpvs(out, "\"");
259 561 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 530 31 561
260 530 const char c = s[i];
261 530 switch (c) {
262 19 case '"': sv_catpvs(out, "\\\""); break;
263 3 case '\\': sv_catpvs(out, "\\\\"); break;
264 11 case '\n': sv_catpvs(out, "\\n"); break;
265 6 case '\r': sv_catpvs(out, "\\r"); break;
266 5 case '\t': sv_catpvs(out, "\\t"); break;
267 486 default:
268 /* EVERY other control byte, as \xHH.
269 *
270 * The four escapes above stop a value forging a LINE. They do
271 * not stop it forging what a line appears to SAY: an ESC in a
272 * log file is a terminal escape sequence, and somebody who
273 * tails that file has their cursor moved, their screen
274 * cleared, or an earlier line overwritten with whatever the
275 * value wanted it to read. A NUL truncates the line for any
276 * reader that treats it as a C string, and a DEL is simply
277 * invisible.
278 *
279 * Values reaching here are frequently request bytes - a
280 * CSP violation report, a header, a form field - so this is
281 * the same class as the escapes above and belongs beside
282 * them.
283 *
284 * Bytes at or above 0x80 are left alone: they are UTF-8
285 * continuation bytes, and escaping them would mangle every
286 * non-ASCII value in the log to protect against nothing. */
287 491 ▶ if ((unsigned char)c < 0x20 || (unsigned char)c == 0x7f) {
Branch: 2 branches
BranchTrueFalseTotal
if ((unsigned char)c < 0x20 || (unsigned char)c == 0x7f) { 482 4 486
if ((unsigned char)c < 0x20 || (unsigned char)c == 0x7f) { 1 481 482
288 static const char HEX[] = "0123456789abcdef";
289 char esc[4];
290 5 esc[0] = '\\'; esc[1] = 'x';
291 5 esc[2] = HEX[((unsigned char)c >> 4) & 0xf];
292 5 esc[3] = HEX[(unsigned char)c & 0xf];
293 5 sv_catpvn(out, esc, 4);
294 }
295 481 else sv_catpvn(out, &c, 1);
296 486 break;
297 }
298 }
299 31 sv_catpvs(out, "\"");
300 }
301  
302 /* The message, the method and the path of a plain line: the control bytes
303 * of each written out as a field value's are. A message is what the
304 * application was handed - a parameter, a name, a die text - and one with a
305 * newline in it is a second line of the log, in the house format, saying
306 * whatever its author liked. The path is request bytes outright. Mortal,
307 * with the argument's encoding flag: only ASCII bytes are rewritten. */
308 326 static SV *pl_plain(pTHX_ const char *s, STRLEN l, int utf8) {
309 326 SV *out = sv_2mortal(newSVpvn("", 0));
310 326 STRLEN i, run = 0;
311 3720 ▶ for (i = 0; i < l; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l; i++) { 3394 326 3720
312 3394 unsigned char c = (unsigned char)s[i];
313 3394 ▶ if (c >= 0x20 && c != 0x7f) { run++; continue; }
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 0x20 && c != 0x7f) { run++; continue; } 3385 9 3394
if (c >= 0x20 && c != 0x7f) { run++; continue; } 3384 1 3385
314 10 ▶ if (run) { sv_catpvn(out, s + i - run, run); run = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (run) { sv_catpvn(out, s + i - run, run); run = 0; } 9 1 10
315 10 ▶ if (c == '\n') sv_catpvs(out, "\\n");
Branch: 1 branch
BranchTrueFalseTotal
if (c == '\n') sv_catpvs(out, "\\n"); 6 4 10
316 4 ▶ else if (c == '\r') sv_catpvs(out, "\\r");
Branch: 1 branch
BranchTrueFalseTotal
else if (c == '\r') sv_catpvs(out, "\\r"); 1 3 4
317 /*
318 # uncoverable branch true
319 # uncoverable condition left count:1 class:ignore_covered_err */
320 3 ▶ else if (c == '\t') sv_catpvs(out, "\\t");
Branch: 1 branch
BranchTrueFalseTotal
else if (c == '\t') sv_catpvs(out, "\\t"); 0 3 3
321 3 else sv_catpvf(out, "\\x%02x", (unsigned)c);
322 }
323 326 ▶ if (run) sv_catpvn(out, s + l - run, run);
Branch: 1 branch
BranchTrueFalseTotal
if (run) sv_catpvn(out, s + l - run, run); 318 8 326
324 /*
325 # uncoverable branch true
326 # uncoverable condition left count:1 class:ignore_covered_err */
327 326 ▶ if (utf8) SvUTF8_on(out);
Branch: 1 branch
BranchTrueFalseTotal
if (utf8) SvUTF8_on(out); 0 326 326
328 326 return out;
329 }
330  
331 /* A record's fields as logfmt pairs, for the plain line and for the message
332 * handed to a psgix.logger. Sorted, because perl's hash order is randomised
333 * per process and a line that reorders itself between runs is one nobody can
334 * diff, grep or test. Mortal, and empty when the record says nothing the
335 * house does not already carry. */
336 58 static SV *pl_logfmt(pTHX_ HV *fields) {
337 58 SV *out = sv_2mortal(newSVpvs(""));
338 AV *keys;
339 HE *he;
340 58 SSize_t i, n = 0;
341 58 ▶ if (!fields || !HvUSEDKEYS(fields)) return out; /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!fields || !HvUSEDKEYS(fields)) return out; /* # uncoverable condition left count:1 class:ignore_covered_err */ 58 0 58
if (!fields || !HvUSEDKEYS(fields)) return out; /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 58 58
if (!fields || !HvUSEDKEYS(fields)) return out; /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 57 58
342 57 keys = (AV *)sv_2mortal((SV *)newAV());
343 57 hv_iterinit(fields);
344 212 ▶ while ((he = hv_iternext(fields))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(fields))) { 155 57 212
345 155 SV *k = hv_iterkeysv(he);
346 STRLEN kl;
347 155 const char *ks = SvPV_const(k, kl);
348 155 ▶ if (pl_reserved(ks, kl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (pl_reserved(ks, kl)) continue; 65 90 155
349 90 av_push(keys, newSVsv(k));
350 90 n++;
351 }
352 57 ▶ if (n > 1) sortsv(AvARRAY(keys), (STRLEN)n, Perl_sv_cmp);
Branch: 1 branch
BranchTrueFalseTotal
if (n > 1) sortsv(AvARRAY(keys), (STRLEN)n, Perl_sv_cmp); 21 36 57
353 147 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 90 57 147
354 90 SV **kp = av_fetch(keys, i, 0);
355 HE *e;
356 /*
357 # uncoverable branch true
358 # uncoverable condition left count:1 class:ignore_covered_err
359 # uncoverable condition left count:2 class:ignore_covered_err */
360 90 ▶ if (!kp || !*kp) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!kp || !*kp) continue; 90 0 90
if (!kp || !*kp) continue; 0 90 90
361 90 ▶ if (SvCUR(out)) sv_catpvs(out, " ");
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(out)) sv_catpvs(out, " "); 43 47 90
362 90 pl_logfmt_key(aTHX_ out, *kp);
363 90 sv_catpvs(out, "=");
364 90 e = hv_fetch_ent(fields, *kp, 0, 0);
365 90 ▶ pl_logfmt_val(aTHX_ out, e ? HeVAL(e) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
pl_logfmt_val(aTHX_ out, e ? HeVAL(e) : NULL); /* # uncoverable condition right count:1 class:ignore_covered_err */ 90 0 90
366 }
367 57 return out;
368 }
369  
370 /* Punk::Logger::__fmt(fmt, @args) = sprintf - a named sub defined once (at
371 * BOOT), so it is a symbol-table entry rather than an SV to keep alive; the
372 * __ name reads as internal, keeping Punk::Logger.pm doc-only. */
373 354 static void pl_define_fmt(pTHX) {
374 354 eval_pv("sub Punk::Logger::__fmt { sprintf(shift, @_) } 1", TRUE);
375 354 }
376  
377 /* the output side of Perl's *STDERR (so `local *STDERR` and PerlIO layers
378 * apply, matching warn), or the raw stderr if that is somehow unset */
379 3 static PerlIO *pl_stderr(pTHX) {
380 3 ▶ IO *io = PL_stderrgv ? GvIO(PL_stderrgv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 5 branches
BranchTrueFalseTotal
IO *io = PL_stderrgv ? GvIO(PL_stderrgv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
IO *io = PL_stderrgv ? GvIO(PL_stderrgv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
IO *io = PL_stderrgv ? GvIO(PL_stderrgv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 3 3
IO *io = PL_stderrgv ? GvIO(PL_stderrgv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 0 0 0
IO *io = PL_stderrgv ? GvIO(PL_stderrgv) : NULL; /* # uncoverable condition right count:1 class:ignore_covered_err */ 3 0 3
381 /*
382 # uncoverable condition right count:1 class:ignore_covered_err
383 # uncoverable condition right count:2 class:ignore_covered_err */
384 3 ▶ return (io && IoOFP(io)) ? IoOFP(io) : PerlIO_stderr();
Branch: 2 branches
BranchTrueFalseTotal
return (io && IoOFP(io)) ? IoOFP(io) : PerlIO_stderr(); 3 0 3
return (io && IoOFP(io)) ? IoOFP(io) : PerlIO_stderr(); 3 0 3
385 }
386  
387 /* a Punk::Logger from an app's frozen `logging` config; ctx (a context, or
388 * undef for the app logger) is held weakly so caching it in the context's
389 * stash makes no cycle */
390 76 static SV *pl_make_logger(pTHX_ SV *app, SV *ctx) {
391 76 HV *h = newHV();
392 76 HV *cfg = NULL;
393 76 int level = PL_INFO;
394 76 SV *format = NULL, *to = NULL, *self;
395 /*
396 # uncoverable branch false
397 # uncoverable condition right count:1 class:ignore_covered_err
398 # uncoverable condition right count:2 class:ignore_covered_err
399 # uncoverable condition right count:3 class:ignore_covered_err */
400 76 ▶ if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { 76 0 76
if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { 76 0 76
if (app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV) { 76 0 76
401 76 SV **lc = hv_fetchs((HV *)SvRV(app), "logging", 0);
402 /*
403 # uncoverable condition right count:2 class:ignore_covered_err
404 # uncoverable condition right count:3 class:ignore_covered_err
405 # uncoverable condition right count:4 class:ignore_covered_err */
406 76 ▶ if (lc && *lc && SvROK(*lc) && SvTYPE(SvRV(*lc)) == SVt_PVHV)
Branch: 4 branches
BranchTrueFalseTotal
if (lc && *lc && SvROK(*lc) && SvTYPE(SvRV(*lc)) == SVt_PVHV) 68 8 76
if (lc && *lc && SvROK(*lc) && SvTYPE(SvRV(*lc)) == SVt_PVHV) 68 0 68
if (lc && *lc && SvROK(*lc) && SvTYPE(SvRV(*lc)) == SVt_PVHV) 68 0 68
if (lc && *lc && SvROK(*lc) && SvTYPE(SvRV(*lc)) == SVt_PVHV) 68 0 68
407 68 cfg = (HV *)SvRV(*lc);
408 }
409 76 ▶ if (cfg) {
Branch: 1 branch
BranchTrueFalseTotal
if (cfg) { 68 8 76
410 68 SV **lv = hv_fetchs(cfg, "level", 0);
411 68 SV **fm = hv_fetchs(cfg, "format", 0);
412 68 SV **t = hv_fetchs(cfg, "to", 0);
413 /*
414 # uncoverable condition right count:2 class:ignore_covered_err
415 # uncoverable condition right count:3 class:ignore_covered_err */
416 68 ▶ if (lv && *lv && SvOK(*lv)) level = pl_level_num(aTHX_ *lv);
Branch: 3 branches
BranchTrueFalseTotal
if (lv && *lv && SvOK(*lv)) level = pl_level_num(aTHX_ *lv); 21 47 68
if (lv && *lv && SvOK(*lv)) level = pl_level_num(aTHX_ *lv); 21 0 21
if (lv && *lv && SvOK(*lv)) level = pl_level_num(aTHX_ *lv); 21 0 21
417 /*
418 # uncoverable condition right count:2 class:ignore_covered_err
419 # uncoverable condition right count:3 class:ignore_covered_err */
420 68 ▶ if (fm && *fm && SvOK(*fm)) format = *fm;
Branch: 3 branches
BranchTrueFalseTotal
if (fm && *fm && SvOK(*fm)) format = *fm; 1 67 68
if (fm && *fm && SvOK(*fm)) format = *fm; 1 0 1
if (fm && *fm && SvOK(*fm)) format = *fm; 1 0 1
421 /*
422 # uncoverable branch false
423 # uncoverable condition right count:1 class:ignore_covered_err
424 # uncoverable condition right count:2 class:ignore_covered_err
425 # uncoverable condition right count:3 class:ignore_covered_err */
426 68 ▶ if (t && *t && SvOK(*t)) to = *t;
Branch: 3 branches
BranchTrueFalseTotal
if (t && *t && SvOK(*t)) to = *t; 68 0 68
if (t && *t && SvOK(*t)) to = *t; 68 0 68
if (t && *t && SvOK(*t)) to = *t; 68 0 68
427 }
428 76 (void)hv_stores(h, "level", newSViv(level));
429 76 ▶ (void)hv_stores(h, "format", format ? newSVsv(format) : newSVpvs("plain"));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "format", format ? newSVsv(format) : newSVpvs("plain")); 1 75 76
430 76 ▶ if (to) (void)hv_stores(h, "to", newSVsv(to));
Branch: 1 branch
BranchTrueFalseTotal
if (to) (void)hv_stores(h, "to", newSVsv(to)); 68 8 76
431 76 ▶ if (ctx && SvOK(ctx)) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ctx && SvOK(ctx)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 74 2 76
if (ctx && SvOK(ctx)) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 74 0 74
432 74 SV *cref = newSVsv(ctx);
433 74 (void)hv_stores(h, "ctx", cref);
434 74 sv_rvweaken(cref); /* no context<->logger cycle */
435 }
436 76 self = sv_bless(newRV_noinc((SV *)h), gv_stashpvs("Punk::Logger", GV_ADD));
437 76 return self;
438 }
439  
440 /* run a coderef with one arg, swallowing a die (a broken sink must not take the
441 * request down) */
442 121 static void pl_call1(pTHX_ SV *cb, SV *arg) {
443 121 dSP;
444 121 ENTER; SAVETMPS;
445 121 ▶ PUSHMARK(SP); EXTEND(SP, 1); PUSHs(arg); PUTBACK;
Branch: 2 branches
BranchTrueFalseTotal
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(arg); PUTBACK; 0 121 121
PUSHMARK(SP); EXTEND(SP, 1); PUSHs(arg); PUTBACK; 0 121 121
446 121 call_sv(cb, G_DISCARD | G_EVAL);
447 121 ▶ SPAGAIN; PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
SPAGAIN; PUTBACK; FREETMPS; LEAVE; 0 121 121
448 121 }
449  
450 /* format and emit one already-built message at level lvl, with the record's
451 * fields (NULL for none) */
452 128 static void pl_emit(pTHX_ SV *self, int lvl, SV *msg, HV *fields) {
453 128 HV *lg = pl_hv(aTHX_ self);
454 128 SV **lev = hv_fetchs(lg, "level", 0);
455 /*
456 # uncoverable condition right count:1 class:ignore_covered_err
457 # uncoverable condition right count:2 class:ignore_covered_err */
458 128 ▶ int threshold = (lev && *lev) ? (int)SvIV(*lev) : PL_INFO;
Branch: 2 branches
BranchTrueFalseTotal
int threshold = (lev && *lev) ? (int)SvIV(*lev) : PL_INFO; 128 0 128
int threshold = (lev && *lev) ? (int)SvIV(*lev) : PL_INFO; 128 0 128
459 SV **ctxp, **fmtp, **top;
460 SV *ctx;
461 128 HV *env = NULL;
462 128 const char *method = "", *path = "";
463 128 STRLEN ml = 0, pl2 = 0;
464 128 SV *reqid = NULL;
465 128 ▶ const char *name = PL_NAMES[lvl < 0 ? 0 : lvl > 4 ? 4 : lvl];
Branch: 1 branch
BranchTrueFalseTotal
const char *name = PL_NAMES[lvl < 0 ? 0 : lvl > 4 ? 4 : lvl]; 128 0 128
466 SV *detail;
467 /*
468 # uncoverable branch true class:ignore_covered_err
469 # uncoverable condition left count:1 class:ignore_covered_err */
470 135 ▶ if (lvl < threshold) return; /* dropped, no work */
Branch: 1 branch
BranchTrueFalseTotal
if (lvl < threshold) return; /* dropped, no work */ 0 128 128
471  
472 /* The context is resolved BEFORE the observers rather than after, because
473 * a ctx-aware observer is given it. Reading one hash slot is not
474 * rendering, so the promise below still holds. */
475 128 ctxp = hv_fetchs(lg, "ctx", 0);
476 /*
477 # uncoverable condition right count:2 class:ignore_covered_err
478 # uncoverable condition right count:3 class:ignore_covered_err */
479 128 ▶ ctx = (ctxp && *ctxp && SvOK(*ctxp)) ? *ctxp : NULL;
Branch: 3 branches
BranchTrueFalseTotal
ctx = (ctxp && *ctxp && SvOK(*ctxp)) ? *ctxp : NULL; 97 31 128
ctx = (ctxp && *ctxp && SvOK(*ctxp)) ? *ctxp : NULL; 97 0 97
ctx = (ctxp && *ctxp && SvOK(*ctxp)) ? *ctxp : NULL; 97 0 97
480  
481 /* The observers, before any rendering. They want the record - the level,
482 * the message and the fields - not a formatted line, and a line that was
483 * dropped for being below the threshold was never a record at all. */
484 /*
485 # uncoverable branch true
486 # uncoverable condition left count:1 class:ignore_covered_err */
487 128 ▶ if (PL_OBS_N) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_OBS_N) { 0 128 128
488 int oi;
489 /*
490 # uncoverable statement
491 # uncoverable branch true
492 # uncoverable branch false
493 # uncoverable condition left count:1
494 # uncoverable condition right count:1 */
495 0 ▶ for (oi = 0; oi < PL_OBS_N; oi++)
Branch: 1 branch
BranchTrueFalseTotal
for (oi = 0; oi < PL_OBS_N; oi++) 0 0 0
496 0 PL_OBS[oi].cb(aTHX_ name, strlen(name), msg, fields, /* # uncoverable statement */
497 PL_OBS[oi].ud); /* # uncoverable statement */
498 }
499 /*
500 # uncoverable branch true
501 # uncoverable condition left count:1 class:ignore_covered_err */
502 128 ▶ if (PL_OBS_CTX_N) {
Branch: 1 branch
BranchTrueFalseTotal
if (PL_OBS_CTX_N) { 0 128 128
503 int oi;
504 /*
505 # uncoverable statement
506 # uncoverable branch true
507 # uncoverable branch false
508 # uncoverable condition left count:1
509 # uncoverable condition right count:1 */
510 0 ▶ for (oi = 0; oi < PL_OBS_CTX_N; oi++)
Branch: 1 branch
BranchTrueFalseTotal
for (oi = 0; oi < PL_OBS_CTX_N; oi++) 0 0 0
511 0 PL_OBS_CTX[oi].cb(aTHX_ ctx, name, strlen(name), msg, fields, /* # uncoverable statement */
512 PL_OBS_CTX[oi].ud); /* # uncoverable statement */
513 }
514  
515 128 ▶ if (ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx) { 97 31 128
516 97 SV *e = pcx_get(aTHX_ pcx_av(aTHX_ ctx), PCX_ENV);
517 /*
518 # uncoverable branch false
519 # uncoverable condition right count:1 class:ignore_covered_err
520 # uncoverable condition right count:2 class:ignore_covered_err
521 # uncoverable condition right count:3 class:ignore_covered_err */
522 97 ▶ if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { 97 0 97
if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { 97 0 97
if (e && SvROK(e) && SvTYPE(SvRV(e)) == SVt_PVHV) { 97 0 97
523 SV **m, **p, **r;
524 97 env = (HV *)SvRV(e);
525 97 m = hv_fetchs(env, "REQUEST_METHOD", 0);
526 97 p = hv_fetchs(env, "PATH_INFO", 0);
527 /*
528 # uncoverable branch false
529 # uncoverable condition right count:1 class:ignore_covered_err
530 # uncoverable condition right count:2 class:ignore_covered_err
531 # uncoverable condition right count:3 class:ignore_covered_err */
532 194 ▶ if (m && *m && SvOK(*m)) method = SvPV_const(*m, ml);
Branch: 3 branches
BranchTrueFalseTotal
if (m && *m && SvOK(*m)) method = SvPV_const(*m, ml); 97 0 97
if (m && *m && SvOK(*m)) method = SvPV_const(*m, ml); 97 0 97
if (m && *m && SvOK(*m)) method = SvPV_const(*m, ml); 97 0 97
533 /*
534 # uncoverable branch false
535 # uncoverable condition right count:1 class:ignore_covered_err
536 # uncoverable condition right count:2 class:ignore_covered_err
537 # uncoverable condition right count:3 class:ignore_covered_err */
538 194 ▶ if (p && *p && SvOK(*p)) path = SvPV_const(*p, pl2);
Branch: 3 branches
BranchTrueFalseTotal
if (p && *p && SvOK(*p)) path = SvPV_const(*p, pl2); 97 0 97
if (p && *p && SvOK(*p)) path = SvPV_const(*p, pl2); 97 0 97
if (p && *p && SvOK(*p)) path = SvPV_const(*p, pl2); 97 0 97
539 /* psgix.request_id ONLY.
540 *
541 * This used to fall back to HTTP_X_REQUEST_ID when the extension
542 * key was absent - that is, to whatever the client sent. A log
543 * field is the one place attacker-chosen bytes should never
544 * arrive by default, and "request_id" is a field a reader trusts
545 * to identify a request rather than to quote a stranger.
546 *
547 * Punk::Plugin::RequestId sets psgix.request_id, and adopting an
548 * inbound header is its `trust_header` option, which validates
549 * before it adopts. That is the supported way to get a client's
550 * id into a log line, and it is opt in. */
551 97 r = hv_fetchs(env, "psgix.request_id", 0);
552 /*
553 # uncoverable condition right count:2 class:ignore_covered_err
554 # uncoverable condition right count:3 class:ignore_covered_err */
555 97 ▶ if (r && *r && SvOK(*r)) reqid = *r;
Branch: 3 branches
BranchTrueFalseTotal
if (r && *r && SvOK(*r)) reqid = *r; 21 76 97
if (r && *r && SvOK(*r)) reqid = *r; 21 0 21
if (r && *r && SvOK(*r)) reqid = *r; 21 0 21
556 }
557 }
558  
559 128 detail = sv_2mortal(newSVpvs(""));
560 128 ▶ if (ml || pl2) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (ml || pl2) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 31 97 128
if (ml || pl2) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 31 31
561 97 sv_catsv(detail, pl_plain(aTHX_ method, ml, 0));
562 97 sv_catpvs(detail, " ");
563 97 sv_catsv(detail, pl_plain(aTHX_ path, pl2, 0));
564 97 sv_catpvs(detail, " - ");
565 }
566 /*
567 # uncoverable branch false
568 # uncoverable condition right count:1 class:ignore_covered_err
569 # uncoverable condition right count:2 class:ignore_covered_err */
570 128 ▶ if (msg && SvOK(msg)) {
Branch: 2 branches
BranchTrueFalseTotal
if (msg && SvOK(msg)) { 128 0 128
if (msg && SvOK(msg)) { 128 0 128
571 STRLEN msl;
572 128 const char *msp = SvPV_const(msg, msl);
573 /* the newline a die text ends with is the end of the message, not
574 * part of it */
575 129 ▶ while (msl && (msp[msl - 1] == '\n' || msp[msl - 1] == '\r')) msl--; /* # uncoverable condition left count:3 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
while (msl && (msp[msl - 1] == '\n' || msp[msl - 1] == '\r')) msl--; /* # uncoverable condition left count:3 class:ignore_covered_err */ 126 3 129
while (msl && (msp[msl - 1] == '\n' || msp[msl - 1] == '\r')) msl--; /* # uncoverable condition left count:3 class:ignore_covered_err */ 1 125 126
while (msl && (msp[msl - 1] == '\n' || msp[msl - 1] == '\r')) msl--; /* # uncoverable condition left count:3 class:ignore_covered_err */ 0 125 125
576 128 sv_catsv(detail, pl_plain(aTHX_ msp, msl, SvUTF8(msg) ? 1 : 0));
577 }
578  
579 /* the fields, as logfmt, after the message. The json branch below merges
580 * them into the object instead and reads `msg`, not this. */
581 128 ▶ if (fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (fields) { 58 70 128
582 58 SV *pairs = pl_logfmt(aTHX_ fields);
583 58 ▶ if (SvCUR(pairs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvCUR(pairs)) { 47 11 58
584 /* not when the message was empty: the method/path separator has
585 * already left a space, and "GET /x - books=3" is a typo, not a
586 * log line */
587 47 ▶ if (SvCUR(detail) && SvEND(detail)[-1] != ' ')
Branch: 2 branches
BranchTrueFalseTotal
if (SvCUR(detail) && SvEND(detail)[-1] != ' ') 46 1 47
if (SvCUR(detail) && SvEND(detail)[-1] != ' ') 45 1 46
588 45 sv_catpvs(detail, " ");
589 47 sv_catsv(detail, pairs);
590 }
591 }
592  
593 /* The request id, as one more logfmt pair.
594 *
595 * A FIELD rather than a new positional element, and that is the
596 * compatibility decision: the line keeps its shape,
597 * `[time] [level] METHOD /path - message`, so anything already splitting
598 * on that prefix still works, and the id joins the fields that were
599 * always allowed to appear after the message. Adding a seventh position
600 * would have broken every reader in exchange for nothing.
601 *
602 * Rendered through pl_logfmt_val like any other value, so a newline in an
603 * id cannot forge a line - the id reaching here has been validated by the
604 * plugin, but this code cannot know that another layer set the key. */
605 128 ▶ if (reqid) {
Branch: 1 branch
BranchTrueFalseTotal
if (reqid) { 21 107 128
606 /*
607 # uncoverable branch false
608 # uncoverable condition right count:1 class:ignore_covered_err
609 # uncoverable condition right count:2 class:ignore_covered_err */
610 21 ▶ if (SvCUR(detail) && SvEND(detail)[-1] != ' ') sv_catpvs(detail, " ");
Branch: 2 branches
BranchTrueFalseTotal
if (SvCUR(detail) && SvEND(detail)[-1] != ' ') sv_catpvs(detail, " "); 21 0 21
if (SvCUR(detail) && SvEND(detail)[-1] != ' ') sv_catpvs(detail, " "); 21 0 21
611 21 sv_catpvs(detail, "request_id=");
612 21 pl_logfmt_val(aTHX_ detail, reqid);
613 }
614  
615 128 ▶ if (env) { /* prefer psgix.logger */
Branch: 1 branch
BranchTrueFalseTotal
if (env) { /* prefer psgix.logger */ 97 31 128
616 97 SV **pgl = hv_fetchs(env, "psgix.logger", 0);
617 /*
618 # uncoverable condition right count:2 class:ignore_covered_err
619 # uncoverable condition right count:3 class:ignore_covered_err
620 # uncoverable condition right count:4 class:ignore_covered_err */
621 97 ▶ if (pgl && *pgl && SvROK(*pgl) && SvTYPE(SvRV(*pgl)) == SVt_PVCV) {
Branch: 4 branches
BranchTrueFalseTotal
if (pgl && *pgl && SvROK(*pgl) && SvTYPE(SvRV(*pgl)) == SVt_PVCV) { 7 90 97
if (pgl && *pgl && SvROK(*pgl) && SvTYPE(SvRV(*pgl)) == SVt_PVCV) { 7 0 7
if (pgl && *pgl && SvROK(*pgl) && SvTYPE(SvRV(*pgl)) == SVt_PVCV) { 7 0 7
if (pgl && *pgl && SvROK(*pgl) && SvTYPE(SvRV(*pgl)) == SVt_PVCV) { 7 0 7
622 7 HV *arg = newHV();
623 7 (void)hv_stores(arg, "level", newSVpv(name, 0));
624 7 (void)hv_stores(arg, "message", newSVsv(detail));
625 7 pl_call1(aTHX_ *pgl, sv_2mortal(newRV_noinc((SV *)arg)));
626 7 return;
627 }
628 }
629  
630 121 fmtp = hv_fetchs(lg, "format", 0);
631 121 top = hv_fetchs(lg, "to", 0);
632 {
633 SV *line;
634 121 ▶ int json = fmtp && *fmtp && SvOK(*fmtp)
Branch: 2 branches
BranchTrueFalseTotal
int json = fmtp && *fmtp && SvOK(*fmtp) 121 0 121
int json = fmtp && *fmtp && SvOK(*fmtp) 121 0 121
635 363 ▶ && strEQ(SvPV_nolen(*fmtp), "json");
Branch: 2 branches
BranchTrueFalseTotal
&& strEQ(SvPV_nolen(*fmtp), "json"); 121 0 121
&& strEQ(SvPV_nolen(*fmtp), "json"); 6 115 121
636 char ts[24];
637 121 pl_iso_time(ts, sizeof ts);
638 121 ▶ if (json) {
Branch: 1 branch
BranchTrueFalseTotal
if (json) { 6 115 121
639 6 HV *o = newHV();
640 6 ▶ if (fields) { /* the record first, the house keys after:
Branch: 1 branch
BranchTrueFalseTotal
if (fields) { /* the record first, the house keys after: 4 2 6
641 * a colliding field is dropped by
642 * pl_reserved, and the six below are the
643 * only things that can occupy those names */
644 HE *he;
645 4 hv_iterinit(fields);
646 21 ▶ while ((he = hv_iternext(fields))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(fields))) { 17 4 21
647 17 SV *k = hv_iterkeysv(he);
648 STRLEN kl;
649 17 const char *ks = SvPV_const(k, kl);
650 17 ▶ if (pl_reserved(ks, kl)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (pl_reserved(ks, kl)) continue; 11 6 17
651 6 (void)hv_store_ent(o, k,
652 newSVsv(pl_safe(aTHX_ HeVAL(he))), 0);
653 }
654 }
655 6 (void)hv_stores(o, "time", newSVpv(ts, 0));
656 6 (void)hv_stores(o, "level", newSVpv(name, 0));
657 6 (void)hv_stores(o, "message", newSVsv(pl_safe(aTHX_ msg)));
658 6 ▶ if (ml) (void)hv_stores(o, "method", newSVpvn(method, ml));
Branch: 1 branch
BranchTrueFalseTotal
if (ml) (void)hv_stores(o, "method", newSVpvn(method, ml)); 1 5 6
659 6 ▶ if (pl2) (void)hv_stores(o, "path", newSVpvn(path, pl2));
Branch: 1 branch
BranchTrueFalseTotal
if (pl2) (void)hv_stores(o, "path", newSVpvn(path, pl2)); 1 5 6
660 6 ▶ if (reqid) (void)hv_stores(o, "request_id", newSVsv(reqid));
Branch: 1 branch
BranchTrueFalseTotal
if (reqid) (void)hv_stores(o, "request_id", newSVsv(reqid)); 1 5 6
661 6 line = sv_2mortal(punk_frj(aTHX)->encode(aTHX_
662 sv_2mortal(newRV_noinc((SV *)o)), NULL));
663 6 sv_catpvs(line, "\n");
664 }
665 else {
666 115 line = sv_2mortal(newSVpvf("[%s] [%s] ", ts, name));
667 115 sv_catsv(line, detail);
668 115 sv_catpvs(line, "\n");
669 }
670 /*
671 # uncoverable condition right count:2 class:ignore_covered_err
672 # uncoverable condition right count:3 class:ignore_covered_err */
673 239 ▶ if (top && *top && SvOK(*top)) {
Branch: 3 branches
BranchTrueFalseTotal
if (top && *top && SvOK(*top)) { 118 3 121
if (top && *top && SvOK(*top)) { 118 0 118
if (top && *top && SvOK(*top)) { 118 0 118
674 118 SV *to = *top;
675 118 ▶ if (SvROK(to) && SvTYPE(SvRV(to)) == SVt_PVCV) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(to) && SvTYPE(SvRV(to)) == SVt_PVCV) /* # uncoverable condition right count:1 class:ignore_covered_err */ 118 0 118
if (SvROK(to) && SvTYPE(SvRV(to)) == SVt_PVCV) /* # uncoverable condition right count:1 class:ignore_covered_err */ 114 4 118
676 114 pl_call1(aTHX_ to, line);
677 else {
678 4 IO *io = sv_2io(to);
679 /*
680 # uncoverable branch false
681 # uncoverable condition right count:1 class:ignore_covered_err
682 # uncoverable condition right count:2 class:ignore_covered_err */
683 4 ▶ if (io && IoOFP(io)) {
Branch: 2 branches
BranchTrueFalseTotal
if (io && IoOFP(io)) { 4 0 4
if (io && IoOFP(io)) { 4 0 4
684 4 STRLEN ll; const char *lp = SvPV_const(line, ll);
685 4 (void)PerlIO_write(IoOFP(io), lp, ll);
686 }
687 }
688 }
689 else {
690 3 STRLEN ll; const char *lp = SvPV_const(line, ll);
691 3 (void)PerlIO_write(pl_stderr(aTHX), lp, ll);
692 }
693 }
694 }
695  
696 /* the shared body of the level methods: build the message (a lone unblessed
697 * hashref is a record, one arg as-is, several via sprintf) and emit.
698 *
699 * The threshold is read here rather than in pl_emit, so a dropped call really
700 * does no work: it used to sprintf first and ask afterwards, which made every
701 * below-threshold ->debug('%s', $expensive) pay for a line nobody would see.
702 *
703 * A record is an UNBLESSED hashref only. An object is a message, however it
704 * is built: one with an overloaded "" is an ordinary thing to log, and
705 * dumping its guts as fields instead would be a silent change of meaning. */
706 132 static void pl_method(pTHX_ SV *self, int lvl, SV **argv, int argc) {
707 SV *msg;
708 132 HV *fields = NULL;
709 132 ▶ if (lvl < pl_threshold(aTHX_ self)) return; /* dropped, no work */
Branch: 1 branch
BranchTrueFalseTotal
if (lvl < pl_threshold(aTHX_ self)) return; /* dropped, no work */ 16 115 131
710 115 ▶ if (argc == 1 && SvROK(argv[0]) && !SvOBJECT(SvRV(argv[0])) /* # uncoverable condition right count:4 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (argc == 1 && SvROK(argv[0]) && !SvOBJECT(SvRV(argv[0])) /* # uncoverable condition right count:4 class:ignore_covered_err */ 110 5 115
if (argc == 1 && SvROK(argv[0]) && !SvOBJECT(SvRV(argv[0])) /* # uncoverable condition right count:4 class:ignore_covered_err */ 60 50 110
if (argc == 1 && SvROK(argv[0]) && !SvOBJECT(SvRV(argv[0])) /* # uncoverable condition right count:4 class:ignore_covered_err */ 58 2 60
711 58 ▶ && SvTYPE(SvRV(argv[0])) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(argv[0])) == SVt_PVHV) { 58 0 58
712 SV **m;
713 58 fields = (HV *)SvRV(argv[0]);
714 58 m = hv_fetchs(fields, "message", 0);
715 /*
716 # uncoverable condition right count:2 class:ignore_covered_err
717 # uncoverable condition right count:3 class:ignore_covered_err */
718 58 ▶ msg = (m && *m && SvOK(*m)) ? *m : sv_2mortal(newSVpvs(""));
Branch: 3 branches
BranchTrueFalseTotal
msg = (m && *m && SvOK(*m)) ? *m : sv_2mortal(newSVpvs("")); 55 3 58
msg = (m && *m && SvOK(*m)) ? *m : sv_2mortal(newSVpvs("")); 55 0 55
msg = (m && *m && SvOK(*m)) ? *m : sv_2mortal(newSVpvs("")); 55 0 55
719 }
720 /*
721 # uncoverable branch true
722 # uncoverable condition left count:1 class:ignore_covered_err */
723 57 ▶ else if (argc <= 0) msg = sv_2mortal(newSVpvs(""));
Branch: 1 branch
BranchTrueFalseTotal
else if (argc <= 0) msg = sv_2mortal(newSVpvs("")); 0 57 57
724 57 ▶ else if (argc == 1) msg = argv[0];
Branch: 1 branch
BranchTrueFalseTotal
else if (argc == 1) msg = argv[0]; 52 5 57
725 else {
726 5 dSP; int count, i;
727 SV *result;
728 5 ENTER; SAVETMPS;
729 5 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 5 5
730 15 ▶ for (i = 0; i < argc; i++) XPUSHs(sv_2mortal(newSVsv(argv[i]))); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < argc; i++) XPUSHs(sv_2mortal(newSVsv(argv[i]))); /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 10 10
for (i = 0; i < argc; i++) XPUSHs(sv_2mortal(newSVsv(argv[i]))); /* # uncoverable condition left count:1 class:ignore_covered_err */ 10 5 15
731 5 PUTBACK;
732 5 count = call_pv("Punk::Logger::__fmt", G_SCALAR);
733 5 SPAGAIN;
734 /* keep result off the temps stack so the FREETMPS below (paired with
735 * our SAVETMPS) does not free it before pl_emit uses it */
736 5 ▶ result = count > 0 ? newSVsv(POPs) : newSVpvs(""); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
result = count > 0 ? newSVsv(POPs) : newSVpvs(""); /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 0 5
737 5 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 5 0 5
738 5 msg = sv_2mortal(result);
739 }
740 115 pl_emit(aTHX_ self, lvl, msg, fields);
741 }
742  
743 #endif /* PUNK_LOG_H */
« include/punk/punk_frozen.h ↑ Summary include/punk/punk_sessionstore.h »