Chandra

 view release on metacpan or  search on metacpan

xs/log.xs  view on Meta::CPAN

 # ---- formatter(fmt) ----

void
formatter(self, fmt)
    SV *self
    SV *fmt
CODE:
{
    chandra_log_ctx *ctx = chandra_log_get_ctx(aTHX_ self);

    if (ctx->fmt_custom) {
        SvREFCNT_dec(ctx->fmt_custom);
        ctx->fmt_custom = NULL;
    }

    if (SvROK(fmt) && SvTYPE(SvRV(fmt)) == SVt_PVCV) {
        ctx->fmt_type   = CHANDRA_LOG_FMT_CUSTOM;
        ctx->fmt_custom = SvREFCNT_inc(fmt);
    } else {
        const char *s = SvPV_nolen(fmt);
        if (strEQ(s, "json"))
            ctx->fmt_type = CHANDRA_LOG_FMT_JSON;
        else if (strEQ(s, "minimal"))
            ctx->fmt_type = CHANDRA_LOG_FMT_MINIMAL;
        else
            ctx->fmt_type = CHANDRA_LOG_FMT_TEXT;
    }
}

 # ---- with(%context) → child logger ----

SV *
with(self, ...)
    SV *self
CODE:
{
    chandra_log_ctx *orig = chandra_log_get_ctx(aTHX_ self);
    chandra_log_ctx *child = chandra_log_new_ctx(aTHX);
    HV *self_hv = newHV();
    int i;

    /* Copy settings */
    child->level        = orig->level;
    child->fmt_type     = orig->fmt_type;
    child->fmt_custom   = orig->fmt_custom
                              ? SvREFCNT_inc(orig->fmt_custom)
                              : NULL;
    child->rotate_max_size = orig->rotate_max_size;
    child->rotate_keep     = orig->rotate_keep;

    /* Copy outputs */
    child->output_count = orig->output_count;
    for (i = 0; i < orig->output_count; i++) {
        child->outputs[i] = orig->outputs[i];
        if (child->outputs[i].path)
            SvREFCNT_inc(child->outputs[i].path);
        if (child->outputs[i].callback)
            SvREFCNT_inc(child->outputs[i].callback);
    }

    /* Merge context: copy parent context then overlay new key-value pairs */
    SvREFCNT_dec((SV *)child->context);
    child->context = newHVhv(orig->context);
    for (i = 1; i < items - 1; i += 2) {
        STRLEN klen;
        const char *key = SvPV(ST(i), klen);
        (void)hv_store(child->context, key, klen,
                       newSVsv(ST(i + 1)), 0);
    }

    (void)hv_stores(self_hv, "_ctx", newSViv(PTR2IV(child)));

    RETVAL = sv_bless(newRV_noinc((SV *)self_hv),
                      gv_stashpv("Chandra::Log", GV_ADD));
}
OUTPUT:
    RETVAL

 # ---- Logging methods ----

void
debug(self, message = &PL_sv_undef, data = NULL)
    SV *self
    SV *message
    SV *data
CODE:
    chandra_log_dispatch(aTHX_ chandra_log_get_ctx(aTHX_ self),
                         CHANDRA_LOG_DEBUG, message, data);

void
info(self, message = &PL_sv_undef, data = NULL)
    SV *self
    SV *message
    SV *data
CODE:
    chandra_log_dispatch(aTHX_ chandra_log_get_ctx(aTHX_ self),
                         CHANDRA_LOG_INFO, message, data);

void
warn(self, message = &PL_sv_undef, data = NULL)
    SV *self
    SV *message
    SV *data
CODE:
    chandra_log_dispatch(aTHX_ chandra_log_get_ctx(aTHX_ self),
                         CHANDRA_LOG_WARN, message, data);

void
error(self, message = &PL_sv_undef, data = NULL)
    SV *self
    SV *message
    SV *data
CODE:
    chandra_log_dispatch(aTHX_ chandra_log_get_ctx(aTHX_ self),
                         CHANDRA_LOG_ERROR, message, data);

void
fatal(self, message = &PL_sv_undef, data = NULL)
    SV *self
    SV *message
    SV *data



( run in 0.324 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )