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
This commit is contained in:
wdfk-prog
2026-02-12 08:57:46 +08:00
committed by Rbb666
parent 9412a2899d
commit db6c0ddbf3
3 changed files with 55 additions and 0 deletions

View File

@@ -783,6 +783,13 @@ void rt_components_board_init(void);
#else
int rt_kprintf(const char *fmt, ...);
void rt_kputs(const char *str);
#ifdef RT_USING_CONSOLE_OUTPUT_CTL
void rt_console_output_set_enabled(rt_bool_t enabled);
rt_bool_t rt_console_output_get_enabled(void);
#else
#define rt_console_output_set_enabled(enabled) ((void)0)
#define rt_console_output_get_enabled() (RT_TRUE)
#endif /* RT_USING_CONSOLE_OUTPUT_CTL */
#endif /* RT_USING_CONSOLE */
rt_err_t rt_backtrace(void);

View File

@@ -430,6 +430,18 @@ if RT_USING_CONSOLE
string "the device name for console"
default "uart1"
config RT_USING_CONSOLE_OUTPUT_CTL
bool "Enable runtime console output control"
default y
help
Enable runtime control for console output.
When enabled, rt_console_output_set_enabled() and
rt_console_output_get_enabled() can be used to switch
rt_kputs()/rt_kprintf() output on or off dynamically.
When disabled, these APIs are not compiled as real symbols and
output path has no runtime check, so console output stays enabled.
endif
config RT_VER_NUM

View File

@@ -208,6 +208,32 @@ rt_device_t rt_console_set_device(const char *name)
RTM_EXPORT(rt_console_set_device);
#endif /* RT_USING_DEVICE */
#ifdef RT_USING_CONSOLE_OUTPUT_CTL
static volatile rt_bool_t _console_output_enabled = RT_TRUE;
/**
* @brief Enable or disable console log output.
*
* @param enabled RT_TRUE to enable output, RT_FALSE to disable output.
*/
void rt_console_output_set_enabled(rt_bool_t enabled)
{
_console_output_enabled = enabled;
}
RTM_EXPORT(rt_console_output_set_enabled);
/**
* @brief Get current console log output enable state.
*
* @return RT_TRUE if output is enabled, RT_FALSE otherwise.
*/
rt_bool_t rt_console_output_get_enabled(void)
{
return _console_output_enabled;
}
RTM_EXPORT(rt_console_output_get_enabled);
#endif /* RT_USING_CONSOLE_OUTPUT_CTL */
rt_weak void rt_hw_console_output(const char *str)
{
/* empty console output */
@@ -346,6 +372,11 @@ void rt_kputs(const char *str)
return;
}
if (!rt_console_output_get_enabled())
{
return;
}
_kputs(str, rt_strlen(str));
}
@@ -362,6 +393,11 @@ rt_weak int rt_kprintf(const char *fmt, ...)
rt_size_t length = 0;
static char rt_log_buf[RT_CONSOLEBUF_SIZE];
if (!rt_console_output_get_enabled())
{
return 0;
}
va_start(args, fmt);
PRINTF_BUFFER_TAKE;