Skip to content

Commit fb15bc7

Browse files
committed
feat(console): Add console output enable switch
- rt_console_output_enabled()/rt_console_output_is_enabled() gate rt_kprintf/rt_kputs output with the switch
1 parent 2b95c34 commit fb15bc7

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

include/rtthread.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,8 @@ void rt_components_board_init(void);
783783
#else
784784
int rt_kprintf(const char *fmt, ...);
785785
void rt_kputs(const char *str);
786+
void rt_console_output_set_enabled(rt_bool_t enabled);
787+
rt_bool_t rt_console_output_get_enabled(void);
786788
#endif /* RT_USING_CONSOLE */
787789

788790
rt_err_t rt_backtrace(void);

src/kservice.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,30 @@ rt_device_t rt_console_set_device(const char *name)
208208
RTM_EXPORT(rt_console_set_device);
209209
#endif /* RT_USING_DEVICE */
210210

211+
static volatile rt_bool_t _console_output_enabled = RT_TRUE;
212+
213+
/**
214+
* @brief Enable or disable console log output.
215+
*
216+
* @param enabled RT_TRUE to enable output, RT_FALSE to disable output.
217+
*/
218+
void rt_console_output_set_enabled(rt_bool_t enabled)
219+
{
220+
_console_output_enabled = enabled;
221+
}
222+
RTM_EXPORT(rt_console_output_set_enabled);
223+
224+
/**
225+
* @brief Get current console log output enable state.
226+
*
227+
* @return RT_TRUE if output is enabled, RT_FALSE otherwise.
228+
*/
229+
rt_bool_t rt_console_output_get_enabled(void)
230+
{
231+
return _console_output_enabled;
232+
}
233+
RTM_EXPORT(rt_console_output_get_enabled);
234+
211235
rt_weak void rt_hw_console_output(const char *str)
212236
{
213237
/* empty console output */
@@ -346,6 +370,11 @@ void rt_kputs(const char *str)
346370
return;
347371
}
348372

373+
if (!rt_console_output_get_enabled())
374+
{
375+
return;
376+
}
377+
349378
_kputs(str, rt_strlen(str));
350379
}
351380

@@ -362,6 +391,11 @@ rt_weak int rt_kprintf(const char *fmt, ...)
362391
rt_size_t length = 0;
363392
static char rt_log_buf[RT_CONSOLEBUF_SIZE];
364393

394+
if (!rt_console_output_get_enabled())
395+
{
396+
return 0;
397+
}
398+
365399
va_start(args, fmt);
366400
PRINTF_BUFFER_TAKE;
367401

0 commit comments

Comments
 (0)