|
| 1 | +/**************************************************************************** |
| 2 | + * drivers/syslog/syslog_stream.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 <nuttx/config.h> |
| 26 | + |
| 27 | +#include <errno.h> |
| 28 | +#include <sys/types.h> |
| 29 | + |
| 30 | +#include <nuttx/kmalloc.h> |
| 31 | +#include <nuttx/streams.h> |
| 32 | +#include <nuttx/syslog/syslog.h> |
| 33 | + |
| 34 | +#ifdef CONFIG_SYSLOG_STREAM |
| 35 | + |
| 36 | +/**************************************************************************** |
| 37 | + * Private Types |
| 38 | + ****************************************************************************/ |
| 39 | + |
| 40 | +/* This structure contains all SYSLOGing state information */ |
| 41 | + |
| 42 | +struct syslog_stream_s |
| 43 | +{ |
| 44 | + struct syslog_channel_s channel; |
| 45 | + FAR struct lib_outstream_s *stream; |
| 46 | +}; |
| 47 | + |
| 48 | +/**************************************************************************** |
| 49 | + * Private Function Prototypes |
| 50 | + ****************************************************************************/ |
| 51 | + |
| 52 | +static ssize_t syslog_stream_write(FAR struct syslog_channel_s *channel, |
| 53 | + FAR const char *buffer, size_t buflen); |
| 54 | +static int syslog_stream_putc(FAR struct syslog_channel_s *channel, int ch); |
| 55 | +static int syslog_stream_force(FAR struct syslog_channel_s *channel, int ch); |
| 56 | +static int syslog_stream_flush(FAR struct syslog_channel_s *channel); |
| 57 | +void syslog_stream_uninit(FAR struct syslog_channel_s *channel); |
| 58 | + |
| 59 | +/**************************************************************************** |
| 60 | + * Private Data |
| 61 | + ****************************************************************************/ |
| 62 | + |
| 63 | +static const struct syslog_channel_ops_s g_syslog_stream_ops = |
| 64 | +{ |
| 65 | + syslog_stream_putc, |
| 66 | + syslog_stream_force, |
| 67 | + syslog_stream_flush, |
| 68 | + syslog_stream_write, |
| 69 | + syslog_stream_uninit |
| 70 | +}; |
| 71 | + |
| 72 | +/**************************************************************************** |
| 73 | + * Private Functions |
| 74 | + ****************************************************************************/ |
| 75 | + |
| 76 | +/**************************************************************************** |
| 77 | + * Name: syslog_stream_write |
| 78 | + * |
| 79 | + * Description: |
| 80 | + * This is the low-level, multiple byte, system logging interface provided |
| 81 | + * for the driver interface. |
| 82 | + * |
| 83 | + * Input Parameters: |
| 84 | + * channel - Handle to syslog channel to be used. |
| 85 | + * buffer - The buffer containing the data to be output. |
| 86 | + * buflen - The number of bytes in the buffer. |
| 87 | + * |
| 88 | + * Returned Value: |
| 89 | + * On success, the number of characters written is returned. A negated |
| 90 | + * errno value is returned on any failure. |
| 91 | + * |
| 92 | + ****************************************************************************/ |
| 93 | + |
| 94 | +static ssize_t syslog_stream_write(FAR struct syslog_channel_s *channel, |
| 95 | + FAR const char *buffer, size_t buflen) |
| 96 | +{ |
| 97 | + FAR struct syslog_stream_s *chan = |
| 98 | + (FAR struct syslog_stream_s *)channel; |
| 99 | + ssize_t nwritten; |
| 100 | + irqstate_t flags; |
| 101 | + |
| 102 | + flags = enter_critical_section(); |
| 103 | + nwritten = lib_stream_puts(chan->stream, buffer, buflen); |
| 104 | + leave_critical_section(flags); |
| 105 | + return nwritten; |
| 106 | +} |
| 107 | + |
| 108 | +/**************************************************************************** |
| 109 | + * Name: syslog_stream_putc |
| 110 | + * |
| 111 | + * Description: |
| 112 | + * This is the low-level, single character, system logging interface |
| 113 | + * provided for the driver interface. |
| 114 | + * |
| 115 | + * Input Parameters: |
| 116 | + * channel - Handle to syslog channel to be used. |
| 117 | + * ch - The character to add to the SYSLOG (must be positive). |
| 118 | + * |
| 119 | + * Returned Value: |
| 120 | + * On success, the character is echoed back to the caller. A negated errno |
| 121 | + * value is returned on any failure. |
| 122 | + * |
| 123 | + ****************************************************************************/ |
| 124 | + |
| 125 | +static int syslog_stream_putc(FAR struct syslog_channel_s *channel, int ch) |
| 126 | +{ |
| 127 | + FAR struct syslog_stream_s *chan = |
| 128 | + (FAR struct syslog_stream_s *)channel; |
| 129 | + irqstate_t flags; |
| 130 | + |
| 131 | + flags = enter_critical_section(); |
| 132 | + lib_stream_put(chan->stream, ch); |
| 133 | + leave_critical_section(flags); |
| 134 | + |
| 135 | + return OK; |
| 136 | +} |
| 137 | + |
| 138 | +/**************************************************************************** |
| 139 | + * Name: syslog_stream_force |
| 140 | + * |
| 141 | + * Description: |
| 142 | + * Force output in interrupt context. |
| 143 | + * |
| 144 | + * Input Parameters: |
| 145 | + * channel - Handle to syslog channel to be used. |
| 146 | + * ch - The character to add to the SYSLOG (must be positive). |
| 147 | + * |
| 148 | + * Returned Value: |
| 149 | + * On success, the character is echoed back to the caller. A negated |
| 150 | + * errno value is returned on any failure. |
| 151 | + * |
| 152 | + ****************************************************************************/ |
| 153 | + |
| 154 | +static int syslog_stream_force(FAR struct syslog_channel_s *channel, int ch) |
| 155 | +{ |
| 156 | + FAR struct syslog_stream_s *chan = |
| 157 | + (FAR struct syslog_stream_s *)channel; |
| 158 | + |
| 159 | + lib_stream_put(chan->stream, ch); |
| 160 | + return OK; |
| 161 | +} |
| 162 | + |
| 163 | +/**************************************************************************** |
| 164 | + * Name: syslog_stream_flush |
| 165 | + * |
| 166 | + * Description: |
| 167 | + * Flush any buffer data in the file system to media. |
| 168 | + * |
| 169 | + * Input Parameters: |
| 170 | + * channel - Handle to syslog channel to be used. |
| 171 | + * |
| 172 | + * Returned Value: |
| 173 | + * Zero (OK) on success; a negated errno value is returned on any failure. |
| 174 | + * |
| 175 | + ****************************************************************************/ |
| 176 | + |
| 177 | +static int syslog_stream_flush(FAR struct syslog_channel_s *channel) |
| 178 | +{ |
| 179 | + FAR struct syslog_stream_s *chan = |
| 180 | + (FAR struct syslog_stream_s *)channel; |
| 181 | + irqstate_t flags; |
| 182 | + |
| 183 | + flags = enter_critical_section(); |
| 184 | + lib_stream_flush(chan->stream); |
| 185 | + leave_critical_section(flags); |
| 186 | + return OK; |
| 187 | +} |
| 188 | + |
| 189 | +/**************************************************************************** |
| 190 | + * Public Functions |
| 191 | + ****************************************************************************/ |
| 192 | + |
| 193 | +/**************************************************************************** |
| 194 | + * Name: syslog_stream_uninit |
| 195 | + * |
| 196 | + * Description: |
| 197 | + * Disable the channel in preparation to use a different |
| 198 | + * SYSLOG device. |
| 199 | + * |
| 200 | + * Input Parameters: |
| 201 | + * channel - Handle to syslog channel to be used. |
| 202 | + * |
| 203 | + * Returned Value: |
| 204 | + * Zero (OK) is returned on success; a negated errno value is returned on |
| 205 | + * any failure. |
| 206 | + * |
| 207 | + * Assumptions: |
| 208 | + * The caller has already switched the SYSLOG source to some safe channel |
| 209 | + * (the default channel). |
| 210 | + * |
| 211 | + ****************************************************************************/ |
| 212 | + |
| 213 | +void syslog_stream_uninit(FAR struct syslog_channel_s *channel) |
| 214 | +{ |
| 215 | + FAR struct syslog_stream_s *chan = |
| 216 | + (FAR struct syslog_stream_s *)channel; |
| 217 | + |
| 218 | + /* Attempt to flush any buffered data. */ |
| 219 | + |
| 220 | + syslog_stream_flush(channel); |
| 221 | + |
| 222 | + /* Free the channel structure */ |
| 223 | + |
| 224 | + kmm_free(chan); |
| 225 | +} |
| 226 | + |
| 227 | +/**************************************************************************** |
| 228 | + * Name: syslog_stream_channel |
| 229 | + * |
| 230 | + * Description: |
| 231 | + * Initialize to use the device stream as the SYSLOG sink. |
| 232 | + * |
| 233 | + * On power up, the SYSLOG facility is non-existent or limited to very |
| 234 | + * low-level output. This function may be called later in the |
| 235 | + * initialization sequence after full driver support has been initialized. |
| 236 | + * (via syslog_initialize()) It installs the configured SYSLOG drivers |
| 237 | + * and enables full SYSLOGing capability. |
| 238 | + * |
| 239 | + * Input Parameters: |
| 240 | + * stream - The stream device to be used. |
| 241 | + * |
| 242 | + * Returned Value: |
| 243 | + * Returns a newly created SYSLOG channel, or NULL in case of any failure. |
| 244 | + * |
| 245 | + ****************************************************************************/ |
| 246 | + |
| 247 | +FAR struct syslog_channel_s * |
| 248 | +syslog_stream_channel(FAR struct lib_outstream_s *stream) |
| 249 | +{ |
| 250 | + FAR struct syslog_stream_s *chan; |
| 251 | + |
| 252 | + DEBUGASSERT(stream != NULL); |
| 253 | + |
| 254 | + chan = (FAR struct syslog_stream_s *) |
| 255 | + kmm_zalloc(sizeof(struct syslog_stream_s)); |
| 256 | + |
| 257 | + if (chan == NULL) |
| 258 | + { |
| 259 | + return NULL; |
| 260 | + } |
| 261 | + |
| 262 | + chan->stream = stream; |
| 263 | + chan->channel.sc_ops = &g_syslog_stream_ops; |
| 264 | + return (FAR struct syslog_channel_s *)chan; |
| 265 | +} |
| 266 | + |
| 267 | +#endif /* CONFIG_SYSLOG_STREAM */ |
0 commit comments