New options: --verbose to verbosely log executed
actions, --log-facility to switch all diagnostic output to syslog.
Showing
1 changed file
with
73 additions
and
9 deletions
... | @@ -27,8 +27,7 @@ | ... | @@ -27,8 +27,7 @@ |
27 | #include <stdlib.h> | 27 | #include <stdlib.h> |
28 | #include <string.h> | 28 | #include <string.h> |
29 | #include <unistd.h> | 29 | #include <unistd.h> |
30 | 30 | #include <syslog.h> | |
31 | #include <sys/stat.h> | ||
32 | 31 | ||
33 | #include <mailutils/argcv.h> | 32 | #include <mailutils/argcv.h> |
34 | #include <mailutils/libsieve.h> | 33 | #include <mailutils/libsieve.h> |
... | @@ -84,6 +83,9 @@ static struct argp_option options[] = | ... | @@ -84,6 +83,9 @@ static struct argp_option options[] = |
84 | {"debug", 'd', "FLAGS", OPTION_ARG_OPTIONAL, | 83 | {"debug", 'd', "FLAGS", OPTION_ARG_OPTIONAL, |
85 | "Debug flags (defaults to \"" D_DEFAULT "\")", 0}, | 84 | "Debug flags (defaults to \"" D_DEFAULT "\")", 0}, |
86 | 85 | ||
86 | {"verbose", 'v', NULL, 0, | ||
87 | "Log all actions", 0}, | ||
88 | |||
87 | {"email", 'e', "ADDRESS", 0, | 89 | {"email", 'e', "ADDRESS", 0, |
88 | "Override user email address", 0}, | 90 | "Override user email address", 0}, |
89 | 91 | ||
... | @@ -97,6 +99,7 @@ struct options { | ... | @@ -97,6 +99,7 @@ struct options { |
97 | char *tickets; | 99 | char *tickets; |
98 | int debug_level; | 100 | int debug_level; |
99 | int sieve_debug; | 101 | int sieve_debug; |
102 | int verbose; | ||
100 | char *mailer; | 103 | char *mailer; |
101 | char *script; | 104 | char *script; |
102 | }; | 105 | }; |
... | @@ -116,6 +119,7 @@ parser (int key, char *arg, struct argp_state *state) | ... | @@ -116,6 +119,7 @@ parser (int key, char *arg, struct argp_state *state) |
116 | opts->mailer = strdup ("sendmail:"); | 119 | opts->mailer = strdup ("sendmail:"); |
117 | if (!opts->debug_level) | 120 | if (!opts->debug_level) |
118 | opts->debug_level = MU_DEBUG_ERROR; | 121 | opts->debug_level = MU_DEBUG_ERROR; |
122 | log_facility = 0; | ||
119 | break; | 123 | break; |
120 | 124 | ||
121 | case 'e': | 125 | case 'e': |
... | @@ -190,6 +194,10 @@ parser (int key, char *arg, struct argp_state *state) | ... | @@ -190,6 +194,10 @@ parser (int key, char *arg, struct argp_state *state) |
190 | } | 194 | } |
191 | break; | 195 | break; |
192 | 196 | ||
197 | case 'v': | ||
198 | opts->verbose = 1; | ||
199 | break; | ||
200 | |||
193 | case ARGP_KEY_ARG: | 201 | case ARGP_KEY_ARG: |
194 | if (opts->script) | 202 | if (opts->script) |
195 | argp_error (state, "only one SCRIPT can be specified"); | 203 | argp_error (state, "only one SCRIPT can be specified"); |
... | @@ -219,25 +227,42 @@ static const char *sieve_argp_capa[] = | ... | @@ -219,25 +227,42 @@ static const char *sieve_argp_capa[] = |
219 | "common", | 227 | "common", |
220 | "mailbox", | 228 | "mailbox", |
221 | "license", | 229 | "license", |
230 | "logging", | ||
222 | NULL | 231 | NULL |
223 | }; | 232 | }; |
224 | 233 | ||
225 | int | 234 | static int |
226 | sieve_debug_printer (void *unused, const char *fmt, va_list ap) | 235 | sieve_stderr_debug_printer (void *unused, const char *fmt, va_list ap) |
227 | { | 236 | { |
228 | vfprintf (stderr, fmt, ap); | 237 | vfprintf (stderr, fmt, ap); |
229 | return 0; | 238 | return 0; |
230 | } | 239 | } |
231 | 240 | ||
232 | static int | 241 | static int |
233 | debug_print (mu_debug_t unused, size_t level, const char *fmt, va_list ap) | 242 | sieve_syslog_debug_printer (void *unused, const char *fmt, va_list ap) |
243 | { | ||
244 | vsyslog (LOG_DEBUG, fmt, ap); | ||
245 | return 0; | ||
246 | } | ||
247 | |||
248 | static int | ||
249 | stderr_debug_print (mu_debug_t unused, size_t level, const char *fmt, | ||
250 | va_list ap) | ||
234 | { | 251 | { |
235 | vfprintf ((level == MU_DEBUG_ERROR) ? stderr : stdout, fmt, ap); | 252 | vfprintf ((level == MU_DEBUG_ERROR) ? stderr : stdout, fmt, ap); |
236 | return 0; | 253 | return 0; |
237 | } | 254 | } |
238 | 255 | ||
256 | static int | ||
257 | syslog_debug_print (mu_debug_t unused, size_t level, const char *fmt, | ||
258 | va_list ap) | ||
259 | { | ||
260 | vsyslog ((level == MU_DEBUG_ERROR) ? LOG_ERR : LOG_DEBUG, fmt, ap); | ||
261 | return 0; | ||
262 | } | ||
263 | |||
239 | static void | 264 | static void |
240 | action_log (void *unused, | 265 | stdout_action_log (void *unused, |
241 | const char *script, size_t msgno, message_t msg, | 266 | const char *script, size_t msgno, message_t msg, |
242 | const char *action, const char *fmt, va_list ap) | 267 | const char *action, const char *fmt, va_list ap) |
243 | { | 268 | { |
... | @@ -254,6 +279,29 @@ action_log (void *unused, | ... | @@ -254,6 +279,29 @@ action_log (void *unused, |
254 | fprintf (stdout, "\n"); | 279 | fprintf (stdout, "\n"); |
255 | } | 280 | } |
256 | 281 | ||
282 | static void | ||
283 | syslog_action_log (void *unused, | ||
284 | const char *script, size_t msgno, message_t msg, | ||
285 | const char *action, const char *fmt, va_list ap) | ||
286 | { | ||
287 | size_t uid = 0; | ||
288 | char *text = NULL; | ||
289 | |||
290 | message_get_uid (msg, &uid); | ||
291 | |||
292 | asprintf (&text, "%s on msg uid %d", action, uid); | ||
293 | if (fmt && strlen (fmt)) | ||
294 | { | ||
295 | char *diag = NULL; | ||
296 | asprintf (&diag, fmt, ap); | ||
297 | syslog (LOG_NOTICE, "%s: %s", text, diag); | ||
298 | free (diag); | ||
299 | } | ||
300 | else | ||
301 | syslog (LOG_NOTICE, "%s", text); | ||
302 | free (text); | ||
303 | } | ||
304 | |||
257 | int | 305 | int |
258 | main (int argc, char *argv[]) | 306 | main (int argc, char *argv[]) |
259 | { | 307 | { |
... | @@ -265,6 +313,7 @@ main (int argc, char *argv[]) | ... | @@ -265,6 +313,7 @@ main (int argc, char *argv[]) |
265 | mailbox_t mbox = 0; | 313 | mailbox_t mbox = 0; |
266 | int rc; | 314 | int rc; |
267 | struct options opts = {0}; | 315 | struct options opts = {0}; |
316 | int (*debugfp) __P ((mu_debug_t, size_t level, const char *, va_list)); | ||
268 | 317 | ||
269 | rc = mu_argp_parse (&argp, &argc, &argv, ARGP_IN_ORDER, sieve_argp_capa, | 318 | rc = mu_argp_parse (&argp, &argc, &argv, ARGP_IN_ORDER, sieve_argp_capa, |
270 | 0, &opts); | 319 | 0, &opts); |
... | @@ -281,8 +330,23 @@ main (int argc, char *argv[]) | ... | @@ -281,8 +330,23 @@ main (int argc, char *argv[]) |
281 | mu_error ("can't initialize sieve machine: %s", mu_errstring (rc)); | 330 | mu_error ("can't initialize sieve machine: %s", mu_errstring (rc)); |
282 | return 1; | 331 | return 1; |
283 | } | 332 | } |
284 | sieve_set_debug (mach, sieve_debug_printer); | 333 | |
285 | sieve_set_logger (mach, action_log); | 334 | if (log_facility) |
335 | { | ||
336 | openlog ("sieve", LOG_PID, log_facility); | ||
337 | mu_error_set_print (mu_syslog_error_printer); | ||
338 | sieve_set_debug (mach, sieve_syslog_debug_printer); | ||
339 | if (opts.verbose) | ||
340 | sieve_set_logger (mach, syslog_action_log); | ||
341 | debugfp = syslog_debug_print; | ||
342 | } | ||
343 | else | ||
344 | { | ||
345 | sieve_set_debug (mach, sieve_stderr_debug_printer); | ||
346 | if (opts.verbose) | ||
347 | sieve_set_logger (mach, stdout_action_log); | ||
348 | debugfp = stderr_debug_print; | ||
349 | } | ||
286 | 350 | ||
287 | rc = sieve_compile (mach, opts.script); | 351 | rc = sieve_compile (mach, opts.script); |
288 | if (rc) | 352 | if (rc) |
... | @@ -327,7 +391,7 @@ main (int argc, char *argv[]) | ... | @@ -327,7 +391,7 @@ main (int argc, char *argv[]) |
327 | mu_errstring (rc)); | 391 | mu_errstring (rc)); |
328 | goto cleanup; | 392 | goto cleanup; |
329 | } | 393 | } |
330 | if ((rc = mu_debug_set_print (debug, debug_print, mach))) | 394 | if ((rc = mu_debug_set_print (debug, debugfp, mach))) |
331 | { | 395 | { |
332 | mu_error ("mu_debug_set_print failed: %s\n", | 396 | mu_error ("mu_debug_set_print failed: %s\n", |
333 | mu_errstring (rc)); | 397 | mu_errstring (rc)); | ... | ... |
-
Please register or sign in to post a comment