diff --git a/include/nuttx/streams.h b/include/nuttx/streams.h index eb0065974ca..0e74d9bdb2f 100644 --- a/include/nuttx/streams.h +++ b/include/nuttx/streams.h @@ -230,6 +230,12 @@ struct lib_bufferedoutstream_s * lib_outstream_s */ +struct lib_syslogstream_s +{ + struct lib_outstream_s public; + int priority; +}; + struct iob_s; /* Forward reference */ struct lib_syslograwstream_s @@ -458,6 +464,24 @@ void lib_zeroinstream(FAR struct lib_instream_s *zeroinstream); void lib_nullinstream(FAR struct lib_instream_s *nullinstream); void lib_nulloutstream(FAR struct lib_outstream_s *nulloutstream); +/**************************************************************************** + * Name: lib_syslogstream + * + * Description: + * Initializes syslog stream + * + * Input Parameters: + * stream - User allocated, uninitialized instance of struct + * lib_syslogstream_s to be initialized. + * priority - log priority. + * + * Returned Value: + * None (User allocated instance initialized). + * + ****************************************************************************/ + +void lib_syslogstream(FAR struct lib_syslogstream_s *stream, int priority); + /**************************************************************************** * Name: lib_syslograwstream_open * diff --git a/libs/libc/stream/Make.defs b/libs/libc/stream/Make.defs index 8b785f42dc8..055a4f7063d 100644 --- a/libs/libc/stream/Make.defs +++ b/libs/libc/stream/Make.defs @@ -26,7 +26,7 @@ CSRCS += lib_memsostream.c lib_lowoutstream.c lib_rawinstream.c CSRCS += lib_rawoutstream.c lib_rawsistream.c lib_rawsostream.c CSRCS += lib_zeroinstream.c lib_nullinstream.c lib_nulloutstream.c CSRCS += lib_mtdoutstream.c lib_libnoflush.c lib_libsnoflush.c -CSRCS += lib_syslograwstream.c lib_bufferedoutstream.c +CSRCS += lib_syslogstream.c lib_syslograwstream.c lib_bufferedoutstream.c # The remaining sources files depend upon C streams diff --git a/libs/libc/stream/lib_syslogstream.c b/libs/libc/stream/lib_syslogstream.c new file mode 100644 index 00000000000..58fa488b71f --- /dev/null +++ b/libs/libc/stream/lib_syslogstream.c @@ -0,0 +1,97 @@ +/**************************************************************************** + * libs/libc/stream/lib_syslogstream.c + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include +#include + +#include + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: syslogstream_putc + ****************************************************************************/ + +static void syslogstream_putc(FAR struct lib_outstream_s *this, int ch) +{ + FAR struct lib_syslogstream_s *stream = + (FAR struct lib_syslogstream_s *)this; + + DEBUGASSERT(stream != NULL); + syslog(stream->priority, "%c", ch); + stream->public.nput++; +} + +static int syslogstream_puts(FAR struct lib_outstream_s *this, + FAR const void *buff, int len) +{ + FAR struct lib_syslogstream_s *stream = + (FAR struct lib_syslogstream_s *)this; + + DEBUGASSERT(stream != NULL); + if (len <= 0) + { + return 0; + } + + syslog(stream->priority, "%.*s", len, (FAR const char *)buff); + return len; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: lib_syslogstream + * + * Description: + * Initializes syslog stream + * + * Input Parameters: + * stream - User allocated, uninitialized instance of struct + * lib_syslogstream_s to be initialized. + * priority - log priority. + * + * Returned Value: + * None (User allocated instance initialized). + * + ****************************************************************************/ + +void lib_syslogstream(FAR struct lib_syslogstream_s *stream, int priority) +{ + DEBUGASSERT(stream != NULL); + + /* Initialize the common fields */ + + stream->public.nput = 0; + stream->public.putc = syslogstream_putc; + stream->public.puts = syslogstream_puts; + stream->public.flush = lib_noflush; + stream->priority = priority; +}