aboutsummaryrefslogtreecommitdiffstats
path: root/debug.c
blob: a3e0fc464d56a148d36f9697d8dde6a78f678f9e (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
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
91
92
93
94
95
96
#define _BSD_SOURCE
#include <syslog.h>
#include <stdarg.h>
#include <stdio.h>
#include "antispam-plugin.h"

enum antispam_debug_target debug_target;
int verbose_debug;

static void _debug(const char *format, va_list ap)
{
	const char *fmt;

	if (debug_target == ADT_NONE)
		return;

	t_push();

	fmt = t_strconcat(stringify(PLUGINNAME), ": ", format, NULL);

	switch (debug_target) {
	case ADT_NONE:
		break;
	case ADT_SYSLOG:
		vsyslog(LOG_DEBUG, fmt, ap);
		break;
	case ADT_STDERR:
		vfprintf(stderr, fmt, ap);
		fflush(stderr);
		break;
	}

	t_pop();
}

void debug(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	_debug(fmt, args);
	va_end(args);
}

void debugv(char **args)
{
	size_t len, pos = 0, buflen = 1024;
	char *buf;
	const char *str;

	t_push();
	buf = t_buffer_get(buflen);

	while (1) {
		str = *args;
		if (!str)
			break;
		len = strlen(str);
		if (pos + len + 1 >= buflen) {
			buflen = nearest_power(pos + len + 2);
			buf = t_buffer_reget(buf, buflen);
		}

		memcpy(buf + pos, str, len);
		pos += len;
		buf[pos++] = ' ';
		args++;
	}

	buf[pos++] = '\0';

	t_buffer_alloc(pos);

	debug("%s", buf);
	t_pop();
}

void debugv_not_stderr(char **args)
{
	if (debug_target == ADT_STDERR)
		return;

	debugv(args);
}

void debug_verbose(const char *fmt, ...)
{
	va_list args;

	if (!verbose_debug)
		return;

	va_start(args, fmt);
	_debug(fmt, args);
	va_end(args);
}