Commit 05df35d0 05df35d07a46f70e7d09a4da39fdd2d7a7d58ed0 by Sergey Poznyakoff

sieve: simplify action logging API

* include/mailutils/sieve.h (mu_sieve_action_log_t): Change proto.
* lib/sieve.c (_sieve_action_log): Rewrite.
* libmu_sieve/util.c (mu_sieve_log_action): Rewrite.
* python/libmu_py/sieve.c (_sieve_action_printer): Rewrite.
* sieve/sieve.c (_sieve_action_log): Rewrite.
1 parent a4c1a0e8
...@@ -35,9 +35,7 @@ typedef struct mu_sieve_machine *mu_sieve_machine_t; ...@@ -35,9 +35,7 @@ typedef struct mu_sieve_machine *mu_sieve_machine_t;
35 35
36 typedef int (*mu_sieve_handler_t) (mu_sieve_machine_t mach, 36 typedef int (*mu_sieve_handler_t) (mu_sieve_machine_t mach,
37 mu_list_t args, mu_list_t tags); 37 mu_list_t args, mu_list_t tags);
38 typedef void (*mu_sieve_action_log_t) (void *data, 38 typedef void (*mu_sieve_action_log_t) (mu_sieve_machine_t mach,
39 mu_stream_t stream,
40 size_t msgno, mu_message_t msg,
41 const char *action, 39 const char *action,
42 const char *fmt, va_list ap); 40 const char *fmt, va_list ap);
43 41
......
...@@ -28,15 +28,17 @@ struct sieve_log_data ...@@ -28,15 +28,17 @@ struct sieve_log_data
28 }; 28 };
29 29
30 static void 30 static void
31 _sieve_action_log (void *data, 31 _sieve_action_log (mu_sieve_machine_t mach,
32 mu_stream_t stream, size_t msgno,
33 mu_message_t msg,
34 const char *action, const char *fmt, va_list ap) 32 const char *action, const char *fmt, va_list ap)
35 { 33 {
36 struct sieve_log_data *ldat = data; 34 struct sieve_log_data *ldat = mu_sieve_get_data (mach);
37 int pfx = 0; 35 int pfx = 0;
36 mu_stream_t stream;
37 mu_message_t msg = mu_sieve_get_message (mach);
38 38
39 mu_sieve_get_diag_stream (mach, &stream);
39 mu_stream_printf (stream, "\033s<%d>", MU_LOG_NOTICE); 40 mu_stream_printf (stream, "\033s<%d>", MU_LOG_NOTICE);
41
40 if (ldat) 42 if (ldat)
41 { 43 {
42 if (ldat->user) 44 if (ldat->user)
...@@ -70,6 +72,7 @@ _sieve_action_log (void *data, ...@@ -70,6 +72,7 @@ _sieve_action_log (void *data,
70 mu_stream_vprintf (stream, fmt, ap); 72 mu_stream_vprintf (stream, fmt, ap);
71 } 73 }
72 mu_stream_printf (stream, "\n"); 74 mu_stream_printf (stream, "\n");
75 mu_stream_unref (stream);
73 } 76 }
74 77
75 static int 78 static int
......
...@@ -344,16 +344,11 @@ mu_sieve_log_action (mu_sieve_machine_t mach, const char *action, ...@@ -344,16 +344,11 @@ mu_sieve_log_action (mu_sieve_machine_t mach, const char *action,
344 344
345 if (!mach->logger) 345 if (!mach->logger)
346 return; 346 return;
347
348 mu_stream_ioctl (mach->errstream, MU_IOCTL_LOGSTREAM,
349 MU_IOCTL_LOGSTREAM_SET_LOCUS, &mach->locus);
347 va_start (ap, fmt); 350 va_start (ap, fmt);
348 mu_stream_printf (mach->errstream, "\033s<%d>", MU_LOG_INFO); 351 mach->logger (mach, action, fmt, ap);
349 if (mach->locus.mu_file)
350 mu_stream_printf (mach->errstream, "\033O<%d>\033f<%u>%s\033l<%u>",
351 MU_LOGMODE_LOCUS,
352 (unsigned) strlen (mach->locus.mu_file),
353 mach->locus.mu_file,
354 mach->locus.mu_line);
355 mach->logger (mach->data, mach->errstream, mach->msgno, mach->msg,
356 action, fmt, ap);
357 va_end (ap); 352 va_end (ap);
358 } 353 }
359 354
......
...@@ -236,8 +236,7 @@ api_sieve_message (PyObject *self, PyObject *args) ...@@ -236,8 +236,7 @@ api_sieve_message (PyObject *self, PyObject *args)
236 } 236 }
237 237
238 static void 238 static void
239 _sieve_action_printer (void *data, mu_stream_t stream, 239 _sieve_action_printer (mu_sieve_machine_t mach,
240 size_t msgno, mu_message_t msg,
241 const char *action, const char *fmt, va_list ap) 240 const char *action, const char *fmt, va_list ap)
242 { 241 {
243 PyObject *py_args; 242 PyObject *py_args;
...@@ -258,10 +257,12 @@ _sieve_action_printer (void *data, mu_stream_t stream, ...@@ -258,10 +257,12 @@ _sieve_action_printer (void *data, mu_stream_t stream,
258 PyStream *py_stm = PyStream_NEW (); 257 PyStream *py_stm = PyStream_NEW ();
259 if (py_stm) 258 if (py_stm)
260 { 259 {
261 py_stm->stm = stream; 260 size_t msgno;
262 mu_stream_ref (stream);
263 261
264 py_msg->msg = msg; 262 mu_sieve_get_diag_stream (mach, &py_stm->stm);
263 msgno = mu_sieve_get_message_num (mach);
264
265 py_msg->msg = mu_sieve_get_message (mach);
265 Py_INCREF (py_msg); 266 Py_INCREF (py_msg);
266 267
267 PyDict_SetItemString (py_dict, "msgno", 268 PyDict_SetItemString (py_dict, "msgno",
...@@ -272,7 +273,7 @@ _sieve_action_printer (void *data, mu_stream_t stream, ...@@ -272,7 +273,7 @@ _sieve_action_printer (void *data, mu_stream_t stream,
272 273
273 if (mu_vasnprintf (&buf, &buflen, fmt, ap)) 274 if (mu_vasnprintf (&buf, &buflen, fmt, ap))
274 { 275 {
275 mu_stream_unref (stream); 276 mu_stream_destroy (&py_stm->stm);
276 return; 277 return;
277 } 278 }
278 PyDict_SetItemString (py_dict, "text", 279 PyDict_SetItemString (py_dict, "text",
...@@ -282,7 +283,7 @@ _sieve_action_printer (void *data, mu_stream_t stream, ...@@ -282,7 +283,7 @@ _sieve_action_printer (void *data, mu_stream_t stream,
282 py_args = PyTuple_New (1); 283 py_args = PyTuple_New (1);
283 if (py_args) 284 if (py_args)
284 { 285 {
285 struct _mu_py_sieve_logger *s = data; 286 struct _mu_py_sieve_logger *s = mu_sieve_get_data (mach);
286 PyObject *py_fnc = s->py_action_printer; 287 PyObject *py_fnc = s->py_action_printer;
287 288
288 Py_INCREF (py_dict); 289 Py_INCREF (py_dict);
......
...@@ -264,12 +264,15 @@ Compatibility debug flags:\n\ ...@@ -264,12 +264,15 @@ Compatibility debug flags:\n\
264 }; 264 };
265 265
266 static void 266 static void
267 _sieve_action_log (void *unused, 267 _sieve_action_log (mu_sieve_machine_t mach,
268 mu_stream_t stream, size_t msgno,
269 mu_message_t msg,
270 const char *action, const char *fmt, va_list ap) 268 const char *action, const char *fmt, va_list ap)
271 { 269 {
272 size_t uid = 0; 270 size_t uid = 0;
271 mu_message_t msg;
272 mu_stream_t stream;
273
274 mu_sieve_get_diag_stream (mach, &stream);
275 msg = mu_sieve_get_message (mach);
273 276
274 mu_message_get_uid (msg, &uid); 277 mu_message_get_uid (msg, &uid);
275 mu_stream_printf (stream, "\033s<%d>\033%c<%d>", MU_LOG_NOTICE, 278 mu_stream_printf (stream, "\033s<%d>\033%c<%d>", MU_LOG_NOTICE,
...@@ -283,6 +286,8 @@ _sieve_action_log (void *unused, ...@@ -283,6 +286,8 @@ _sieve_action_log (void *unused,
283 mu_stream_vprintf (stream, fmt, ap); 286 mu_stream_vprintf (stream, fmt, ap);
284 } 287 }
285 mu_stream_printf (stream, "\n"); 288 mu_stream_printf (stream, "\n");
289
290 mu_stream_unref (stream);
286 } 291 }
287 292
288 static int 293 static int
......