@@ -405,6 +405,19 @@ event_t *event_wait_timeout_ztimer(event_queue_t *queue,
405405 ztimer_clock_t * clock , uint32_t timeout );
406406#endif
407407
408+ /**
409+ * @brief Threshold in microseconds below which event_handler
410+ * are not printed while debugging.
411+ *
412+ * This can be used for debugging event loops. Setting a value
413+ * other than zero will also remove the event_handler start message.
414+ *
415+ * Use this to prevent *a lot* of output when debugging.
416+ */
417+ #ifndef CONFIG_EVENT_LOOP_DEBUG_THRESHOLD_US
418+ # define CONFIG_EVENT_LOOP_DEBUG_THRESHOLD_US (0)
419+ #endif
420+
408421/**
409422 * @brief Simple event loop with multiple queues
410423 *
@@ -438,21 +451,28 @@ static inline void event_loop_multi(event_queue_t *queues, size_t n_queues)
438451 while (1 ) {
439452 event_t * event = event_wait_multi (queues , n_queues );
440453 if (IS_USED (MODULE_EVENT_LOOP_DEBUG )) {
441- uint32_t now ;
442454 ztimer_acquire (ZTIMER_USEC );
443455
444456 void _event_callback_handler (event_t * event );
445- if (!IS_USED (MODULE_EVENT_CALLBACK ) ||
446- event -> handler != _event_callback_handler ) {
457+ if ((CONFIG_EVENT_LOOP_DEBUG_THRESHOLD_US == 0 ) &&
458+ (!IS_USED (MODULE_EVENT_CALLBACK ) ||
459+ event -> handler != _event_callback_handler )) {
447460 printf ("event: executing %p->%p\n" ,
448461 (void * )event , (void * )(uintptr_t )event -> handler );
449462 }
450- now = ztimer_now (ZTIMER_USEC );
463+ uint32_t now = ztimer_now (ZTIMER_USEC );
464+ /* events might modify themself */
465+ void * handler_ptr = event -> handler ;
451466
452467 event -> handler (event );
453468
454- printf ("event: %p took %" PRIu32 " µs\n" ,
455- (void * )event , ztimer_now (ZTIMER_USEC ) - now );
469+ uint32_t dt = ztimer_now (ZTIMER_USEC ) - now ;
470+
471+ if ((CONFIG_EVENT_LOOP_DEBUG_THRESHOLD_US == 0 ) ||
472+ dt > CONFIG_EVENT_LOOP_DEBUG_THRESHOLD_US ) {
473+ printf ("event: %p->%p ran: %" PRIu32 " usec\n" ,
474+ (void * )event , handler_ptr , dt );
475+ }
456476 ztimer_release (ZTIMER_USEC );
457477 }
458478 else {
0 commit comments