Welcome to little lamb

Code » limb » release » tree

[release] / src / liblimb / autoopt.h / autoopt_debug.c

/* This file is part of limb                           https://lila.oss/limb
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <errno.h>
#include <limb/buffer.h>
#include <limb/obuffer.h>
#include "autoopt.h"

static char buf[BUFFER_OUTSIZE];
buffer buffer_dbg_ = BUFFER_INIT(&fd_writev, -1, buf, sizeof(buf));
obuffer obuffer_dbg1_ = OBUFFER_INIT(&buffer_dbg_, OLVL_NORMAL);
obuffer obuffer_dbg2_ = OBUFFER_INIT(&buffer_dbg_, OLVL_NORMAL);

int
autoopt_debug(const struct option *option, const char *arg)
{
    if (!arg) {
        obuffer_setlevel(obuffer_1, OLVL_DEBUG);
        return 1;
    }

    if (buffer_dbg_.fd >= 0)
        return (errno = EADDRINUSE, 0);

    u8 lvl = OLVL_DEBUG;
    int fd = autoopt_parse_buffer_dest(option, arg, &lvl);
    if (fd < 0) return 0;
    buffer_dbg_.fd = fd;

    obuffer_setlevel(&obuffer_dbg1_, lvl);
    obuffer_attach(&obuffer_dbg1_, obuffer_1);

    obuffer_setlevel(&obuffer_dbg2_, lvl);
    obuffer_attach(&obuffer_dbg2_, obuffer_2);

    return 1;
}