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:
chao an
2024-12-11 15:34:19 +08:00
committed by Xiang Xiao
parent 5f5350c05b
commit c7a0288a3f
3 changed files with 122 additions and 261 deletions
+22
View File
@@ -230,6 +230,28 @@ int syslog_add_intbuffer(int ch);
#ifdef CONFIG_SYSLOG_INTBUFFER #ifdef CONFIG_SYSLOG_INTBUFFER
int syslog_flush_intbuffer(bool force); int syslog_flush_intbuffer(bool force);
#endif #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 */ #endif /* CONFIG_SYSLOG */
#undef EXTERN #undef EXTERN
+7 -76
View File
@@ -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; irqstate_t flags;
uint32_t outndx; uint32_t outndx;
@@ -185,46 +185,11 @@ int syslog_add_intbuffer(int ch)
if (inuse == CONFIG_SYSLOG_INTBUFSIZE - 1) if (inuse == CONFIG_SYSLOG_INTBUFSIZE - 1)
{ {
int oldch = syslog_remove_intbuffer(); char oldch = syslog_remove_intbuffer();
int i;
for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++) syslog_write_foreach(&oldch, 1, true);
{
FAR syslog_channel_t *channel = g_syslog_channel[i];
if (channel == NULL) ret = -ENOSPC;
{
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);
}
ret = -ENOSPC;
} }
/* Copy one character */ /* Copy one character */
@@ -265,6 +230,7 @@ int syslog_add_intbuffer(int ch)
int syslog_flush_intbuffer(bool force) int syslog_flush_intbuffer(bool force)
{ {
irqstate_t flags; irqstate_t flags;
char c;
int ch; int ch;
/* This logic is performed with the scheduler disabled to protect from /* This logic is performed with the scheduler disabled to protect from
@@ -275,8 +241,6 @@ int syslog_flush_intbuffer(bool force)
for (; ; ) for (; ; )
{ {
int i;
/* Transfer one character to time. This is inefficient, but is /* Transfer one character to time. This is inefficient, but is
* done in this way to: (1) Deal with concurrent modification of * done in this way to: (1) Deal with concurrent modification of
* the interrupt buffer from interrupt activity, (2) Avoid keeper * the interrupt buffer from interrupt activity, (2) Avoid keeper
@@ -290,42 +254,9 @@ int syslog_flush_intbuffer(bool force)
break; break;
} }
for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++) c = (char)ch;
{
FAR syslog_channel_t *channel = g_syslog_channel[i];
syslog_putc_t putfunc;
if (channel == NULL) syslog_write_foreach(&c, 1, force);
{
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);
}
} }
leave_critical_section(flags); leave_critical_section(flags);
+93 -185
View File
@@ -77,7 +77,11 @@ static bool syslog_safe_to_block(void)
} }
/**************************************************************************** /****************************************************************************
* Name: syslog_default_write * Public Functions
****************************************************************************/
/****************************************************************************
* Name: syslog_write_foreach
* *
* Description: * Description:
* This provides a default write method for syslog devices that do not * This provides a default write method for syslog devices that do not
@@ -94,227 +98,118 @@ static bool syslog_safe_to_block(void)
* *
****************************************************************************/ ****************************************************************************/
static ssize_t syslog_default_write(FAR const char *buffer, size_t buflen) ssize_t syslog_write_foreach(FAR const char *buffer,
size_t buflen, bool force)
{ {
size_t nwritten; syslog_write_t write;
syslog_putc_t putc;
size_t nwritten = 0;
ssize_t ret;
int i;
if (!syslog_safe_to_block()) for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++)
{ {
#ifdef CONFIG_SYSLOG_INTBUFFER FAR syslog_channel_t *channel = g_syslog_channel[i];
if (up_interrupt_context())
if (channel == NULL)
{ {
for (nwritten = 0; nwritten < buflen; nwritten++) break;
{
syslog_add_intbuffer(buffer[nwritten]);
}
} }
else
#endif
{
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 #ifdef CONFIG_SYSLOG_IOCTL
if (channel->sc_state & SYSLOG_CHANNEL_DISABLE) if (channel->sc_state & SYSLOG_CHANNEL_DISABLE)
{ {
continue; continue;
} }
#endif #endif
if (channel->sc_ops->sc_write_force != NULL) write = !force ? channel->sc_ops->sc_write :
{ channel->sc_ops->sc_write_force;
#ifdef CONFIG_SYSLOG_CRLF if (write != NULL)
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; 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 #ifdef CONFIG_SYSLOG_CRLF
if (!(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF)) if (!(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF))
{ {
size_t head; size_t head;
for (head = 0; head < buflen; head++) for (head = 0; head < buflen; head++)
{
if (buffer[head] != '\n')
{ {
size_t ret; continue;
/* Check for LF */
if (buffer[head] != '\n')
{
continue;
}
ret = channel->sc_ops->sc_write(channel,
buffer + nwritten,
head - nwritten);
if (ret < 0)
{
return ret;
}
/* Add CR */
ret = channel->sc_ops->sc_write(channel, "\r\n", 2);
if (ret < 0)
{
return ret;
}
nwritten = head + 1;
} }
}
#endif
if (nwritten < buflen) ret = write(channel, buffer + nwritten, head - nwritten);
{ if (ret >= 0)
ssize_t ret; {
ret = write(channel, "\r\n", 2);
}
ret = channel->sc_ops->sc_write(channel,
buffer + nwritten,
buflen - nwritten);
if (ret < 0) if (ret < 0)
{ {
return ret; return ret;
} }
else
{ nwritten = head + 1;
nwritten += ret;
}
} }
} }
else #endif
if (nwritten < buflen)
{
ret = write(channel, buffer + nwritten, buflen - nwritten);
if (ret < 0)
{
return ret;
}
else
{
nwritten += ret;
}
}
}
else
{
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
{ {
DEBUGASSERT(channel->sc_ops->sc_putc != NULL);
for (nwritten = 0; nwritten < buflen; nwritten++) for (nwritten = 0; nwritten < buflen; nwritten++)
{ {
putc(channel, buffer[nwritten]);
}
}
#ifdef CONFIG_SYSLOG_CRLF #ifdef CONFIG_SYSLOG_CRLF
/* Check for LF */ else
{
if (buffer[nwritten] == '\n' && for (nwritten = 0; nwritten < buflen; nwritten++)
!(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF)) {
if (buffer[nwritten] == '\n')
{ {
/* Add CR */ /* Add CR */
channel->sc_ops->sc_putc(channel, '\r'); putc(channel, '\r');
} }
#endif
channel->sc_ops->sc_putc(channel, buffer[nwritten]); putc(channel, buffer[nwritten]);
} }
} }
#endif
} }
} }
return nwritten; return nwritten;
} }
/****************************************************************************
* Public Functions
****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: syslog_write * 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) ssize_t syslog_write(FAR const char *buffer, size_t buflen)
{ {
bool force = !syslog_safe_to_block();
#ifdef CONFIG_SYSLOG_INTBUFFER #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 /* Flush any characters that may have been added to the interrupt
* buffer. * buffer.
@@ -344,5 +252,5 @@ ssize_t syslog_write(FAR const char *buffer, size_t buflen)
} }
#endif #endif
return syslog_default_write(buffer, buflen); return syslog_write_foreach(buffer, buflen, force);
} }