File: testsuite-log.c

package info (click to toggle)
dovecot 1:2.2.13-12~deb8u4
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 38,792 kB
  • sloc: ansic: 341,472; sh: 16,920; makefile: 5,393; cpp: 1,474; perl: 265; xml: 44; python: 34; pascal: 27
file content (290 lines) | stat: -rw-r--r-- 7,474 bytes parent folder | download | duplicates (3)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/* Copyright (c) 2002-2013 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "str.h"
#include "array.h"

#include "sieve-common.h"
#include "sieve-stringlist.h"
#include "sieve-error-private.h"

#include "testsuite-common.h"
#include "testsuite-log.h"

/*
 * Configuration
 */

bool _testsuite_log_stdout = FALSE;

/*
 * Testsuite log error handlers
 */

struct sieve_error_handler *testsuite_log_ehandler = NULL;
struct sieve_error_handler *testsuite_log_main_ehandler = NULL;

struct _testsuite_log_message {
	const char *location;
	const char *message;
};

static pool_t _testsuite_logmsg_pool = NULL;
ARRAY(struct _testsuite_log_message) _testsuite_log_errors;
ARRAY(struct _testsuite_log_message) _testsuite_log_warnings;
ARRAY(struct _testsuite_log_message) _testsuite_log_messages;

static inline void ATTR_FORMAT(3, 0) _testsuite_stdout_vlog
(const char *prefix, const char *location, const char *fmt,
	va_list args)
{
	if ( _testsuite_log_stdout ) {
		va_list args_copy;
		VA_COPY(args_copy, args);

		if ( location == NULL || *location == '\0' )
			fprintf(stdout,
				"LOG: %s: %s\n", prefix, t_strdup_vprintf(fmt, args_copy));
		else
			fprintf(stdout,
				"LOG: %s: %s: %s\n", prefix, location, t_strdup_vprintf(fmt, args_copy));
	}
}

static void ATTR_FORMAT(4, 0) _testsuite_log_verror
(struct sieve_error_handler *ehandler ATTR_UNUSED,
	unsigned int flags ATTR_UNUSED, const char *location, const char *fmt,
	va_list args)
{
	pool_t pool = _testsuite_logmsg_pool;
	struct _testsuite_log_message msg;

	_testsuite_stdout_vlog("error", location, fmt, args);

	msg.location = p_strdup(pool, location);
	msg.message = p_strdup_vprintf(pool, fmt, args);

	array_append(&_testsuite_log_errors, &msg, 1);
}

static void ATTR_FORMAT(4, 0) _testsuite_log_main_verror
(struct sieve_error_handler *ehandler ATTR_UNUSED,
	unsigned int flags ATTR_UNUSED, const char *location, const char *fmt,
	va_list args)
{
	if ( location == NULL || *location == '\0' )
		fprintf(stderr,
			"error: %s\n", t_strdup_vprintf(fmt, args));
	else
		fprintf(stderr,
			"%s: error: %s\n", location, t_strdup_vprintf(fmt, args));
}

static void ATTR_FORMAT(4, 0) _testsuite_log_vwarning
(struct sieve_error_handler *ehandler ATTR_UNUSED,
	unsigned int flags ATTR_UNUSED, const char *location, const char *fmt,
	va_list args)
{
	pool_t pool = _testsuite_logmsg_pool;
	struct _testsuite_log_message msg;

	_testsuite_stdout_vlog("warning", location, fmt, args);

	msg.location = p_strdup(pool, location);
	msg.message = p_strdup_vprintf(pool, fmt, args);

	array_append(&_testsuite_log_warnings, &msg, 1);
}

static void ATTR_FORMAT(4, 0) _testsuite_log_vinfo
(struct sieve_error_handler *ehandler ATTR_UNUSED,
	unsigned int flags ATTR_UNUSED, const char *location, const char *fmt,
	va_list args)
{
	pool_t pool = _testsuite_logmsg_pool;
	struct _testsuite_log_message msg;

	_testsuite_stdout_vlog("info", location, fmt, args);

	msg.location = p_strdup(pool, location);
	msg.message = p_strdup_vprintf(pool, fmt, args);

	array_append(&_testsuite_log_messages, &msg, 1);
}


static void ATTR_FORMAT(4, 0) _testsuite_log_vdebug
(struct sieve_error_handler *ehandler ATTR_UNUSED,
	unsigned int flags ATTR_UNUSED, const char *location, const char *fmt,
	va_list args)
{
	_testsuite_stdout_vlog("debug", location, fmt, args);
}

static struct sieve_error_handler *_testsuite_log_ehandler_create(void)
{
	pool_t pool;
	struct sieve_error_handler *ehandler;

	pool = pool_alloconly_create
		("testsuite_log_ehandler", sizeof(struct sieve_error_handler));
	ehandler = p_new(pool, struct sieve_error_handler, 1);
	sieve_error_handler_init(ehandler, testsuite_sieve_instance, pool, 0);

