File: debug.c

package info (click to toggle)
dovecot-antispam 2.0%2B20171229-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 220 kB
  • sloc: ansic: 3,156; makefile: 91; sh: 14
file content (132 lines) | stat: -rw-r--r-- 2,395 bytes parent folder | download | duplicates (4)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <syslog.h>
#include <stdarg.h>
#include <stdio.h>
#include "antispam-plugin.h"
#include "antispam-version.h"


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

	if (cfg->target == ADT_NONE)
		return;

	T_BEGIN {

	fmt = t_strconcat(cfg->prefix, format, NULL);

	switch (cfg->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_END;
}

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

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

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

	T_BEGIN {
	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(cfg, "%s", buf);
	} T_END;
}

void debugv_not_stderr(const struct antispam_debug_config *cfg, char **args)
{
	if (cfg->target == ADT_STDERR)
		return;

	debugv(cfg, args);
}

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

	if (!cfg->verbose)
		return;

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

int debug_init(struct antispam_debug_config *cfg,
	       const char *(getenv)(const char *env, void *data),
	       void *getenv_data)
{
	const char *tmp;

	tmp = getenv("DEBUG_TARGET", getenv_data);
	if (tmp) {
		if (strcmp(tmp, "syslog") == 0)
			cfg->target = ADT_SYSLOG;
		else if (strcmp(tmp, "stderr") == 0)
			cfg->target = ADT_STDERR;
		else
			return -1;
	}

	cfg->prefix = getenv("DEBUG_PREFIX", getenv_data);
	if (!cfg->prefix)
		cfg->prefix = "antispam: ";

	debug(cfg, "plugin initialising (%s)\n", ANTISPAM_VERSION);

	tmp = getenv("VERBOSE_DEBUG", getenv_data);
	if (tmp) {
		char *endp;
		unsigned long val = strtoul(tmp, &endp, 10);
		if (*endp || val >= 2) {
			debug(cfg, "Invalid verbose_debug setting\n");
			return -1;
		}
		cfg->verbose = val;
		debug_verbose(cfg, "verbose debug enabled\n");
	}

	return 0;
}