Skip to content

Commit c7a0288

Browse files
anchaoxiaoxiang781216
authored andcommitted
syslog/channel: unify syslog channel writing to reduce redundant code
unify syslog channel writing to reduce redundant code Signed-off-by: chao an <[email protected]>
1 parent 5f5350c commit c7a0288

File tree

3 files changed

+111
-250
lines changed

3 files changed

+111
-250
lines changed

drivers/syslog/syslog.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,28 @@ int syslog_add_intbuffer(int ch);
230230
#ifdef CONFIG_SYSLOG_INTBUFFER
231231
int syslog_flush_intbuffer(bool force);
232232
#endif
233+
234+
/****************************************************************************
235+
* Name: syslog_write_foreach
236+
*
237+
* Description:
238+
* This provides a default write method for syslog devices that do not
239+
* support multiple byte writes This functions simply loops, outputting
240+
* one character at a time.
241+
*
242+
* Input Parameters:
243+
* buffer - The buffer containing the data to be output
244+
* buflen - The number of bytes in the buffer
245+
* force - Use the force() method of the channel vs. the putc() method.
246+
*
247+
* Returned Value:
248+
* On success, the number of characters written is returned. A negated
249+
* errno value is returned on any failure.
250+
*
251+
****************************************************************************/
252+
253+
ssize_t syslog_write_foreach(FAR const char *buffer,
254+
size_t buflen, bool force);
233255
#endif /* CONFIG_SYSLOG */
234256

235257
#undef EXTERN

drivers/syslog/syslog_intbuffer.c

Lines changed: 7 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static struct g_syslog_intbuffer_s g_syslog_intbuffer;
8989
*
9090
****************************************************************************/
9191

92-
int syslog_remove_intbuffer(void)
92+
static int syslog_remove_intbuffer(void)
9393
{
9494
irqstate_t flags;
9595
uint32_t outndx;
@@ -185,46 +185,11 @@ int syslog_add_intbuffer(int ch)
185185

186186
if (inuse == CONFIG_SYSLOG_INTBUFSIZE - 1)
187187
{
188-
int oldch = syslog_remove_intbuffer();
189-
int i;
188+
char oldch = syslog_remove_intbuffer();
190189

191-
for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++)
192-
{
193-
FAR syslog_channel_t *channel = g_syslog_channel[i];
194-
195-
if (channel == NULL)
196-
{
197-
break;
198-
}
199-
200-
#ifdef CONFIG_SYSLOG_IOCTL
201-
if (channel->sc_state & SYSLOG_CHANNEL_DISABLE)
202-
{
203-
continue;
204-
}
205-
#endif
206-
207-
if (channel->sc_ops->sc_force == NULL)
208-
{
209-
continue;
210-
}
211-
212-
#ifdef CONFIG_SYSLOG_CRLF
213-
/* Check for LF */
214-
215-
if (oldch == '\n' &&
216-
!(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF))
217-
{
218-
/* Add CR */
190+
syslog_write_foreach(&oldch, 1, true);
219191

220-
channel->sc_ops->sc_force(channel, '\r');
221-
}
222-
#endif
223-
224-
channel->sc_ops->sc_force(channel, oldch);
225-
}
226-
227-
ret = -ENOSPC;
192+
ret = -ENOSPC;
228193
}
229194

230195
/* Copy one character */
@@ -265,6 +230,7 @@ int syslog_add_intbuffer(int ch)
265230
int syslog_flush_intbuffer(bool force)
266231
{
267232
irqstate_t flags;
233+
char c;
268234
int ch;
269235

270236
/* This logic is performed with the scheduler disabled to protect from
@@ -275,8 +241,6 @@ int syslog_flush_intbuffer(bool force)
275241

276242
for (; ; )
277243
{
278-
int i;
279-
280244
/* Transfer one character to time. This is inefficient, but is
281245
* done in this way to: (1) Deal with concurrent modification of
282246
* the interrupt buffer from interrupt activity, (2) Avoid keeper
@@ -290,42 +254,9 @@ int syslog_flush_intbuffer(bool force)
290254
break;
291255
}
292256

293-
for (i = 0; i < CONFIG_SYSLOG_MAX_CHANNELS; i++)
294-
{
295-
FAR syslog_channel_t *channel = g_syslog_channel[i];
296-
syslog_putc_t putfunc;
297-
298-
if (channel == NULL)
299-
{
300-
break;
301-
}
302-
303-
#ifdef CONFIG_SYSLOG_IOCTL
304-
if (channel->sc_state & SYSLOG_CHANNEL_DISABLE)
305-
{
306-
continue;
307-
}
308-
#endif
309-
310-
/* Select which putc function to use for this flush */
311-
312-
putfunc = force ? channel->sc_ops->sc_force :
313-
channel->sc_ops->sc_putc;
314-
315-
#ifdef CONFIG_SYSLOG_CRLF
316-
/* Check for LF */
257+
c = (char)ch;
317258

318-
if (ch == '\n' &&
319-
!(channel->sc_state & SYSLOG_CHANNEL_DISABLE_CRLF))
320-
{
321-
/* Add CR */
322-
323-
putfunc(channel, '\r');
324-
}
325-
#endif
326-
327-
putfunc(channel, ch);
328-
}
259+
syslog_write_foreach(&c, 1, force);
329260
}
330261

331262
leave_critical_section(flags);

0 commit comments

Comments
 (0)