summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/rtthread.h7
-rw-r--r--src/Kconfig12
-rw-r--r--src/kservice.c36
3 files changed, 55 insertions, 0 deletions
diff --git a/include/rtthread.h b/include/rtthread.h
index 7f97c068cd..a87515907e 100644
--- a/include/rtthread.h
+++ b/include/rtthread.h
@@ -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);
diff --git a/src/Kconfig b/src/Kconfig
index f57b305ca2..d278169da0 100644
--- a/src/Kconfig
+++ b/src/Kconfig
@@ -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
diff --git a/src/kservice.c b/src/kservice.c
index 259eac9a70..29c9022c89 100644
--- a/src/kservice.c
+++ b/src/kservice.c
@@ -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;