syslog/channel: unify syslog channel writing to reduce redundant code
unify syslog channel writing to reduce redundant code Signed-off-by: chao an <anchao@lixiang.com>
This commit is contained in:
parent
5f5350c05b
commit
c7a0288a3f
3 changed files with 122 additions and 261 deletions
|
@ -230,6 +230,28 @@ int syslog_add_intbuffer(int ch);
|
|||
#ifdef CONFIG_SYSLOG_INTBUFFER
|
||||
int syslog_flush_intbuffer(bool force);
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name: syslog_write_foreach
|
||||
*
|
||||
* Description:
|
||||
* This provides a default write method for syslog devices that do not
|
||||
* support multiple byte writes This functions simply loops, outputting
|
||||
* one character at a time.
|
||||
*
|
||||
* Input Parameters:
|
||||
* buffer - The buffer containing the data to be output
|
||||
* buflen - The number of bytes in the buffer
|
||||
* force - Use the force() method of the channel vs. the putc() method.
|
||||
*
|
||||
* Returned Value:
|
||||
* On success, the number of characters written is returned. A negated
|
||||
* errno value is returned on any failure.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
ssize_t syslog_write_foreach(FAR const char *buffer,
|
||||
size_t buflen, bool force);
|
||||
#endif /* CONFIG_SYSLOG */
|
||||
|
||||
#undef EXTERN
|
||||
|
|
|
@ -89,7 +89,7 @@ static struct g_syslog_intbuffer_s g_syslog_intbuffer;
|
|||
*
|
||||
****************************************************************************/
|
||||
|
||||
int syslog_remove_intbuffer(void)
|
||||
static int syslog_remove_intbuffer(void)
|
||||
{
|
||||
irqstate_t flags;
|
||||
uint32_t outndx;
|
||||
|
@ -185,44 +185,9 @@ int syslog_add_intbuffer(int ch)
|
|||
|
||||
if (inuse == CONFIG_SYSLOG_INTBUFSIZE - 1)
|
||||
{
|
||||
int oldch = syslog_remove_intbuffer();
|
||||
int i;
|
||||
char oldch = syslog_remove_intbuffer();
|
||||
|
||||
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)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SYSLOG_CRLF
|
||||
/* Check for LF */
|
||||
|
||||
if (oldch == '\n' &&
|
||||
!(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF))
|
||||
{
|
||||
/* Add CR */
|
||||
|
||||
channel->sc_ops->sc_force(channel, '\r');
|
||||
}
|
||||
#endif
|
||||
|
||||
channel->sc_ops->sc_force(channel, oldch);
|
||||
}
|
||||
syslog_write_foreach(&oldch, 1, true);
|
||||
|
||||
ret = -ENOSPC;
|
||||
}
|
||||
|
@ -265,6 +230,7 @@ int syslog_add_intbuffer(int ch)
|
|||
int syslog_flush_intbuffer(bool force)
|
||||
{
|
||||
irqstate_t flags;
|
||||
char c;
|
||||
int ch;
|
||||
|
||||
/* This logic is performed with the scheduler disabled to protect from
|
||||
|
@ -275,8 +241,6 @@ int syslog_flush_intbuffer(bool force)
|
|||
|
||||
for (; ; )
|
||||
{
|
||||
int i;
|
||||
|
||||
/* Transfer one character to time. This is inefficient, but is
|
||||
* done in this way to: (1) Deal with concurrent modification of
|
||||
* the interrupt buffer from interrupt activity, (2) Avoid keeper
|
||||
|
@ -290,42 +254,9 @@ int syslog_flush_intbuffer(bool force)
|
|||
break;
|
||||
}
|
||||
|
||||
for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++)
|
||||
{
|
||||
FAR syslog_channel_t *channel = g_syslog_channel[i];
|
||||
syslog_putc_t putfunc;
|
||||
c = (char)ch;
|
||||
|
||||
if (channel == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SYSLOG_IOCTL
|
||||
if (channel->sc_state & SYSLOG_CHANNEL_DISABLE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Select which putc function to use for this flush */
|
||||
|
||||
putfunc = force ? channel->sc_ops->sc_force :
|
||||
channel->sc_ops->sc_putc;
|
||||
|
||||
#ifdef CONFIG_SYSLOG_CRLF
|
||||
/* Check for LF */
|
||||
|
||||
if (ch == '\n' &&
|
||||
!(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF))
|
||||
{
|
||||
/* Add CR */
|
||||
|
||||
putfunc(channel, '\r');
|
||||
}
|
||||
#endif
|
||||
|
||||
putfunc(channel, ch);
|
||||
}
|
||||
syslog_write_foreach(&c, 1, force);
|
||||
}
|
||||
|
||||
leave_critical_section(flags);
|
||||
|
|
|
@ -77,7 +77,11 @@ static bool syslog_safe_to_block(void)
|
|||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Name: syslog_default_write
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: syslog_write_foreach
|
||||
*
|
||||
* Description:
|
||||
* This provides a default write method for syslog devices that do not
|
||||
|
@ -94,29 +98,18 @@ static bool syslog_safe_to_block(void)
|
|||
*
|
||||
****************************************************************************/
|
||||
|
||||
static ssize_t syslog_default_write(FAR const char *buffer, size_t buflen)
|
||||
{
|
||||
size_t nwritten;
|
||||
|
||||
if (!syslog_safe_to_block())
|
||||
{
|
||||
#ifdef CONFIG_SYSLOG_INTBUFFER
|
||||
if (up_interrupt_context())
|
||||
{
|
||||
for (nwritten = 0; nwritten < buflen; nwritten++)
|
||||
{
|
||||
syslog_add_intbuffer(buffer[nwritten]);
|
||||
}
|
||||
}
|
||||
else
|
||||
#endif
|
||||
ssize_t syslog_write_foreach(FAR const char *buffer,
|
||||
size_t buflen, bool force)
|
||||
{
|
||||
syslog_write_t write;
|
||||
syslog_putc_t putc;
|
||||
size_t nwritten = 0;
|
||||
ssize_t ret;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++)
|
||||
{
|
||||
FAR syslog_channel_t *channel = g_syslog_channel[i];
|
||||
nwritten = 0;
|
||||
|
||||
if (channel == NULL)
|
||||
{
|
||||
|
@ -130,107 +123,12 @@ static ssize_t syslog_default_write(FAR const char *buffer, size_t buflen)
|
|||
}
|
||||
#endif
|
||||
|
||||
if (channel->sc_ops->sc_write_force != NULL)
|
||||
write = !force ? channel->sc_ops->sc_write :
|
||||
channel->sc_ops->sc_write_force;
|
||||
if (write != NULL)
|
||||
{
|
||||
#ifdef CONFIG_SYSLOG_CRLF
|
||||
if (!(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF))
|
||||
{
|
||||
size_t head;
|
||||
|
||||
for (head = 0; head < buflen; head++)
|
||||
{
|
||||
ssize_t ret;
|
||||
|
||||
/* Check for LF */
|
||||
|
||||
if (buffer[head] != '\n')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ret = channel->sc_ops->sc_write_force(channel,
|
||||
buffer + nwritten,
|
||||
head - nwritten);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = channel->sc_ops->sc_write_force(channel,
|
||||
"\r\n", 2);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
nwritten = head + 1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if (nwritten < buflen)
|
||||
{
|
||||
ssize_t ret;
|
||||
|
||||
ret = channel->sc_ops->sc_write_force(channel,
|
||||
buffer + nwritten,
|
||||
buflen - nwritten);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
nwritten += ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DEBUGASSERT(channel->sc_ops->sc_force != NULL);
|
||||
for (nwritten = 0; nwritten < buflen; nwritten++)
|
||||
{
|
||||
#ifdef CONFIG_SYSLOG_CRLF
|
||||
/* Check for LF */
|
||||
|
||||
if (buffer[nwritten] == '\n' &&
|
||||
!(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF))
|
||||
{
|
||||
/* Add CR */
|
||||
|
||||
channel->sc_ops->sc_force(channel, '\r');
|
||||
}
|
||||
#endif
|
||||
|
||||
channel->sc_ops->sc_force(channel, buffer[nwritten]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++)
|
||||
{
|
||||
FAR syslog_channel_t *channel = g_syslog_channel[i];
|
||||
nwritten = 0;
|
||||
|
||||
if (channel == NULL)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SYSLOG_IOCTL
|
||||
if (channel->sc_state & SYSLOG_CHANNEL_DISABLE)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (channel->sc_ops->sc_write != NULL)
|
||||
{
|
||||
#ifdef CONFIG_SYSLOG_CRLF
|
||||
if (!(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF))
|
||||
{
|
||||
|
@ -238,26 +136,17 @@ static ssize_t syslog_default_write(FAR const char *buffer, size_t buflen)
|
|||
|
||||
for (head = 0; head < buflen; head++)
|
||||
{
|
||||
size_t ret;
|
||||
|
||||
/* Check for LF */
|
||||
|
||||
if (buffer[head] != '\n')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
ret = channel->sc_ops->sc_write(channel,
|
||||
buffer + nwritten,
|
||||
head - nwritten);
|
||||
if (ret < 0)
|
||||
ret = write(channel, buffer + nwritten, head - nwritten);
|
||||
if (ret >= 0)
|
||||
{
|
||||
return ret;
|
||||
ret = write(channel, "\r\n", 2);
|
||||
}
|
||||
|
||||
/* Add CR */
|
||||
|
||||
ret = channel->sc_ops->sc_write(channel, "\r\n", 2);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
|
@ -270,11 +159,7 @@ static ssize_t syslog_default_write(FAR const char *buffer, size_t buflen)
|
|||
|
||||
if (nwritten < buflen)
|
||||
{
|
||||
ssize_t ret;
|
||||
|
||||
ret = channel->sc_ops->sc_write(channel,
|
||||
buffer + nwritten,
|
||||
buflen - nwritten);
|
||||
ret = write(channel, buffer + nwritten, buflen - nwritten);
|
||||
if (ret < 0)
|
||||
{
|
||||
return ret;
|
||||
|
@ -287,34 +172,44 @@ static ssize_t syslog_default_write(FAR const char *buffer, size_t buflen)
|
|||
}
|
||||
else
|
||||
{
|
||||
DEBUGASSERT(channel->sc_ops->sc_putc != NULL);
|
||||
putc = !force ? channel->sc_ops->sc_putc :
|
||||
channel->sc_ops->sc_force;
|
||||
if (putc == NULL)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_SYSLOG_CRLF
|
||||
if (channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF)
|
||||
#endif
|
||||
{
|
||||
for (nwritten = 0; nwritten < buflen; nwritten++)
|
||||
{
|
||||
putc(channel, buffer[nwritten]);
|
||||
}
|
||||
}
|
||||
#ifdef CONFIG_SYSLOG_CRLF
|
||||
/* Check for LF */
|
||||
|
||||
if (buffer[nwritten] == '\n' &&
|
||||
!(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF))
|
||||
else
|
||||
{
|
||||
for (nwritten = 0; nwritten < buflen; nwritten++)
|
||||
{
|
||||
if (buffer[nwritten] == '\n')
|
||||
{
|
||||
/* Add CR */
|
||||
|
||||
channel->sc_ops->sc_putc(channel, '\r');
|
||||
putc(channel, '\r');
|
||||
}
|
||||
|
||||
putc(channel, buffer[nwritten]);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
channel->sc_ops->sc_putc(channel, buffer[nwritten]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nwritten;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: syslog_write
|
||||
*
|
||||
|
@ -333,8 +228,21 @@ static ssize_t syslog_default_write(FAR const char *buffer, size_t buflen)
|
|||
|
||||
ssize_t syslog_write(FAR const char *buffer, size_t buflen)
|
||||
{
|
||||
bool force = !syslog_safe_to_block();
|
||||
|
||||
#ifdef CONFIG_SYSLOG_INTBUFFER
|
||||
if (!up_interrupt_context() && !sched_idletask())
|
||||
if (force)
|
||||
{
|
||||
size_t nwritten;
|
||||
|
||||
for (nwritten = 0; nwritten < buflen; nwritten++)
|
||||
{
|
||||
syslog_add_intbuffer(buffer[nwritten]);
|
||||
}
|
||||
|
||||
return buflen;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Flush any characters that may have been added to the interrupt
|
||||
* buffer.
|
||||
|
@ -344,5 +252,5 @@ ssize_t syslog_write(FAR const char *buffer, size_t buflen)
|
|||
}
|
||||
#endif
|
||||
|
||||
return syslog_default_write(buffer, buflen);
|
||||
return syslog_write_foreach(buffer, buflen, force);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue