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