From ed004cba0e8d1d383def76f795b1e63ba0aaa89a Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 9 Feb 2025 15:04:27 +0100 Subject: First commit (everything was written 2024) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- liblog_use_output.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 liblog_use_output.c (limited to 'liblog_use_output.c') diff --git a/liblog_use_output.c b/liblog_use_output.c new file mode 100644 index 0000000..3471722 --- /dev/null +++ b/liblog_use_output.c @@ -0,0 +1,54 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#ifndef TEST + +struct liblog_output * +liblog_use_output(struct liblog_context *ctx, const struct liblog_output *output) +{ + void *new; + + if (!ctx || !output) + goto einval; + + if (output->lowest_verbosity_unlimited && output->highest_verbosity_unlimited) + if (output->lowest_verbosity > output->highest_verbosity) + goto einval; + + if (output->sink_type == LIBLOG_STREAM) { + if (!output->sink.stream.stream) + goto einval; + } else if (output->sink_type != LIBLOG_SYSLOG && output->sink_type != LIBLOG_FILE) { + /* + * We do not want to validate syslog level, + * nor do we want to validate file descriptor, + * indeed the user may want to output to a negative + * file descriptor so that it only visible during + * system call tracing + */ + einval: + errno = EINVAL; + return NULL; + } + + if (ctx->noutputs > SIZE_MAX / sizeof(*ctx->outputs) - 1) + goto enomem; + new = realloc(ctx->outputs, (ctx->noutputs + 1) * sizeof(*ctx->outputs)); + if (!new) { + enomem: + errno = ENOMEM; + return NULL; + } + + memcpy(&ctx->outputs[ctx->noutputs], output, sizeof(*output)); + + if (!ctx->outputs[ctx->noutputs].prefixfmt) + ctx->outputs[ctx->noutputs].prefixfmt = DEFAULT_PREFIXFMT; + + return &ctx->outputs[ctx->noutputs++]; +} + +#else + +int main(void) {return 0;} /* TODO test */ + +#endif -- cgit v1.2.3-70-g09d2