This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit f2403405e250898e3e95241e04f9ef5f70e3d93a Author: xuxingliang <[email protected]> AuthorDate: Wed Dec 24 18:05:24 2025 +0800 syslog: add option to use syslog default only in panic With CONFIG_SYSLOG_DEFAULT_PANIC_ONLY the default channel, which writes through up_putc(), starts disabled (SYSLOG_CHANNEL_DISABLE in sc_state), and a panic notifier enables it when an assertion is reported, so the console shows the crash output while the other channels get all messages as before. The console drop mask of SYSLOG_CONSOLE_LEVEL is now given to sc_dropmask by name instead of after a placeholder sc_state, since with both options the initializer would otherwise get two values for sc_state. Assisted-by: Kiro:claude-opus-5-5 Signed-off-by: xuxingliang <[email protected]> --- drivers/syslog/Kconfig | 12 +++++++++++ drivers/syslog/syslog.h | 9 ++++++++ drivers/syslog/syslog_channel.c | 42 ++++++++++++++++++++++++++++++++++---- drivers/syslog/syslog_initialize.c | 4 ++++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/drivers/syslog/Kconfig b/drivers/syslog/Kconfig index 2cbaf69d889..4ff808c1ace 100644 --- a/drivers/syslog/Kconfig +++ b/drivers/syslog/Kconfig @@ -444,6 +444,18 @@ config RAMLOG_BUFSIZE endif # RAMLOG_SYSLOG +if SYSLOG_DEFAULT + +config SYSLOG_DEFAULT_PANIC_ONLY + bool "Use syslog default channel for panic only" + default n + depends on SYSLOG_IOCTL + ---help--- + If enabled, the default SYSLOG device will be used only + when panic() is called. It is disabled by default. + +endif # SYSLOG_DEFAULT + if SYSLOG_RPMSG config SYSLOG_RPMSG_SERVER_NAME diff --git a/drivers/syslog/syslog.h b/drivers/syslog/syslog.h index f77417e62fc..f13ea702fa5 100644 --- a/drivers/syslog/syslog.h +++ b/drivers/syslog/syslog.h @@ -324,6 +324,15 @@ void syslog_flush_intbuffer(bool force); ssize_t syslog_write_foreach(int priority, FAR const char *buffer, size_t buflen, bool force); + +#ifdef CONFIG_SYSLOG_DEFAULT_PANIC_ONLY +/**************************************************************************** + * Name: syslog_panic_notifier_register + ****************************************************************************/ + +void syslog_panic_notifier_register(void); +#endif + #endif /* CONFIG_SYSLOG */ #ifdef __cplusplus diff --git a/drivers/syslog/syslog_channel.c b/drivers/syslog/syslog_channel.c index b6642b45113..aadd775da1d 100644 --- a/drivers/syslog/syslog_channel.c +++ b/drivers/syslog/syslog_channel.c @@ -55,6 +55,10 @@ # include <nuttx/arch.h> #endif +#ifdef CONFIG_SYSLOG_DEFAULT_PANIC_ONLY +# include <nuttx/panic_notifier.h> +#endif + #include "syslog.h" /**************************************************************************** @@ -68,6 +72,12 @@ static ssize_t syslog_default_write(FAR syslog_channel_t *channel, FAR const char *buffer, size_t buflen); #endif +#ifdef CONFIG_SYSLOG_DEFAULT_PANIC_ONLY +static int syslog_default_panic_notifier(FAR struct notifier_block *nb, + unsigned long event, + FAR void *data); +#endif + #ifdef CONFIG_SYSLOG_CDCACM static int syslog_cdcacm_putc(FAR syslog_channel_t *channel, int ch); @@ -179,13 +189,22 @@ static syslog_channel_t g_default_channel = # ifdef CONFIG_SYSLOG_IOCTL , "default" # endif + +# ifdef CONFIG_SYSLOG_DEFAULT_PANIC_ONLY + , true +# endif # ifdef CONFIG_SYSLOG_CONSOLE_LEVEL -# if defined(CONFIG_SYSLOG_IOCTL) || defined(CONFIG_SYSLOG_CRLF) - , 0 -# endif - , SYSLOG_CONSOLE_DROPMASK + , .sc_dropmask = SYSLOG_CONSOLE_DROPMASK # endif }; + +# ifdef CONFIG_SYSLOG_DEFAULT_PANIC_ONLY +static struct notifier_block g_default_channel_nb = +{ + .notifier_call = syslog_default_panic_notifier, + .priority = INT_MAX - 100, +}; +# endif #endif /* This is a simply sanity check to avoid we have more elements than the @@ -253,6 +272,14 @@ g_syslog_channel[CONFIG_SYSLOG_MAX_CHANNELS] = /**************************************************************************** * Private Functions ****************************************************************************/ +#ifdef CONFIG_SYSLOG_DEFAULT_PANIC_ONLY +static int syslog_default_panic_notifier(FAR struct notifier_block *nb, + unsigned long event, FAR void *data) +{ + g_default_channel.sc_state = false; + return OK; +} +#endif #if defined(CONFIG_SYSLOG_DEFAULT) && defined(CONFIG_ARCH_LOWPUTC) @@ -476,3 +503,10 @@ int syslog_channel_unregister(FAR syslog_channel_t *channel) return -EINVAL; } #endif + +#ifdef CONFIG_SYSLOG_DEFAULT_PANIC_ONLY +void syslog_panic_notifier_register(void) +{ + panic_notifier_chain_register(&g_default_channel_nb); +} +#endif diff --git a/drivers/syslog/syslog_initialize.c b/drivers/syslog/syslog_initialize.c index eca3697fb47..4d293507f13 100644 --- a/drivers/syslog/syslog_initialize.c +++ b/drivers/syslog/syslog_initialize.c @@ -71,6 +71,10 @@ int syslog_initialize(void) { int ret = OK; +#ifdef CONFIG_SYSLOG_DEFAULT_PANIC_ONLY + syslog_panic_notifier_register(); +#endif + #if defined(CONFIG_SYSLOG_CHAR) /* Enable use of a character device as the SYSLOG device */
