2016-06-21 21:33:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/syslog/syslog_filechannel.c
|
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* 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
|
2016-06-21 21:33:17 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-21 21:33:17 +02:00
|
|
|
*
|
2021-03-04 07:10:42 +01:00
|
|
|
* 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.
|
2016-06-21 21:33:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
2016-06-22 00:39:28 +02:00
|
|
|
#include <sched.h>
|
2016-06-21 21:33:17 +02:00
|
|
|
#include <fcntl.h>
|
2021-03-29 14:36:58 +02:00
|
|
|
#include <errno.h>
|
2016-06-21 21:33:17 +02:00
|
|
|
|
|
|
|
#include <nuttx/syslog/syslog.h>
|
|
|
|
|
|
|
|
#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 Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-29 14:36:58 +02:00
|
|
|
/* Handle to the SYSLOG channel */
|
|
|
|
|
|
|
|
FAR static struct syslog_channel_s *g_syslog_file_channel;
|
|
|
|
|
2016-06-21 21:33:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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:
|
2021-06-03 12:24:49 +02:00
|
|
|
* A pointer to the new SYSLOG channel; NULL is returned on any failure.
|
2016-06-21 21:33:17 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-03 12:24:49 +02:00
|
|
|
FAR struct syslog_channel_s *syslog_file_channel(FAR const char *devpath)
|
2016-06-21 21:33:17 +02:00
|
|
|
{
|
|
|
|
/* 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
|
2016-06-22 00:39:28 +02:00
|
|
|
* important debug output is lost while we futz with the channels.
|
2016-06-21 21:33:17 +02:00
|
|
|
*/
|
|
|
|
|
2016-06-22 00:39:28 +02:00
|
|
|
sched_lock();
|
2016-06-21 21:33:17 +02:00
|
|
|
|
|
|
|
/* Uninitialize any driver interface that may have been in place */
|
|
|
|
|
2021-03-29 14:36:58 +02:00
|
|
|
if (g_syslog_file_channel != NULL)
|
|
|
|
{
|
|
|
|
syslog_dev_uninitialize(g_syslog_file_channel);
|
|
|
|
}
|
2017-06-28 21:17:17 +02:00
|
|
|
|
2016-06-21 21:33:17 +02:00
|
|
|
/* Then initialize the file interface */
|
|
|
|
|
2021-03-29 14:36:58 +02:00
|
|
|
g_syslog_file_channel = syslog_dev_initialize(devpath, OPEN_FLAGS,
|
|
|
|
OPEN_MODE);
|
|
|
|
if (g_syslog_file_channel == NULL)
|
2016-06-21 21:33:17 +02:00
|
|
|
{
|
2016-06-22 00:39:28 +02:00
|
|
|
goto errout_with_lock;
|
2016-06-21 21:33:17 +02:00
|
|
|
}
|
|
|
|
|
2016-06-22 00:39:28 +02:00
|
|
|
/* Use the file as the SYSLOG channel. If this fails we are pretty much
|
|
|
|
* screwed.
|
|
|
|
*/
|
|
|
|
|
2021-06-03 12:24:49 +02:00
|
|
|
if (syslog_channel(g_syslog_file_channel) != OK)
|
|
|
|
{
|
|
|
|
syslog_dev_uninitialize(g_syslog_file_channel);
|
|
|
|
g_syslog_file_channel = NULL;
|
|
|
|
}
|
2016-06-21 21:33:17 +02:00
|
|
|
|
2016-06-22 00:39:28 +02:00
|
|
|
errout_with_lock:
|
|
|
|
sched_unlock();
|
2021-06-03 12:24:49 +02:00
|
|
|
return g_syslog_file_channel;
|
2016-06-21 21:33:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_SYSLOG_FILE */
|