summaryrefslogtreecommitdiff
path: root/src/warning.c
blob: b0bc7c03d5a9aa0b90453e717f246254dac366d3 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "warning.h"
#include "buffer.h"
#include <stdarg.h>

static git_warning_callback _warning_cb = NULL;
static void *_warning_payload = NULL;

void git_warning_set_callback(git_warning_callback cb, void *payload)
{
	_warning_cb = cb;
	_warning_payload = payload;
}

static int git_warning__send(
	git_warning *warning, int default_rval, const char *fmt, va_list ap)
{
	int error = 0;
	git_buf buf = GIT_BUF_INIT;
	git_warning_callback cb = _warning_cb;

	if (!cb)
		return default_rval;

	if (!(error = git_buf_vprintf(&buf, fmt, ap))) {
		warning->message = git_buf_cstr(&buf);
		error = cb(warning, default_rval, _warning_payload);
	}

	git_buf_free(&buf);

	return error;
}

int git_warn(
	git_warning_t type,
	int default_rval,
	const char *fmt,
	...)
{
	int error;
	va_list ap;
	git_warning warning;

	if (!_warning_cb)
		return default_rval;

	warning.type = type;

	va_start(ap, fmt);
	error = git_warning__send(&warning, default_rval, fmt, ap);
	va_end(ap);

	return error;
}

int git_warn_invalid_data(
	git_warning_t type,
	int default_rval,
	const char *data,
	int datalen,
	const char *fmt,
	...)
{
	int error;
	va_list ap;
	git_warning_invalid_data warning;

	if (!_warning_cb)
		return default_rval;

	warning.base.type = type;
	warning.invalid_data = git__strndup(data, datalen);
	GITERR_CHECK_ALLOC(warning.invalid_data);
	warning.invalid_data_len = datalen;

	va_start(ap, fmt);
	error = git_warning__send((git_warning *)&warning, default_rval, fmt, ap);
	va_end(ap);

	git__free((char *)warning.invalid_data);

	return error;
}