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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
/* Copyright (c) 2002-2018 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
_testsuite_stdout_log(const struct sieve_error_params *params,
const char *prefix, const char *message)
{
if (_testsuite_log_stdout) {
if (params->location == NULL || *params->location == '\0') {
fprintf(stdout, "LOG: %s: %s\n",
prefix, message);
} else {
fprintf(stdout, "LOG: %s: %s: %s\n",
params->location, prefix, message);
}
}
}
static void
_testsuite_log(struct sieve_error_handler *ehandler ATTR_UNUSED,
const struct sieve_error_params *params,
enum sieve_error_flags flags ATTR_UNUSED, const char *message)
{
pool_t pool = _testsuite_logmsg_pool;
struct _testsuite_log_message msg;
const char *prefix;
switch (params->log_type) {
case LOG_TYPE_ERROR:
prefix = "error";
break;
case LOG_TYPE_WARNING:
prefix = "warning";
break;
case LOG_TYPE_INFO:
prefix = "info";
break;
case LOG_TYPE_DEBUG:
prefix = "debug";
break;
default:
i_unreached();
}
_testsuite_stdout_log(params, prefix, message);
msg.location = p_strdup(pool, params->location);
msg.message = p_strdup(pool, message);
switch (params->log_type) {
case LOG_TYPE_ERROR:
array_append(&_testsuite_log_errors, &msg, 1);
break;
case LOG_TYPE_WARNING:
array_append(&_testsuite_log_warnings, &msg, 1);
break;
case LOG_TYPE_INFO:
array_append(&_testsuite_log_messages, &msg, 1);
break;
case LOG_TYPE_DEBUG:
break;
default:
i_unreached();
}
}
static void
_testsuite_main_log(struct sieve_error_handler *ehandler,
const struct sieve_error_params *params,
enum sieve_error_flags flags, const char *message)
{
if (params->log_type != LOG_TYPE_ERROR)
return _testsuite_log(ehandler, params, flags, message);
if (params->location == NULL || *params->location == '\0')
fprintf(stderr, "error: %s\n", message);
else
fprintf(stderr, "%s: error: %s\n", params->location, message);
}
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->log = _testsuite_log;
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->log = _testsuite_main_log;
return ehandler;
}
static void ATTR_FORMAT(2, 0)
testsuite_error_handler(const struct failure_context *ctx, const char *fmt,
va_list args)
{
struct sieve_error_params params = {
.location = NULL,
};
pool_t pool = _testsuite_logmsg_pool;
struct _testsuite_log_message msg;
i_zero(&msg);
switch (ctx->type) {
case LOG_TYPE_DEBUG:
T_BEGIN {
_testsuite_stdout_log(¶ms, "debug",
t_strdup_vprintf(fmt, args));
} T_END;
break;
case LOG_TYPE_INFO:
msg.message = p_strdup_vprintf(pool, fmt, args);
array_append(&_testsuite_log_messages, &msg, 1);
_testsuite_stdout_log(¶ms, "info", msg.message);
break;
case LOG_TYPE_WARNING:
msg.message = p_strdup_vprintf(pool, fmt, args);
array_append(&_testsuite_log_warnings, &msg, 1);
_testsuite_stdout_log(¶ms, "warning", msg.message);
break;
case LOG_TYPE_ERROR:
msg.message = p_strdup_vprintf(pool, fmt, args);
array_append(&_testsuite_log_errors, &msg, 1);
_testsuite_stdout_log(¶ms, "error", msg.message);
break;
default:
default_error_handler(ctx, fmt, args);
break;
}
}
/*
*
*/
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);
i_set_error_handler(testsuite_error_handler);
i_set_info_handler(testsuite_error_handler);
i_set_debug_handler(testsuite_error_handler);
testsuite_log_clear_messages();
}
void testsuite_log_deinit(void)
{
sieve_error_handler_unref(&testsuite_log_ehandler);
sieve_error_handler_unref(&testsuite_log_main_ehandler);
i_set_error_handler(default_error_handler);
i_set_info_handler(default_error_handler);
i_set_debug_handler(default_error_handler);
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;
}
|