Skip to content

Commit 0203839

Browse files
stream: Add syslogstream implementation
which forward the output to syslog Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com> Signed-off-by: chao an <anchao@xiaomi.com>
1 parent 20ea607 commit 0203839

File tree

3 files changed

+122
-1
lines changed

3 files changed

+122
-1
lines changed

include/nuttx/streams.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ struct lib_bufferedoutstream_s
230230
* lib_outstream_s
231231
*/
232232

233+
struct lib_syslogstream_s
234+
{
235+
struct lib_outstream_s public;
236+
int priority;
237+
};
238+
233239
struct iob_s; /* Forward reference */
234240

235241
struct lib_syslograwstream_s
@@ -458,6 +464,24 @@ void lib_zeroinstream(FAR struct lib_instream_s *zeroinstream);
458464
void lib_nullinstream(FAR struct lib_instream_s *nullinstream);
459465
void lib_nulloutstream(FAR struct lib_outstream_s *nulloutstream);
460466

467+
/****************************************************************************
468+
* Name: lib_syslogstream
469+
*
470+
* Description:
471+
* Initializes syslog stream
472+
*
473+
* Input Parameters:
474+
* stream - User allocated, uninitialized instance of struct
475+
* lib_syslogstream_s to be initialized.
476+
* priority - log priority.
477+
*
478+
* Returned Value:
479+
* None (User allocated instance initialized).
480+
*
481+
****************************************************************************/
482+
483+
void lib_syslogstream(FAR struct lib_syslogstream_s *stream, int priority);
484+
461485
/****************************************************************************
462486
* Name: lib_syslograwstream_open
463487
*

libs/libc/stream/Make.defs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ CSRCS += lib_memsostream.c lib_lowoutstream.c lib_rawinstream.c
2626
CSRCS += lib_rawoutstream.c lib_rawsistream.c lib_rawsostream.c
2727
CSRCS += lib_zeroinstream.c lib_nullinstream.c lib_nulloutstream.c
2828
CSRCS += lib_mtdoutstream.c lib_libnoflush.c lib_libsnoflush.c
29-
CSRCS += lib_syslograwstream.c lib_bufferedoutstream.c
29+
CSRCS += lib_syslogstream.c lib_syslograwstream.c lib_bufferedoutstream.c
3030

3131
# The remaining sources files depend upon C streams
3232

libs/libc/stream/lib_syslogstream.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/****************************************************************************
2+
* libs/libc/stream/lib_syslogstream.c
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one or more
5+
* contributor license agreements. See the NOTICE file distributed with
6+
* this work for additional information regarding copyright ownership. The
7+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance with the
9+
* License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
* License for the specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
****************************************************************************/
20+
21+
/****************************************************************************
22+
* Included Files
23+
****************************************************************************/
24+
25+
#include <assert.h>
26+
#include <errno.h>
27+
#include <stddef.h>
28+
#include <syslog.h>
29+
30+
#include <nuttx/streams.h>
31+
32+
/****************************************************************************
33+
* Private Functions
34+
****************************************************************************/
35+
36+
/****************************************************************************
37+
* Name: syslogstream_putc
38+
****************************************************************************/
39+
40+
static void syslogstream_putc(FAR struct lib_outstream_s *this, int ch)
41+
{
42+
FAR struct lib_syslogstream_s *stream =
43+
(FAR struct lib_syslogstream_s *)this;
44+
45+
DEBUGASSERT(stream != NULL);
46+
syslog(stream->priority, "%c", ch);
47+
stream->public.nput++;
48+
}
49+
50+
static int syslogstream_puts(FAR struct lib_outstream_s *this,
51+
FAR const void *buff, int len)
52+
{
53+
FAR struct lib_syslogstream_s *stream =
54+
(FAR struct lib_syslogstream_s *)this;
55+
56+
DEBUGASSERT(stream != NULL);
57+
if (len <= 0)
58+
{
59+
return 0;
60+
}
61+
62+
syslog(stream->priority, "%.*s", len, (FAR const char *)buff);
63+
return len;
64+
}
65+
66+
/****************************************************************************
67+
* Public Functions
68+
****************************************************************************/
69+
70+
/****************************************************************************
71+
* Name: lib_syslogstream
72+
*
73+
* Description:
74+
* Initializes syslog stream
75+
*
76+
* Input Parameters:
77+
* stream - User allocated, uninitialized instance of struct
78+
* lib_syslogstream_s to be initialized.
79+
* priority - log priority.
80+
*
81+
* Returned Value:
82+
* None (User allocated instance initialized).
83+
*
84+
****************************************************************************/
85+
86+
void lib_syslogstream(FAR struct lib_syslogstream_s *stream, int priority)
87+
{
88+
DEBUGASSERT(stream != NULL);
89+
90+
/* Initialize the common fields */
91+
92+
stream->public.nput = 0;
93+
stream->public.putc = syslogstream_putc;
94+
stream->public.puts = syslogstream_puts;
95+
stream->public.flush = lib_noflush;
96+
stream->priority = priority;
97+
}

0 commit comments

Comments
 (0)