|
| 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