	ehandler->verror = _testsuite_log_verror;
	ehandler->vwarning = _testsuite_log_vwarning;
	ehandler->vinfo = _testsuite_log_vinfo;
	ehandler->vdebug = _testsuite_log_vdebug;

	return ehandler;
}

static struct sieve_error_handler *_testsuite_log_main_ehandler_create(void)
{
	pool_t pool;
	struct sieve_error_handler *ehandler;

	pool = pool_alloconly_create
		("testsuite_log_main_ehandler", sizeof(struct sieve_error_handler));
	ehandler = p_new(pool, struct sieve_error_handler, 1);
	sieve_error_handler_init(ehandler, testsuite_sieve_instance, pool, 0);

	ehandler->verror = _testsuite_log_main_verror;
	ehandler->vwarning = _testsuite_log_vwarning;
	ehandler->vinfo = _testsuite_log_vinfo;
	ehandler->vdebug = _testsuite_log_vdebug;

	return ehandler;
}

/*
 *
 */

void testsuite_log_clear_messages(void)
{
	if ( _testsuite_logmsg_pool != NULL ) {
		if ( array_count(&_testsuite_log_errors) == 0 )
			return;
		pool_unref(&_testsuite_logmsg_pool);
	}

	_testsuite_logmsg_pool = pool_alloconly_create
		("testsuite_log_messages", 8192);

	p_array_init(&_testsuite_log_errors, _testsuite_logmsg_pool, 128);
	p_array_init(&_testsuite_log_warnings, _testsuite_logmsg_pool, 128);
	p_array_init(&_testsuite_log_messages, _testsuite_logmsg_pool, 128);

	sieve_error_handler_reset(testsuite_log_ehandler);
}

/*
 *
 */

void testsuite_log_init(bool log_stdout)
{
	_testsuite_log_stdout = log_stdout;

	testsuite_log_ehandler = _testsuite_log_ehandler_create();
	sieve_error_handler_accept_infolog(testsuite_log_ehandler, TRUE);
	sieve_error_handler_accept_debuglog(testsuite_log_ehandler, TRUE);

	testsuite_log_main_ehandler = _testsuite_log_main_ehandler_create();
	sieve_error_handler_accept_infolog(testsuite_log_main_ehandler, TRUE);
	sieve_error_handler_accept_debuglog(testsuite_log_main_ehandler, TRUE);

	sieve_system_ehandler_set(testsuite_log_ehandler);

	testsuite_log_clear_messages();
}

void testsuite_log_deinit(void)
{
	sieve_error_handler_unref(&testsuite_log_ehandler);
	sieve_error_handler_unref(&testsuite_log_main_ehandler);

	pool_unref(&_testsuite_logmsg_pool);
}

/*
 * Log stringlist
 */

/* Forward declarations */

static int testsuite_log_stringlist_next_item
	(struct sieve_stringlist *_strlist, string_t **str_r);
static void testsuite_log_stringlist_reset
	(struct sieve_stringlist *_strlist);

/* Stringlist object */

struct testsuite_log_stringlist {
	struct sieve_stringlist strlist;

	int pos, index;
};

struct sieve_stringlist *testsuite_log_stringlist_create
(const struct sieve_runtime_env *renv, int index)
{
	struct testsuite_log_stringlist *strlist;

	strlist = t_new(struct testsuite_log_stringlist, 1);
	strlist->strlist.runenv = renv;
	strlist->strlist.exec_status = SIEVE_EXEC_OK;
	strlist->strlist.next_item = testsuite_log_stringlist_next_item;
	strlist->strlist.reset = testsuite_log_stringlist_reset;

 	strlist->index = index;
	strlist->pos = 0;

	return &strlist->strlist;
}

static int testsuite_log_stringlist_next_item
(struct sieve_stringlist *_strlist, string_t **str_r)
{
	struct testsuite_log_stringlist *strlist =
		(struct testsuite_log_stringlist *) _strlist;
	const struct _testsuite_log_message *msg;
	int pos;

	*str_r = NULL;

	if ( strlist->pos < 0 )
		return 0;

	if ( strlist->index > 0 ) {
		pos = strlist->index - 1;
		strlist->pos = -1;
	} else {
		pos = strlist->pos++;
	}

	if ( pos >= (int) array_count(&_testsuite_log_errors) ) {
		strlist->pos = -1;
		return 0;
	}

	msg = array_idx(&_testsuite_log_errors, (unsigned int) pos);

	*str_r = t_str_new_const(msg->message, strlen(msg->message));
	return 1;
}

static void testsuite_log_stringlist_reset
(struct sieve_stringlist *_strlist)
{
	struct testsuite_log_stringlist *strlist =
		(struct testsuite_log_stringlist *) _strlist;

	strlist->pos = 0;
}