Skip to content

Commit 0455167

Browse files
anchaoxiaoxiang781216
authored andcommitted
stream/syslog: use internal buffer to decoupling syslog with iob
Signed-off-by: chao an <[email protected]>
1 parent 1bc4b8d commit 0455167

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

drivers/syslog/Kconfig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ endif
7272
config SYSLOG_BUFFER
7373
bool "Use buffered output"
7474
default n
75-
select MM_IOB
7675
---help---
7776
Enables an buffering logic that will be used to serialize debug
7877
output from concurrent tasks. This enables allocation of one buffer
@@ -82,6 +81,16 @@ config SYSLOG_BUFFER
8281
then the output from multiple tasks that attempt to generate SYSLOG
8382
output may be interleaved and difficult to read.
8483

84+
if SYSLOG_BUFFER
85+
86+
config SYSLOG_BUFSIZE
87+
int "Syslog buffer size"
88+
default 64
89+
---help---
90+
The size of the syslog buffer in bytes.
91+
92+
endif
93+
8594
config SYSLOG_INTBUFFER
8695
bool "Use interrupt buffer"
8796
default n

include/nuttx/streams.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,15 @@ struct lib_syslogstream_s
236236
{
237237
struct lib_outstream_s public;
238238
#ifdef CONFIG_SYSLOG_BUFFER
239+
# ifdef CONFIG_MM_IOB
239240
FAR struct iob_s *iob;
241+
# else
242+
char buffer[CONFIG_SYSLOG_BUFSIZE];
243+
# endif
240244
#endif
245+
FAR char *base;
246+
int size;
247+
int offset;
241248
int last_ch;
242249
};
243250

libs/libc/stream/lib_syslogstream.c

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,42 +34,40 @@
3434
* Private Functions
3535
****************************************************************************/
3636

37+
#ifdef CONFIG_SYSLOG_BUFFER
3738
/****************************************************************************
3839
* Name: syslogstream_flush
3940
****************************************************************************/
4041

41-
#ifdef CONFIG_SYSLOG_BUFFER
42-
static int syslogstream_flush(FAR struct lib_syslogstream_s *stream)
42+
static int syslogstream_flush(FAR struct lib_outstream_s *ostream)
4343
{
44-
FAR struct iob_s *iob;
44+
FAR struct lib_syslogstream_s *stream = (FAR void *)ostream;
4545
int ret = OK;
4646

4747
DEBUGASSERT(stream != NULL);
48-
iob = stream->iob;
4948

5049
/* Do we have an IO buffer? Is there anything buffered? */
5150

52-
if (iob != NULL && iob->io_len > 0)
51+
if (stream->base != NULL && stream->offset > 0)
5352
{
5453
/* Yes write the buffered data */
5554

5655
do
5756
{
58-
ssize_t nbytes = syslog_write((FAR const char *)iob->io_data,
59-
iob->io_len);
57+
ssize_t nbytes = syslog_write(stream->base, stream->offset);
6058
if (nbytes < 0)
6159
{
6260
ret = nbytes;
6361
}
6462
else
6563
{
66-
iob->io_len = 0;
6764
ret = OK;
6865
}
6966
}
7067
while (ret == -EINTR);
7168
}
7269

70+
stream->offset = 0;
7371
return ret;
7472
}
7573
#endif
@@ -82,48 +80,49 @@ static int syslogstream_flush(FAR struct lib_syslogstream_s *stream)
8280
static void syslogstream_addchar(FAR struct lib_syslogstream_s *stream,
8381
int ch)
8482
{
85-
FAR struct iob_s *iob = stream->iob;
86-
8783
/* Add the incoming character to the buffer */
8884

89-
iob->io_data[iob->io_len] = ch;
90-
iob->io_len++;
85+
stream->base[stream->offset] = ch;
86+
stream->offset++;
9187

9288
/* Increment the total number of bytes buffered. */
9389

9490
stream->public.nput++;
9591

9692
/* Is the buffer full? */
9793

98-
if (iob->io_len >= CONFIG_IOB_BUFSIZE)
94+
if (stream->offset >= stream->size)
9995
{
10096
/* Yes.. then flush the buffer */
10197

102-
syslogstream_flush(stream);
98+
syslogstream_flush(&stream->public);
10399
}
104100
}
105101

