Skip to content

Commit

Permalink
syslog/channel: unify syslog channel writing to reduce redundant code
Browse files Browse the repository at this point in the history
unify syslog channel writing to reduce redundant code

Signed-off-by: chao an <[email protected]>
  • Loading branch information
anchao committed Dec 11, 2024
1 parent 5607eec commit 064c653
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 251 deletions.
22 changes: 22 additions & 0 deletions drivers/syslog/syslog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 5 additions & 77 deletions drivers/syslog/syslog_intbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,46 +185,11 @@ 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 */
syslog_write_foreach(&oldch, 1, true);

channel->sc_ops->sc_force(channel, '\r');
}
#endif

channel->sc_ops->sc_force(channel, oldch);
}

ret = -ENOSPC;
ret = -ENOSPC;
}

/* Copy one character */
Expand Down Expand Up @@ -265,7 +230,7 @@ int syslog_add_intbuffer(int ch)
int syslog_flush_intbuffer(bool force)
{
irqstate_t flags;
int ch;
char ch;

/* This logic is performed with the scheduler disabled to protect from
* concurrent modification by other tasks.
Expand All @@ -275,8 +240,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
Expand All @@ -290,42 +253,7 @@ 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;

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(&ch, 1, force);
}

leave_critical_section(flags);
Expand Down
Loading

0 comments on commit 064c653

Please sign in to comment.