lib/Medusa/XS.xs

filter: Statementstmt 69.4% 766 / 1103 Branchbran 42.4% 398 / 938 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 57.0% 1164 / 2041 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Medusa/XS.pm »
1 /*
2 * XS.xs - High-performance auditing via custom ops for Medusa::XS
3 *
4 * Named after Medusa, the Gorgon whose gaze turned onlookers to stone.
5 * This XS module turns slow Perl auditing into lightning-fast C operations.
6 *
7 * Architecture:
8 * - Custom ops injected into subroutine optrees at compile time
9 * - Direct caller stack walking via cx_stack (no Perl caller() overhead)
10 * - Loo library for pure-XS data serialisation with colour support
11 * - Horus library for RFC 9562 UUID generation (v1-v8, NIL, MAX)
12 *
13 * Requires Perl 5.10+. XOP debug names require 5.14+ (gracefully degraded).
14 */
15  
16 #define PERL_NO_GET_CONTEXT
17 /* Before the perl headers, not after: Strawberry's XSUB.h turns
18 * gettimeofday into a macro, and a prototype read after that is a
19 * syntax error. */
20 #include <sys/time.h> /* For gettimeofday() */
21 #include "EXTERN.h"
22 #include "perl.h"
23 #include "XSUB.h"
24  
25 #include "../../ppport.h"
26  
27 #include <ctype.h> /* For toupper() */
28  
29 /* Horus UUID library - pure C, no Perl deps */
30 #define HORUS_FATAL(msg) croak("%s", (msg))
31 #include "horus_core.h"
32  
33 /* Loo Data Dumper - pure XS with colour support */
34 #include "loo.h"
35  
36 /* ------------------------------------------------------------------ */
37 /* Time helpers */
38 /* ------------------------------------------------------------------ */
39  
40 /* tv_sec is not a time_t everywhere: it is a long on Windows, where
41 * time_t is 64 bits wide. Copy it rather than pass its address. */
42 static struct tm *
43 52 medusa_tm(const struct timeval *tv, int use_gmtime) {
44 52 time_t secs = (time_t)tv->tv_sec;
45 52 ▶ return use_gmtime ? gmtime(&secs) : localtime(&secs);
Branch: 1 branch
BranchTrueFalseTotal
return use_gmtime ? gmtime(&secs) : localtime(&secs); 51 1 52
46 }
47  
48 /* The default timestamp is what scalar gmtime prints, and that is
49 * English whatever the locale. strftime's %a and %b follow LC_TIME. */
50 static void
51 48 medusa_default_time(char *buf, size_t size, const struct tm *tm) {
52 static const char days[7][4] =
53 { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
54 static const char months[12][4] =
55 { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
56 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
57 48 snprintf(buf, size, "%s %s %2d %02d:%02d:%02d %d",
58 48 days[tm->tm_wday % 7], months[tm->tm_mon % 12], tm->tm_mday,
59 48 tm->tm_hour, tm->tm_min, tm->tm_sec, tm->tm_year + 1900);
60 48 }
61  
62 /* ------------------------------------------------------------------ */
63 /* Compatibility macros for older Perls */
64 /* ------------------------------------------------------------------ */
65  
66 /* mg_findext was added in 5.13.2; ppport.h may not backport it */
67 #ifndef mg_findext
68 static MAGIC *
69 medusa_mg_findext(const SV *sv, int type, const MGVTBL *vtbl) {
70 MAGIC *mg;
71 if (sv) {
72 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
73 if (mg->mg_type == type && mg->mg_virtual == vtbl)
74 return mg;
75 }
76 }
77 return NULL;
78 }
79 #define mg_findext(sv, type, vtbl) medusa_mg_findext(sv, type, vtbl)
80 #endif
81  
82 /* GvCV_set was added in 5.13.3 */
83 #ifndef GvCV_set
84 # define GvCV_set(gv, cv) (GvGP(gv)->gp_cv = (CV *)(cv))
85 #endif
86  
87 /* CvGV_set was added in 5.13.3 */
88 #ifndef CvGV_set
89 # define CvGV_set(cv, gv) (CvGV(cv) = (GV *)(gv))
90 #endif
91  
92 /* ------------------------------------------------------------------ */
93 /* Compatibility macros for op sibling handling (5.22+) */
94 /* ------------------------------------------------------------------ */
95  
96 #ifndef OpSIBLING
97 # define OpSIBLING(o) ((o)->op_sibling)
98 #endif
99  
100 #ifndef OpMORESIB_set
101 # define OpMORESIB_set(o, sib) ((o)->op_sibling = (sib))
102 #endif
103  
104 #ifndef OpLASTSIB_set
105 # define OpLASTSIB_set(o, parent) ((o)->op_sibling = NULL)
106 #endif
107  
108 /* ------------------------------------------------------------------ */
109 /* Constants */
110 /* ------------------------------------------------------------------ */
111  
112 #define MEDUSA_CALLER_STACK_INITIAL_SIZE 256
113 #define MEDUSA_GUID_LEN 36 /* UUID hyphenated: xxxxxxxx-xxxx-Vxxx-yxxx-xxxxxxxxxxxx */
114 #define MEDUSA_TIME_BUF_SIZE 64
115 #define MEDUSA_LOG_BUF_SIZE 4096
116 #define MEDUSA_LOGGER_WRITE_BUF 8192
117  
118 /* ------------------------------------------------------------------ */
119 /* Medusa::XS::Logger - Pure C file logger with flock */
120 /* ------------------------------------------------------------------ */
121  
122 typedef struct {
123 PerlIO *fh; /* Open file handle */
124 char *filename; /* Allocated copy of filename */
125 STRLEN filename_len;
126 } MedusaLogger;
127  
128 /* Magic vtable for MedusaLogger destructor */
129 static int medusa_logger_mg_free(pTHX_ SV *sv, MAGIC *mg);
130 static MGVTBL medusa_logger_vtbl = {0, 0, 0, 0, medusa_logger_mg_free, 0, 0, 0};
131  
132 static int
133 8 medusa_logger_mg_free(pTHX_ SV *sv, MAGIC *mg) {
134 8 MedusaLogger *logger = (MedusaLogger *)mg->mg_ptr;
135 PERL_UNUSED_VAR(sv);
136 8 ▶ if (logger) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger) { 8 0 8
137 8 ▶ if (logger->fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger->fh) { 8 0 8
138 8 PerlIO_close(logger->fh);
139 8 logger->fh = NULL;
140 }
141 8 ▶ if (logger->filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger->filename) { 8 0 8
142 8 Safefree(logger->filename);
143 8 logger->filename = NULL;
144 }
145 8 Safefree(logger);
146 }
147 8 return 0;
148 }
149  
150 /* Get the C struct from a blessed SV */
151 static MedusaLogger *
152 5 medusa_logger_from_sv(pTHX_ SV *sv) {
153 MAGIC *mg;
154 5 ▶ if (!sv || !SvROK(sv)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvROK(sv)) return NULL; 5 0 5
if (!sv || !SvROK(sv)) return NULL; 0 5 5
155 5 mg = mg_findext(SvRV(sv), PERL_MAGIC_ext, &medusa_logger_vtbl);
156 5 ▶ if (mg && mg->mg_ptr) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && mg->mg_ptr) { 5 0 5
if (mg && mg->mg_ptr) { 5 0 5
157 5 return (MedusaLogger *)mg->mg_ptr;
158 }
159 0 return NULL;
160 }
161  
162 /* Core log write - pure C, no Perl dispatch.
163 * Does flock(LOCK_EX), write, flock(LOCK_UN) in one shot. */
164 static void
165 6 medusa_logger_write(pTHX_ MedusaLogger *logger, const char *line, STRLEN len) {
166 int fd;
167 6 ▶ if (!logger || !logger->fh) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!logger || !logger->fh) return; 6 0 6
if (!logger || !logger->fh) return; 0 6 6
168
169 6 fd = PerlIO_fileno(logger->fh);
170
171 /* LOCK_EX = 2 */
172 6 flock(fd, 2);
173 6 PerlIO_write(logger->fh, line, len);
174 6 PerlIO_write(logger->fh, "\n", 1);
175 6 PerlIO_flush(logger->fh);
176 /* LOCK_UN = 8 */
177 6 flock(fd, 8);
178 }
179  
180 /* ------------------------------------------------------------------ */
181 /* Forward declarations */
182 /* ------------------------------------------------------------------ */
183  
184 static void medusa_generate_guid(pTHX_ char *buf, int version,
185 const char *ns_str, STRLEN ns_len,
186 const char *name_str, STRLEN name_len);
187 static SV * medusa_format_time(pTHX_ bool use_gmtime, const char *fmt);
188 static int medusa_resolve_colour(pTHX_ HV *options);
189 static const char * medusa_resolve_theme(pTHX_ HV *options);
190 static void medusa_loo_style_init(pTHX_ DDCStyle *style, int use_colour,
191 const char *theme_name);
192 static SV * medusa_dump_param(pTHX_ SV *value, DDCStyle *style);
193 static void medusa_dump_param_into(pTHX_ SV *value, DDCStyle *style, SV *out);
194 static SV * medusa_collect_caller_stack(pTHX);
195 static SV * medusa_build_log_message(pTHX_ HV *log_config, const char *level,
196 SV *guid, SV *caller_stack, SV *method_name,
197 const char *msg_type, AV *params,
198 const char *prefix, double elapsed);
199 static void medusa_collect_caller_stack_into(pTHX_ SV *result);
200  
201 /* ------------------------------------------------------------------ */
202 /* Cached metadata structures */
203 /* ------------------------------------------------------------------ */
204  
205 /* AUDIT_CACHE - Cached metadata for fast wrapper execution */
206 typedef struct {
207 CV *original_cv; /* Original subroutine CV */
208 const char *method; /* Method name (points into CV's GV) */
209 STRLEN method_len;
210 const char *package; /* Package name (points into stash) */
211 STRLEN package_len;
212 HV *log_config; /* Cached pointer to %Medusa::XS::LOG */
213 HV *options; /* Cached pointer to $LOG{OPTIONS} hashref */
214 bool opt_guid; /* Cached option values */
215 bool opt_caller;
216 bool opt_elapsed;
217 bool opt_date;
218 bool opt_level;
219 int guid_version; /* UUID version: 0=NIL, 1-8, -1=MAX (default 4) */
220 char *guid_namespace; /* For v3/v5: namespace string (allocated copy) */
221 STRLEN guid_namespace_len;
222 char *guid_name; /* For v3/v5: name string (allocated copy) */
223 STRLEN guid_name_len;
224 SV *format_message; /* Cached FORMAT_MESSAGE coderef (or NULL for XS default) */
225 bool use_xs_format; /* TRUE if using XS FORMAT_MESSAGE */
226 /* Pre-formatted message strings (saves snprintf per call) */
227 SV *entry_msg; /* "subroutine X called with args:" */
228 SV *exit_msg; /* "subroutine X returned:" */
229 /* Cached logger info */
230 const char *log_level; /* Cached LOG_LEVEL string */
231 const char *log_method; /* Cached method name for logging */
232 /* Cached format config (avoids per-call hash lookups in format_message) */
233 const char *quote; /* Cached QUOTE character */
234 const char *time_type; /* Cached TIME type ("gmtime"/"localtime") */
235 const char *time_fmt; /* Cached TIME_FORMAT string */
236 bool time_use_gm; /* Pre-computed: use gmtime? */
237 const char *time_fmt_c; /* Pre-computed: NULL if "default", else fmt string */
238 /* Cached log level uppercase (avoids toupper loop per call) */
239 char level_upper[32];
240 /* Cached logger object (avoid hv_fetchs per call) */
241 SV *log_obj; /* Cached $LOG{LOG} logger object */
242 bool log_obj_initialized; /* TRUE after first LOG_INIT call */
243 /* Cached resolved method CV for logger dispatch (avoids call_method lookup) */
244 CV *log_method_cv; /* Resolved CV for $log_obj->$log_method */
245 /* --- Per-call scratch buffers (avoid SV allocation per call) --- */
246 SV *scratch_result; /* Reusable SV for log message formatting */
247 SV *scratch_guid; /* Reusable SV for GUID string */
248 SV *scratch_caller; /* Reusable SV for caller stack */
249 SV *scratch_dump; /* Reusable SV for dump output */
250 /* --- Loo dumper style (pre-configured, cached) --- */
251 DDCStyle dump_style; /* Pre-configured Loo style for param dumps */
252 int dump_style_init; /* TRUE after style has been initialised */
253 /* --- Direct C logger write bypass --- */
254 bool use_direct_write; /* TRUE when logger is Medusa::XS::Logger */
255 MedusaLogger *direct_logger; /* Cached C logger struct for direct write */
256 } AUDIT_CACHE;
257  
258 /* ------------------------------------------------------------------ */
259 /* Custom op forward declarations */
260 /* ------------------------------------------------------------------ */
261  
262 static OP *pp_medusa_caller_stack(pTHX);
263 static OP *pp_medusa_format_log(pTHX);
264 static OP *pp_medusa_log_write(pTHX);
265  
266 #if PERL_VERSION >= 14
267 static XOP medusa_xop_caller_stack;
268 static XOP medusa_xop_format_log;
269 static XOP medusa_xop_log_write;
270 #endif
271  
272 /* ------------------------------------------------------------------ */
273 /* Optree injection: inject_audit_wrapper() */
274 /* ------------------------------------------------------------------ */
275  
276 /* Forward declarations */
277 static CV * wrap_existing_cv(pTHX_ CV *orig_cv, const char *method, STRLEN method_len,
278 const char *pkg, STRLEN pkg_len);
279  
280 /*
281 * inject_audit_wrapper - Wrap a CV with audit enter/leave ops
282 *
283 * This creates a wrapper that:
284 * 1. Logs entry (call args, GUID, caller stack, timestamp)
285 * 2. Calls the original subroutine
286 * 3. Logs exit (return values, elapsed time)
287 */
288  
289 /* Magic vtable to identify wrapped CVs */
290 static MGVTBL medusa_cv_vtbl = {0, 0, 0, 0, 0, 0, 0, 0};
291  
292 /* Magic vtable for AUDIT_CACHE - with destructor */
293 static int medusa_cache_free(pTHX_ SV *sv, MAGIC *mg);
294 static MGVTBL medusa_cache_vtbl = {0, 0, 0, 0, medusa_cache_free, 0, 0, 0};
295  
296 static int
297 0 medusa_cache_free(pTHX_ SV *sv, MAGIC *mg) {
298 0 AUDIT_CACHE *cache = (AUDIT_CACHE *)mg->mg_ptr;
299 PERL_UNUSED_VAR(sv);
300 0 ▶ if (cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache) { 0 0 0
301 /* Free pre-formatted message strings */
302 0 ▶ if (cache->entry_msg) SvREFCNT_dec(cache->entry_msg);
Branch: 1 branch
BranchTrueFalseTotal
if (cache->entry_msg) SvREFCNT_dec(cache->entry_msg); 0 0 0
303 0 ▶ if (cache->exit_msg) SvREFCNT_dec(cache->exit_msg);
Branch: 1 branch
BranchTrueFalseTotal
if (cache->exit_msg) SvREFCNT_dec(cache->exit_msg); 0 0 0
304 /* Free scratch buffers */
305 0 ▶ if (cache->scratch_result) SvREFCNT_dec(cache->scratch_result);
Branch: 1 branch
BranchTrueFalseTotal
if (cache->scratch_result) SvREFCNT_dec(cache->scratch_result); 0 0 0
306 0 ▶ if (cache->scratch_guid) SvREFCNT_dec(cache->scratch_guid);
Branch: 1 branch
BranchTrueFalseTotal
if (cache->scratch_guid) SvREFCNT_dec(cache->scratch_guid); 0 0 0
307 0 ▶ if (cache->scratch_caller) SvREFCNT_dec(cache->scratch_caller);
Branch: 1 branch
BranchTrueFalseTotal
if (cache->scratch_caller) SvREFCNT_dec(cache->scratch_caller); 0 0 0
308 0 ▶ if (cache->scratch_dump) SvREFCNT_dec(cache->scratch_dump);
Branch: 1 branch
BranchTrueFalseTotal
if (cache->scratch_dump) SvREFCNT_dec(cache->scratch_dump); 0 0 0
309 /* Free Loo dump style internals */
310 0 ▶ if (cache->dump_style_init)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->dump_style_init) 0 0 0
311 0 ddc_style_destroy(aTHX_ &cache->dump_style);
312 /* Free GUID config strings */
313 0 ▶ if (cache->guid_namespace) Safefree(cache->guid_namespace);
Branch: 1 branch
BranchTrueFalseTotal
if (cache->guid_namespace) Safefree(cache->guid_namespace); 0 0 0
314 0 ▶ if (cache->guid_name) Safefree(cache->guid_name);
Branch: 1 branch
BranchTrueFalseTotal
if (cache->guid_name) Safefree(cache->guid_name); 0 0 0
315 /* Don't free method/package - they point into GV/stash */
316 /* Don't free format_message - it's owned by %LOG */
317 /* Don't free direct_logger - owned by logger object magic */
318 0 Safefree(cache);
319 }
320 0 return 0;
321 }
322  
323 /* Get cached audit metadata from wrapper CV */
324 static AUDIT_CACHE *
325 20 get_audit_cache(pTHX_ CV *cv) {
326 20 MAGIC *mg = mg_findext((SV*)cv, PERL_MAGIC_ext, &medusa_cache_vtbl);
327 20 ▶ if (mg && mg->mg_ptr) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && mg->mg_ptr) { 20 0 20
if (mg && mg->mg_ptr) { 20 0 20
328 20 return (AUDIT_CACHE *)mg->mg_ptr;
329 }
330 0 return NULL;
331 }
332  
333 /* Create and attach AUDIT_CACHE to wrapper CV */
334 static AUDIT_CACHE *
335 25 create_audit_cache(pTHX_ CV *wrapper_cv, CV *orig_cv,
336 const char *method, STRLEN method_len,
337 const char *pkg, STRLEN pkg_len) {
338 AUDIT_CACHE *cache;
339 HV *log_config;
340 SV **svp;
341 char msg_buf[256];
342
343 25 Newxz(cache, 1, AUDIT_CACHE);
344
345 25 cache->original_cv = orig_cv;
346 25 cache->method = method;
347 25 cache->method_len = method_len;
348 25 cache->package = pkg;
349 25 cache->package_len = pkg_len;
350
351 /* Pre-format message strings (saves snprintf per call) */
352 25 snprintf(msg_buf, sizeof(msg_buf), "subroutine %s called with args:", method);
353 25 cache->entry_msg = newSVpv(msg_buf, 0);
354 25 SvREADONLY_on(cache->entry_msg);
355
356 25 snprintf(msg_buf, sizeof(msg_buf), "subroutine %s returned:", method);
357 25 cache->exit_msg = newSVpv(msg_buf, 0);
358 25 SvREADONLY_on(cache->exit_msg);
359
360 /* Cache %LOG reference */
361 25 log_config = get_hv("Medusa::XS::LOG", 0);
362 25 cache->log_config = log_config;
363
364 /* Cache OPTIONS and individual flags */
365 25 cache->opt_guid = cache->opt_caller = cache->opt_elapsed = TRUE;
366 25 cache->opt_date = cache->opt_level = TRUE;
367 25 cache->use_xs_format = TRUE;
368 25 cache->log_level = "debug";
369 25 cache->log_method = "debug";
370
371 25 ▶ if (log_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (log_config) { 25 0 25
372 25 svp = hv_fetchs(log_config, "OPTIONS", 0);
373 25 ▶ if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 25 0 25
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 25 0 25
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 25 0 25
374 25 HV *options = (HV *)SvRV(*svp);
375 SV **opt;
376
377 25 cache->options = options;
378
379 25 opt = hv_fetchs(options, "guid", 0);
380 25 ▶ if (opt) cache->opt_guid = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) cache->opt_guid = SvTRUE(*opt); 25 0 25
381
382 25 opt = hv_fetchs(options, "caller", 0);
383 25 ▶ if (opt) cache->opt_caller = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) cache->opt_caller = SvTRUE(*opt); 25 0 25
384
385 25 opt = hv_fetchs(options, "elapsed_call", 0);
386 25 ▶ if (opt) cache->opt_elapsed = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) cache->opt_elapsed = SvTRUE(*opt); 25 0 25
387
388 25 opt = hv_fetchs(options, "date", 0);
389 25 ▶ if (opt) cache->opt_date = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) cache->opt_date = SvTRUE(*opt); 25 0 25
390
391 25 opt = hv_fetchs(options, "level", 0);
392 25 ▶ if (opt) cache->opt_level = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) cache->opt_level = SvTRUE(*opt); 25 0 25
393  
394 /* GUID version config */
395 25 cache->guid_version = 4; /* default: UUID v4 */
396 25 opt = hv_fetchs(options, "guid_version", 0);
397 25 ▶ if (opt && SvOK(*opt)) cache->guid_version = SvIV(*opt);
Branch: 2 branches
BranchTrueFalseTotal
if (opt && SvOK(*opt)) cache->guid_version = SvIV(*opt); 25 0 25
if (opt && SvOK(*opt)) cache->guid_version = SvIV(*opt); 25 0 25
398  
399 /* GUID namespace (for v3/v5) */
400 25 opt = hv_fetchs(options, "guid_namespace", 0);
401 25 ▶ if (opt && SvPOK(*opt)) {
Branch: 2 branches
BranchTrueFalseTotal
if (opt && SvPOK(*opt)) { 25 0 25
if (opt && SvPOK(*opt)) { 0 25 25
402 0 const char *ns = SvPV(*opt, cache->guid_namespace_len);
403 0 Newx(cache->guid_namespace, cache->guid_namespace_len + 1, char);
404 0 Copy(ns, cache->guid_namespace, cache->guid_namespace_len, char);
405 0 cache->guid_namespace[cache->guid_namespace_len] = '\0';
406 }
407  
408 /* GUID name (for v3/v5) */
409 25 opt = hv_fetchs(options, "guid_name", 0);
410 25 ▶ if (opt && SvPOK(*opt)) {
Branch: 2 branches
BranchTrueFalseTotal
if (opt && SvPOK(*opt)) { 25 0 25
if (opt && SvPOK(*opt)) { 0 25 25
411 0 const char *nm = SvPV(*opt, cache->guid_name_len);
412 0 Newx(cache->guid_name, cache->guid_name_len + 1, char);
413 0 Copy(nm, cache->guid_name, cache->guid_name_len, char);
414 0 cache->guid_name[cache->guid_name_len] = '\0';
415 }
416 }
417
418 /* Cache log level */
419 25 svp = hv_fetchs(log_config, "LOG_LEVEL", 0);
420 25 ▶ if (svp && SvPOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) { 25 0 25
if (svp && SvPOK(*svp)) { 25 0 25
421 50 cache->log_level = SvPV_nolen(*svp);
422 }
423
424 /* Pre-compute uppercase log level */
425 {
426 int i;
427 150 ▶ for (i = 0; cache->log_level[i] && i < 31; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; cache->log_level[i] && i < 31; i++) { 125 25 150
for (i = 0; cache->log_level[i] && i < 31; i++) { 125 0 125
428 125 cache->level_upper[i] = toupper((unsigned char)cache->log_level[i]);
429 }
430 25 cache->level_upper[i] = '\0';
431 }
432
433 /* Cache log method from LOG_FUNCTIONS */
434 25 svp = hv_fetchs(log_config, "LOG_FUNCTIONS", 0);
435 25 ▶ if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 25 0 25
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 25 0 25
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 25 0 25
436 25 HV *funcs = (HV *)SvRV(*svp);
437 25 SV **meth = hv_fetch(funcs, cache->log_level, strlen(cache->log_level), 0);
438 25 ▶ if (meth && SvPOK(*meth)) {
Branch: 2 branches
BranchTrueFalseTotal
if (meth && SvPOK(*meth)) { 25 0 25
if (meth && SvPOK(*meth)) { 25 0 25
439 50 cache->log_method = SvPV_nolen(*meth);
440 }
441 }
442
443 /* Cache QUOTE character */
444 25 cache->quote = "\xE2\x80\xA0"; /* default: † */
445 25 svp = hv_fetchs(log_config, "QUOTE", 0);
446 25 ▶ if (svp && SvPOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) { 25 0 25
if (svp && SvPOK(*svp)) { 25 0 25
447 50 cache->quote = SvPV_nolen(*svp);
448 }
449
450 /* Cache TIME settings */
451 25 cache->time_type = "gmtime";
452 25 svp = hv_fetchs(log_config, "TIME", 0);
453 25 ▶ if (svp && SvPOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) { 25 0 25
if (svp && SvPOK(*svp)) { 25 0 25
454 50 cache->time_type = SvPV_nolen(*svp);
455 }
456 25 cache->time_use_gm = (strcmp(cache->time_type, "gmtime") == 0);
457
458 25 cache->time_fmt = "default";
459 25 svp = hv_fetchs(log_config, "TIME_FORMAT", 0);
460 25 ▶ if (svp && SvPOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) { 25 0 25
if (svp && SvPOK(*svp)) { 25 0 25
461 50 cache->time_fmt = SvPV_nolen(*svp);
462 }
463 25 ▶ cache->time_fmt_c = (strcmp(cache->time_fmt, "default") == 0) ? NULL : cache->time_fmt;
Branch: 1 branch
BranchTrueFalseTotal
cache->time_fmt_c = (strcmp(cache->time_fmt, "default") == 0) ? NULL : cache->time_fmt; 0 25 25
464
465 /* Don't cache LOG object at compile time — it may change at runtime.
466 * The lazy init in medusa_xs_log_cached will pick up the correct
467 * logger on the first call. */
468 25 cache->log_obj = NULL;
469 25 cache->log_obj_initialized = FALSE;
470 25 cache->log_method_cv = NULL;
471 25 cache->use_direct_write = FALSE;
472 25 cache->direct_logger = NULL;
473
474 /* Check if FORMAT_MESSAGE is the default XS wrapper */
475 25 svp = hv_fetchs(log_config, "FORMAT_MESSAGE", 0);
476 25 ▶ if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 25 0 25
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 25 0 25
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 25 0 25
477 25 CV *fm_cv = (CV *)SvRV(*svp);
478 /* Detect our own xs_format_message XSUB — still use fast path */
479 25 GV *xs_fmt_gv = gv_fetchpvs("Medusa::XS::xs_format_message",
480 GV_NOADD_NOINIT, SVt_PVCV);
481 25 ▶ if (xs_fmt_gv && GvCV(xs_fmt_gv) && fm_cv == GvCV(xs_fmt_gv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (xs_fmt_gv && GvCV(xs_fmt_gv) && fm_cv == GvCV(xs_fmt_gv)) { 25 0 25
if (xs_fmt_gv && GvCV(xs_fmt_gv) && fm_cv == GvCV(xs_fmt_gv)) { 25 0 25
if (xs_fmt_gv && GvCV(xs_fmt_gv) && fm_cv == GvCV(xs_fmt_gv)) { 24 1 25
482 /* It's our default — keep use_xs_format = TRUE */
483 } else {
484 1 cache->format_message = *svp;
485 1 cache->use_xs_format = FALSE;
486 }
487 }
488 }
489
490 /* Allocate scratch buffers (reused across calls) */
491 25 cache->scratch_result = newSV(MEDUSA_LOG_BUF_SIZE);
492 25 SvPOK_on(cache->scratch_result);
493 25 SvCUR_set(cache->scratch_result, 0);
494 25 *SvPVX(cache->scratch_result) = '\0';
495
496 25 cache->scratch_guid = newSV(MEDUSA_GUID_LEN + 1);
497 25 SvPOK_on(cache->scratch_guid);
498 25 SvCUR_set(cache->scratch_guid, 0);
499
500 25 cache->scratch_caller = newSV(MEDUSA_CALLER_STACK_INITIAL_SIZE);
501 25 SvPOK_on(cache->scratch_caller);
502 25 SvCUR_set(cache->scratch_caller, 0);
503
504 25 cache->scratch_dump = newSV(256);
505 25 SvPOK_on(cache->scratch_dump);
506 25 SvCUR_set(cache->scratch_dump, 0);
507  
508 /* Initialise Loo dump style with colour from OPTIONS */
509 {
510 25 int use_colour = medusa_resolve_colour(aTHX_ cache->options);
511 25 const char *theme = medusa_resolve_theme(aTHX_ cache->options);
512 25 medusa_loo_style_init(aTHX_ &cache->dump_style, use_colour, theme);
513 25 cache->dump_style_init = 1;
514 }
515
516 /* Attach cache to wrapper CV */
517 25 sv_magicext((SV*)wrapper_cv, NULL, PERL_MAGIC_ext, &medusa_cache_vtbl,
518 (const char *)cache, 0);
519
520 25 return cache;
521 }
522  
523 /* Check if a CV is already wrapped */
524 #define CV_IS_AUDITED(cv) (SvMAGICAL((SV*)cv) && mg_findext((SV*)cv, PERL_MAGIC_ext, &medusa_cv_vtbl))
525  
526 /* Store original CV in magic for later retrieval */
527 static void
528 50 mark_cv_audited(pTHX_ CV *cv, const char *method, STRLEN method_len) {
529 MAGIC *mg;
530 50 ▶ if (!CV_IS_AUDITED(cv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!CV_IS_AUDITED(cv)) { 0 50 50
if (!CV_IS_AUDITED(cv)) { 0 0 0
531 50 mg = sv_magicext((SV*)cv, NULL, PERL_MAGIC_ext, &medusa_cv_vtbl, method, method_len);
532 50 mg->mg_flags |= MGf_DUP;
533 }
534 50 }
535  
536 /* Get method name from audited CV magic */
537 static const char *
538 0 get_audited_method(pTHX_ CV *cv, STRLEN *len) {
539 0 MAGIC *mg = mg_findext((SV*)cv, PERL_MAGIC_ext, &medusa_cv_vtbl);
540 0 ▶ if (mg && mg->mg_ptr) {
Branch: 2 branches
BranchTrueFalseTotal
if (mg && mg->mg_ptr) { 0 0 0
if (mg && mg->mg_ptr) { 0 0 0
541 0 *len = mg->mg_len;
542 0 return mg->mg_ptr;
543 }
544 0 *len = 0;
545 0 return NULL;
546 }
547  
548 /*
549 * Deferred wrapping for older Perls (5.10-5.12).
550 *
551 * On 5.10, MODIFY_CODE_ATTRIBUTES fires inside newATTRSUB before the CV
552 * is installed into the glob. CvGV(cv) is NULL at that point.
553 *
554 * Strategy: queue (original_cv, pkg_name) pairs. In CHECK phase,
555 * scan the package stash to find which GV now points to each CV,
556 * then create the wrapper and replace the GV's CV.
557 */
558 static AV *medusa_pending_wraps = NULL;
559  
560 /* Queue an original CV + package name for deferred wrapping */
561 static void
562 0 medusa_queue_deferred_wrap(pTHX_ CV *cv, const char *pkg, STRLEN pkg_len)
563 {
564 0 ▶ if (!medusa_pending_wraps) {
Branch: 1 branch
BranchTrueFalseTotal
if (!medusa_pending_wraps) { 0 0 0
565 0 medusa_pending_wraps = newAV();
566 }
567 0 av_push(medusa_pending_wraps, newRV_inc((SV *)cv));
568 0 av_push(medusa_pending_wraps, newSVpvn(pkg, pkg_len));
569 0 }
570  
571 /* CHECK-phase callback: apply all deferred wraps */
572 static void
573 12 medusa_check_apply_wraps(pTHX_ void *data)
574 {
575 I32 i, len;
576 PERL_UNUSED_VAR(data);
577  
578 12 ▶ if (!medusa_pending_wraps) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!medusa_pending_wraps) return; 12 0 12
579 0 len = av_len(medusa_pending_wraps);
580  
581 /* Each entry is a pair: [cv_rv, pkg_sv] stored as consecutive elements */
582 0 ▶ for (i = 0; i <= len; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; i += 2) { 0 0 0
583 0 SV **cv_rvp = av_fetch(medusa_pending_wraps, i, 0);
584 0 SV **pkg_svp = av_fetch(medusa_pending_wraps, i + 1, 0);
585 0 ▶ if (cv_rvp && SvROK(*cv_rvp) && pkg_svp && SvPOK(*pkg_svp)) {
Branch: 4 branches
BranchTrueFalseTotal
if (cv_rvp && SvROK(*cv_rvp) && pkg_svp && SvPOK(*pkg_svp)) { 0 0 0
if (cv_rvp && SvROK(*cv_rvp) && pkg_svp && SvPOK(*pkg_svp)) { 0 0 0
if (cv_rvp && SvROK(*cv_rvp) && pkg_svp && SvPOK(*pkg_svp)) { 0 0 0
if (cv_rvp && SvROK(*cv_rvp) && pkg_svp && SvPOK(*pkg_svp)) { 0 0 0
586 0 CV *orig_cv = (CV *)SvRV(*cv_rvp);
587 const char *pkg;
588 STRLEN pkg_len;
589 HV *stash;
590 HE *he;
591 0 GV *found_gv = NULL;
592 0 const char *method_name = NULL;
593 0 STRLEN method_len = 0;
594  
595 0 pkg = SvPV(*pkg_svp, pkg_len);
596 0 stash = gv_stashpvn(pkg, pkg_len, 0);
597 0 ▶ if (!stash) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!stash) continue; 0 0 0
598  
599 /* Scan the stash to find which GV holds this CV */
600 0 hv_iterinit(stash);
601 0 ▶ while ((he = hv_iternext(stash))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(stash))) { 0 0 0
602 0 GV *gv = (GV *)HeVAL(he);
603 0 ▶ if (isGV(gv) && GvCV(gv) == orig_cv) {
Branch: 2 branches
BranchTrueFalseTotal
if (isGV(gv) && GvCV(gv) == orig_cv) { 0 0 0
if (isGV(gv) && GvCV(gv) == orig_cv) { 0 0 0
604 0 found_gv = gv;
605 0 method_name = GvNAME(gv);
606 0 method_len = GvNAMELEN(gv);
607 0 break;
608 }
609 }
610  
611 0 ▶ if (found_gv && method_name) {
Branch: 2 branches
BranchTrueFalseTotal
if (found_gv && method_name) { 0 0 0
if (found_gv && method_name) { 0 0 0
612 CV *wrapper_cv;
613  
614 /* Update the audited mark with the real method name */
615 0 mark_cv_audited(aTHX_ orig_cv, method_name, method_len);
616  
617 /* Create wrapper CV */
618 0 wrapper_cv = wrap_existing_cv(aTHX_ orig_cv, method_name,
619 method_len, pkg, pkg_len);
620 0 ▶ if (wrapper_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (wrapper_cv) { 0 0 0
621 0 GvCV_set(found_gv, wrapper_cv);
622 0 CvGV_set(wrapper_cv, found_gv);
623 }
624 }
625 }
626 }
627 0 av_clear(medusa_pending_wraps);
628 }
629  
630 /*
631 * inject_audit_wrapper - Main entry point for wrapping a subroutine
632 *
633 * Called when :Audit attribute is applied to a subroutine.
634 * Replaces the CV's body with the audit wrapper XSUB.
635 */
636 static void
637 26 inject_audit_wrapper(pTHX_ CV *cv, const char *method, STRLEN method_len,
638 const char *pkg, STRLEN pkg_len) {
639 GV *gv;
640 CV *wrapper_cv;
641  
642 26 ▶ if (!cv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!cv) return; 0 26 26
643  
644 /* Don't wrap if already audited */
645 26 ▶ if (CV_IS_AUDITED(cv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (CV_IS_AUDITED(cv)) { 1 25 26
if (CV_IS_AUDITED(cv)) { 1 0 1
646 1 return;
647 }
648  
649 /* Get the GV for this CV so we can replace it in the stash */
650 25 gv = CvGV(cv);
651  
652 25 ▶ if (!gv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gv) { 0 25 25
653 /* On 5.10, CvGV is NULL inside MODIFY_CODE_ATTRIBUTES because
654 * newATTRSUB hasn't installed the CV into the glob yet.
655 * Queue for deferred wrapping in CHECK phase. */
656 0 medusa_queue_deferred_wrap(aTHX_ cv, pkg, pkg_len);
657 0 return;
658 }
659  
660 /* Mark the original CV as audited */
661 25 mark_cv_audited(aTHX_ cv, method, method_len);
662  
663 /* Create wrapper CV */
664 25 wrapper_cv = wrap_existing_cv(aTHX_ cv, method, method_len, pkg, pkg_len);
665 25 ▶ if (!wrapper_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!wrapper_cv) { 0 25 25
666 0 return;
667 }
668  
669 /* Replace the CV in the glob */
670 25 GvCV_set(gv, wrapper_cv);
671 25 CvGV_set(wrapper_cv, gv);
672 }
673  
674 /* Forward declaration of wrapper XSUB */
675 XS(XS_Medusa__XS_audit_wrapper);
676  
677 /*
678 * wrap_existing_cv - Wrap an existing CV with audit functionality
679 *
680 * Creates XSUB wrapper + AUDIT_CACHE with all config pre-computed.
681 */
682 static CV *
683 25 wrap_existing_cv(pTHX_ CV *orig_cv, const char *method, STRLEN method_len,
684 const char *pkg, STRLEN pkg_len) {
685 CV *wrapper_cv;
686 AUDIT_CACHE *cache;
687
688 25 ▶ if (!orig_cv) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!orig_cv) return NULL; 0 25 25
689
690 /* Create a new wrapper CV */
691 25 wrapper_cv = newXS(NULL, XS_Medusa__XS_audit_wrapper, __FILE__);
692
693 /* Store original CV ref in wrapper for later retrieval */
694 25 CvXSUBANY(wrapper_cv).any_ptr = (void *)orig_cv;
695 25 SvREFCNT_inc((SV *)orig_cv);
696
697 /* Mark the wrapper as audited */
698 25 mark_cv_audited(aTHX_ wrapper_cv, method, method_len);
699
700 /* Create cached metadata for fast wrapper execution */
701 25 cache = create_audit_cache(aTHX_ wrapper_cv, orig_cv,
702 method, method_len, pkg, pkg_len);
703
704 25 return wrapper_cv;
705 }
706  
707 /* ------------------------------------------------------------------ */
708 /* XS integrated logging - calls Perl FORMAT_MESSAGE and logger */
709 /* ------------------------------------------------------------------ */
710  
711 /* Forward declaration */
712 static void medusa_xs_log_message(pTHX_ HV *params);
713  
714 /* ------------------------------------------------------------------ */
715 /* Colour resolution helpers */
716 /* ------------------------------------------------------------------ */
717  
718 /* Resolve colour setting from OPTIONS hash: 0=off, 1=on, 'auto'=detect */
719 static int
720 83 medusa_resolve_colour(pTHX_ HV *options)
721 {
722 SV **svp;
723  
724 83 ▶ if (!options) return 1; /* default: enabled */
Branch: 1 branch
BranchTrueFalseTotal
if (!options) return 1; /* default: enabled */ 0 83 83
725  
726 83 svp = hv_fetchs(options, "colour", 0);
727 83 ▶ if (!svp || !SvOK(*svp)) return 1; /* default: enabled */
Branch: 2 branches
BranchTrueFalseTotal
if (!svp || !SvOK(*svp)) return 1; /* default: enabled */ 83 0 83
if (!svp || !SvOK(*svp)) return 1; /* default: enabled */ 0 83 83
728  
729 /* Check for 'auto' string */
730 83 ▶ if (SvPOK(*svp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOK(*svp)) { 0 83 83
731 0 const char *val = SvPV_nolen(*svp);
732 0 ▶ if (strEQ(val, "auto"))
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(val, "auto")) 0 0 0
733 0 return loo_detect_colour(aTHX);
734 /* "0" or "1" as string */
735 0 return SvTRUE(*svp) ? 1 : 0;
736 }
737  
738 83 return SvIV(*svp) ? 1 : 0;
739 }
740  
741 /* Resolve theme name from OPTIONS hash */
742 static const char *
743 45 medusa_resolve_theme(pTHX_ HV *options)
744 {
745 SV **svp;
746  
747 45 ▶ if (!options) return "default";
Branch: 1 branch
BranchTrueFalseTotal
if (!options) return "default"; 0 45 45
748  
749 45 svp = hv_fetchs(options, "colour_theme", 0);
750 45 ▶ if (svp && SvPOK(*svp))
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) 45 0 45
if (svp && SvPOK(*svp)) 45 0 45
751 90 return SvPV_nolen(*svp);
752  
753 0 return "default";
754 }
755  
756 /* ------------------------------------------------------------------ */
757 /* Loo-based SV serializer (replaces custom dump and Data::Dumper) */
758 /* ------------------------------------------------------------------ */
759  
760 /* Initialise a DDCStyle for Medusa's compact log output */
761 static void
762 45 medusa_loo_style_init(pTHX_ DDCStyle *style, int use_colour,
763 const char *theme_name)
764 {
765 45 ddc_style_init(style);
766  
767 /* Compact output for log lines */
768 45 style->terse = 1; /* no $VARn = */
769 45 style->indent = 0; /* single-line output */
770 45 style->sortkeys = 1; /* deterministic key order */
771 45 style->maxdepth = 8; /* same limit as before */
772 45 style->quotekeys = 0; /* cleaner output */
773 45 style->useqq = 1; /* escape non-printable chars */
774  
775 /* Colour */
776 45 style->use_colour = use_colour;
777 45 ▶ if (use_colour && theme_name) {
Branch: 2 branches
BranchTrueFalseTotal
if (use_colour && theme_name) { 31 14 45
if (use_colour && theme_name) { 31 0 31
778 31 const LooTheme *theme = loo_find_theme(theme_name);
779 31 ▶ if (theme) {
Branch: 1 branch
BranchTrueFalseTotal
if (theme) { 31 0 31
780 31 ▶ style->c_string_fg = theme->string_fg ? ddc_resolve_colour_named(theme->string_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_string_fg = theme->string_fg ? ddc_resolve_colour_named(theme->string_fg, 0) : NULL; 31 0 31
781 31 ▶ style->c_number_fg = theme->number_fg ? ddc_resolve_colour_named(theme->number_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_number_fg = theme->number_fg ? ddc_resolve_colour_named(theme->number_fg, 0) : NULL; 31 0 31
782 31 ▶ style->c_key_fg = theme->key_fg ? ddc_resolve_colour_named(theme->key_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_key_fg = theme->key_fg ? ddc_resolve_colour_named(theme->key_fg, 0) : NULL; 31 0 31
783 31 ▶ style->c_brace_fg = theme->brace_fg ? ddc_resolve_colour_named(theme->brace_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_brace_fg = theme->brace_fg ? ddc_resolve_colour_named(theme->brace_fg, 0) : NULL; 31 0 31
784 31 ▶ style->c_bracket_fg = theme->bracket_fg ? ddc_resolve_colour_named(theme->bracket_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_bracket_fg = theme->bracket_fg ? ddc_resolve_colour_named(theme->bracket_fg, 0) : NULL; 31 0 31
785 31 ▶ style->c_paren_fg = theme->paren_fg ? ddc_resolve_colour_named(theme->paren_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_paren_fg = theme->paren_fg ? ddc_resolve_colour_named(theme->paren_fg, 0) : NULL; 31 0 31
786 31 ▶ style->c_arrow_fg = theme->arrow_fg ? ddc_resolve_colour_named(theme->arrow_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_arrow_fg = theme->arrow_fg ? ddc_resolve_colour_named(theme->arrow_fg, 0) : NULL; 31 0 31
787 31 ▶ style->c_comma_fg = theme->comma_fg ? ddc_resolve_colour_named(theme->comma_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_comma_fg = theme->comma_fg ? ddc_resolve_colour_named(theme->comma_fg, 0) : NULL; 31 0 31
788 31 ▶ style->c_undef_fg = theme->undef_fg ? ddc_resolve_colour_named(theme->undef_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_undef_fg = theme->undef_fg ? ddc_resolve_colour_named(theme->undef_fg, 0) : NULL; 31 0 31
789 31 ▶ style->c_blessed_fg = theme->blessed_fg ? ddc_resolve_colour_named(theme->blessed_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_blessed_fg = theme->blessed_fg ? ddc_resolve_colour_named(theme->blessed_fg, 0) : NULL; 31 0 31
790 31 ▶ style->c_regex_fg = theme->regex_fg ? ddc_resolve_colour_named(theme->regex_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_regex_fg = theme->regex_fg ? ddc_resolve_colour_named(theme->regex_fg, 0) : NULL; 31 0 31
791 31 ▶ style->c_code_fg = theme->code_fg ? ddc_resolve_colour_named(theme->code_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_code_fg = theme->code_fg ? ddc_resolve_colour_named(theme->code_fg, 0) : NULL; 31 0 31
792 31 ▶ style->c_variable_fg = theme->variable_fg ? ddc_resolve_colour_named(theme->variable_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_variable_fg = theme->variable_fg ? ddc_resolve_colour_named(theme->variable_fg, 0) : NULL; 31 0 31
793 31 ▶ style->c_quote_fg = theme->quote_fg ? ddc_resolve_colour_named(theme->quote_fg, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
style->c_quote_fg = theme->quote_fg ? ddc_resolve_colour_named(theme->quote_fg, 0) : NULL; 31 0 31
794 31 style->c_reset = "\033[0m";
795 }
796 }
797 45 }
798  
799 /* Dump a single SV to a compact string via Loo */
800 static SV *
801 20 medusa_dump_param(pTHX_ SV *value, DDCStyle *style)
802 {
803 SV *out;
804  
805 /* Reset per-dump state */
806 20 style->level = 0;
807 20 ▶ if (style->seen) hv_clear(style->seen);
Branch: 1 branch
BranchTrueFalseTotal
if (style->seen) hv_clear(style->seen); 2 18 20
808 18 else style->seen = newHV();
809 20 ▶ if (style->post) av_clear(style->post);
Branch: 1 branch
BranchTrueFalseTotal
if (style->post) av_clear(style->post); 2 18 20
810 18 else style->post = newAV();
811  
812 20 out = newSVpvn("", 0);
813 20 ▶ SvGROW(out, 256);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(out, 256); 20 0 20
SvGROW(out, 256); 20 0 20
814 20 style->out = out;
815  
816 20 ddc_dump_ref(aTHX_ value, style, 0);
817  
818 20 return out;
819 }
820  
821 /* Dump a single SV into a pre-allocated buffer (hot-path, no alloc) */
822 static void
823 42 medusa_dump_param_into(pTHX_ SV *value, DDCStyle *style, SV *out)
824 {
825 /* Reset per-dump state */
826 42 style->level = 0;
827 42 ▶ if (style->seen) hv_clear(style->seen);
Branch: 1 branch
BranchTrueFalseTotal
if (style->seen) hv_clear(style->seen); 27 15 42
828 15 else style->seen = newHV();
829 42 ▶ if (style->post) av_clear(style->post);
Branch: 1 branch
BranchTrueFalseTotal
if (style->post) av_clear(style->post); 27 15 42
830 15 else style->post = newAV();
831  
832 42 SvCUR_set(out, 0);
833 42 *SvPVX(out) = '\0';
834 42 style->out = out;
835  
836 42 ddc_dump_ref(aTHX_ value, style, 0);
837 42 }
838  
839  
840  
841  
842 /*
843 * XS implementation of FORMAT_MESSAGE
844 * Matches the Perl implementation in original Medusa
845 */
846 static SV *
847 7 medusa_xs_format_message(pTHX_ HV *params, HV *log_config) {
848 SV *result;
849 SV **svp;
850 7 HV *options = NULL;
851 bool opt_date, opt_guid, opt_level, opt_caller, opt_elapsed;
852 7 const char *quote = "†";
853 7 const char *time_type = "gmtime";
854 7 const char *time_fmt = "default";
855 7 const char *log_level = "debug";
856 7 SV *timestamp = NULL;
857
858 /* Get quote character */
859 7 svp = hv_fetchs(log_config, "QUOTE", 0);
860 7 ▶ if (svp && SvPOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) { 7 0 7
if (svp && SvPOK(*svp)) { 7 0 7
861 14 quote = SvPV_nolen(*svp);
862 }
863
864 /* Get TIME setting */
865 7 svp = hv_fetchs(log_config, "TIME", 0);
866 7 ▶ if (svp && SvPOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) { 7 0 7
if (svp && SvPOK(*svp)) { 7 0 7
867 14 time_type = SvPV_nolen(*svp);
868 }
869
870 /* Get TIME_FORMAT */
871 7 svp = hv_fetchs(log_config, "TIME_FORMAT", 0);
872 7 ▶ if (svp && SvPOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) { 7 0 7
if (svp && SvPOK(*svp)) { 7 0 7
873 14 time_fmt = SvPV_nolen(*svp);
874 }
875
876 /* Get options hash */
877 7 opt_date = opt_guid = opt_level = opt_caller = opt_elapsed = TRUE;
878 7 svp = hv_fetchs(log_config, "OPTIONS", 0);
879 7 ▶ if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 7 0 7
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 7 0 7
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 7 0 7
880 7 options = (HV *)SvRV(*svp);
881 SV **opt;
882
883 7 opt = hv_fetchs(options, "date", 0);
884 7 ▶ if (opt) opt_date = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) opt_date = SvTRUE(*opt); 7 0 7
885
886 7 opt = hv_fetchs(options, "guid", 0);
887 7 ▶ if (opt) opt_guid = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) opt_guid = SvTRUE(*opt); 7 0 7
888
889 7 opt = hv_fetchs(options, "level", 0);
890 7 ▶ if (opt) opt_level = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) opt_level = SvTRUE(*opt); 7 0 7
891
892 7 opt = hv_fetchs(options, "caller", 0);
893 7 ▶ if (opt) opt_caller = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) opt_caller = SvTRUE(*opt); 7 0 7
894
895 7 opt = hv_fetchs(options, "elapsed_call", 0);
896 7 ▶ if (opt) opt_elapsed = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) opt_elapsed = SvTRUE(*opt); 7 0 7
897 }
898
899 /* Get level from params */
900 7 svp = hv_fetchs(params, "level", 0);
901 7 ▶ if (svp && SvPOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) { 7 0 7
if (svp && SvPOK(*svp)) { 7 0 7
902 14 log_level = SvPV_nolen(*svp);
903 }
904
905 7 result = newSVpvn("", 0);
906 7 ▶ SvGROW(result, MEDUSA_LOG_BUF_SIZE);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(result, MEDUSA_LOG_BUF_SIZE); 7 0 7
SvGROW(result, MEDUSA_LOG_BUF_SIZE); 7 0 7
907
908 /* Add timestamp */
909 7 ▶ if (opt_date) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt_date) { 7 0 7
910 7 bool use_gm = (strcmp(time_type, "gmtime") == 0);
911 7 ▶ const char *fmt = (strcmp(time_fmt, "default") == 0) ? NULL : time_fmt;
Branch: 1 branch
BranchTrueFalseTotal
const char *fmt = (strcmp(time_fmt, "default") == 0) ? NULL : time_fmt; 0 7 7
912 7 timestamp = medusa_format_time(aTHX_ use_gm, fmt);
913 7 sv_catpvf(result, "%s ", SvPV_nolen(timestamp));
914 7 SvREFCNT_dec(timestamp);
915 }
916
917 /* Add GUID */
918 7 ▶ if (opt_guid) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt_guid) { 7 0 7
919 7 svp = hv_fetchs(params, "guid", 0);
920 7 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 0 7 7
if (svp && SvOK(*svp)) { 0 0 0
921 0 sv_catpvf(result, "%s ", SvPV_nolen(*svp));
922 }
923 }
924
925 /* Add log level (uppercase) */
926 7 ▶ if (opt_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt_level) { 7 0 7
927 char level_upper[32];
928 int i;
929 41 ▶ for (i = 0; log_level[i] && i < 31; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; log_level[i] && i < 31; i++) { 34 7 41
for (i = 0; log_level[i] && i < 31; i++) { 34 0 34
930 34 level_upper[i] = toupper((unsigned char)log_level[i]);
931 }
932 7 level_upper[i] = '\0';
933 7 sv_catpv(result, level_upper);
934 }
935
936 /* Add caller stack */
937 7 ▶ if (opt_caller) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt_caller) { 7 0 7
938 7 svp = hv_fetchs(params, "caller", 0);
939 7 ▶ if (svp && SvOK(*svp) && SvCUR(*svp) > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp) && SvCUR(*svp) > 0) { 0 7 7
if (svp && SvOK(*svp) && SvCUR(*svp) > 0) { 0 0 0
if (svp && SvOK(*svp) && SvCUR(*svp) > 0) { 0 0 0
940 0 sv_catpvf(result, " caller=%s%s%s", quote, SvPV_nolen(*svp), quote);
941 }
942 }
943
944 /* Add message */
945 7 svp = hv_fetchs(params, "message", 0);
946 7 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 7 0 7
if (svp && SvOK(*svp)) { 7 0 7
947 14 sv_catpvf(result, " message=%s%s%s", quote, SvPV_nolen(*svp), quote);
948 }
949
950 /* Add elapsed if present (check both 'elapsed' and 'elapsed_call' keys) */
951 7 ▶ if (opt_elapsed) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt_elapsed) { 7 0 7
952 7 svp = hv_fetchs(params, "elapsed_call", 0);
953 7 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 0 7 7
if (svp && SvOK(*svp)) { 0 0 0
954 0 sv_catpvf(result, " elapsed_call=%s%s%s", quote, SvPV_nolen(*svp), quote);
955 } else {
956 7 svp = hv_fetchs(params, "elapsed", 0);
957 7 ▶ if (svp && SvOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvOK(*svp)) { 1 6 7
if (svp && SvOK(*svp)) { 1 0 1
958 2 sv_catpvf(result, " elapsed=%s%s%s", quote, SvPV_nolen(*svp), quote);
959 }
960 }
961 }
962
963 /* Add params array (arg0, arg1, ... or return0, return1, ...) */
964 7 svp = hv_fetchs(params, "params", 0);
965 7 ▶ if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) {
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) { 6 1 7
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) { 6 0 6
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVAV) { 6 0 6
966 6 AV *params_av = (AV *)SvRV(*svp);
967 6 I32 len = av_len(params_av);
968 I32 i;
969 6 SV **prefix_sv = hv_fetchs(params, "prefix", 0);
970 12 ▶ const char *prefix = (prefix_sv && SvPOK(*prefix_sv)) ? SvPV_nolen(*prefix_sv) : "arg";
Branch: 2 branches
BranchTrueFalseTotal
const char *prefix = (prefix_sv && SvPOK(*prefix_sv)) ? SvPV_nolen(*prefix_sv) : "arg"; 6 0 6
const char *prefix = (prefix_sv && SvPOK(*prefix_sv)) ? SvPV_nolen(*prefix_sv) : "arg"; 6 0 6
971  
972 {
973 DDCStyle style;
974 6 int use_colour = medusa_resolve_colour(aTHX_ options);
975 6 const char *theme = medusa_resolve_theme(aTHX_ options);
976 6 medusa_loo_style_init(aTHX_ &style, use_colour, theme);
977 12 ▶ for (i = 0; i <= len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; i++) { 6 6 12
978 6 SV **elem = av_fetch(params_av, i, 0);
979 6 ▶ if (elem && *elem) {
Branch: 2 branches
BranchTrueFalseTotal
if (elem && *elem) { 6 0 6
if (elem && *elem) { 6 0 6
980 6 SV *cleaned = medusa_dump_param(aTHX_ *elem, &style);
981 6 sv_catpvf(result, " %s%d=%s%s%s", prefix, (int)i, quote, SvPV_nolen(cleaned), quote);
982 6 SvREFCNT_dec(cleaned);
983 }
984 }
985 6 ddc_style_destroy(aTHX_ &style);
986 }
987 }
988  
989 7 return result;
990 }
991  
992 /*
993 * Fast-path format+log that uses AUDIT_CACHE directly.
994 * Eliminates ALL hash lookups from the log pipeline.
995 * Uses scratch SVs to avoid per-call SV allocation.
996 * Uses direct C write bypass for Medusa::XS::Logger.
997 * Only used when use_xs_format is TRUE (no custom Perl FORMAT_MESSAGE).
998 */
999 static void
1000 38 medusa_xs_log_cached(pTHX_ AUDIT_CACHE *cache, SV *guid_sv, SV *caller_sv,
1001 SV *message_sv, AV *params_av, const char *prefix,
1002 const char *elapsed_str) {
1003 38 dSP;
1004 SV *result;
1005 SV *log_obj;
1006
1007 /* Get/initialize logger object (lazy — only on first call) */
1008 38 ▶ if (!cache->log_obj_initialized) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cache->log_obj_initialized) { 17 21 38
1009 17 HV *log_config = cache->log_config;
1010 SV **svp;
1011
1012 17 ▶ if (!log_config) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!log_config) return; 0 17 17
1013
1014 17 svp = hv_fetchs(log_config, "LOG", 0);
1015 17 ▶ if (!svp || !SvROK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!svp || !SvROK(*svp)) { 17 0 17
if (!svp || !SvROK(*svp)) { 0 17 17
1016 /* Call LOG_INIT */
1017 0 SV **init_cb = hv_fetchs(log_config, "LOG_INIT", 0);
1018 0 ▶ if (init_cb && SvROK(*init_cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (init_cb && SvROK(*init_cb)) { 0 0 0
if (init_cb && SvROK(*init_cb)) { 0 0 0
1019 0 ENTER;
1020 0 SAVETMPS;
1021 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1022 0 PUTBACK;
1023 0 call_sv(*init_cb, G_SCALAR);
1024 0 SPAGAIN;
1025 0 log_obj = POPs;
1026 0 ▶ if (SvROK(log_obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(log_obj)) { 0 0 0
1027 0 hv_stores(log_config, "LOG", SvREFCNT_inc(log_obj));
1028 0 cache->log_obj = log_obj;
1029 }
1030 0 PUTBACK;
1031 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1032 0 LEAVE;
1033 }
1034 0 svp = hv_fetchs(log_config, "LOG", 0);
1035 }
1036
1037 17 ▶ if (!svp || !SvROK(*svp)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!svp || !SvROK(*svp)) return; 17 0 17
if (!svp || !SvROK(*svp)) return; 0 17 17
1038 17 cache->log_obj = *svp;
1039 17 cache->log_obj_initialized = TRUE;
1040 /* Resolve method CV for direct dispatch */
1041 {
1042 17 GV *method_gv = gv_fetchmethod_autoload(
1043 SvSTASH(SvRV(*svp)), cache->log_method, FALSE);
1044 17 ▶ if (method_gv && isGV(method_gv) && GvCV(method_gv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (method_gv && isGV(method_gv) && GvCV(method_gv)) { 17 0 17
if (method_gv && isGV(method_gv) && GvCV(method_gv)) { 17 0 17
if (method_gv && isGV(method_gv) && GvCV(method_gv)) { 17 0 17
1045 17 cache->log_method_cv = GvCV(method_gv);
1046 }
1047 }
1048 /* Detect Medusa::XS::Logger for direct C write bypass */
1049 {
1050 17 HV *stash = SvSTASH(SvRV(*svp));
1051 17 ▶ if (stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (stash) { 17 0 17
1052 17 ▶ const char *name = HvNAME(stash);
Branch: 6 branches
BranchTrueFalseTotal
const char *name = HvNAME(stash); 17 0 17
const char *name = HvNAME(stash); 17 0 17
const char *name = HvNAME(stash); 0 17 17
const char *name = HvNAME(stash); 0 0 0
const char *name = HvNAME(stash); 17 0 17
const char *name = HvNAME(stash); 0 17 17
1053 17 ▶ if (name && strcmp(name, "Medusa::XS::Logger") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (name && strcmp(name, "Medusa::XS::Logger") == 0) { 17 0 17
if (name && strcmp(name, "Medusa::XS::Logger") == 0) { 1 16 17
1054 1 MedusaLogger *lg = medusa_logger_from_sv(aTHX_ *svp);
1055 1 ▶ if (lg) {
Branch: 1 branch
BranchTrueFalseTotal
if (lg) { 1 0 1
1056 1 cache->use_direct_write = TRUE;
1057 1 cache->direct_logger = lg;
1058 }
1059 }
1060 }
1061 }
1062 }
1063
1064 38 log_obj = cache->log_obj;
1065 38 ▶ if (!log_obj || !SvROK(log_obj)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!log_obj || !SvROK(log_obj)) return; 38 0 38
if (!log_obj || !SvROK(log_obj)) return; 0 38 38
1066  
1067 /* Runtime colour toggle: re-read colour setting from OPTIONS and
1068 * reinit the dump style if it changed (Option A: ~50ns hash fetch) */
1069 38 ▶ if (cache->options) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->options) { 38 0 38
1070 38 int current_colour = medusa_resolve_colour(aTHX_ cache->options);
1071 38 ▶ if (cache->dump_style.use_colour != current_colour) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->dump_style.use_colour != current_colour) { 0 38 38
1072 0 const char *theme = medusa_resolve_theme(aTHX_ cache->options);
1073 0 ▶ if (cache->dump_style_init)
Branch: 1 branch
BranchTrueFalseTotal
if (cache->dump_style_init) 0 0 0
1074 0 ddc_style_destroy(aTHX_ &cache->dump_style);
1075 0 medusa_loo_style_init(aTHX_ &cache->dump_style, current_colour, theme);
1076 0 cache->dump_style_init = 1;
1077 }
1078 }
1079
1080 /* Reuse scratch result SV (avoid newSV + SvGROW per call) */
1081 38 result = cache->scratch_result;
1082 38 SvCUR_set(result, 0);
1083 38 *SvPVX(result) = '\0';
1084
1085 /* Timestamp — write directly into a stack buffer */
1086 38 ▶ if (cache->opt_date) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->opt_date) { 38 0 38
1087 struct timeval tv;
1088 struct tm *tm_info;
1089 char tbuf[MEDUSA_TIME_BUF_SIZE];
1090
1091 38 gettimeofday(&tv, NULL);
1092 38 tm_info = medusa_tm(&tv, cache->time_use_gm);
1093
1094 38 ▶ if (!cache->time_fmt_c) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cache->time_fmt_c) { 38 0 38
1095 38 medusa_default_time(tbuf, sizeof(tbuf), tm_info);
1096 } else {
1097 char *ms_pos;
1098 char fmt_copy[128];
1099 0 strncpy(fmt_copy, cache->time_fmt_c, sizeof(fmt_copy) - 1);
1100 0 fmt_copy[sizeof(fmt_copy) - 1] = '\0';
1101 0 ms_pos = strstr(fmt_copy, "%ms");
1102 0 ▶ if (ms_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (ms_pos) { 0 0 0
1103 char ms_buf[16];
1104 STRLEN len;
1105 0 snprintf(ms_buf, sizeof(ms_buf), "%03ld", (long)(tv.tv_usec / 1000));
1106 0 *ms_pos = '\0';
1107 0 strftime(tbuf, sizeof(tbuf), fmt_copy, tm_info);
1108 0 len = strlen(tbuf);
1109 0 snprintf(tbuf + len, sizeof(tbuf) - len, "%s%s", ms_buf, ms_pos + 3);
1110 } else {
1111 0 strftime(tbuf, sizeof(tbuf), fmt_copy, tm_info);
1112 }
1113 }
1114 38 sv_catpv(result, tbuf);
1115 38 sv_catpvn(result, " ", 1);
1116 }
1117
1118 /* GUID — use sv_catpvn instead of sv_catpvf */
1119 38 ▶ if (cache->opt_guid && guid_sv) {
Branch: 2 branches
BranchTrueFalseTotal
if (cache->opt_guid && guid_sv) { 38 0 38
if (cache->opt_guid && guid_sv) { 38 0 38
1120 38 sv_catsv(result, guid_sv);
1121 38 sv_catpvn(result, " ", 1);
1122 }
1123
1124 /* Level (pre-computed uppercase) */
1125 38 ▶ if (cache->opt_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->opt_level) { 38 0 38
1126 38 sv_catpv(result, cache->level_upper);
1127 }
1128
1129 /* Caller stack — decompose sv_catpvf into sv_catpvn calls */
1130 38 ▶ if (cache->opt_caller && caller_sv && SvCUR(caller_sv) > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (cache->opt_caller && caller_sv && SvCUR(caller_sv) > 0) { 38 0 38
if (cache->opt_caller && caller_sv && SvCUR(caller_sv) > 0) { 38 0 38
if (cache->opt_caller && caller_sv && SvCUR(caller_sv) > 0) { 4 34 38
1131 4 sv_catpvn(result, " caller=", 8);
1132 4 sv_catpv(result, cache->quote);
1133 4 sv_catsv(result, caller_sv);
1134 4 sv_catpv(result, cache->quote);
1135 }
1136
1137 /* Message */
1138 38 ▶ if (message_sv && SvOK(message_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (message_sv && SvOK(message_sv)) { 38 0 38
if (message_sv && SvOK(message_sv)) { 38 0 38
1139 38 sv_catpvn(result, " message=", 9);
1140 38 sv_catpv(result, cache->quote);
1141 38 sv_catsv(result, message_sv);
1142 38 sv_catpv(result, cache->quote);
1143 }
1144
1145 /* Elapsed */
1146 38 ▶ if (cache->opt_elapsed && elapsed_str) {
Branch: 2 branches
BranchTrueFalseTotal
if (cache->opt_elapsed && elapsed_str) { 38 0 38
if (cache->opt_elapsed && elapsed_str) { 19 19 38
1147 19 sv_catpvn(result, " elapsed_call=", 14);
1148 19 sv_catpv(result, cache->quote);
1149 19 sv_catpv(result, elapsed_str);
1150 19 sv_catpv(result, cache->quote);
1151 }
1152
1153 /* Params — use Loo dump style with scratch_dump buffer */
1154 38 ▶ if (params_av) {
Branch: 1 branch
BranchTrueFalseTotal
if (params_av) { 38 0 38
1155 38 I32 len = av_len(params_av);
1156 I32 i;
1157 char idx_buf[16];
1158 80 ▶ for (i = 0; i <= len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; i++) { 42 38 80
1159 42 SV **elem = av_fetch(params_av, i, 0);
1160 42 ▶ if (elem && *elem) {
Branch: 2 branches
BranchTrueFalseTotal
if (elem && *elem) { 42 0 42
if (elem && *elem) { 42 0 42
1161 int idx_len;
1162 /* Dump into scratch SV via Loo */
1163 42 medusa_dump_param_into(aTHX_ *elem, &cache->dump_style, cache->scratch_dump);
1164 /* Build " prefix0=†value†" without sv_catpvf */
1165 42 sv_catpvn(result, " ", 1);
1166 42 sv_catpv(result, prefix);
1167 42 idx_len = snprintf(idx_buf, sizeof(idx_buf), "%d=", (int)i);
1168 42 sv_catpvn(result, idx_buf, idx_len);
1169 42 sv_catpv(result, cache->quote);
1170 42 sv_catsv(result, cache->scratch_dump);
1171 42 sv_catpv(result, cache->quote);
1172 }
1173 }
1174 }
1175
1176 /* Dispatch to logger */
1177 38 ▶ if (cache->use_direct_write && cache->direct_logger) {
Branch: 2 branches
BranchTrueFalseTotal
if (cache->use_direct_write && cache->direct_logger) { 2 36 38
if (cache->use_direct_write && cache->direct_logger) { 2 0 2
1178 /* FASTEST: direct C write — no Perl call overhead at all */
1179 2 medusa_logger_write(aTHX_ cache->direct_logger,
1180 2 SvPVX(result), SvCUR(result));
1181 } else {
1182 /* Perl dispatch path — push a mortal copy so logger can safely
1183 * store the string without aliasing our scratch SV */
1184 36 ENTER;
1185 36 SAVETMPS;
1186 36 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 36 36
1187 36 ▶ XPUSHs(log_obj);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(log_obj); 0 36 36
1188 36 ▶ XPUSHs(sv_2mortal(newSVpvn(SvPVX(result), SvCUR(result))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(SvPVX(result), SvCUR(result)))); 0 36 36
1189 36 PUTBACK;
1190 36 ▶ if (cache->log_method_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache->log_method_cv) { 36 0 36
1191 36 call_sv((SV *)cache->log_method_cv, G_DISCARD);
1192 } else {
1193 0 call_method(cache->log_method, G_DISCARD);
1194 }
1195 36 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 36 0 36
1196 36 LEAVE;
1197 }
1198 }
1199  
1200 /*
1201 * XS implementation of log_message
1202 * Can call Perl FORMAT_MESSAGE callback or use XS default
1203 */
1204 static void
1205 4 medusa_xs_log_message(pTHX_ HV *params) {
1206 4 dSP;
1207 HV *log_config;
1208 SV **svp;
1209 SV *log_obj;
1210 SV *format_message_cb;
1211 SV *formatted_msg;
1212 4 const char *log_level = "debug";
1213 const char *log_method;
1214
1215 /* Get %Medusa::XS::LOG */
1216 4 log_config = get_hv("Medusa::XS::LOG", 0);
1217 4 ▶ if (!log_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (!log_config) { 0 4 4
1218 0 return;
1219 }
1220
1221 /* Initialize logger if needed */
1222 4 svp = hv_fetchs(log_config, "LOG", 0);
1223 4 ▶ if (!svp || !SvROK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!svp || !SvROK(*svp)) { 4 0 4
if (!svp || !SvROK(*svp)) { 0 4 4
1224 /* Call LOG_INIT */
1225 0 SV **init_cb = hv_fetchs(log_config, "LOG_INIT", 0);
1226 0 ▶ if (init_cb && SvROK(*init_cb)) {
Branch: 2 branches
BranchTrueFalseTotal
if (init_cb && SvROK(*init_cb)) { 0 0 0
if (init_cb && SvROK(*init_cb)) { 0 0 0
1227 0 ENTER;
1228 0 SAVETMPS;
1229 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
1230 0 PUTBACK;
1231 0 call_sv(*init_cb, G_SCALAR);
1232 0 SPAGAIN;
1233 0 log_obj = POPs;
1234 0 ▶ if (SvROK(log_obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(log_obj)) { 0 0 0
1235 0 hv_stores(log_config, "LOG", SvREFCNT_inc(log_obj));
1236 }
1237 0 PUTBACK;
1238 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
1239 0 LEAVE;
1240 }
1241 0 svp = hv_fetchs(log_config, "LOG", 0);
1242 }
1243
1244 4 ▶ if (!svp || !SvROK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!svp || !SvROK(*svp)) { 4 0 4
if (!svp || !SvROK(*svp)) { 0 4 4
1245 0 return; /* Still no logger */
1246 }
1247 4 log_obj = *svp;
1248
1249 /* Get log level */
1250 4 svp = hv_fetchs(params, "level", 0);
1251 4 ▶ if (svp && SvPOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) { 0 4 4
if (svp && SvPOK(*svp)) { 0 0 0
1252 0 log_level = SvPV_nolen(*svp);
1253 } else {
1254 4 svp = hv_fetchs(log_config, "LOG_LEVEL", 0);
1255 4 ▶ if (svp && SvPOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) { 4 0 4
if (svp && SvPOK(*svp)) { 4 0 4
1256 8 log_level = SvPV_nolen(*svp);
1257 }
1258 }
1259
1260 /* Store level in params for FORMAT_MESSAGE */
1261 4 hv_stores(params, "level", newSVpv(log_level, 0));
1262
1263 /* Get log method name from LOG_FUNCTIONS */
1264 4 log_method = log_level;
1265 4 svp = hv_fetchs(log_config, "LOG_FUNCTIONS", 0);
1266 4 ▶ if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 4 0 4
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 4 0 4
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 4 0 4
1267 4 HV *funcs = (HV *)SvRV(*svp);
1268 4 SV **meth = hv_fetch(funcs, log_level, strlen(log_level), 0);
1269 4 ▶ if (meth && SvPOK(*meth)) {
Branch: 2 branches
BranchTrueFalseTotal
if (meth && SvPOK(*meth)) { 4 0 4
if (meth && SvPOK(*meth)) { 4 0 4
1270 8 log_method = SvPV_nolen(*meth);
1271 }
1272 }
1273
1274 /* Get FORMAT_MESSAGE callback or use XS default */
1275 4 svp = hv_fetchs(log_config, "FORMAT_MESSAGE", 0);
1276 8 ▶ if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) {
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 4 0 4
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 4 0 4
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVCV) { 4 0 4
1277 /* Call Perl FORMAT_MESSAGE callback */
1278 I32 count;
1279 HE *he;
1280
1281 4 ENTER;
1282 4 SAVETMPS;
1283 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
1284
1285 /* Push params hash as list */
1286 4 hv_iterinit(params);
1287 21 ▶ while ((he = hv_iternext(params))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(params))) { 17 4 21
1288 17 ▶ XPUSHs(hv_iterkeysv(he));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(hv_iterkeysv(he)); 0 17 17
1289 17 ▶ XPUSHs(hv_iterval(params, he));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(hv_iterval(params, he)); 0 17 17
1290 }
1291 4 PUTBACK;
1292
1293 4 count = call_sv(*svp, G_SCALAR);
1294 4 SPAGAIN;
1295
1296 4 ▶ if (count >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count >= 1) { 4 0 4
1297 4 formatted_msg = SvREFCNT_inc(POPs);
1298 } else {
1299 0 formatted_msg = newSVpvn("", 0);
1300 }
1301
1302 4 PUTBACK;
1303 4 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 4 0 4
1304 4 LEAVE;
1305 } else {
1306 /* Use XS FORMAT_MESSAGE implementation */
1307 0 formatted_msg = medusa_xs_format_message(aTHX_ params, log_config);
1308 }
1309
1310 /* Call $LOG{LOG}->$log_method($formatted_msg) */
1311 4 ENTER;
1312 4 SAVETMPS;
1313 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
1314 4 ▶ XPUSHs(log_obj); /* Push the blessed reference, not SvRV */
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(log_obj); /* Push the blessed reference, not SvRV */ 0 4 4
1315 4 ▶ XPUSHs(sv_2mortal(formatted_msg));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(formatted_msg)); 0 4 4
1316 4 PUTBACK;
1317 4 call_method(log_method, G_DISCARD);
1318 4 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 4 0 4
1319 4 LEAVE;
1320 }
1321  
1322 /* XSUB wrapper that performs actual audit logging - full Medusa-compatible
1323 * Uses AUDIT_CACHE for fast config access (no hash lookups per call) */
1324 20 XS(XS_Medusa__XS_audit_wrapper) {
1325 20 dXSARGS;
1326 AUDIT_CACHE *cache;
1327 CV *orig_cv;
1328 I32 count;
1329 const char *method_name;
1330 char guid[MEDUSA_GUID_LEN + 1];
1331 struct timeval start_time, end_time;
1332 double elapsed;
1333 HV *log_config;
1334 I32 gimme;
1335 AV *args_copy;
1336 bool opt_guid, opt_caller, opt_elapsed;
1337 20 SV *guid_sv = NULL;
1338 20 SV *caller_sv = NULL;
1339 I32 i;
1340 bool use_fast_path;
1341
1342 /* Get cached metadata - this is the key optimization */
1343 20 cache = get_audit_cache(aTHX_ cv);
1344 20 ▶ use_fast_path = (cache && cache->use_xs_format);
Branch: 2 branches
BranchTrueFalseTotal
use_fast_path = (cache && cache->use_xs_format); 20 0 20
use_fast_path = (cache && cache->use_xs_format); 19 1 20
1345
1346 20 ▶ if (cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (cache) { 20 0 20
1347 /* Fast path: use cached values */
1348 20 orig_cv = cache->original_cv;
1349 20 method_name = cache->method;
1350 20 log_config = cache->log_config;
1351 20 opt_guid = cache->opt_guid;
1352 20 opt_caller = cache->opt_caller;
1353 20 opt_elapsed = cache->opt_elapsed;
1354 } else {
1355 /* Fallback: get from CV (shouldn't happen) */
1356 GV *gv;
1357 HV *stash;
1358 SV **svp;
1359
1360 0 orig_cv = (CV *)CvXSUBANY(cv).any_ptr;
1361 0 ▶ if (!orig_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (!orig_cv) { 0 0 0
1362 0 croak("Medusa::XS: wrapper CV has no original CV reference");
1363 }
1364
1365 0 method_name = "unknown";
1366 0 gv = CvGV(orig_cv);
1367 0 ▶ if (gv) {
Branch: 1 branch
BranchTrueFalseTotal
if (gv) { 0 0 0
1368 0 method_name = GvNAME(gv);
1369 }
1370
1371 0 log_config = get_hv("Medusa::XS::LOG", 0);
1372 0 opt_guid = opt_caller = opt_elapsed = TRUE;
1373
1374 0 ▶ if (log_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (log_config) { 0 0 0
1375 0 svp = hv_fetchs(log_config, "OPTIONS", 0);
1376 0 ▶ if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 0 0 0
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 0 0 0
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 0 0 0
1377 0 HV *options = (HV *)SvRV(*svp);
1378 SV **opt;
1379
1380 0 opt = hv_fetchs(options, "guid", 0);
1381 0 ▶ if (opt) opt_guid = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) opt_guid = SvTRUE(*opt); 0 0 0
1382
1383 0 opt = hv_fetchs(options, "caller", 0);
1384 0 ▶ if (opt) opt_caller = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) opt_caller = SvTRUE(*opt); 0 0 0
1385
1386 0 opt = hv_fetchs(options, "elapsed_call", 0);
1387 0 ▶ if (opt) opt_elapsed = SvTRUE(*opt);
Branch: 1 branch
BranchTrueFalseTotal
if (opt) opt_elapsed = SvTRUE(*opt); 0 0 0
1388 }
1389 }
1390 }
1391
1392 20 ▶ if (!log_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (!log_config) { 0 20 20
1393 /* No config, just forward without logging */
1394 0 ▶ PUSHMARK(SP - items);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP - items); 0 0 0
1395 0 count = call_sv((SV *)orig_cv, GIMME_V);
1396 0 SPAGAIN;
1397 0 XSRETURN(count);
1398 }
1399
1400 /* Generate GUID via Horus (version from cache or default v4) */
1401 20 ▶ if (opt_guid) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt_guid) { 20 0 20
1402 20 ▶ int gv = cache ? cache->guid_version : 4;
Branch: 1 branch
BranchTrueFalseTotal
int gv = cache ? cache->guid_version : 4; 20 0 20
1403 20 ▶ const char *gns = cache ? cache->guid_namespace : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *gns = cache ? cache->guid_namespace : NULL; 20 0 20
1404 20 ▶ STRLEN gns_len = cache ? cache->guid_namespace_len : 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN gns_len = cache ? cache->guid_namespace_len : 0; 20 0 20
1405 20 ▶ const char *gnm = cache ? cache->guid_name : NULL;
Branch: 1 branch
BranchTrueFalseTotal
const char *gnm = cache ? cache->guid_name : NULL; 20 0 20
1406 20 ▶ STRLEN gnm_len = cache ? cache->guid_name_len : 0;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN gnm_len = cache ? cache->guid_name_len : 0; 20 0 20
1407 20 medusa_generate_guid(aTHX_ guid, gv, gns, gns_len, gnm, gnm_len);
1408 20 ▶ if (use_fast_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (use_fast_path) { 19 1 20
1409 /* Reuse scratch SV — no allocation */
1410 19 guid_sv = cache->scratch_guid;
1411 19 sv_setpvn(guid_sv, guid, MEDUSA_GUID_LEN);
1412 } else {
1413 1 guid_sv = newSVpvn(guid, MEDUSA_GUID_LEN);
1414 }
1415 }
1416
1417 /* Collect caller stack */
1418 20 ▶ if (opt_caller) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt_caller) { 20 0 20
1419 20 ▶ if (use_fast_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (use_fast_path) { 19 1 20
1420 /* Reuse scratch SV — no allocation */
1421 19 caller_sv = cache->scratch_caller;
1422 19 medusa_collect_caller_stack_into(aTHX_ caller_sv);
1423 } else {
1424 1 caller_sv = medusa_collect_caller_stack(aTHX);
1425 }
1426 }
1427
1428 /* Copy arguments for logging (before they might be modified) */
1429 20 args_copy = newAV();
1430 40 ▶ for (i = 0; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i++) { 20 20 40
1431 20 av_push(args_copy, SvREFCNT_inc(ST(i)));
1432 }
1433
1434 /* Capture start time */
1435 20 gettimeofday(&start_time, NULL);
1436
1437 /* Log entry */
1438 20 ▶ if (use_fast_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (use_fast_path) { 19 1 20
1439 /* FAST PATH: no HV allocation, no hash lookups, direct format+log */
1440 19 medusa_xs_log_cached(aTHX_ cache, guid_sv, caller_sv,
1441 cache->entry_msg, args_copy, "arg", NULL);
1442 } else {
1443 /* Slow path: build params hash for medusa_xs_log_message */
1444 1 HV *params = newHV();
1445
1446 1 ▶ if (caller_sv && SvCUR(caller_sv) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (caller_sv && SvCUR(caller_sv) > 0) { 1 0 1
if (caller_sv && SvCUR(caller_sv) > 0) { 0 1 1
1447 0 hv_stores(params, "caller", SvREFCNT_inc(caller_sv));
1448 }
1449 1 ▶ if (guid_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (guid_sv) { 1 0 1
1450 1 hv_stores(params, "guid", SvREFCNT_inc(guid_sv));
1451 }
1452 1 ▶ if (cache && cache->entry_msg) {
Branch: 2 branches
BranchTrueFalseTotal
if (cache && cache->entry_msg) { 1 0 1
if (cache && cache->entry_msg) { 1 0 1
1453 1 hv_stores(params, "message", SvREFCNT_inc(cache->entry_msg));
1454 } else {
1455 char msg_buf[256];
1456 0 snprintf(msg_buf, sizeof(msg_buf), "subroutine %s called with args:", method_name);
1457 0 hv_stores(params, "message", newSVpv(msg_buf, 0));
1458 }
1459 1 hv_stores(params, "params", newRV_noinc((SV *)args_copy));
1460 1 args_copy = NULL; /* ownership transferred */
1461 1 hv_stores(params, "prefix", newSVpvs("arg"));
1462
1463 1 medusa_xs_log_message(aTHX_ params);
1464 1 SvREFCNT_dec((SV *)params);
1465 }
1466
1467 /* Call the original function - always in LIST context to capture all values */
1468 20 gimme = GIMME_V;
1469 20 ▶ PUSHMARK(SP - items);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP - items); 0 20 20
1470 20 count = call_sv((SV *)orig_cv, G_ARRAY | G_EVAL);
1471 20 SPAGAIN;
1472
1473 /* Capture end time */
1474 20 gettimeofday(&end_time, NULL);
1475 20 elapsed = (end_time.tv_sec - start_time.tv_sec) +
1476 20 (end_time.tv_usec - start_time.tv_usec) / 1000000.0;
1477
1478 /* Copy return values for logging */
1479 {
1480 20 AV *return_copy = newAV();
1481 char elapsed_buf[32];
1482
1483 45 ▶ for (i = 0; i < count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < count; i++) { 25 20 45
1484 25 av_push(return_copy, SvREFCNT_inc(SP[-count+1+i]));
1485 }
1486
1487 20 ▶ if (opt_elapsed) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt_elapsed) { 20 0 20
1488 20 snprintf(elapsed_buf, sizeof(elapsed_buf), "%.6f", elapsed);
1489 }
1490
1491 20 ▶ if (use_fast_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (use_fast_path) { 19 1 20
1492 /* FAST PATH: direct format+log */
1493 19 ▶ medusa_xs_log_cached(aTHX_ cache, guid_sv, caller_sv,
Branch: 1 branch
BranchTrueFalseTotal
medusa_xs_log_cached(aTHX_ cache, guid_sv, caller_sv, 19 0 19
1494 cache->exit_msg, return_copy, "return",
1495 opt_elapsed ? elapsed_buf : NULL);
1496 } else {
1497 /* Slow path */
1498 1 HV *params = newHV();
1499
1500 1 ▶ if (caller_sv && SvCUR(caller_sv) > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (caller_sv && SvCUR(caller_sv) > 0) { 1 0 1
if (caller_sv && SvCUR(caller_sv) > 0) { 0 1 1
1501 0 hv_stores(params, "caller", SvREFCNT_inc(caller_sv));
1502 }
1503 1 ▶ if (guid_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (guid_sv) { 1 0 1
1504 1 hv_stores(params, "guid", SvREFCNT_inc(guid_sv));
1505 }
1506 1 ▶ if (cache && cache->exit_msg) {
Branch: 2 branches
BranchTrueFalseTotal
if (cache && cache->exit_msg) { 1 0 1
if (cache && cache->exit_msg) { 1 0 1
1507 1 hv_stores(params, "message", SvREFCNT_inc(cache->exit_msg));
1508 } else {
1509 char msg_buf[256];
1510 0 snprintf(msg_buf, sizeof(msg_buf), "subroutine %s returned:", method_name);
1511 0 hv_stores(params, "message", newSVpv(msg_buf, 0));
1512 }
1513 1 hv_stores(params, "params", newRV_noinc((SV *)return_copy));
1514 1 return_copy = NULL; /* ownership transferred */
1515 1 hv_stores(params, "prefix", newSVpvs("return"));
1516
1517 1 ▶ if (opt_elapsed) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt_elapsed) { 1 0 1
1518 1 hv_stores(params, "elapsed_call", newSVpv(elapsed_buf, 0));
1519 }
1520
1521 1 medusa_xs_log_message(aTHX_ params);
1522 1 SvREFCNT_dec((SV *)params);
1523 }
1524
1525 /* Free return_copy if not transferred */
1526 20 ▶ if (return_copy) SvREFCNT_dec((SV *)return_copy);
Branch: 1 branch
BranchTrueFalseTotal
if (return_copy) SvREFCNT_dec((SV *)return_copy); 19 1 20
1527 }
1528
1529 /* Clean up — only free non-scratch SVs (slow path allocated them) */
1530 20 ▶ if (!use_fast_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (!use_fast_path) { 1 19 20
1531 1 ▶ if (guid_sv) SvREFCNT_dec(guid_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (guid_sv) SvREFCNT_dec(guid_sv); 1 0 1
1532 1 ▶ if (caller_sv) SvREFCNT_dec(caller_sv);
Branch: 1 branch
BranchTrueFalseTotal
if (caller_sv) SvREFCNT_dec(caller_sv); 1 0 1
1533 }
1534
1535 /* Check for exceptions */
1536 20 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 20 0 20
if (SvTRUE(ERRSV)) { 2 18 20
1537 2 ▶ croak_sv(ERRSV);
Branch: 1 branch
BranchTrueFalseTotal
croak_sv(ERRSV); 2 0 2
1538 }
1539
1540 /* Handle context: in scalar context, return only first element (like Perl shift) */
1541 18 ▶ if (gimme == G_SCALAR && count > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (gimme == G_SCALAR && count > 1) { 11 7 18
if (gimme == G_SCALAR && count > 1) { 1 10 11
1542 /* Return only the first value - shift other items off */
1543 1 SV *first = SP[-count+1]; /* First return value */
1544 1 SP -= count; /* Remove all items */
1545 1 SP++;
1546 1 ▶ if (first) {
Branch: 1 branch
BranchTrueFalseTotal
if (first) { 1 0 1
1547 1 *SP = first;
1548 }
1549 1 XSRETURN(1);
1550 }
1551
1552 17 XSRETURN(count);
1553 }
1554  
1555 /* ------------------------------------------------------------------ */
1556 /* Utility: Generate UUID via Horus (RFC 9562, all versions) */
1557 /* ------------------------------------------------------------------ */
1558  
1559 /* Persistent state for time-based UUID versions */
1560 static horus_v1_state_t medusa_v1_state = {{{0}}, 0, 0, 0};
1561 static horus_v6_state_t medusa_v6_state = {{{0}}, 0};
1562 static horus_v7_state_t medusa_v7_state = {0, {{0}}};
1563  
1564 /* Resolve a namespace string to a 16-byte UUID.
1565 * Accepts well-known names (dns, url, oid, x500) or a UUID string. */
1566 static void
1567 0 medusa_resolve_namespace(const char *ns_str, STRLEN ns_len,
1568 unsigned char *ns_uuid)
1569 {
1570 0 ▶ if (ns_len == 3 && strncmp(ns_str, "dns", 3) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (ns_len == 3 && strncmp(ns_str, "dns", 3) == 0) 0 0 0
if (ns_len == 3 && strncmp(ns_str, "dns", 3) == 0) 0 0 0
1571 0 memcpy(ns_uuid, HORUS_NS_DNS, 16);
1572 0 ▶ else if (ns_len == 3 && strncmp(ns_str, "url", 3) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (ns_len == 3 && strncmp(ns_str, "url", 3) == 0) 0 0 0
else if (ns_len == 3 && strncmp(ns_str, "url", 3) == 0) 0 0 0
1573 0 memcpy(ns_uuid, HORUS_NS_URL, 16);
1574 0 ▶ else if (ns_len == 3 && strncmp(ns_str, "oid", 3) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (ns_len == 3 && strncmp(ns_str, "oid", 3) == 0) 0 0 0
else if (ns_len == 3 && strncmp(ns_str, "oid", 3) == 0) 0 0 0
1575 0 memcpy(ns_uuid, HORUS_NS_OID, 16);
1576 0 ▶ else if (ns_len == 4 && strncmp(ns_str, "x500", 4) == 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (ns_len == 4 && strncmp(ns_str, "x500", 4) == 0) 0 0 0
else if (ns_len == 4 && strncmp(ns_str, "x500", 4) == 0) 0 0 0
1577 0 memcpy(ns_uuid, HORUS_NS_X500, 16);
1578 else {
1579 /* Try parsing as UUID string */
1580 0 ▶ if (!horus_parse_uuid(ns_uuid, ns_str, ns_len))
Branch: 1 branch
BranchTrueFalseTotal
if (!horus_parse_uuid(ns_uuid, ns_str, ns_len)) 0 0 0
1581 0 memcpy(ns_uuid, HORUS_NS_DNS, 16); /* fallback */
1582 }
1583 0 }
1584  
1585 static void
1586 122 medusa_generate_guid(pTHX_ char *buf, int version,
1587 const char *ns_str, STRLEN ns_len,
1588 const char *name_str, STRLEN name_len)
1589 {
1590 unsigned char uuid[16];
1591  
1592 122 switch (version) {
1593 0 case 1:
1594 0 ▶ if (!medusa_v1_state.initialized)
Branch: 1 branch
BranchTrueFalseTotal
if (!medusa_v1_state.initialized) 0 0 0
1595 0 horus_v1_init_state(&medusa_v1_state);
1596 0 horus_uuid_v1(uuid, &medusa_v1_state);
1597 0 break;
1598 0 case 3: {
1599 unsigned char ns_uuid[16];
1600 0 ▶ if (ns_str && ns_len > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (ns_str && ns_len > 0) 0 0 0
if (ns_str && ns_len > 0) 0 0 0
1601 0 medusa_resolve_namespace(ns_str, ns_len, ns_uuid);
1602 else
1603 0 memcpy(ns_uuid, HORUS_NS_DNS, 16);
1604 0 horus_uuid_v3(uuid, ns_uuid,
1605 (const unsigned char *)name_str,
1606 name_len);
1607 0 break;
1608 }
1609 122 case 4:
1610 122 horus_uuid_v4(uuid);
1611 122 break;
1612 0 case 5: {
1613 unsigned char ns_uuid[16];
1614 0 ▶ if (ns_str && ns_len > 0)
Branch: 2 branches
BranchTrueFalseTotal
if (ns_str && ns_len > 0) 0 0 0
if (ns_str && ns_len > 0) 0 0 0
1615 0 medusa_resolve_namespace(ns_str, ns_len, ns_uuid);
1616 else
1617 0 memcpy(ns_uuid, HORUS_NS_DNS, 16);
1618 0 horus_uuid_v5(uuid, ns_uuid,
1619 (const unsigned char *)name_str,
1620 name_len);
1621 0 break;
1622 }
1623 0 case 6:
1624 0 ▶ if (!medusa_v1_state.initialized)
Branch: 1 branch
BranchTrueFalseTotal
if (!medusa_v1_state.initialized) 0 0 0
1625 0 horus_v1_init_state(&medusa_v1_state);
1626 0 horus_uuid_v6(uuid, &medusa_v1_state, &medusa_v6_state);
1627 0 break;
1628 0 case 7:
1629 0 horus_uuid_v7(uuid, &medusa_v7_state);
1630 0 break;
1631 0 case 8: {
1632 unsigned char custom[16];
1633 0 horus_random_bytes(custom, 16);
1634 0 horus_uuid_v8(uuid, custom);
1635 0 break;
1636 }
1637 0 case 0: /* NIL */
1638 0 horus_uuid_nil(uuid);
1639 0 break;
1640 0 case -1: /* MAX */
1641 0 horus_uuid_max(uuid);
1642 0 break;
1643 0 default:
1644 0 horus_uuid_v4(uuid);
1645 0 break;
1646 }
1647  
1648 122 horus_format_uuid(buf, uuid, HORUS_FMT_STR);
1649 122 buf[HORUS_FMT_STR_LEN] = '\0';
1650 122 }
1651  
1652 /* ------------------------------------------------------------------ */
1653 /* Utility: Format timestamp with optional milliseconds */
1654 /* ------------------------------------------------------------------ */
1655  
1656 static SV *
1657 14 medusa_format_time(pTHX_ bool use_gmtime, const char *fmt) {
1658 struct timeval tv;
1659 struct tm *tm_info;
1660 char buf[MEDUSA_TIME_BUF_SIZE];
1661 char ms_buf[16];
1662 STRLEN len;
1663
1664 14 gettimeofday(&tv, NULL);
1665
1666 14 tm_info = medusa_tm(&tv, use_gmtime);
1667
1668 14 ▶ if (fmt == NULL || strcmp(fmt, "default") == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (fmt == NULL || strcmp(fmt, "default") == 0) { 4 10 14
if (fmt == NULL || strcmp(fmt, "default") == 0) { 0 4 4
1669 /* Default format: asctime style */
1670 10 medusa_default_time(buf, sizeof(buf), tm_info);
1671 } else {
1672 /* Custom format with %ms placeholder for milliseconds */
1673 char *ms_pos;
1674 char fmt_copy[128];
1675
1676 4 strncpy(fmt_copy, fmt, sizeof(fmt_copy) - 1);
1677 4 fmt_copy[sizeof(fmt_copy) - 1] = '\0';
1678
1679 4 ms_pos = strstr(fmt_copy, "%ms");
1680 4 ▶ if (ms_pos) {
Branch: 1 branch
BranchTrueFalseTotal
if (ms_pos) { 2 2 4
1681 /* Replace %ms with actual milliseconds */
1682 2 snprintf(ms_buf, sizeof(ms_buf), "%03ld", (long)(tv.tv_usec / 1000));
1683 2 *ms_pos = '\0';
1684 2 strftime(buf, sizeof(buf), fmt_copy, tm_info);
1685 2 len = strlen(buf);
1686 2 snprintf(buf + len, sizeof(buf) - len, "%s%s", ms_buf, ms_pos + 3);
1687 } else {
1688 2 strftime(buf, sizeof(buf), fmt_copy, tm_info);
1689 }
1690 }
1691
1692 14 return newSVpv(buf, 0);
1693 }
1694  
1695 /* ------------------------------------------------------------------ */
1696 /* Utility: Collect caller stack (replaces Perl caller() loop) */
1697 /* ------------------------------------------------------------------ */
1698  
1699 static SV *
1700 3 medusa_collect_caller_stack(pTHX) {
1701 SV *result;
1702 const PERL_CONTEXT *cx;
1703 I32 cxix;
1704 char buf[MEDUSA_CALLER_STACK_INITIAL_SIZE];
1705 3 STRLEN pos = 0;
1706 3 bool first = TRUE;
1707
1708 3 result = newSVpvn("", 0);
1709 3 ▶ SvGROW(result, MEDUSA_CALLER_STACK_INITIAL_SIZE);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(result, MEDUSA_CALLER_STACK_INITIAL_SIZE); 3 0 3
SvGROW(result, MEDUSA_CALLER_STACK_INITIAL_SIZE); 3 0 3
1710
1711 /* Walk the context stack */
1712 9 ▶ for (cxix = cxstack_ix; cxix >= 0; cxix--) {
Branch: 1 branch
BranchTrueFalseTotal
for (cxix = cxstack_ix; cxix >= 0; cxix--) { 6 3 9
1713 6 cx = &cxstack[cxix];
1714
1715 /* Only process subroutine contexts */
1716 6 ▶ if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_EVAL) {
Branch: 2 branches
BranchTrueFalseTotal
if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_EVAL) { 5 1 6
if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_EVAL) { 0 5 5
1717 const char *pkg;
1718 I32 line;
1719
1720 /* Get package name and line number */
1721 1 ▶ if (cx->blk_oldcop) {
Branch: 1 branch
BranchTrueFalseTotal
if (cx->blk_oldcop) { 1 0 1
1722 1 ▶ pkg = CopSTASHPV(cx->blk_oldcop);
Branch: 7 branches
BranchTrueFalseTotal
pkg = CopSTASHPV(cx->blk_oldcop); 1 0 1
pkg = CopSTASHPV(cx->blk_oldcop); 1 0 1
pkg = CopSTASHPV(cx->blk_oldcop); 1 0 1
pkg = CopSTASHPV(cx->blk_oldcop); 0 1 1
pkg = CopSTASHPV(cx->blk_oldcop); 0 0 0
pkg = CopSTASHPV(cx->blk_oldcop); 1 0 1
pkg = CopSTASHPV(cx->blk_oldcop); 0 1 1
1723 1 line = CopLINE(cx->blk_oldcop);
1724 } else {
1725 0 continue;
1726 }
1727
1728 1 ▶ if (!first) {
Branch: 1 branch
BranchTrueFalseTotal
if (!first) { 0 1 1
1729 0 sv_catpvn(result, "->", 2);
1730 }
1731 1 first = FALSE;
1732
1733 1 ▶ sv_catpvf(result, "%s:%d", pkg ? pkg : "(unknown)", (int)line);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(result, "%s:%d", pkg ? pkg : "(unknown)", (int)line); 1 0 1
1734 }
1735 }
1736
1737 3 return result;
1738 }
1739  
1740 /* Collect caller stack into a pre-allocated SV (avoids per-call newSV) */
1741 static void
1742 19 medusa_collect_caller_stack_into(pTHX_ SV *result) {
1743 const PERL_CONTEXT *cx;
1744 I32 cxix;
1745 19 bool first = TRUE;
1746
1747 19 SvCUR_set(result, 0);
1748 19 *SvPVX(result) = '\0';
1749
1750 56 ▶ for (cxix = cxstack_ix; cxix >= 0; cxix--) {
Branch: 1 branch
BranchTrueFalseTotal
for (cxix = cxstack_ix; cxix >= 0; cxix--) { 37 19 56
1751 37 cx = &cxstack[cxix];
1752 37 ▶ if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_EVAL) {
Branch: 2 branches
BranchTrueFalseTotal
if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_EVAL) { 37 0 37
if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_EVAL) { 2 35 37
1753 const char *pkg;
1754 I32 line;
1755 2 ▶ if (cx->blk_oldcop) {
Branch: 1 branch
BranchTrueFalseTotal
if (cx->blk_oldcop) { 2 0 2
1756 2 ▶ pkg = CopSTASHPV(cx->blk_oldcop);
Branch: 7 branches
BranchTrueFalseTotal
pkg = CopSTASHPV(cx->blk_oldcop); 2 0 2
pkg = CopSTASHPV(cx->blk_oldcop); 2 0 2
pkg = CopSTASHPV(cx->blk_oldcop); 2 0 2
pkg = CopSTASHPV(cx->blk_oldcop); 0 2 2
pkg = CopSTASHPV(cx->blk_oldcop); 0 0 0
pkg = CopSTASHPV(cx->blk_oldcop); 2 0 2
pkg = CopSTASHPV(cx->blk_oldcop); 0 2 2
1757 2 line = CopLINE(cx->blk_oldcop);
1758 } else {
1759 0 continue;
1760 }
1761 2 ▶ if (!first) sv_catpvn(result, "->", 2);
Branch: 1 branch
BranchTrueFalseTotal
if (!first) sv_catpvn(result, "->", 2); 0 2 2
1762 2 first = FALSE;
1763 2 ▶ sv_catpvf(result, "%s:%d", pkg ? pkg : "(unknown)", (int)line);
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(result, "%s:%d", pkg ? pkg : "(unknown)", (int)line); 2 0 2
1764 }
1765 }
1766 19 }
1767  
1768 /* ------------------------------------------------------------------ */
1769 /* Utility: Get option from %LOG hash */
1770 /* ------------------------------------------------------------------ */
1771  
1772 static bool
1773 0 get_log_option(pTHX_ HV *log_config, const char *key) {
1774 SV **svp;
1775 HV *options;
1776
1777 0 ▶ if (!log_config) return TRUE; /* Default to enabled */
Branch: 1 branch
BranchTrueFalseTotal
if (!log_config) return TRUE; /* Default to enabled */ 0 0 0
1778
1779 0 svp = hv_fetchs(log_config, "OPTIONS", 0);
1780 0 ▶ if (!svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (!svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVHV) { 0 0 0
if (!svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVHV) { 0 0 0
if (!svp || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVHV) { 0 0 0
1781 0 return TRUE;
1782 }
1783
1784 0 options = (HV *)SvRV(*svp);
1785 0 svp = hv_fetch(options, key, strlen(key), 0);
1786 0 ▶ if (!svp) return TRUE;
Branch: 1 branch
BranchTrueFalseTotal
if (!svp) return TRUE; 0 0 0
1787
1788 0 return SvTRUE(*svp);
1789 }
1790  
1791 static SV *
1792 0 get_log_value(pTHX_ HV *log_config, const char *key) {
1793 SV **svp;
1794
1795 0 ▶ if (!log_config) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!log_config) return NULL; 0 0 0
1796
1797 0 svp = hv_fetch(log_config, key, strlen(key), 0);
1798 0 ▶ return svp ? *svp : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return svp ? *svp : NULL; 0 0 0
1799 }
1800  
1801 /* ------------------------------------------------------------------ */
1802 /* Utility: Format log message (Phase 3 implementation) */
1803 /* ------------------------------------------------------------------ */
1804  
1805 /*
1806 * Build a formatted log message string:
1807 * [timestamp] [guid] LEVEL message key=†value† ...
1808 */
1809 static SV *
1810 0 medusa_build_log_message(pTHX_ HV *log_config, const char *level,
1811 SV *guid, SV *caller_stack, SV *method_name,
1812 const char *msg_type, AV *params,
1813 const char *prefix, double elapsed) {
1814 SV *result;
1815 0 SV *timestamp = NULL;
1816 SV *quote_sv;
1817 0 const char *quote = "†";
1818 bool opt_date, opt_guid, opt_level, opt_caller, opt_elapsed;
1819 char buf[MEDUSA_LOG_BUF_SIZE];
1820 0 STRLEN pos = 0;
1821
1822 /* Get options */
1823 0 opt_date = get_log_option(aTHX_ log_config, "date");
1824 0 opt_guid = get_log_option(aTHX_ log_config, "guid");
1825 0 opt_level = get_log_option(aTHX_ log_config, "level");
1826 0 opt_caller = get_log_option(aTHX_ log_config, "caller");
1827 0 opt_elapsed = get_log_option(aTHX_ log_config, "elapsed_call");
1828
1829 /* Get quote character */
1830 0 quote_sv = get_log_value(aTHX_ log_config, "QUOTE");
1831 0 ▶ if (quote_sv && SvPOK(quote_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (quote_sv && SvPOK(quote_sv)) { 0 0 0
if (quote_sv && SvPOK(quote_sv)) { 0 0 0
1832 0 quote = SvPV_nolen(quote_sv);
1833 }
1834
1835 0 result = newSVpvn("", 0);
1836 0 ▶ SvGROW(result, MEDUSA_LOG_BUF_SIZE);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(result, MEDUSA_LOG_BUF_SIZE); 0 0 0
SvGROW(result, MEDUSA_LOG_BUF_SIZE); 0 0 0
1837
1838 /* Add timestamp */
1839 0 ▶ if (opt_date) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt_date) { 0 0 0
1840 0 SV *time_type = get_log_value(aTHX_ log_config, "TIME");
1841 0 bool use_gmtime = TRUE;
1842 0 ▶ if (time_type && SvPOK(time_type)) {
Branch: 2 branches
BranchTrueFalseTotal
if (time_type && SvPOK(time_type)) { 0 0 0
if (time_type && SvPOK(time_type)) { 0 0 0
1843 0 const char *tt = SvPV_nolen(time_type);
1844 0 use_gmtime = (strcmp(tt, "gmtime") == 0);
1845 }
1846
1847 0 SV *time_fmt = get_log_value(aTHX_ log_config, "TIME_FORMAT");
1848 0 const char *fmt = NULL;
1849 0 ▶ if (time_fmt && SvPOK(time_fmt)) {
Branch: 2 branches
BranchTrueFalseTotal
if (time_fmt && SvPOK(time_fmt)) { 0 0 0
if (time_fmt && SvPOK(time_fmt)) { 0 0 0
1850 0 fmt = SvPV_nolen(time_fmt);
1851 0 ▶ if (strcmp(fmt, "default") == 0) fmt = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(fmt, "default") == 0) fmt = NULL; 0 0 0
1852 }
1853
1854 0 timestamp = medusa_format_time(aTHX_ use_gmtime, fmt);
1855 0 sv_catpvf(result, "%s ", SvPV_nolen(timestamp));
1856 0 SvREFCNT_dec(timestamp);
1857 }
1858
1859 /* Add GUID */
1860 0 ▶ if (opt_guid && guid && SvOK(guid)) {
Branch: 3 branches
BranchTrueFalseTotal
if (opt_guid && guid && SvOK(guid)) { 0 0 0
if (opt_guid && guid && SvOK(guid)) { 0 0 0
if (opt_guid && guid && SvOK(guid)) { 0 0 0
1861 0 sv_catpvf(result, "%s ", SvPV_nolen(guid));
1862 }
1863
1864 /* Add log level */
1865 0 ▶ if (opt_level && level) {
Branch: 2 branches
BranchTrueFalseTotal
if (opt_level && level) { 0 0 0
if (opt_level && level) { 0 0 0
1866 /* Uppercase the level */
1867 char level_upper[32];
1868 int i;
1869 0 ▶ for (i = 0; level[i] && i < 31; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; level[i] && i < 31; i++) { 0 0 0
for (i = 0; level[i] && i < 31; i++) { 0 0 0
1870 0 level_upper[i] = toupper(level[i]);
1871 }
1872 0 level_upper[i] = '\0';
1873 0 sv_catpv(result, level_upper);
1874 }
1875
1876 /* Add caller stack */
1877 0 ▶ if (opt_caller && caller_stack && SvOK(caller_stack) && SvCUR(caller_stack) > 0) {
Branch: 4 branches
BranchTrueFalseTotal
if (opt_caller && caller_stack && SvOK(caller_stack) && SvCUR(caller_stack) > 0) { 0 0 0
if (opt_caller && caller_stack && SvOK(caller_stack) && SvCUR(caller_stack) > 0) { 0 0 0
if (opt_caller && caller_stack && SvOK(caller_stack) && SvCUR(caller_stack) > 0) { 0 0 0
if (opt_caller && caller_stack && SvOK(caller_stack) && SvCUR(caller_stack) > 0) { 0 0 0
1878 0 sv_catpvf(result, " caller=%s%s%s", quote, SvPV_nolen(caller_stack), quote);
1879 }
1880
1881 /* Add message */
1882 0 ▶ if (method_name && SvOK(method_name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (method_name && SvOK(method_name)) { 0 0 0
if (method_name && SvOK(method_name)) { 0 0 0
1883 0 sv_catpvf(result, " message=%ssubroutine %s %s:%s",
1884 quote, SvPV_nolen(method_name), msg_type, quote);
1885 }
1886
1887 /* Add elapsed time (only for leave) */
1888 0 ▶ if (opt_elapsed && elapsed > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (opt_elapsed && elapsed > 0) { 0 0 0
if (opt_elapsed && elapsed > 0) { 0 0 0
1889 0 sv_catpvf(result, " elapsed_call=%s%.6f%s", quote, elapsed, quote);
1890 }
1891
1892 /* Add parameters */
1893 0 ▶ if (params && prefix) {
Branch: 2 branches
BranchTrueFalseTotal
if (params && prefix) { 0 0 0
if (params && prefix) { 0 0 0
1894 0 I32 len = av_len(params);
1895 I32 i;
1896
1897 {
1898 DDCStyle style;
1899 0 medusa_loo_style_init(aTHX_ &style, 0, NULL);
1900 0 ▶ for (i = 0; i <= len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= len; i++) { 0 0 0
1901 0 SV **elem = av_fetch(params, i, 0);
1902 0 ▶ if (elem && *elem) {
Branch: 2 branches
BranchTrueFalseTotal
if (elem && *elem) { 0 0 0
if (elem && *elem) { 0 0 0
1903 0 SV *dumped = medusa_dump_param(aTHX_ *elem, &style);
1904 0 sv_catpvf(result, " %s%d=%s%s%s",
1905 prefix, (int)i, quote, SvPV_nolen(dumped), quote);
1906 0 SvREFCNT_dec(dumped);
1907 }
1908 }
1909 0 ddc_style_destroy(aTHX_ &style);
1910 }
1911 }
1912
1913 0 return result;
1914 }
1915  
1916 /* ------------------------------------------------------------------ */
1917 /* Custom op pp functions */
1918 /* ------------------------------------------------------------------ */
1919  
1920 static OP *
1921 0 pp_medusa_caller_stack(pTHX) {
1922 0 dSP;
1923 0 SV *stack = medusa_collect_caller_stack(aTHX);
1924 0 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 0 0
1925 0 PUSHs(sv_2mortal(stack));
1926 0 PUTBACK;
1927 0 return NORMAL;
1928 }
1929  
1930 /*
1931 * pp_medusa_format_log - Format a log message
1932 *
1933 * Stack: method_name, msg_type, guid, caller_stack, params_av
1934 * Returns: formatted log message SV
1935 */
1936 static OP *
1937 0 pp_medusa_format_log(pTHX) {
1938 0 dSP;
1939 SV *params_sv, *caller_sv, *guid_sv, *msg_type_sv, *method_sv;
1940 0 AV *params_av = NULL;
1941 SV *result;
1942 0 HV *log_config = NULL;
1943 GV *gv;
1944
1945 /* Pop args from stack */
1946 0 params_sv = POPs;
1947 0 caller_sv = POPs;
1948 0 guid_sv = POPs;
1949 0 msg_type_sv = POPs;
1950 0 method_sv = POPs;
1951
1952 /* Get params array */
1953 0 ▶ if (SvROK(params_sv) && SvTYPE(SvRV(params_sv)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(params_sv) && SvTYPE(SvRV(params_sv)) == SVt_PVAV) { 0 0 0
if (SvROK(params_sv) && SvTYPE(SvRV(params_sv)) == SVt_PVAV) { 0 0 0
1954 0 params_av = (AV *)SvRV(params_sv);
1955 }
1956
1957 /* Get log config */
1958 0 gv = gv_fetchpvn_flags("Medusa::XS::LOG", 15, 0, SVt_PVHV);
1959 0 ▶ if (gv) log_config = GvHV(gv);
Branch: 1 branch
BranchTrueFalseTotal
if (gv) log_config = GvHV(gv); 0 0 0
1960
1961 /* Build message */
1962 0 result = medusa_build_log_message(aTHX_ log_config, "debug",
1963 guid_sv, caller_sv, method_sv,
1964 0 SvPV_nolen(msg_type_sv),
1965 params_av, "arg", 0);
1966
1967 0 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 0 0
1968 0 PUSHs(sv_2mortal(result));
1969 0 PUTBACK;
1970 0 return NORMAL;
1971 }
1972  
1973 /*
1974 * pp_medusa_log_write - Custom op for direct file logging
1975 *
1976 * Stack: logger_sv (blessed Medusa::XS::Logger), message_sv
1977 * Performs flock + write + flush in pure C, zero Perl dispatch.
1978 */
1979 static OP *
1980 0 pp_medusa_log_write(pTHX) {
1981 0 dSP;
1982 SV *message_sv;
1983 SV *logger_sv;
1984 MedusaLogger *logger;
1985 const char *line;
1986 STRLEN len;
1987
1988 0 message_sv = POPs;
1989 0 logger_sv = POPs;
1990
1991 0 logger = medusa_logger_from_sv(aTHX_ logger_sv);
1992 0 ▶ if (logger) {
Branch: 1 branch
BranchTrueFalseTotal
if (logger) { 0 0 0
1993 0 line = SvPV(message_sv, len);
1994 0 medusa_logger_write(aTHX_ logger, line, len);
1995 }
1996
1997 0 PUTBACK;
1998 0 return NORMAL;
1999 }
2000  
2001 /* ------------------------------------------------------------------ */
2002 /* XS Module */
2003 /* ------------------------------------------------------------------ */
2004  
2005 MODULE = Medusa::XS PACKAGE = Medusa::XS
2006  
2007 BOOT:
2008 #if PERL_VERSION >= 14
2009 /* Register custom ops */
2010 15 XopENTRY_set(&medusa_xop_caller_stack, xop_name, "medusa_caller_stack");
2011 15 XopENTRY_set(&medusa_xop_caller_stack, xop_desc, "Medusa caller stack collection");
2012 15 XopENTRY_set(&medusa_xop_caller_stack, xop_class, OA_BASEOP);
2013 15 Perl_custom_op_register(aTHX_ pp_medusa_caller_stack, &medusa_xop_caller_stack);
2014
2015 15 XopENTRY_set(&medusa_xop_format_log, xop_name, "medusa_format_log");
2016 15 XopENTRY_set(&medusa_xop_format_log, xop_desc, "Medusa log message formatting");
2017 15 XopENTRY_set(&medusa_xop_format_log, xop_class, OA_BASEOP);
2018 15 Perl_custom_op_register(aTHX_ pp_medusa_format_log, &medusa_xop_format_log);
2019
2020 15 XopENTRY_set(&medusa_xop_log_write, xop_name, "medusa_log_write");
2021 15 XopENTRY_set(&medusa_xop_log_write, xop_desc, "Medusa direct file log write");
2022 15 XopENTRY_set(&medusa_xop_log_write, xop_class, OA_BASEOP);
2023 15 Perl_custom_op_register(aTHX_ pp_medusa_log_write, &medusa_xop_log_write);
2024 #endif
2025  
2026 # ------------------------------------------------------------------ #
2027 # XSUB: Generate UUID (all RFC 9562 versions via Horus) #
2028 # ------------------------------------------------------------------ #
2029  
2030 SV *
2031 generate_guid(...)
2032 PREINIT:
2033 char buf[HORUS_FMT_STR_LEN + 1];
2034 102 int version = 4;
2035 102 const char *ns_str = NULL;
2036 102 STRLEN ns_len = 0;
2037 102 const char *name_str = NULL;
2038 102 STRLEN name_len = 0;
2039 CODE:
2040 102 ▶ if (items >= 1) version = SvIV(ST(0));
Branch: 1 branch
BranchTrueFalseTotal
if (items >= 1) version = SvIV(ST(0)); 0 102 102
2041 102 ▶ if (items >= 2 && SvOK(ST(1))) ns_str = SvPV(ST(1), ns_len);
Branch: 2 branches
BranchTrueFalseTotal
if (items >= 2 && SvOK(ST(1))) ns_str = SvPV(ST(1), ns_len); 0 102 102
if (items >= 2 && SvOK(ST(1))) ns_str = SvPV(ST(1), ns_len); 0 0 0
2042 102 ▶ if (items >= 3 && SvOK(ST(2))) name_str = SvPV(ST(2), name_len);
Branch: 2 branches
BranchTrueFalseTotal
if (items >= 3 && SvOK(ST(2))) name_str = SvPV(ST(2), name_len); 0 102 102
if (items >= 3 && SvOK(ST(2))) name_str = SvPV(ST(2), name_len); 0 0 0
2043 102 medusa_generate_guid(aTHX_ buf, version,
2044 ns_str, ns_len, name_str, name_len);
2045 102 RETVAL = newSVpvn(buf, HORUS_FMT_STR_LEN);
2046 OUTPUT:
2047 RETVAL
2048  
2049 # ------------------------------------------------------------------ #
2050 # XSUB: Format timestamp #
2051 # ------------------------------------------------------------------ #
2052  
2053 SV *
2054 format_time(use_gmtime = TRUE, fmt = NULL)
2055 bool use_gmtime
2056 const char *fmt
2057 CODE:
2058 7 RETVAL = medusa_format_time(aTHX_ use_gmtime, fmt);
2059 OUTPUT:
2060 RETVAL
2061  
2062 # ------------------------------------------------------------------ #
2063 # XSUB: Collect caller stack #
2064 # ------------------------------------------------------------------ #
2065  
2066 SV *
2067 collect_caller_stack()
2068 CODE:
2069 2 RETVAL = medusa_collect_caller_stack(aTHX);
2070 OUTPUT:
2071 RETVAL
2072  
2073 # ------------------------------------------------------------------ #
2074 # XSUB: Clean dumper output (legacy compat - passes through to Loo) #
2075 # ------------------------------------------------------------------ #
2076  
2077 SV *
2078 clean_dumper(input)
2079 SV *input
2080 CODE:
2081 /* Legacy: with Loo, dump output is already clean.
2082 * This just returns the input for backward compatibility. */
2083 10 RETVAL = newSVsv(input);
2084 OUTPUT:
2085 RETVAL
2086  
2087 # ------------------------------------------------------------------ #
2088 # XSUB: SV serializer via Loo #
2089 # ------------------------------------------------------------------ #
2090  
2091 SV *
2092 dump_sv(value)
2093 SV *value
2094 PREINIT:
2095 DDCStyle style;
2096 HV *log_config;
2097 14 HV *options = NULL;
2098 14 int use_colour = 0;
2099 14 const char *theme = NULL;
2100 CODE:
2101 /* Read colour settings from %LOG{OPTIONS} */
2102 14 log_config = get_hv("Medusa::XS::LOG", 0);
2103 14 ▶ if (log_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (log_config) { 14 0 14
2104 14 SV **svp = hv_fetchs(log_config, "OPTIONS", 0);
2105 14 ▶ if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 14 0 14
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 14 0 14
if (svp && SvROK(*svp) && SvTYPE(SvRV(*svp)) == SVt_PVHV) { 14 0 14
2106 14 options = (HV *)SvRV(*svp);
2107 14 use_colour = medusa_resolve_colour(aTHX_ options);
2108 14 theme = medusa_resolve_theme(aTHX_ options);
2109 }
2110 }
2111 14 medusa_loo_style_init(aTHX_ &style, use_colour, theme);
2112 14 RETVAL = medusa_dump_param(aTHX_ value, &style);
2113 14 ddc_style_destroy(aTHX_ &style);
2114 OUTPUT:
2115 RETVAL
2116  
2117 # ------------------------------------------------------------------ #
2118 # XSUB: Wrap a subroutine with auditing #
2119 # ------------------------------------------------------------------ #
2120  
2121 void
2122 wrap_sub(coderef, method_name = NULL)
2123 SV *coderef
2124 SV *method_name
2125 PREINIT:
2126 CV *cv;
2127 CV *wrapper;
2128 const char *method;
2129 STRLEN method_len;
2130 const char *pkg;
2131 STRLEN pkg_len;
2132 GV *gv;
2133 PPCODE:
2134 /* Get the CV from the coderef */
2135 13 ▶ if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) { 12 1 13
if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) { 1 11 12
2136 2 croak("wrap_sub: argument must be a code reference");
2137 }
2138 11 cv = (CV *)SvRV(coderef);
2139
2140 /* Get method name from CV or argument */
2141 11 ▶ if (method_name && SvOK(method_name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (method_name && SvOK(method_name)) { 7 4 11
if (method_name && SvOK(method_name)) { 7 0 7
2142 7 method = SvPV(method_name, method_len);
2143 } else {
2144 4 gv = CvGV(cv);
2145 4 ▶ if (gv) {
Branch: 1 branch
BranchTrueFalseTotal
if (gv) { 4 0 4
2146 4 method = GvNAME(gv);
2147 4 method_len = GvNAMELEN(gv);
2148 } else {
2149 0 method = "__ANON__";
2150 0 method_len = 8;
2151 }
2152 }
2153
2154 /* Get package name from CV */
2155 11 gv = CvGV(cv);
2156 11 ▶ if (gv && GvSTASH(gv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (gv && GvSTASH(gv)) { 11 0 11
if (gv && GvSTASH(gv)) { 11 0 11
2157 11 ▶ pkg = HvNAME(GvSTASH(gv));
Branch: 6 branches
BranchTrueFalseTotal
pkg = HvNAME(GvSTASH(gv)); 11 0 11
pkg = HvNAME(GvSTASH(gv)); 11 0 11
pkg = HvNAME(GvSTASH(gv)); 0 11 11
pkg = HvNAME(GvSTASH(gv)); 0 0 0
pkg = HvNAME(GvSTASH(gv)); 11 0 11
pkg = HvNAME(GvSTASH(gv)); 0 11 11
2158 11 ▶ pkg_len = pkg ? strlen(pkg) : 0;
Branch: 1 branch
BranchTrueFalseTotal
pkg_len = pkg ? strlen(pkg) : 0; 11 0 11
2159 } else {
2160 0 pkg = "main";
2161 0 pkg_len = 4;
2162 }
2163
2164 /* Inject the audit wrapper */
2165 11 inject_audit_wrapper(aTHX_ cv, method, method_len, pkg, pkg_len);
2166
2167 /* Return the wrapped CV */
2168 11 ▶ XPUSHs(coderef);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(coderef); 0 11 11
2169 11 XSRETURN(1);
2170  
2171 bool
2172 is_audited(coderef)
2173 SV *coderef
2174 PREINIT:
2175 CV *cv;
2176 CODE:
2177 16 ▶ if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) { 14 2 16
if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) { 1 13 14
2178 3 RETVAL = FALSE;
2179 } else {
2180 13 cv = (CV *)SvRV(coderef);
2181 13 ▶ RETVAL = CV_IS_AUDITED(cv) ? TRUE : FALSE;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = CV_IS_AUDITED(cv) ? TRUE : FALSE; 10 3 13
RETVAL = CV_IS_AUDITED(cv) ? TRUE : FALSE; 10 0 10
2182 }
2183 OUTPUT:
2184 RETVAL
2185  
2186 SV *
2187 _make_test_audit_op(method, package)
2188 const char *method
2189 const char *package
2190 CODE:
2191 /* Legacy test hook — AUDITOP struct removed, return confirmation */
2192 2 RETVAL = newSVpvf("AUDIT_CACHE@%s::%s", package, method);
2193 OUTPUT:
2194 RETVAL
2195  
2196 # ------------------------------------------------------------------ #
2197 # XSUB: _apply_deferred_wraps - Re-install wrappers queued on 5.10 #
2198 # ------------------------------------------------------------------ #
2199  
2200 void
2201 _apply_deferred_wraps()
2202 PPCODE:
2203 12 medusa_check_apply_wraps(aTHX_ NULL);
2204 12 XSRETURN(0);
2205  
2206 # ------------------------------------------------------------------ #
2207 # XSUB: log_message - Full XS logging implementation #
2208 # ------------------------------------------------------------------ #
2209  
2210 void
2211 log_message(...)
2212 PREINIT:
2213 HV *params;
2214 int i;
2215 PPCODE:
2216 /* Build params hash from @_ */
2217 2 ▶ if (items % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (items % 2 != 0) { 0 2 2
2218 0 croak("log_message: odd number of arguments");
2219 }
2220 2 params = newHV();
2221 6 ▶ for (i = 0; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i += 2) { 4 2 6
2222 4 SV *key = ST(i);
2223 4 SV *val = ST(i+1);
2224 STRLEN len;
2225 4 const char *k = SvPV(key, len);
2226 4 hv_store(params, k, len, SvREFCNT_inc(val), 0);
2227 }
2228 2 medusa_xs_log_message(aTHX_ params);
2229 2 SvREFCNT_dec((SV *)params);
2230 2 XSRETURN(0);
2231  
2232 # ------------------------------------------------------------------ #
2233 # XSUB: xs_format_message - XS default FORMAT_MESSAGE #
2234 # ------------------------------------------------------------------ #
2235  
2236 SV *
2237 xs_format_message(...)
2238 PREINIT:
2239 HV *params;
2240 HV *log_config;
2241 int i;
2242 CODE:
2243 /* Build params hash from @_ */
2244 7 ▶ if (items % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (items % 2 != 0) { 0 7 7
2245 0 croak("xs_format_message: odd number of arguments");
2246 }
2247 7 params = newHV();
2248 34 ▶ for (i = 0; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; i += 2) { 27 7 34
2249 27 SV *key = ST(i);
2250 27 SV *val = ST(i+1);
2251 STRLEN len;
2252 27 const char *k = SvPV(key, len);
2253 27 hv_store(params, k, len, SvREFCNT_inc(val), 0);
2254 }
2255 7 log_config = get_hv("Medusa::XS::LOG", 0);
2256 7 ▶ if (!log_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (!log_config) { 0 7 7
2257 0 log_config = newHV();
2258 }
2259 7 RETVAL = medusa_xs_format_message(aTHX_ params, log_config);
2260 7 SvREFCNT_dec((SV *)params);
2261 OUTPUT:
2262 RETVAL
2263  
2264 # ------------------------------------------------------------------ #
2265 # XSUB: init_logger - Initialize the logger from LOG_INIT #
2266 # ------------------------------------------------------------------ #
2267  
2268 void
2269 init_logger()
2270 PREINIT:
2271 HV *log_config;
2272 SV **svp;
2273 PPCODE:
2274 15 log_config = get_hv("Medusa::XS::LOG", 0);
2275 15 ▶ if (!log_config) {
Branch: 1 branch
BranchTrueFalseTotal
if (!log_config) { 0 15 15
2276 0 XSRETURN(0);
2277 }
2278
2279 /* Check if LOG already initialized */
2280 15 svp = hv_fetchs(log_config, "LOG", 0);
2281 15 ▶ if (svp && SvROK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp)) { 15 0 15
if (svp && SvROK(*svp)) { 12 3 15
2282 12 XSRETURN(0);
2283 }
2284
2285 /* Call LOG_INIT */
2286 3 svp = hv_fetchs(log_config, "LOG_INIT", 0);
2287 3 ▶ if (svp && SvROK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvROK(*svp)) { 3 0 3
if (svp && SvROK(*svp)) { 3 0 3
2288 3 dSP;
2289 SV *log_obj;
2290 int count;
2291
2292 3 ENTER;
2293 3 SAVETMPS;
2294 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
2295 3 PUTBACK;
2296 3 count = call_sv(*svp, G_SCALAR);
2297 3 SPAGAIN;
2298
2299 3 ▶ if (count >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count >= 1) { 3 0 3
2300 3 log_obj = POPs;
2301 3 ▶ if (SvROK(log_obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(log_obj)) { 3 0 3
2302 3 hv_stores(log_config, "LOG", SvREFCNT_inc(log_obj));
2303 }
2304 }
2305
2306 3 PUTBACK;
2307 3 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 3 0 3
2308 3 LEAVE;
2309 }
2310 3 XSRETURN(0);
2311  
2312 # ------------------------------------------------------------------ #
2313 # XSUB: import - Set up calling package to use Medusa::XS #
2314 # ------------------------------------------------------------------ #
2315  
2316 void
2317 import(...)
2318 PREINIT:
2319 HV *log_config;
2320 const char *caller_pkg;
2321 HV *caller_stash;
2322 AV *isa;
2323 GV *isa_gv;
2324 int i;
2325 PPCODE:
2326 /* Check for odd number of args (after class name) */
2327 23 ▶ if ((items - 1) % 2 != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((items - 1) % 2 != 0) { 1 22 23
2328 1 croak("odd number of params passed in import");
2329 }
2330
2331 /* Get caller package */
2332 22 ▶ caller_pkg = CopSTASHPV(PL_curcop);
Branch: 7 branches
BranchTrueFalseTotal
caller_pkg = CopSTASHPV(PL_curcop); 22 0 22
caller_pkg = CopSTASHPV(PL_curcop); 22 0 22
caller_pkg = CopSTASHPV(PL_curcop); 22 0 22
caller_pkg = CopSTASHPV(PL_curcop); 0 22 22
caller_pkg = CopSTASHPV(PL_curcop); 0 0 0
caller_pkg = CopSTASHPV(PL_curcop); 22 0 22
caller_pkg = CopSTASHPV(PL_curcop); 0 22 22
2333 22 ▶ if (!caller_pkg) caller_pkg = "main";
Branch: 1 branch
BranchTrueFalseTotal
if (!caller_pkg) caller_pkg = "main"; 0 22 22
2334
2335 /* Push Medusa::XS onto caller's @ISA */
2336 22 caller_stash = gv_stashpv(caller_pkg, GV_ADD);
2337 22 isa_gv = gv_fetchpvn_flags("ISA", 3, GV_ADD, SVt_PVAV);
2338 22 ▶ if (caller_stash) {
Branch: 1 branch
BranchTrueFalseTotal
if (caller_stash) { 22 0 22
2339 22 GV *pkg_isa = gv_fetchmethod_autoload(caller_stash, "ISA", FALSE);
2340 22 SV *isa_name = newSVpvf("%s::ISA", caller_pkg);
2341 22 AV *isa_av = get_av(SvPV_nolen(isa_name), GV_ADD);
2342 22 SvREFCNT_dec(isa_name);
2343 22 av_push(isa_av, newSVpvs("Medusa::XS"));
2344 }
2345
2346 /* Process key=value pairs into %LOG (make copies of values) */
2347 22 log_config = get_hv("Medusa::XS::LOG", GV_ADD);
2348 27 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 5 22 27
2349 5 SV *key = ST(i);
2350 5 SV *val = ST(i + 1);
2351 STRLEN klen;
2352 5 const char *kstr = SvPV(key, klen);
2353 /* Use newSVsv to copy - stack values may be read-only */
2354 5 hv_store(log_config, kstr, klen, newSVsv(val), 0);
2355 }
2356
2357 22 XSRETURN(0);
2358  
2359 # ------------------------------------------------------------------ #
2360 # XSUB: MODIFY_CODE_ATTRIBUTES - Handle :Audit attribute #
2361 # ------------------------------------------------------------------ #
2362  
2363 void
2364 MODIFY_CODE_ATTRIBUTES(pkg, coderef, ...)
2365 SV *pkg
2366 SV *coderef
2367 PREINIT:
2368 CV *cv;
2369 GV *gv;
2370 15 const char *method_name = "__ANON__";
2371 15 STRLEN method_len = 8;
2372 const char *pkg_name;
2373 STRLEN pkg_len;
2374 HV *audited;
2375 int i;
2376 15 bool found_audit = FALSE;
2377 PPCODE:
2378 /* Check if any attribute is "Audit" */
2379 15 ▶ for (i = 2; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < items; i++) { 15 0 15
2380 STRLEN len;
2381 15 const char *attr = SvPV(ST(i), len);
2382 15 ▶ if (len >= 5 && strncmp(attr, "Audit", 5) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (len >= 5 && strncmp(attr, "Audit", 5) == 0) { 15 0 15
if (len >= 5 && strncmp(attr, "Audit", 5) == 0) { 15 0 15
2383 15 found_audit = TRUE;
2384 15 break;
2385 }
2386 }
2387
2388 15 ▶ if (!found_audit) {
Branch: 1 branch
BranchTrueFalseTotal
if (!found_audit) { 0 15 15
2389 /* Return remaining attributes (none handled) */
2390 0 ▶ for (i = 2; i < items; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < items; i++) { 0 0 0
2391 0 ▶ XPUSHs(ST(i));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(ST(i)); 0 0 0
2392 }
2393 0 XSRETURN(items - 2);
2394 }
2395
2396 /* Get the CV */
2397 15 ▶ if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) { 15 0 15
if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) { 0 15 15
2398 0 croak("MODIFY_CODE_ATTRIBUTES: not a code reference");
2399 }
2400 15 cv = (CV *)SvRV(coderef);
2401
2402 /* Initialize logger */
2403 {
2404 15 dSP;
2405 15 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 15 15
2406 15 PUTBACK;
2407 15 call_pv("Medusa::XS::init_logger", G_DISCARD);
2408 }
2409
2410 /* Get method name from CV's GV */
2411 15 gv = CvGV(cv);
2412 15 ▶ if (gv) {
Branch: 1 branch
BranchTrueFalseTotal
if (gv) { 15 0 15
2413 15 method_name = GvNAME(gv);
2414 15 method_len = GvNAMELEN(gv);
2415 }
2416
2417 /* Get package name */
2418 15 pkg_name = SvPV(pkg, pkg_len);
2419
2420 /* Track in %AUDITED */
2421 15 audited = get_hv("Medusa::XS::AUDITED", GV_ADD);
2422 {
2423 char addr_key[32];
2424 15 int addr_len = snprintf(addr_key, sizeof(addr_key), "%lu",
2425 (unsigned long)PTR2UV(cv));
2426 15 hv_store(audited, addr_key, addr_len, newSViv(1), 0);
2427 }
2428
2429 /* Wrap the sub with XS auditing */
2430 15 inject_audit_wrapper(aTHX_ cv, method_name, method_len,
2431 pkg_name, pkg_len);
2432
2433 /* Return empty list (we handled Audit, no unhandled attrs) */
2434 15 XSRETURN(0);
2435  
2436 # ------------------------------------------------------------------ #
2437 # XSUB: FETCH_CODE_ATTRIBUTES - Report attributes on a coderef #
2438 # ------------------------------------------------------------------ #
2439  
2440 void
2441 FETCH_CODE_ATTRIBUTES(pkg, coderef)
2442 SV *pkg
2443 SV *coderef
2444 PREINIT:
2445 CV *cv;
2446 HV *audited;
2447 2 bool is_audited_cv = FALSE;
2448 PPCODE:
2449 PERL_UNUSED_VAR(pkg);
2450
2451 2 ▶ if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) { 2 0 2
if (!SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) { 0 2 2
2452 0 XSRETURN(0);
2453 }
2454 2 cv = (CV *)SvRV(coderef);
2455
2456 /* Check %AUDITED hash */
2457 2 audited = get_hv("Medusa::XS::AUDITED", 0);
2458 2 ▶ if (audited) {
Branch: 1 branch
BranchTrueFalseTotal
if (audited) { 2 0 2
2459 char addr_key[32];
2460 2 int addr_len = snprintf(addr_key, sizeof(addr_key), "%lu",
2461 (unsigned long)PTR2UV(cv));
2462 2 ▶ if (hv_exists(audited, addr_key, addr_len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(audited, addr_key, addr_len)) { 0 2 2
2463 0 is_audited_cv = TRUE;
2464 }
2465 }
2466
2467 /* Check XS magic */
2468 2 ▶ if (!is_audited_cv && CV_IS_AUDITED(cv)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!is_audited_cv && CV_IS_AUDITED(cv)) { 2 0 2
if (!is_audited_cv && CV_IS_AUDITED(cv)) { 1 1 2
if (!is_audited_cv && CV_IS_AUDITED(cv)) { 1 0 1
2469 1 is_audited_cv = TRUE;
2470 }
2471
2472 2 ▶ if (is_audited_cv) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_audited_cv) { 1 1 2
2473 1 ▶ XPUSHs(sv_2mortal(newSVpvs("Audit")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("Audit"))); 0 1 1
2474 1 XSRETURN(1);
2475 }
2476
2477 1 XSRETURN(0);
2478  
2479 # ================================================================== #
2480 # Medusa::XS::Logger — Pure XS file logger with flock #
2481 # ================================================================== #
2482  
2483 MODULE = Medusa::XS PACKAGE = Medusa::XS::Logger
2484  
2485 SV *
2486 new(pkg, ...)
2487 const char *pkg
2488 PREINIT:
2489 MedusaLogger *logger;
2490 SV *self_rv;
2491 SV *self_sv;
2492 8 const char *filename = "audit.log";
2493 STRLEN filename_len;
2494 HV *args;
2495 SV **svp;
2496 I32 i;
2497 CODE:
2498 /* Parse args: new(file => 'x') or new({file => 'x'}) */
2499 8 ▶ if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { 0 8 8
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { 0 0 0
if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { 0 0 0
2500 0 args = (HV *)SvRV(ST(1));
2501 8 ▶ } else if (items > 1 && (items - 1) % 2 == 0) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (items > 1 && (items - 1) % 2 == 0) { 7 1 8
} else if (items > 1 && (items - 1) % 2 == 0) { 7 0 7
2502 7 args = newHV();
2503 7 sv_2mortal((SV *)args);
2504 14 ▶ for (i = 1; i < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i += 2) { 7 7 14
2505 const char *key;
2506 STRLEN klen;
2507 7 key = SvPV(ST(i), klen);
2508 7 (void)hv_store(args, key, klen, SvREFCNT_inc(ST(i+1)), 0);
2509 }
2510 } else {
2511 1 args = newHV();
2512 1 sv_2mortal((SV *)args);
2513 }
2514
2515 8 svp = hv_fetchs(args, "file", 0);
2516 8 ▶ if (svp && SvPOK(*svp)) {
Branch: 2 branches
BranchTrueFalseTotal
if (svp && SvPOK(*svp)) { 7 1 8
if (svp && SvPOK(*svp)) { 7 0 7
2517 14 filename = SvPV(*svp, filename_len);
2518 } else {
2519 1 filename_len = 9; /* strlen("audit.log") */
2520 }
2521
2522 8 Newxz(logger, 1, MedusaLogger);
2523 8 Newx(logger->filename, filename_len + 1, char);
2524 8 Copy(filename, logger->filename, filename_len, char);
2525 8 logger->filename[filename_len] = '\0';
2526 8 logger->filename_len = filename_len;
2527
2528 8 logger->fh = PerlIO_open(filename, "a");
2529 8 ▶ if (!logger->fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (!logger->fh) { 0 8 8
2530 0 Safefree(logger->filename);
2531 0 Safefree(logger);
2532 0 croak("Medusa::XS::Logger: cannot open '%s': %s", filename, strerror(errno));
2533 }
2534
2535 /* Bless a ref to an SV, attach C struct via magic */
2536 8 self_sv = newSV(0);
2537 8 sv_magicext(self_sv, NULL, PERL_MAGIC_ext, &medusa_logger_vtbl,
2538 (const char *)logger, 0);
2539 8 self_rv = newRV_noinc(self_sv);
2540 8 sv_bless(self_rv, gv_stashpv(pkg, GV_ADD));
2541
2542 8 RETVAL = self_rv;
2543 OUTPUT:
2544 RETVAL
2545  
2546 void
2547 debug(self, line)
2548 SV *self
2549 SV *line
2550 PREINIT:
2551 MedusaLogger *logger;
2552 const char *msg;
2553 STRLEN len;
2554 CODE:
2555 3 logger = medusa_logger_from_sv(aTHX_ self);
2556 3 ▶ if (!logger) croak("Medusa::XS::Logger: invalid logger object");
Branch: 1 branch
BranchTrueFalseTotal
if (!logger) croak("Medusa::XS::Logger: invalid logger object"); 0 3 3
2557 3 msg = SvPV(line, len);
2558 3 medusa_logger_write(aTHX_ logger, msg, len);
2559  
2560 void
2561 info(self, line)
2562 SV *self
2563 SV *line
2564 PREINIT:
2565 MedusaLogger *logger;
2566 const char *msg;
2567 STRLEN len;
2568 CODE:
2569 1 logger = medusa_logger_from_sv(aTHX_ self);
2570 1 ▶ if (!logger) croak("Medusa::XS::Logger: invalid logger object");
Branch: 1 branch
BranchTrueFalseTotal
if (!logger) croak("Medusa::XS::Logger: invalid logger object"); 0 1 1
2571 1 msg = SvPV(line, len);
2572 1 medusa_logger_write(aTHX_ logger, msg, len);
2573  
2574 void
2575 error(self, line)
2576 SV *self
2577 SV *line
2578 PREINIT:
2579 MedusaLogger *logger;
2580 const char *msg;
2581 STRLEN len;
2582 CODE:
2583 0 logger = medusa_logger_from_sv(aTHX_ self);
2584 0 ▶ if (!logger) croak("Medusa::XS::Logger: invalid logger object");
Branch: 1 branch
BranchTrueFalseTotal
if (!logger) croak("Medusa::XS::Logger: invalid logger object"); 0 0 0
2585 0 msg = SvPV(line, len);
2586 0 medusa_logger_write(aTHX_ logger, msg, len);
2587  
2588 void
2589 log(self, line)
2590 SV *self
2591 SV *line
2592 PREINIT:
2593 MedusaLogger *logger;
2594 const char *msg;
2595 STRLEN len;
2596 CODE:
2597 0 logger = medusa_logger_from_sv(aTHX_ self);
2598 0 ▶ if (!logger) croak("Medusa::XS::Logger: invalid logger object");
Branch: 1 branch
BranchTrueFalseTotal
if (!logger) croak("Medusa::XS::Logger: invalid logger object"); 0 0 0
2599 0 msg = SvPV(line, len);
2600 0 medusa_logger_write(aTHX_ logger, msg, len);
2601  
2602 void
2603 DESTROY(self)
2604 SV *self
2605 CODE:
2606 /* Magic destructor handles cleanup automatically */
2607 PERL_UNUSED_VAR(self);
↑ Summary blib/lib/Medusa/XS.pm »