From 0e1e3bc404e0ba2c8b69e9eff4143d472a5b6ab8 Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Tue, 21 Jun 2016 13:33:17 -0600 Subject: [PATCH] Add a file that I forgot in the last commit; Also update some comments. --- drivers/syslog/syslog_filechannel.c | 186 ++++++++++++++++++++++++++++ include/syslog.h | 52 ++++++++ 2 files changed, 238 insertions(+) create mode 100644 drivers/syslog/syslog_filechannel.c diff --git a/drivers/syslog/syslog_filechannel.c b/drivers/syslog/syslog_filechannel.c new file mode 100644 index 0000000000..b703b88928 --- /dev/null +++ b/drivers/syslog/syslog_filechannel.c @@ -0,0 +1,186 @@ +/**************************************************************************** + * drivers/syslog/syslog_filechannel.c + * + * Copyright (C) 2016 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include + +#include "syslog.h" + +#ifdef CONFIG_SYSLOG_FILE + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define OPEN_FLAGS (O_WRONLY | O_CREAT | O_APPEND) +#define OPEN_MODE (S_IROTH | S_IRGRP | S_IRUSR | S_IWUSR) + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* SYSLOG channel methods */ + +static int syslog_file_force(int ch); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* This structure describes the SYSLOG channel */ + +static const struct syslog_channel_s g_syslog_file_channel = +{ + syslog_dev_putc, + syslog_file_force, + syslog_dev_flush, +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: syslog_file_force + * + * Description: + * A dummy FORCE method + * + ****************************************************************************/ + +static int syslog_file_force(int ch) +{ + return ch; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: syslog_file_channel + * + * Description: + * Configure to use a file in a mounted file system at 'devpath' as the + * SYSLOG channel. + * + * This tiny function is simply a wrapper around syslog_dev_initialize() + * and syslog_channel(). It calls syslog_dev_initialize() to configure + * the character file at 'devpath then calls syslog_channel() to use that + * device as the SYSLOG output channel. + * + * File SYSLOG channels differ from other SYSLOG channels in that they + * cannot be established until after fully booting and mounting the target + * file system. This function would need to be called from board-specific + * bring-up logic AFTER mounting the file system containing 'devpath'. + * + * SYSLOG data generated prior to calling syslog_file_channel will, of + * course, not be included in the file. + * + * NOTE interrupt level SYSLOG output will be lost in this case unless + * the interrupt buffer is used. + * + * Input Parameters: + * devpath - The full path to the file to be used for SYSLOG output. + * This may be an existing file or not. If the file exists, + * syslog_file_channel() will append new SYSLOG data to the end of the + * file. If it does not, then syslog_file_channel() will create the + * file. + * + * Returned Value: + * Zero (OK) is returned on success; a negated errno value is returned on + * any failure. + * + ****************************************************************************/ + +int syslog_file_channel(FAR const char *devpath) +{ + FAR const struct syslog_channel_s *saved_channel; + int ret; + + /* Reset the default SYSLOG channel so that we can safely modify the + * SYSLOG device. This is an atomic operation and we should be safe + * after the default channel has been selected. + * + * We disable pre-emption only so that we are not suspended and a lot of + * important debug output is lost. + */ + + saved_channel = g_syslog_channel; + ret = syslog_channel(&g_default_syslog_channel); + if (ret < 0) + { + return ret; + } + + /* Uninitialize any driver interface that may have been in place */ + + ret = syslog_dev_uninitialize(); + if (ret < 0) + { + /* Nothing fatal has happened yet, we can restore the last channel + * since it was not uninitialized (was it?) + */ + + (void)syslog_channel(saved_channel); + return ret; + } + + /* Then initialize the file interface */ + + ret = syslog_dev_initialize(devpath, OPEN_FLAGS, OPEN_MODE); + if (ret < 0) + { + /* We should still be able to back-up an re-initialized everything */ + + (void)syslog_initialize(SYSLOG_INIT_EARLY); + (void)syslog_initialize(SYSLOG_INIT_LATE); + return ret; + } + + /* Use the file as the SYSLOG channel */ + + return syslog_channel(&g_syslog_file_channel); +} + +#endif /* CONFIG_SYSLOG_FILE */ diff --git a/include/syslog.h b/include/syslog.h index 075a42bfe8..4c1112cf76 100644 --- a/include/syslog.h +++ b/include/syslog.h @@ -137,8 +137,60 @@ extern "C" { #endif +/**************************************************************************** + * Name: openlog + * + * Description: + * The openlog() function sets process attributes that affect subsequent + * calls to syslog(). The ident argument is a string that is prepended to + * every message. The logopt argument indicates logging options. Values + * for logopt are constructed by a bitwise-inclusive OR of zero or more of + * the following: + * + * LOG_PID - Log the process ID with each message. This is useful for + * identifying specific processes. + * + * LOG_CONS - Write messages to the system console if they cannot be + * sent to the logging facility. The syslog() function ensures that + * the process does not acquire the console as a controlling terminal + * in the process of writing the message. + * + * LOG_NDELAY - Open the connection to the logging facility immediately. + * Normally the open is delayed until the first message is logged. + * This is useful for programs that need to manage the order in which + * file descriptors are allocated. + * + * LOG_ODELAY - Delay open until syslog() is called. + * + * LOG_NOWAIT - Do not wait for child processes that may have been + * created during the course of logging the message. This option + * should be used by processes that enable notification of child + * termination using SIGCHLD, since syslog() may otherwise block + * waiting for a child whose exit status has already been collected. + * + * The facility argument encodes a default facility to be assigned to all + * messages that do not have an explicit facility already encoded. The + * initial default facility is LOG_USER. + * + * It is not necessary to call openlog() prior to calling syslog(). + * + ****************************************************************************/ + #if 0 /* Not supported */ void openlog(FAR const char *ident, int option, int facility); +#endif + +/**************************************************************************** + * Name: closelog + * + * Description: + * The openlog() and syslog() functions may allocate a file descriptor. + * The closelog() function will close any open file descriptors allocated + * by previous calls to openlog() or syslog(). + * + ****************************************************************************/ + +#if 0 /* Not supported */ void closelog(void); #endif