aboutsummaryrefslogtreecommitdiffstats
path: root/core/errorhelper.c
blob: 7c30867a7757e99317773af27b8a99f6c1b015fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// SPDX-License-Identifier: GPL-2.0
#ifdef __clang__
// Clang has a bug on zero-initialization of C structs.
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
#endif
#include <stdarg.h>
#include "errorhelper.h"
#include "membuffer.h"

#define VA_BUF(b, fmt) do { va_list args; va_start(args, fmt); put_vformat(b, fmt, args); va_end(args); } while (0)

int verbose;

static void (*error_cb)(char *) = NULL;

int report_error(const char *fmt, ...)
{
	struct membuffer buf = { 0 };

	/* if there is no error callback registered, don't produce errors */
	if (!error_cb)
		return -1;

	VA_BUF(&buf, fmt);
	mb_cstring(&buf);
	error_cb(detach_buffer(&buf));

	return -1;
}

void set_error_cb(void(*cb)(char *))
{
	error_cb = cb;
}