mirror of
https://git.rtems.org/rtems-libbsd/
synced 2025-10-16 20:18:13 +08:00
subr_prf.c: Port to RTEMS
This commit is contained in:
@@ -54,8 +54,6 @@
|
||||
|
||||
#include <machine/rtems-bsd-kernel-namespace.h>
|
||||
|
||||
#include <machine/rtems-bsd-printf-to-iprintf.h>
|
||||
|
||||
/* Assert */
|
||||
|
||||
void rtems_bsd_assert_func(const char *file, int line, const char *func, const char *expr);
|
||||
|
@@ -44,6 +44,8 @@
|
||||
#include <sys/queue.h>
|
||||
#include <sys/kernel.h>
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <rtems.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
@@ -191,6 +193,27 @@ int rtems_bsd_bus_root_resume(void);
|
||||
*/
|
||||
int rtems_bsd_bus_root_detach(void);
|
||||
|
||||
/**
|
||||
* @brief Sets the output back-end for logging functions.
|
||||
*
|
||||
* @param new_vprintf_handler The new output back-end for logging functions.
|
||||
*
|
||||
* @see rtems_bsd_vprintf().
|
||||
*/
|
||||
void rtems_bsd_set_vprintf_handler(int (*new_vprintf_handler)
|
||||
(int, const char *, va_list));
|
||||
|
||||
/**
|
||||
* @brief Output back-end for logging functions.
|
||||
*
|
||||
* Used by kernel space printf(), vprintf(), log() and vlog(). Used by user
|
||||
* space syslog() and vsyslog().
|
||||
*
|
||||
* The default uses putchar() and uses a mutex to serialize the output. It may
|
||||
* be customized via rtems_bsd_set_vprintf_handler().
|
||||
*/
|
||||
int rtems_bsd_vprintf(int level, const char *fmt, va_list ap);
|
||||
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
@@ -1,46 +0,0 @@
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @ingroup rtems_bsd_rtems
|
||||
*
|
||||
* @brief TODO.
|
||||
*/
|
||||
|
||||
/*
|
||||
* COPYRIGHT (c) 2012. On-Line Applications Research Corporation (OAR).
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <machine/rtems-bsd-kernel-space.h>
|
||||
|
||||
#include <rtems/bsd/sys/types.h>
|
||||
#include <sys/systm.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void
|
||||
log(int level, const char *fmt, ...)
|
||||
{
|
||||
puts(fmt);
|
||||
}
|
119
rtemsbsd/rtems/rtems-bsd-vprintf.c
Normal file
119
rtemsbsd/rtems/rtems-bsd-vprintf.c
Normal file
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* @file
|
||||
*
|
||||
* @ingroup rtems_bsd_rtems
|
||||
*
|
||||
* @brief TODO.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2014, 2016 embedded brains GmbH. All rights reserved.
|
||||
*
|
||||
* embedded brains GmbH
|
||||
* Dornierstr. 4
|
||||
* 82178 Puchheim
|
||||
* Germany
|
||||
* <rtems@embedded-brains.de>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
|
||||
*/
|
||||
|
||||
#include <machine/rtems-bsd-kernel-space.h>
|
||||
|
||||
#include <rtems/bsd/sys/types.h>
|
||||
#include <sys/lock.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/syslog.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <rtems/bsd/bsd.h>
|
||||
|
||||
#undef printf
|
||||
|
||||
#define VPRINTF_LOCK() _Mutex_Acquire(&vprintf_mtx)
|
||||
#define VPRINTF_UNLOCK() _Mutex_Release(&vprintf_mtx)
|
||||
|
||||
static const char * const log_priorities[] = {
|
||||
[LOG_EMERG] = "emerg",
|
||||
[LOG_ALERT] = "alert",
|
||||
[LOG_CRIT] = "crit",
|
||||
[LOG_ERR] = "err",
|
||||
[LOG_WARNING] = "warning",
|
||||
[LOG_NOTICE] = "notice",
|
||||
[LOG_INFO] = "info",
|
||||
[LOG_DEBUG] = "debug"
|
||||
};
|
||||
|
||||
/* Use a <sys/lock.h> mutex due to the static initialization capability */
|
||||
static struct _Mutex_Control vprintf_mtx = _MUTEX_INITIALIZER;
|
||||
|
||||
static void
|
||||
vprintf_putchar(int c, void *arg)
|
||||
{
|
||||
int *last;
|
||||
|
||||
last = arg;
|
||||
*last = c;
|
||||
putchar(c);
|
||||
}
|
||||
|
||||
static int
|
||||
default_vprintf_handler(int level, const char *fmt, va_list ap)
|
||||
{
|
||||
int n;
|
||||
int last;
|
||||
|
||||
VPRINTF_LOCK();
|
||||
|
||||
if (level != LOG_PRINTF) {
|
||||
printf("%s: ", log_priorities[LOG_PRI(level)]);
|
||||
}
|
||||
|
||||
last = -1;
|
||||
n = kvprintf(fmt, vprintf_putchar, &last, 10, ap);
|
||||
|
||||
if (level != LOG_PRINTF && last != '\n') {
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
VPRINTF_UNLOCK();
|
||||
return (n);
|
||||
}
|
||||
|
||||
static int (*vprintf_handler)(int, const char *, va_list) =
|
||||
default_vprintf_handler;
|
||||
|
||||
void
|
||||
rtems_bsd_set_vprintf_handler(int (*new_vprintf_handler)
|
||||
(int, const char *, va_list))
|
||||
{
|
||||
|
||||
vprintf_handler = new_vprintf_handler;
|
||||
}
|
||||
|
||||
int
|
||||
rtems_bsd_vprintf(int level, const char *fmt, va_list ap)
|
||||
{
|
||||
|
||||
return ((*vprintf_handler)(level, fmt, ap));
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
/*
|
||||
* RTEMS version of syslog initialization
|
||||
*/
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
|
||||
struct in_addr rtems_bsdnet_log_host_address = {0};
|
||||
|
||||
int rtems_syslog_initialize(const char *log_host)
|
||||
{
|
||||
struct in_addr *i;
|
||||
|
||||
i = &rtems_bsdnet_log_host_address;
|
||||
|
||||
if (log_host)
|
||||
i->s_addr = inet_addr(log_host);
|
||||
return 0;
|
||||
}
|
@@ -7,7 +7,7 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2014 embedded brains GmbH. All rights reserved.
|
||||
* Copyright (c) 2014, 2016 embedded brains GmbH. All rights reserved.
|
||||
*
|
||||
* embedded brains GmbH
|
||||
* Dornierstr. 4
|
||||
@@ -37,46 +37,9 @@
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <machine/rtems-bsd-user-space.h>
|
||||
#include <machine/rtems-bsd-program.h>
|
||||
|
||||
#include <syslog.h>
|
||||
#include <stdarg.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct {
|
||||
int mask;
|
||||
} rtems_bsd_syslog_context;
|
||||
|
||||
/* FIXME: This should be thread specific */
|
||||
static rtems_bsd_syslog_context rtems_bsd_syslog_instance = {
|
||||
.mask = LOG_UPTO(LOG_DEBUG)
|
||||
};
|
||||
|
||||
static const char * const rtems_bsd_syslog_priorities[] = {
|
||||
[LOG_EMERG] = "emerg",
|
||||
[LOG_ALERT] = "alert",
|
||||
[LOG_CRIT] = "crit",
|
||||
[LOG_ERR] = "err",
|
||||
[LOG_WARNING] = "warning",
|
||||
[LOG_NOTICE] = "notice",
|
||||
[LOG_INFO] = "info",
|
||||
[LOG_DEBUG] = "debug"
|
||||
};
|
||||
|
||||
static rtems_bsd_syslog_context *
|
||||
rtems_bsd_syslog_get_context(void)
|
||||
{
|
||||
return &rtems_bsd_syslog_instance;
|
||||
}
|
||||
|
||||
static void
|
||||
rtems_bsd_syslog_format_buffer_overflow(void)
|
||||
{
|
||||
fputs("err: syslog: format buffer overflow\n", stderr);
|
||||
}
|
||||
#include <rtems/bsd/bsd.h>
|
||||
|
||||
void
|
||||
syslog(int priority, const char *format, ...)
|
||||
@@ -91,83 +54,8 @@ syslog(int priority, const char *format, ...)
|
||||
void
|
||||
vsyslog(int priority, const char *format, va_list ap)
|
||||
{
|
||||
rtems_bsd_syslog_context *ctx = rtems_bsd_syslog_get_context();
|
||||
int pri = LOG_PRI(priority);
|
||||
char fmt[128];
|
||||
char buf[128];
|
||||
const char *src;
|
||||
char *dst;
|
||||
size_t rem;
|
||||
char *m;
|
||||
int n;
|
||||
size_t len;
|
||||
|
||||
if ((LOG_MASK(pri) & ctx->mask) == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Expand the %m in the format string and add a newline character */
|
||||
|
||||
src = format;
|
||||
dst = &fmt[0];
|
||||
rem = sizeof(fmt) - 2;
|
||||
|
||||
while ((m = strstr(src, "%m")) != NULL) {
|
||||
size_t c = m - src;
|
||||
|
||||
if (c > rem) {
|
||||
rtems_bsd_syslog_format_buffer_overflow();
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(dst, src, c);
|
||||
dst += c;
|
||||
src += c + 2;
|
||||
rem -= c;
|
||||
|
||||
n = sniprintf(dst, rem, "%s", strerror(errno));
|
||||
if (n > rem || n < 0) {
|
||||
rtems_bsd_syslog_format_buffer_overflow();
|
||||
return;
|
||||
}
|
||||
|
||||
dst += (size_t) n;
|
||||
rem -= (size_t) n;
|
||||
}
|
||||
|
||||
len = strlen(src);
|
||||
if (len > rem) {
|
||||
rtems_bsd_syslog_format_buffer_overflow();
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(dst, src, len);
|
||||
dst += len;
|
||||
dst[0] = '\n';
|
||||
dst[1] = '\0';
|
||||
|
||||
/* Print into buffer */
|
||||
|
||||
dst = &buf[0];
|
||||
rem = sizeof(buf) - 1;
|
||||
|
||||
n = sniprintf(dst, rem, "%s: ", rtems_bsd_syslog_priorities[pri]);
|
||||
if (n <= rem) {
|
||||
dst += (size_t) n;
|
||||
rem -= (size_t) n;
|
||||
}
|
||||
|
||||
n = sniprintf(dst, rem, "%s: ", rtems_bsd_program_get_name());
|
||||
if (n <= rem) {
|
||||
dst += (size_t) n;
|
||||
rem -= (size_t) n;
|
||||
}
|
||||
|
||||
vsniprintf(dst, rem, &fmt[0], ap);
|
||||
|
||||
/* Write in one rush */
|
||||
|
||||
fputs(&buf[0], stderr);
|
||||
rtems_bsd_vprintf(priority, format, ap);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -185,10 +73,5 @@ closelog(void)
|
||||
int
|
||||
setlogmask(int mask)
|
||||
{
|
||||
rtems_bsd_syslog_context *ctx = rtems_bsd_syslog_get_context();
|
||||
int cur = ctx->mask;
|
||||
|
||||
ctx->mask = mask & LOG_UPTO(LOG_DEBUG);
|
||||
|
||||
return cur;
|
||||
/* TODO */
|
||||
}
|
||||
|
Reference in New Issue
Block a user