Skip to content

Commit 5de7e41

Browse files
Tom Burdickkartben
authored andcommitted
logging: Add a log flush operation
Ensure all pending log messages are processed by the log processing thread when log_flush is called, blocking the caller until done. Signed-off-by: Tom Burdick <[email protected]>
1 parent 44d5d8a commit 5de7e41

File tree

4 files changed

+38
-68
lines changed

4 files changed

+38
-68
lines changed

include/zephyr/logging/log_ctrl.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,17 @@ __syscall void log_panic(void);
9595
*/
9696
__syscall bool log_process(void);
9797

98+
/**
99+
* @brief Process all pending log messages
100+
*/
101+
#ifdef CONFIG_LOG_MODE_DEFERRED
102+
void log_flush(void);
103+
#else
104+
static inline void log_flush(void)
105+
{
106+
}
107+
#endif
108+
98109
/**
99110
* @brief Return number of buffered log messages.
100111
*

subsys/llext/llext.c

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -203,60 +203,17 @@ int llext_load(struct llext_loader *ldr, const char *name, struct llext **ext,
203203

204204
#include <zephyr/logging/log_ctrl.h>
205205

206-
static void llext_log_flush(void)
207-
{
208-
#ifdef CONFIG_LOG_MODE_DEFERRED
209-
extern struct k_thread logging_thread;
210-
int cur_prio = k_thread_priority_get(k_current_get());
211-
int log_prio = k_thread_priority_get(&logging_thread);
212-
int target_prio;
213-
bool adjust_cur, adjust_log;
214-
215-
/*
216-
* Our goal is to raise the logger thread priority above current, but if
217-
* current has the highest possble priority, both need to be adjusted,
218-
* particularly if the logger thread has the lowest possible priority
219-
*/
220-
if (log_prio < cur_prio) {
221-
adjust_cur = false;
222-
adjust_log = false;
223-
target_prio = 0;
224-
} else if (cur_prio == K_HIGHEST_THREAD_PRIO) {
225-
adjust_cur = true;
226-
adjust_log = true;
227-
target_prio = cur_prio;
228-
k_thread_priority_set(k_current_get(), cur_prio + 1);
229-
} else {
230-
adjust_cur = false;
231-
adjust_log = true;
232-
target_prio = cur_prio - 1;
233-
}
234-
235-
/* adjust logging thread priority if needed */
236-
if (adjust_log) {
237-
k_thread_priority_set(&logging_thread, target_prio);
238-
}
239-
240-
log_thread_trigger();
241-
k_yield();
242-
243-
if (adjust_log) {
244-
k_thread_priority_set(&logging_thread, log_prio);
245-
}
246-
if (adjust_cur) {
247-
k_thread_priority_set(&logging_thread, cur_prio);
248-
}
249-
#endif
250-
}
251-
252206
int llext_unload(struct llext **ext)
253207
{
254208
__ASSERT(*ext, "Expected non-null extension");
255209
struct llext *tmp = *ext;
256210

257-
k_mutex_lock(&llext_lock, K_FOREVER);
211+
/* Flush pending log messages, as the deferred formatting may be referencing
212+
* strings/args in the extension we are about to unload
213+
*/
214+
log_flush();
258215

259-
llext_log_flush();
216+
k_mutex_lock(&llext_lock, K_FOREVER);
260217

261218
__ASSERT(tmp->use_count, "A valid LLEXT cannot have a zero use-count!");
262219

subsys/logging/log_core.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -992,4 +992,19 @@ static int enable_logger(void)
992992
return 0;
993993
}
994994

995+
#ifdef CONFIG_LOG_MODE_DEFERRED
996+
void log_flush(void)
997+
{
998+
if (IS_ENABLED(CONFIG_LOG_PROCESS_THREAD)) {
999+
while (log_data_pending()) {
1000+
k_sleep(K_MSEC(10));
1001+
}
1002+
k_sleep(K_MSEC(10));
1003+
} else {
1004+
while (LOG_PROCESS()) {
1005+
}
1006+
}
1007+
}
1008+
#endif
1009+
9951010
SYS_INIT(enable_logger, POST_KERNEL, CONFIG_LOG_CORE_INIT_PRIORITY);

subsys/testsuite/ztest/src/ztest.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -934,19 +934,6 @@ static void __ztest_init_unit_test_result_for_suite(struct ztest_suite_node *sui
934934
}
935935
}
936936

937-
static void flush_log(void)
938-
{
939-
if (IS_ENABLED(CONFIG_LOG_PROCESS_THREAD)) {
940-
while (log_data_pending()) {
941-
k_sleep(K_MSEC(10));
942-
}
943-
k_sleep(K_MSEC(10));
944-
} else {
945-
while (LOG_PROCESS()) {
946-
}
947-
}
948-
}
949-
950937
/* Show one line summary for a test suite.
951938
*/
952939
static void __ztest_show_suite_summary_oneline(struct ztest_suite_node *suite)
@@ -994,7 +981,7 @@ static void __ztest_show_suite_summary_oneline(struct ztest_suite_node *suite)
994981
TC_RESULT_TO_STR(suite_result), passrate_major, passrate_minor,
995982
suite->name, distinct_pass, distinct_fail, distinct_skip, distinct_total,
996983
suite_duration_worst_ms / 1000, suite_duration_worst_ms % 1000);
997-
flush_log();
984+
log_flush();
998985
}
999986

1000987
static void __ztest_show_suite_summary_verbose(struct ztest_suite_node *suite)
@@ -1035,12 +1022,12 @@ static void __ztest_show_suite_summary_verbose(struct ztest_suite_node *suite)
10351022

10361023
if (flush_frequency % 3 == 0) {
10371024
/** Reduce the flush frequency a bit to speed up the output */
1038-
flush_log();
1025+
log_flush();
10391026
}
10401027
flush_frequency++;
10411028
}
10421029
TC_SUMMARY_PRINT("\n");
1043-
flush_log();
1030+
log_flush();
10441031
}
10451032

10461033
static void __ztest_show_suite_summary(void)
@@ -1051,17 +1038,17 @@ static void __ztest_show_suite_summary(void)
10511038
/* Flush the log a lot to ensure that no summary content
10521039
* is dropped if it goes through the logging subsystem.
10531040
*/
1054-
flush_log();
1041+
log_flush();
10551042
TC_SUMMARY_PRINT("\n------ TESTSUITE SUMMARY START ------\n\n");
1056-
flush_log();
1043+
log_flush();
10571044
for (struct ztest_suite_node *ptr = _ztest_suite_node_list_start;
10581045
ptr < _ztest_suite_node_list_end; ++ptr) {
10591046

10601047
__ztest_show_suite_summary_oneline(ptr);
10611048
__ztest_show_suite_summary_verbose(ptr);
10621049
}
10631050
TC_SUMMARY_PRINT("------ TESTSUITE SUMMARY END ------\n\n");
1064-
flush_log();
1051+
log_flush();
10651052
}
10661053

10671054
static int __ztest_run_test_suite(struct ztest_suite_node *ptr, const void *state, bool shuffle,
@@ -1474,7 +1461,7 @@ int main(void)
14741461
#ifndef CONFIG_ZTEST_SHELL
14751462
test_main();
14761463
end_report();
1477-
flush_log();
1464+
log_flush();
14781465
LOG_PANIC();
14791466
if (IS_ENABLED(CONFIG_ZTEST_RETEST_IF_PASSED)) {
14801467
static __noinit struct {

0 commit comments

Comments
 (0)