From 5fd1379d3f9dc13e67d8c5d2734eb0bc0cdc3107 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Thu, 24 Nov 2022 01:48:20 +0800 Subject: [PATCH] syslog: Move syslog stream to libc like other stream implementation Signed-off-by: Xiang Xiao --- drivers/syslog/Make.defs | 4 +- drivers/syslog/syslog.h | 113 ------------------ drivers/syslog/vsyslog.c | 9 +- include/nuttx/streams.h | 54 +++++++++ include/nuttx/syslog/syslog.h | 54 +++++++++ libs/libc/stream/Make.defs | 1 + .../libc/stream/lib_syslogstream.c | 17 ++- 7 files changed, 122 insertions(+), 130 deletions(-) rename drivers/syslog/syslog_stream.c => libs/libc/stream/lib_syslogstream.c (95%) diff --git a/drivers/syslog/Make.defs b/drivers/syslog/Make.defs index 2b49d2a6f9..0fbb977c14 100644 --- a/drivers/syslog/Make.defs +++ b/drivers/syslog/Make.defs @@ -21,8 +21,8 @@ ############################################################################ # Include SYSLOG Infrastructure -CSRCS += vsyslog.c syslog_stream.c syslog_channel.c -CSRCS += syslog_putc.c syslog_write.c syslog_force.c syslog_flush.c +CSRCS += vsyslog.c syslog_channel.c syslog_putc.c +CSRCS += syslog_write.c syslog_force.c syslog_flush.c ifeq ($(CONFIG_SYSLOG_INTBUFFER),y) CSRCS += syslog_intbuffer.c diff --git a/drivers/syslog/syslog.h b/drivers/syslog/syslog.h index 6552518a29..f3c35856a4 100644 --- a/drivers/syslog/syslog.h +++ b/drivers/syslog/syslog.h @@ -26,29 +26,9 @@ ****************************************************************************/ #include -#include #include -/**************************************************************************** - * Public Types - ****************************************************************************/ - -/* This is a special stream that does buffered character I/O. NOTE that is - * CONFIG_SYSLOG_BUFFER is not defined, it is the same as struct - * lib_outstream_s - */ - -struct iob_s; /* Forward reference */ - -struct lib_syslogstream_s -{ - struct lib_outstream_s public; -#ifdef CONFIG_SYSLOG_BUFFER - FAR struct iob_s *iob; -#endif -}; - /**************************************************************************** * Public Data ****************************************************************************/ @@ -244,99 +224,6 @@ int syslog_add_intbuffer(int ch); int syslog_flush_intbuffer(bool force); #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 - * - * Description: - * This is the low-level, multiple character, system logging interface. - * - * Input Parameters: - * buffer - The buffer containing the data to be output - * buflen - The number of bytes in the buffer - * - * Returned Value: - * On success, the number of characters written is returned. A negated - * errno value is returned on any failure. - * - ****************************************************************************/ - -ssize_t syslog_write(FAR const char *buffer, size_t buflen); - -/**************************************************************************** - * Name: syslog_force - * - * Description: - * This is the low-level system logging interface. This version forces - * the output and is only used in emergency situations (e.g., in assertion - * handling). - * - * 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_force(int ch); - -/**************************************************************************** - * Name: syslogstream_create - * - * Description: - * Initializes a stream for use with the configured syslog interface. - * Only accessible from with the OS SYSLOG logic. - * - * Input Parameters: - * stream - User allocated, uninitialized instance of struct - * lib_syslogstream_s to be initialized. - * - * Returned Value: - * None (User allocated instance initialized). - * - ****************************************************************************/ - -void syslogstream_create(FAR struct lib_syslogstream_s *stream); - -/**************************************************************************** - * Name: syslogstream_destroy - * - * Description: - * Free resources held by the syslog stream. - * - * Input Parameters: - * stream - User allocated, uninitialized instance of struct - * lib_syslogstream_s to be initialized. - * - * Returned Value: - * None (Resources freed). - * - ****************************************************************************/ - -#ifdef CONFIG_SYSLOG_BUFFER -void syslogstream_destroy(FAR struct lib_syslogstream_s *stream); -#else -# define syslogstream_destroy(s) -#endif - #undef EXTERN #ifdef __cplusplus } diff --git a/drivers/syslog/vsyslog.c b/drivers/syslog/vsyslog.c index 05e5643f51..e5b2b1c541 100644 --- a/drivers/syslog/vsyslog.c +++ b/drivers/syslog/vsyslog.c @@ -30,8 +30,8 @@ #include #include -#include #include +#include #include "syslog.h" @@ -84,7 +84,7 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap) * do the work. */ - syslogstream_create(&stream); + lib_syslogstream_open(&stream); #ifdef CONFIG_SYSLOG_TIMESTAMP ts.tv_sec = 0; @@ -228,11 +228,8 @@ int nx_vsyslog(int priority, FAR const IPTR char *fmt, FAR va_list *ap) ret += lib_sprintf(&stream.public, "\e[0m"); #endif -#ifdef CONFIG_SYSLOG_BUFFER /* Flush and destroy the syslog stream buffer */ - syslogstream_destroy(&stream); -#endif - + lib_syslogstream_close(&stream); return ret; } diff --git a/include/nuttx/streams.h b/include/nuttx/streams.h index ddd721ca65..da85e1d9ea 100644 --- a/include/nuttx/streams.h +++ b/include/nuttx/streams.h @@ -198,6 +198,21 @@ struct lib_rawsostream_s int fd; }; +/* This is a special stream that does buffered character I/O. NOTE that is + * CONFIG_SYSLOG_BUFFER is not defined, it is the same as struct + * lib_outstream_s + */ + +struct iob_s; /* Forward reference */ + +struct lib_syslogstream_s +{ + struct lib_outstream_s public; +#ifdef CONFIG_SYSLOG_BUFFER + FAR struct iob_s *iob; +#endif +}; + /* LZF compressed stream pipeline */ #ifdef CONFIG_LIBC_LZF @@ -388,6 +403,45 @@ void lib_zeroinstream(FAR struct lib_instream_s *zeroinstream); void lib_nullinstream(FAR struct lib_instream_s *nullinstream); void lib_nulloutstream(FAR struct lib_outstream_s *nulloutstream); +/**************************************************************************** + * Name: lib_syslogstream_open + * + * Description: + * Initializes a stream for use with the configured syslog interface. + * Only accessible from with the OS SYSLOG logic. + * + * Input Parameters: + * stream - User allocated, uninitialized instance of struct + * lib_syslogstream_s to be initialized. + * + * Returned Value: + * None (User allocated instance initialized). + * + ****************************************************************************/ + +void lib_syslogstream_open(FAR struct lib_syslogstream_s *stream); + +/**************************************************************************** + * Name: lib_syslogstream_close + * + * Description: + * Free resources held by the syslog stream. + * + * Input Parameters: + * stream - User allocated, uninitialized instance of struct + * lib_syslogstream_s to be initialized. + * + * Returned Value: + * None (Resources freed). + * + ****************************************************************************/ + +#ifdef CONFIG_SYSLOG_BUFFER +void lib_syslogstream_close(FAR struct lib_syslogstream_s *stream); +#else +# define lib_syslogstream_close(s) +#endif + /**************************************************************************** * Name: lib_lzfoutstream * diff --git a/include/nuttx/syslog/syslog.h b/include/nuttx/syslog/syslog.h index ec47110c1b..81866d897d 100644 --- a/include/nuttx/syslog/syslog.h +++ b/include/nuttx/syslog/syslog.h @@ -243,6 +243,41 @@ int syslog_initialize(void); FAR struct syslog_channel_s *syslog_file_channel(FAR const char *devpath); #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 + * + * Description: + * This is the low-level, multiple character, system logging interface. + * + * Input Parameters: + * buffer - The buffer containing the data to be output + * buflen - The number of bytes in the buffer + * + * Returned Value: + * On success, the number of characters written is returned. A negated + * errno value is returned on any failure. + * + ****************************************************************************/ + +ssize_t syslog_write(FAR const char *buffer, size_t buflen); + /**************************************************************************** * Name: syslog_flush * @@ -272,6 +307,25 @@ FAR struct syslog_channel_s *syslog_file_channel(FAR const char *devpath); int syslog_flush(void); +/**************************************************************************** + * Name: syslog_force + * + * Description: + * This is the low-level system logging interface. This version forces + * the output and is only used in emergency situations (e.g., in assertion + * handling). + * + * 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_force(int ch); + /**************************************************************************** * Name: nx_vsyslog * diff --git a/libs/libc/stream/Make.defs b/libs/libc/stream/Make.defs index f7cddfde39..868ff05576 100644 --- a/libs/libc/stream/Make.defs +++ b/libs/libc/stream/Make.defs @@ -26,6 +26,7 @@ CSRCS += lib_memsostream.c lib_lowoutstream.c lib_rawinstream.c CSRCS += lib_rawoutstream.c lib_rawsistream.c lib_rawsostream.c CSRCS += lib_zeroinstream.c lib_nullinstream.c lib_nulloutstream.c CSRCS += lib_mtdoutstream.c lib_libnoflush.c lib_libsnoflush.c +CSRCS += lib_syslogstream.c # The remaining sources files depend upon C streams diff --git a/drivers/syslog/syslog_stream.c b/libs/libc/stream/lib_syslogstream.c similarity index 95% rename from drivers/syslog/syslog_stream.c rename to libs/libc/stream/lib_syslogstream.c index 72d40f71c8..3f22cae40f 100644 --- a/drivers/syslog/syslog_stream.c +++ b/libs/libc/stream/lib_syslogstream.c @@ -1,5 +1,5 @@ /**************************************************************************** - * drivers/syslog/syslog_stream.c + * libs/libc/stream/lib_syslogstream.c * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -28,10 +28,9 @@ #include #include +#include #include -#include "syslog.h" - /**************************************************************************** * Private Functions ****************************************************************************/ @@ -58,10 +57,10 @@ static int syslogstream_flush(FAR struct lib_syslogstream_s *stream) do { ssize_t nbytes = syslog_write((FAR const char *)iob->io_data, - (size_t)iob->io_len); + iob->io_len); if (nbytes < 0) { - ret = (int)nbytes; + ret = nbytes; } else { @@ -176,7 +175,7 @@ static void syslogstream_putc(FAR struct lib_outstream_s *this, int ch) ****************************************************************************/ /**************************************************************************** - * Name: syslogstream_create + * Name: lib_syslogstream_open * * Description: * Initializes a stream for use with the configured syslog interface. @@ -191,7 +190,7 @@ static void syslogstream_putc(FAR struct lib_outstream_s *this, int ch) * ****************************************************************************/ -void syslogstream_create(FAR struct lib_syslogstream_s *stream) +void lib_syslogstream_open(FAR struct lib_syslogstream_s *stream) { #ifdef CONFIG_SYSLOG_BUFFER FAR struct iob_s *iob; @@ -223,7 +222,7 @@ void syslogstream_create(FAR struct lib_syslogstream_s *stream) } /**************************************************************************** - * Name: syslogstream_destroy + * Name: lib_syslogstream_close * * Description: * Free resources held by the syslog stream. @@ -238,7 +237,7 @@ void syslogstream_create(FAR struct lib_syslogstream_s *stream) ****************************************************************************/ #ifdef CONFIG_SYSLOG_BUFFER -void syslogstream_destroy(FAR struct lib_syslogstream_s *stream) +void lib_syslogstream_close(FAR struct lib_syslogstream_s *stream) { DEBUGASSERT(stream != NULL);