From 8f05661a530ec6884f72318c4a43b8e26c9bfe50 Mon Sep 17 00:00:00 2001 From: dongjiuzhu1 Date: Thu, 29 Dec 2022 21:56:27 +0800 Subject: [PATCH] syslog: A trailing newline is added if none is present. refs: https://www.manpagez.com/man/3/syslog/ Signed-off-by: dongjiuzhu1 --- include/nuttx/streams.h | 5 +---- libs/libc/stream/lib_syslogstream.c | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/include/nuttx/streams.h b/include/nuttx/streams.h index bb7ee561c8..979eedbeab 100644 --- a/include/nuttx/streams.h +++ b/include/nuttx/streams.h @@ -230,6 +230,7 @@ struct lib_syslogstream_s #ifdef CONFIG_SYSLOG_BUFFER FAR struct iob_s *iob; #endif + int last_ch; }; /* LZF compressed stream pipeline */ @@ -455,11 +456,7 @@ void lib_syslogstream_open(FAR struct lib_syslogstream_s *stream); * ****************************************************************************/ -#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/libs/libc/stream/lib_syslogstream.c b/libs/libc/stream/lib_syslogstream.c index 0ee5f0a9f5..f981d7afbc 100644 --- a/libs/libc/stream/lib_syslogstream.c +++ b/libs/libc/stream/lib_syslogstream.c @@ -111,16 +111,17 @@ static void syslogstream_addchar(FAR struct lib_syslogstream_s *stream, static void syslogstream_putc(FAR struct lib_outstream_s *this, int ch) { + FAR struct lib_syslogstream_s *stream = + (FAR struct lib_syslogstream_s *)this; + + DEBUGASSERT(stream != NULL); + stream->last_ch = ch; + /* Discard carriage returns */ if (ch != '\r') { #ifdef CONFIG_SYSLOG_BUFFER - FAR struct lib_syslogstream_s *stream = - (FAR struct lib_syslogstream_s *)this; - - DEBUGASSERT(stream != NULL); - /* Do we have an IO buffer? */ if (stream->iob != NULL) @@ -213,11 +214,16 @@ void lib_syslogstream_open(FAR struct lib_syslogstream_s *stream) * ****************************************************************************/ -#ifdef CONFIG_SYSLOG_BUFFER void lib_syslogstream_close(FAR struct lib_syslogstream_s *stream) { DEBUGASSERT(stream != NULL); + if (stream->last_ch != '\n') + { + syslogstream_putc(&stream->public, '\n'); + } + +#ifdef CONFIG_SYSLOG_BUFFER /* Verify that there is an IOB attached (there should be) */ if (stream->iob != NULL) @@ -231,5 +237,5 @@ void lib_syslogstream_close(FAR struct lib_syslogstream_s *stream) iob_free(stream->iob); stream->iob = NULL; } -} #endif +}