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
The following commit(s) were added to refs/heads/master by this push:
new 10998bb4167 drivers/syslog: Make openlog() ident optional
10998bb4167 is described below
commit 10998bb41679dc8b4500bab41e19e4aadd665e5f
Author: jingfei <[email protected]>
AuthorDate: Fri Nov 13 23:49:40 2026 +0800
drivers/syslog: Make openlog() ident optional
The openlog() support keeps a 32-byte ident and the options in every
task group and formats them into each syslog header, which costs about
300 bytes of flash on small targets. Calling openlog() is optional in
POSIX, and without it the messages carry no ident.
Add SYSLOG_OPENLOG, defaulting to !DEFAULT_SMALL. When it is off,
openlog() and closelog() remain as functions that do nothing, so
programs still build and link.
stm32f103-minimum:nsh: flash -304 bytes, bss -40 bytes.
Signed-off-by: jingfei <[email protected]>
---
Documentation/reference/user/14_logging.rst | 4 +++-
drivers/syslog/Kconfig | 11 +++++++++++
drivers/syslog/syslog.h | 2 ++
drivers/syslog/vsyslog.c | 15 ++++++++++++++-
drivers/syslog/vsyslog_rfc5424.c | 8 ++++++++
include/nuttx/tls.h | 4 ++++
include/syslog.h | 7 ++++---
libs/libc/syslog/lib_openlog.c | 6 ++++++
libs/libc/syslog/lib_syslog.c | 4 ++--
9 files changed, 54 insertions(+), 7 deletions(-)
diff --git a/Documentation/reference/user/14_logging.rst
b/Documentation/reference/user/14_logging.rst
index 51abe09156a..4078da3a1c9 100644
--- a/Documentation/reference/user/14_logging.rst
+++ b/Documentation/reference/user/14_logging.rst
@@ -14,7 +14,9 @@ interface.
message, ``ident[pid]: `` with ``LOG_PID``, and writes the message to
stderr too with ``LOG_PERROR``. The messages that the OS prints while the
process runs get the same prefix. NuttX does not use the other options or
- the facility. :c:func:`closelog` has no descriptor to close.
+ the facility. :c:func:`closelog` has no descriptor to close. Without
+ ``CONFIG_SYSLOG_OPENLOG`` (off by default with ``CONFIG_DEFAULT_SMALL``),
+ :c:func:`openlog` does nothing and the messages have no ident.
.. c:function:: void syslog(int priority, const char *fmt, ...)
diff --git a/drivers/syslog/Kconfig b/drivers/syslog/Kconfig
index 9a71a41b969..ae34e1f7b9a 100644
--- a/drivers/syslog/Kconfig
+++ b/drivers/syslog/Kconfig
@@ -77,6 +77,17 @@ config SYSLOG_DEFAULT_MASK
hex "syslog mask default value"
default 0xff
+config SYSLOG_OPENLOG
+ bool "openlog() ident and options"
+ default !DEFAULT_SMALL
+ depends on SYSLOG
+ ---help---
+ Keep the ident and the options that openlog() sets, and put
+ "ident: " ("ident[pid]: " with LOG_PID) before the messages of
the
+ process, and copy them to stderr with LOG_PERROR. If disabled,
+ openlog() and closelog() still exist but do nothing, and the
+ messages have no ident, as when a program never calls openlog().
+
if SYSLOG
comment "SYSLOG options"
diff --git a/drivers/syslog/syslog.h b/drivers/syslog/syslog.h
index e6d65b44f80..f77417e62fc 100644
--- a/drivers/syslog/syslog.h
+++ b/drivers/syslog/syslog.h
@@ -91,6 +91,7 @@ g_syslog_channel[CONFIG_SYSLOG_MAX_CHANNELS];
*
****************************************************************************/
+#ifdef CONFIG_SYSLOG_OPENLOG
static inline FAR const char *syslog_ident(FAR char *tail, size_t size)
{
FAR struct task_info_s *info;
@@ -124,6 +125,7 @@ static inline FAR const char *syslog_ident(FAR char *tail,
size_t size)
return info->ta_openlog.ol_ident;
}
+#endif
/****************************************************************************
* Public Function Prototypes
diff --git a/drivers/syslog/vsyslog.c b/drivers/syslog/vsyslog.c
index d7d95d3f74f..5c8bbc831f3 100644
--- a/drivers/syslog/vsyslog.c
+++ b/drivers/syslog/vsyslog.c
@@ -87,9 +87,11 @@ static FAR const char * const g_priority_str[] =
int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap)
{
struct lib_syslograwstream_s stream;
+#ifdef CONFIG_SYSLOG_OPENLOG
FAR const char *ident;
char tail[16];
- int ret;
+#endif
+ int ret = 0;
#ifdef CONFIG_SYSLOG_PROCESS_NAME
FAR struct tcb_s *tcb = nxsched_self();
#endif
@@ -158,9 +160,15 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR
va_list *ap)
# endif
#endif
+#if defined(CONFIG_SYSLOG_COLOR_OUTPUT) || defined(CONFIG_SYSLOG_TIMESTAMP) ||
\
+ defined(CONFIG_SMP) || defined(CONFIG_SYSLOG_PROCESSID) || \
+ defined(CONFIG_SYSLOG_PRIORITY) || defined(CONFIG_SYSLOG_PREFIX) || \
+ defined(CONFIG_SYSLOG_PROCESS_NAME) || defined(CONFIG_SYSLOG_OPENLOG)
+# ifdef CONFIG_SYSLOG_OPENLOG
/* The ident and the process ID of openlog() */
ident = syslog_ident(tail, sizeof(tail));
+# endif
ret = lib_sprintf_internal(&stream.common,
#if defined(CONFIG_SYSLOG_COLOR_OUTPUT)
@@ -218,9 +226,11 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR
va_list *ap)
"%s: "
#endif
+#ifdef CONFIG_SYSLOG_OPENLOG
/* Prepend the ident of openlog() */
"%.*s%s"
+#endif
#ifdef CONFIG_SYSLOG_TIMESTAMP
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMATTED)
# if defined(CONFIG_SYSLOG_TIMESTAMP_FORMAT_MICROSECOND)
@@ -273,10 +283,13 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt,
FAR va_list *ap)
, get_task_name(tcb)
#endif
+#ifdef CONFIG_SYSLOG_OPENLOG
/* Prepend the ident of openlog() */
, OPENLOG_IDENT_SIZE - 1, ident, tail
+#endif
);
+#endif
/* Generate the output */
diff --git a/drivers/syslog/vsyslog_rfc5424.c b/drivers/syslog/vsyslog_rfc5424.c
index 08761625019..4d9074612fe 100644
--- a/drivers/syslog/vsyslog_rfc5424.c
+++ b/drivers/syslog/vsyslog_rfc5424.c
@@ -105,8 +105,10 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR
va_list *ap)
#ifdef CONFIG_SYSLOG_RFC5424_HOSTNAME
char hostname_buf[HOST_NAME_MAX + 1];
#endif
+#ifdef CONFIG_SYSLOG_OPENLOG
FAR const char *ident;
char tail[16];
+#endif
/* Wrap the low-level output in a stream object and let lib_vsprintf
* do the work.
@@ -156,9 +158,11 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR
va_list *ap)
}
#endif
+#ifdef CONFIG_SYSLOG_OPENLOG
/* The ident and the process ID of openlog() start the MSG */
ident = syslog_ident(tail, sizeof(tail));
+#endif
/* Output the RFC5424 header */
@@ -202,7 +206,9 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR
va_list *ap)
#if HAVE_RFC5424_SDATA
" " /* Space at the end of structured data before message */
#endif
+#ifdef CONFIG_SYSLOG_OPENLOG
"%.*s%s" /* Ident of openlog() */
+#endif
/* Beginning of formatted arguments */
@@ -228,9 +234,11 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR
va_list *ap)
, 0 /* TODO: Not sure if time zone known */
#endif
+#ifdef CONFIG_SYSLOG_OPENLOG
/* Ident of openlog() */
, OPENLOG_IDENT_SIZE - 1, ident, tail
+#endif
/* End of formatted arguments */
diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h
index d14c0897a98..32ae6a206b1 100644
--- a/include/nuttx/tls.h
+++ b/include/nuttx/tls.h
@@ -136,6 +136,7 @@ struct usershell_s
};
#endif
+#ifdef CONFIG_SYSLOG_OPENLOG
/* This structure holds the settings of openlog() */
#define OPENLOG_IDENT_SIZE 32
@@ -146,6 +147,7 @@ struct openlog_s
int ol_option; /* LOG_PID, LOG_PERROR ... */
int ol_facility; /* Default facility */
};
+#endif
#ifdef CONFIG_PTHREAD_ATFORK
/* This structure defines the pthread_atfork_s, which is used to manage
@@ -188,7 +190,9 @@ struct task_info_s
* libc at the first use
*/
#endif
+#ifdef CONFIG_SYSLOG_OPENLOG
struct openlog_s ta_openlog; /* Settings of openlog() */
+#endif
#if CONFIG_LIBC_MAX_EXITFUNS > 0
struct atexit_list_s ta_exit; /* Exit functions */
#endif
diff --git a/include/syslog.h b/include/syslog.h
index 46ea81cf17b..5dcae570093 100644
--- a/include/syslog.h
+++ b/include/syslog.h
@@ -174,9 +174,10 @@ extern "C"
*
* It is not necessary to call openlog() prior to calling syslog().
*
- * NuttX puts the ident (up to 31 characters) and, with LOG_PID, the
- * process ID before each message, and writes the messages to stderr too
- * with LOG_PERROR. It does not use the other options or the facility.
+ * With CONFIG_SYSLOG_OPENLOG, NuttX puts the ident (up to 31
+ * characters) and, with LOG_PID, the process ID before each message, and
+ * writes the messages to stderr too with LOG_PERROR. It does not use
+ * the other options or the facility. Without it, openlog() does nothing.
*
****************************************************************************/
diff --git a/libs/libc/syslog/lib_openlog.c b/libs/libc/syslog/lib_openlog.c
index cdefe5ea4c8..7b34aec9e5c 100644
--- a/libs/libc/syslog/lib_openlog.c
+++ b/libs/libc/syslog/lib_openlog.c
@@ -51,6 +51,7 @@
void openlog(FAR const char *ident, int option, int facility)
{
+#ifdef CONFIG_SYSLOG_OPENLOG
FAR struct task_info_s *info = task_get_info();
FAR struct openlog_s *state;
size_t n = 0;
@@ -72,6 +73,11 @@ void openlog(FAR const char *ident, int option, int facility)
state->ol_option = option;
state->ol_facility = facility;
nxrmutex_unlock(&info->ta_lock);
+#else
+ UNUSED(ident);
+ UNUSED(option);
+ UNUSED(facility);
+#endif
}
/****************************************************************************
diff --git a/libs/libc/syslog/lib_syslog.c b/libs/libc/syslog/lib_syslog.c
index 1d144dba478..0b44065357c 100644
--- a/libs/libc/syslog/lib_syslog.c
+++ b/libs/libc/syslog/lib_syslog.c
@@ -40,7 +40,7 @@
* Private Functions
****************************************************************************/
-#ifndef __KERNEL__
+#if defined(CONFIG_SYSLOG_OPENLOG) && !defined(__KERNEL__)
/****************************************************************************
* Name: vsyslog_perror
*
@@ -135,7 +135,7 @@ void vsyslog(int priority, FAR const IPTR char *fmt,
va_list ap)
nx_vsyslog(priority, fmt, &ap);
#endif
-#ifndef __KERNEL__
+#if defined(CONFIG_SYSLOG_OPENLOG) && !defined(__KERNEL__)
vsyslog_perror(fmt, ap);
#endif
}