Skip to content

Commit d0d6e11

Browse files
dcpleungcarlescufi
authored andcommitted
logging: backend: notify when process thread finishes one cycle
This adds the bits to allow the process thread to notify backends when it has finished processing pending log messages. This allows, for example, flash-based backends to group writing log messages to storage to limit wear, since log_output_flush() is called per message. Also the backend does not have to resort to using timer to periodically flush its buffer to storage, avoiding waking up the device when there are no messages. Another use of this is for backends requiring DMA transfers. Grouping all pending log messages into one DMA transfer is sometimes preferrable. Signed-off-by: Daniel Leung <[email protected]>
1 parent 31c13c8 commit d0d6e11

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

include/zephyr/logging/log_backend.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ struct log_backend;
3131
* @brief Backend events
3232
*/
3333
enum log_backend_evt {
34+
/**
35+
* @brief Event when process thread finishes processing.
36+
*
37+
* This event is emitted when the process thread finishes
38+
* processing pending log messages.
39+
*
40+
* @note This is not emitted when there are no pending
41+
* log messages being processed.
42+
*
43+
* @note Deferred mode only.
44+
*/
45+
LOG_BACKEND_EVT_PROCESS_THREAD_DONE,
46+
3447
/** @brief Maximum number of backend events */
3548
LOG_BACKEND_EVT_MAX,
3649
};

subsys/logging/log_core.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,16 @@ int log_mem_get_max_usage(uint32_t *max)
624624
return mpsc_pbuf_get_max_utilization(&log_buffer, max);
625625
}
626626

627+
static void log_backend_notify_all(enum log_backend_evt event,
628+
union log_backend_evt_arg *arg)
629+
{
630+
for (int i = 0; i < log_backend_count_get(); i++) {
631+
const struct log_backend *backend = log_backend_get(i);
632+
633+
log_backend_notify(backend, event, arg);
634+
}
635+
}
636+
627637
static void log_process_thread_timer_expiry_fn(struct k_timer *timer)
628638
{
629639
k_sem_give(&log_process_thread_sem);
@@ -635,6 +645,7 @@ static void log_process_thread_func(void *dummy1, void *dummy2, void *dummy3)
635645

636646
uint32_t activate_mask = z_log_init(false, false);
637647
k_timeout_t timeout = K_MSEC(50); /* Arbitrary value */
648+
bool processed_any = false;
638649

639650
thread_set(k_current_get());
640651

@@ -650,7 +661,13 @@ static void log_process_thread_func(void *dummy1, void *dummy2, void *dummy3)
650661
}
651662

652663
if (log_process() == false) {
664+
if (processed_any) {
665+
processed_any = false;
666+
log_backend_notify_all(LOG_BACKEND_EVT_PROCESS_THREAD_DONE, NULL);
667+
}
653668
(void)k_sem_take(&log_process_thread_sem, timeout);
669+
} else {
670+
processed_any = true;
654671
}
655672
}
656673
}

0 commit comments

Comments
 (0)