From 238cddde3adf39171790031b9bd5c30388287476 Mon Sep 17 00:00:00 2001 From: chao an Date: Fri, 15 Nov 2024 17:19:53 +0800 Subject: [PATCH] drivers/syslog: remove implement of syslog_putc() syslog_putc() have a lot of duplicate logic with syslog_write(). remove syslog_putc() and reuse syslog_write() to simplify syslog printing. Signed-off-by: chao an --- Documentation/guides/logging_rambuffer.rst | 6 +- arch/arm/src/efm32/efm32_start.c | 2 +- drivers/syslog/CMakeLists.txt | 9 +- drivers/syslog/Kconfig | 2 +- drivers/syslog/Make.defs | 2 +- drivers/syslog/syslog_putc.c | 208 --------------------- include/nuttx/syslog/syslog.h | 17 -- libs/libc/stream/lib_syslograwstream.c | 10 +- 8 files changed, 13 insertions(+), 243 deletions(-) delete mode 100644 drivers/syslog/syslog_putc.c diff --git a/Documentation/guides/logging_rambuffer.rst b/Documentation/guides/logging_rambuffer.rst index 515be6aa57..fa7f13f777 100644 --- a/Documentation/guides/logging_rambuffer.rst +++ b/Documentation/guides/logging_rambuffer.rst @@ -42,7 +42,7 @@ The RAMLOG device is a special character device that can really be used for most any purpose. However, the RAMLOG device has some special attributes that make it ideal for use as a syslogging device. -* It supports the ``syslog_putc`` interface needed for system logging +* It supports the ``syslog_write`` interface needed for system logging * It behaves much like a pipe: It implements a queue. Writing to the RAMLOG device adds data to the head of the queue; reading from the RAMLOG device removes data from the tail of the queue. @@ -91,7 +91,7 @@ These enables the RAMLOG and configure it for use as the syslog device CONFIG_RAMLOG_CONSOLE_BUFSIZE=8192 CONFIG_RAMLOG_NONBLOCKING=y CONFIG_RAMLOG_SYSLOG=y - #CONFIG_SYSLOG_CHAR undefined, else duplicate output with syslog_putc() + #CONFIG_SYSLOG_CHAR undefined, else duplicate output with syslog_write() Now when I run NuttX, I get output like this. The ``dmesg`` command now appears as an NSH command: @@ -167,4 +167,4 @@ As a side note, the ``posix_spawn_exec`` error will occur on each command in this configuration. That is because NSH first tries to execute a command from a file found in the file system on the ``PATH`` variable. You will not see this error in your system unless you have ``CONFIG_NSH_FILE_APPS=y`` -defined in your configuration. \ No newline at end of file +defined in your configuration. diff --git a/arch/arm/src/efm32/efm32_start.c b/arch/arm/src/efm32/efm32_start.c index 2b6a62a60f..c47dacd373 100644 --- a/arch/arm/src/efm32/efm32_start.c +++ b/arch/arm/src/efm32/efm32_start.c @@ -84,7 +84,7 @@ const uintptr_t g_idle_topstack = HEAP_BASE; #ifdef CONFIG_DEBUG_FEATURES # if defined(CONFIG_ARMV7M_ITMSYSLOG) -# define showprogress(c) syslog_putc(c) +# define showprogress(c) up_putc(c) # elif defined(HAVE_UART_CONSOLE) || defined(HAVE_LEUART_CONSOLE) # define showprogress(c) efm32_lowputc(c) # else diff --git a/drivers/syslog/CMakeLists.txt b/drivers/syslog/CMakeLists.txt index ce8738fc8f..b8e8804d33 100644 --- a/drivers/syslog/CMakeLists.txt +++ b/drivers/syslog/CMakeLists.txt @@ -25,14 +25,7 @@ set(SRCS) if(CONFIG_SYSLOG) - list( - APPEND - SRCS - vsyslog.c - syslog_channel.c - syslog_putc.c - syslog_write.c - syslog_flush.c) + list(APPEND SRCS vsyslog.c syslog_channel.c syslog_write.c syslog_flush.c) endif() if(CONFIG_SYSLOG_INTBUFFER) diff --git a/drivers/syslog/Kconfig b/drivers/syslog/Kconfig index fe29955e8a..68924b986f 100644 --- a/drivers/syslog/Kconfig +++ b/drivers/syslog/Kconfig @@ -389,7 +389,7 @@ config CONSOLE_SYSLOG ---help--- Use the syslog logging device as a system console. If this feature is enabled (along with DEV_CONSOLE), then all console output will be - re-directed to syslog output (syslog_putc). This is useful, for + re-directed to syslog output (syslog_write). This is useful, for example, if the only console is a Telnet console. Then in that case, console output from non-Telnet threads will go to the syslog output. diff --git a/drivers/syslog/Make.defs b/drivers/syslog/Make.defs index 6b4e5521f7..00e1e6b3c7 100644 --- a/drivers/syslog/Make.defs +++ b/drivers/syslog/Make.defs @@ -24,7 +24,7 @@ # Include SYSLOG Infrastructure ifeq ($(CONFIG_SYSLOG),y) -CSRCS += vsyslog.c syslog_channel.c syslog_putc.c +CSRCS += vsyslog.c syslog_channel.c CSRCS += syslog_write.c syslog_flush.c endif diff --git a/drivers/syslog/syslog_putc.c b/drivers/syslog/syslog_putc.c deleted file mode 100644 index 44c4c6fc31..0000000000 --- a/drivers/syslog/syslog_putc.c +++ /dev/null @@ -1,208 +0,0 @@ -/**************************************************************************** - * drivers/syslog/syslog_putc.c - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include -#include - -#include -#include -#include - -#include "syslog.h" - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: syslog_putc - * - * Description: - * This is the low-level system logging interface. - * - * Input Parameters: - * ch - The character to add to the SYSLOG (must be positive). - * - * Returned Value: - * On success, the character is echoed back to the caller. A negated - * errno value is returned on any failure. - * - ****************************************************************************/ - -int syslog_putc(int ch) -{ - /* Is this an attempt to do SYSLOG output from an interrupt handler? */ - - if (up_interrupt_context() || sched_idletask()) - { -#ifdef CONFIG_SYSLOG_INTBUFFER - if (up_interrupt_context()) - { - /* Buffer the character in the interrupt buffer. - * The interrupt buffer will be flushed before the next - * normal,non-interrupt SYSLOG output. - */ - - return syslog_add_intbuffer(ch); - } - else -#endif - { - int i; - - /* Force the character to the SYSLOG device immediately - * (if possible). - * This means that the interrupt data may not be in - * synchronization with output data that may have been - * buffered by sc_putc(). - */ - - for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++) - { - FAR syslog_channel_t *channel = g_syslog_channel[i]; - - if (channel == NULL) - { - break; - } - -#ifdef CONFIG_SYSLOG_IOCTL - if (channel->sc_state & SYSLOG_CHANNEL_DISABLE) - { - continue; - } -#endif - - if (channel->sc_ops->sc_force != NULL) - { -#ifdef CONFIG_SYSLOG_CRLF - /* Check for LF */ - - if (ch == '\n' && - !(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF)) - { - /* Add CR */ - - channel->sc_ops->sc_force(channel, '\r'); - } -#endif - - channel->sc_ops->sc_force(channel, ch); - } - else - { - char tmp = ch; - - DEBUGASSERT(channel->sc_ops->sc_write_force != NULL); - -#ifdef CONFIG_SYSLOG_CRLF - /* Check for LF */ - - if (tmp == '\n' && - !(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF)) - { - /* Add CR */ - - channel->sc_ops->sc_write_force(channel, "\r", 1); - } -#endif - - channel->sc_ops->sc_write_force(channel, &tmp, 1); - } - } - } - } - else - { - int i; - -#ifdef CONFIG_SYSLOG_INTBUFFER - /* Flush any characters that may have been added to the interrupt - * buffer. - */ - - syslog_flush_intbuffer(false); -#endif - - for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++) - { - FAR syslog_channel_t *channel = g_syslog_channel[i]; - - if (channel == NULL) - { - break; - } - -#ifdef CONFIG_SYSLOG_IOCTL - if (channel->sc_state & SYSLOG_CHANNEL_DISABLE) - { - continue; - } -#endif - - if (channel->sc_ops->sc_putc != NULL) - { -#ifdef CONFIG_SYSLOG_CRLF - /* Check for LF */ - - if (ch == '\n' && - !(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF)) - { - /* Add CR */ - - channel->sc_ops->sc_putc(channel, '\r'); - } -#endif - - channel->sc_ops->sc_putc(channel, ch); - } - else - { - char tmp = ch; - DEBUGASSERT(channel->sc_ops->sc_write != NULL); - -#ifdef CONFIG_SYSLOG_CRLF - /* Check for LF */ - - if (tmp == '\n' && - !(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF)) - { - /* Add CR */ - - channel->sc_ops->sc_write(channel, "\r", 1); - } -#endif - - channel->sc_ops->sc_write(channel, &tmp, 1); - } - } - } - - return ch; -} diff --git a/include/nuttx/syslog/syslog.h b/include/nuttx/syslog/syslog.h index b0b46a9b9f..52f1c8570a 100644 --- a/include/nuttx/syslog/syslog.h +++ b/include/nuttx/syslog/syslog.h @@ -310,23 +310,6 @@ FAR syslog_channel_t * syslog_stream_channel(FAR struct lib_outstream_s *stream); #endif -/**************************************************************************** - * Name: syslog_putc - * - * Description: - * This is the low-level, single character, system logging interface. - * - * Input Parameters: - * ch - The character to add to the SYSLOG (must be positive). - * - * Returned Value: - * On success, the character is echoed back to the caller. A negated - * errno value is returned on any failure. - * - ****************************************************************************/ - -int syslog_putc(int ch); - /**************************************************************************** * Name: syslog_write * diff --git a/libs/libc/stream/lib_syslograwstream.c b/libs/libc/stream/lib_syslograwstream.c index 427b365e1a..1173f423a7 100644 --- a/libs/libc/stream/lib_syslograwstream.c +++ b/libs/libc/stream/lib_syslograwstream.c @@ -162,18 +162,20 @@ static void syslograwstream_putc(FAR struct lib_outstream_s *self, int ch) do { + char c = ch; + /* Write the character to the supported logging device. On - * failure, syslog_putc returns a negated errno value. + * failure, syslog_write returns a negated errno value. */ - ret = syslog_putc(ch); + ret = syslog_write(&c, 1); if (ret >= 0) { self->nput++; return; } - /* The special return value -EINTR means that syslog_putc() was + /* The special return value -EINTR means that syslog_write() was * awakened by a signal. This is not a real error and must be * ignored in this context. */ @@ -221,7 +223,7 @@ static ssize_t syslograwstream_puts(FAR struct lib_outstream_s *self, return ret; } - /* The special return value -EINTR means that syslog_putc() was + /* The special return value -EINTR means that syslog_write() was * awakened by a signal. This is not a real error and must be * ignored in this context. */