102+
/****************************************************************************
103+
* Name: syslogstream_addstring
104+
****************************************************************************/
105+
106106
static int syslogstream_addstring(FAR struct lib_syslogstream_s *stream,
107107
FAR const char *buff, int len)
108108
{
109-
FAR struct iob_s *iob = stream->iob;
110109
int ret = 0;
111110

112111
do
113112
{
114-
int remain = CONFIG_IOB_BUFSIZE - iob->io_len;
113+
int remain = stream->size - stream->offset;
115114
remain = remain > len - ret ? len - ret : remain;
116-
memcpy(iob->io_data + iob->io_len, buff + ret, remain);
117-
iob->io_len += remain;
115+
memcpy(stream->base + stream->offset, buff + ret, remain);
116+
stream->offset += remain;
118117
ret += remain;
119118

120119
/* Is the buffer enough? */
121120

122-
if (iob->io_len >= CONFIG_IOB_BUFSIZE)
121+
if (stream->offset >= stream->size)
123122
{
124123
/* Yes.. then flush the buffer */
125124

126-
syslogstream_flush(stream);
125+
syslogstream_flush(&stream->public);
127126
}
128127
}
129128
while (ret < len);
@@ -151,17 +150,17 @@ static void syslogstream_putc(FAR struct lib_outstream_s *this, int ch)
151150

152151
if (ch != '\r')
153152
{
154-
#ifdef CONFIG_SYSLOG_BUFFER
153+
# ifdef CONFIG_SYSLOG_BUFFER
155154
/* Do we have an IO buffer? */
156155

157-
if (stream->iob != NULL)
156+
if (stream->base != NULL)
158157
{
159158
/* Add the incoming character to the buffer */
160159

161160
syslogstream_addchar(stream, ch);
162161
}
163162
else
164-
#endif
163+
# endif
165164
{
166165
int ret;
167166

@@ -208,9 +207,10 @@ static int syslogstream_puts(FAR struct lib_outstream_s *this,
208207
stream->last_ch = ((FAR const char *)buff)[len - 1];
209208

210209
#ifdef CONFIG_SYSLOG_BUFFER
210+
211211
/* Do we have an IO buffer? */
212212

213-
if (stream->iob != NULL)
213+
if (stream->base != NULL)
214214
{
215215
/* Add the incoming string to the buffer */
216216

@@ -275,14 +275,28 @@ void lib_syslogstream_open(FAR struct lib_syslogstream_s *stream)
275275

276276
stream->public.putc = syslogstream_putc;
277277
stream->public.puts = syslogstream_puts;
278-
stream->public.flush = lib_noflush;
279278
stream->public.nput = 0;
280279

281280
#ifdef CONFIG_SYSLOG_BUFFER
281+
stream->public.flush = syslogstream_flush;
282+
282283
/* Allocate an IOB */
283284

285+
# ifdef CONFIG_MM_IOB
284286
stream->iob = iob_tryalloc(true);
287+
if (stream->iob != NULL)
288+
{
289+
stream->base = (FAR void *)stream->iob->io_data;
290+
stream->size = sizeof(stream->iob->io_data);
291+
}
292+
# else
293+
stream->base = stream->buffer;
294+
stream->size = sizeof(stream->buffer);
295+
# endif
296+
#else
297+
stream->public.flush = lib_noflush;
285298
#endif
299+
stream->offset = 0;
286300
}
287301

288302
/****************************************************************************
@@ -307,16 +321,20 @@ void lib_syslogstream_close(FAR struct lib_syslogstream_s *stream)
307321

308322
/* Verify that there is an IOB attached (there should be) */
309323

324+
# ifdef CONFIG_MM_IOB
310325
if (stream->iob != NULL)
311326
{
312327
/* Flush the output buffered in the IOB */
313328

314-
syslogstream_flush(stream);
329+
syslogstream_flush(&stream->public);
315330

316331
/* Free the IOB */
317332

318333
iob_free(stream->iob);
319334
stream->iob = NULL;
320335
}
336+
# else
337+
syslogstream_flush(&stream->public);
338+
# endif
321339
}
322340
#endif

0 commit comments

Comments
 (0)