syslog: Move syslog stream to libc like other stream implementation

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
Xiang Xiao 2022-11-24 01:48:20 +08:00 committed by archer
parent 90c641d4c1
commit 5fd1379d3f
7 changed files with 122 additions and 130 deletions

View File

@ -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

View File

@ -26,29 +26,9 @@
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/streams.h>
#include <stdbool.h>
/****************************************************************************
* 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
}

View File

@ -30,8 +30,8 @@
#include <nuttx/arch.h>
#include <nuttx/init.h>
#include <nuttx/arch.h>
#include <nuttx/clock.h>
#include <nuttx/streams.h>
#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;
}

View File

@ -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
*

View File

@ -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
*

View File

@ -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

View File

@ -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 <errno.h>
#include <nuttx/mm/iob.h>
#include <nuttx/streams.h>
#include <nuttx/syslog/syslog.h>
#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);