2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
2016-06-20 02:19:34 +02:00
|
|
|
* drivers/syslog/syslog_stream.c
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2017-05-10 22:42:43 +02:00
|
|
|
* Copyright (C) 2012, 2016-2017 Gregory Nutt. All rights reserved.
|
2012-11-10 17:06:01 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2014-10-09 03:08:26 +02:00
|
|
|
#include <stdio.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2014-10-09 03:08:26 +02:00
|
|
|
#include <nuttx/streams.h>
|
2017-05-11 01:36:08 +02:00
|
|
|
#include <nuttx/mm/iob.h>
|
|
|
|
#include <nuttx/syslog/syslog.h>
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2016-06-19 21:03:20 +02:00
|
|
|
#include "syslog.h"
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-12 14:58:33 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: syslogstream_flush
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSLOG_BUFFER
|
|
|
|
static int syslogstream_flush(FAR struct lib_syslogstream_s *stream)
|
|
|
|
{
|
|
|
|
FAR struct iob_s *iob;
|
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
DEBUGASSERT(stream != NULL);
|
|
|
|
iob = stream->iob;
|
|
|
|
|
|
|
|
/* Do we have an IO buffer? Is there anything buffered? */
|
|
|
|
|
|
|
|
if (iob != NULL && iob->io_len > 0)
|
|
|
|
{
|
|
|
|
/* Yes write the buffered data */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
ssize_t nbytes = syslog_write((FAR const char *)iob->io_data,
|
|
|
|
(size_t)iob->io_len);
|
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
2018-01-31 00:23:10 +01:00
|
|
|
ret = (int)nbytes;
|
2017-05-12 14:58:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
iob->io_len = 0;
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (ret == -EINTR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-05-12 15:31:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: syslogstream_addchar
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-12 15:57:45 +02:00
|
|
|
#ifdef CONFIG_SYSLOG_BUFFER
|
2017-05-12 15:31:50 +02:00
|
|
|
static void syslogstream_addchar(FAR struct lib_syslogstream_s *stream, int ch)
|
|
|
|
{
|
|
|
|
FAR struct iob_s *iob = stream->iob;
|
|
|
|
|
|
|
|
/* Add the incoming character to the buffer */
|
|
|
|
|
|
|
|
iob->io_data[iob->io_len] = ch;
|
|
|
|
iob->io_len++;
|
|
|
|
|
|
|
|
/* Increment the total number of bytes buffered. */
|
|
|
|
|
|
|
|
stream->public.nput++;
|
|
|
|
|
|
|
|
/* Is the buffer full? */
|
|
|
|
|
|
|
|
if (iob->io_len >= CONFIG_IOB_BUFSIZE)
|
|
|
|
{
|
|
|
|
/* Yes.. then flush the buffer */
|
|
|
|
|
|
|
|
syslogstream_flush(stream);
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 15:57:45 +02:00
|
|
|
#endif
|
2017-05-12 15:31:50 +02:00
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: syslogstream_putc
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void syslogstream_putc(FAR struct lib_outstream_s *this, int ch)
|
|
|
|
{
|
2017-05-11 00:13:21 +02:00
|
|
|
/* Discard carriage returns */
|
2017-05-10 22:42:43 +02:00
|
|
|
|
2017-05-11 00:13:21 +02:00
|
|
|
if (ch != '\r')
|
|
|
|
{
|
2017-05-11 01:36:08 +02:00
|
|
|
#ifdef CONFIG_SYSLOG_BUFFER
|
2017-05-12 15:31:50 +02:00
|
|
|
FAR struct lib_syslogstream_s *stream =
|
|
|
|
(FAR struct lib_syslogstream_s *)this;
|
2017-05-10 22:42:43 +02:00
|
|
|
|
2017-05-12 15:31:50 +02:00
|
|
|
DEBUGASSERT(stream != NULL);
|
2017-05-10 22:42:43 +02:00
|
|
|
|
2017-05-11 01:36:08 +02:00
|
|
|
/* Do we have an IO buffer? */
|
2017-05-10 22:42:43 +02:00
|
|
|
|
2017-05-12 15:31:50 +02:00
|
|
|
if (stream->iob != NULL)
|
2017-05-11 00:13:21 +02:00
|
|
|
{
|
2017-05-12 15:31:50 +02:00
|
|
|
/* Is this a linefeed? */
|
2017-05-11 00:13:21 +02:00
|
|
|
|
2017-05-12 15:31:50 +02:00
|
|
|
if (ch == '\n')
|
2017-05-11 01:36:08 +02:00
|
|
|
{
|
2017-05-12 15:31:50 +02:00
|
|
|
/* Yes... pre-pend carriage return */
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-05-12 15:31:50 +02:00
|
|
|
syslogstream_addchar(stream, '\r');
|
2017-05-11 01:36:08 +02:00
|
|
|
}
|
2017-05-12 15:31:50 +02:00
|
|
|
|
|
|
|
/* Add the incoming character to the buffer */
|
|
|
|
|
|
|
|
syslogstream_addchar(stream, ch);
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
2017-05-11 01:36:08 +02:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Try writing until the write was successful or until an
|
|
|
|
* irrecoverable error occurs.
|
|
|
|
*/
|
2012-11-10 17:06:01 +01:00
|
|
|
|
2017-05-11 01:36:08 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* Write the character to the supported logging device. On
|
|
|
|
* failure, syslog_putc returns EOF with the errno value set;
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = syslog_putc(ch);
|
2018-01-31 00:23:10 +01:00
|
|
|
if (ret >= 0)
|
2017-05-11 01:36:08 +02:00
|
|
|
{
|
|
|
|
this->nput++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-31 00:23:10 +01:00
|
|
|
/* The special return value -EINTR means that syslog_putc() was
|
2017-05-11 01:36:08 +02:00
|
|
|
* awakened by a signal. This is not a real error and must be
|
|
|
|
* ignored in this context.
|
|
|
|
*/
|
|
|
|
}
|
2018-01-31 00:23:10 +01:00
|
|
|
while (ret == -EINTR);
|
2017-05-11 01:36:08 +02:00
|
|
|
}
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
2017-05-10 22:42:43 +02:00
|
|
|
}
|
|
|
|
|
2012-11-10 17:06:01 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-05-11 01:36:08 +02:00
|
|
|
* Name: syslogstream_create
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initializes a stream for use with the configured syslog interface.
|
2016-06-19 15:56:24 +02:00
|
|
|
* Only accessible from with the OS SYSLOG logic.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2016-06-19 15:56:24 +02:00
|
|
|
* stream - User allocated, uninitialized instance of struct
|
2017-05-11 01:36:08 +02:00
|
|
|
* lib_lowoutstream_s to be initialized.
|
2012-11-10 17:06:01 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None (User allocated instance initialized).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-05-11 01:36:08 +02:00
|
|
|
void syslogstream_create(FAR struct lib_syslogstream_s *stream)
|
2012-11-10 17:06:01 +01:00
|
|
|
{
|
2017-05-10 22:42:43 +02:00
|
|
|
#ifdef CONFIG_SYSLOG_BUFFER
|
2017-05-11 01:36:08 +02:00
|
|
|
FAR struct iob_s *iob;
|
2017-05-12 14:58:33 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
DEBUGASSERT(stream != NULL);
|
2017-05-11 01:36:08 +02:00
|
|
|
|
|
|
|
/* Initialize the common fields */
|
|
|
|
|
2017-05-10 22:42:43 +02:00
|
|
|
stream->public.put = syslogstream_putc;
|
2017-05-12 14:58:33 +02:00
|
|
|
stream->public.flush = lib_noflush;
|
2017-05-10 22:42:43 +02:00
|
|
|
stream->public.nput = 0;
|
2017-05-11 01:36:08 +02:00
|
|
|
|
2017-05-12 14:58:33 +02:00
|
|
|
#ifdef CONFIG_SYSLOG_BUFFER
|
2017-05-11 01:36:08 +02:00
|
|
|
/* Allocate an IOB */
|
|
|
|
|
|
|
|
iob = iob_alloc(true);
|
|
|
|
stream->iob = iob;
|
|
|
|
|
|
|
|
if (iob != NULL)
|
|
|
|
{
|
|
|
|
/* Initialize the IOB */
|
|
|
|
|
|
|
|
iob->io_len = 0;
|
|
|
|
iob->io_offset = 0;
|
|
|
|
iob->io_pktlen = 0;
|
|
|
|
}
|
2017-05-10 22:42:43 +02:00
|
|
|
#endif
|
2012-11-10 17:06:01 +01:00
|
|
|
}
|
2017-05-11 01:36:08 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: syslogstream_destroy
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Free resources held by the syslog stream.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2017-05-11 01:36:08 +02:00
|
|
|
* stream - User allocated, uninitialized instance of struct
|
|
|
|
* lib_lowoutstream_s to be initialized.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None (Resources freed).
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSLOG_BUFFER
|
|
|
|
void syslogstream_destroy(FAR struct lib_syslogstream_s *stream)
|
|
|
|
{
|
|
|
|
DEBUGASSERT(stream != NULL);
|
|
|
|
|
|
|
|
/* Verify that there is an IOB attached (there should be) */
|
|
|
|
|
|
|
|
if (stream->iob != NULL)
|
|
|
|
{
|
2017-05-12 14:58:33 +02:00
|
|
|
/* Flush the output buffered in the IOB */
|
|
|
|
|
|
|
|
syslogstream_flush(stream);
|
|
|
|
|
2017-05-11 01:36:08 +02:00
|
|
|
/* Free the IOB */
|
|
|
|
|
|
|
|
iob_free(stream->iob);
|
|
|
|
stream->iob = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|