diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2016-05-04 13:04:42 +0200 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2016-05-06 14:07:31 +0200 |
commit | 54a577d44fc467221d6e7d831bd6f9c1d88849c5 (patch) | |
tree | 1b0ff9bab84ac1739076ce28b8103ddb0490d21f /rtemsbsd | |
parent | bcd.c: Import from FreeBSD (diff) | |
download | rtems-libbsd-54a577d44fc467221d6e7d831bd6f9c1d88849c5.tar.bz2 |
subr_prf.c: Port to RTEMS
Diffstat (limited to 'rtemsbsd')
-rw-r--r-- | rtemsbsd/include/machine/rtems-bsd-kernel-space.h | 2 | ||||
-rw-r--r-- | rtemsbsd/include/rtems/bsd/bsd.h | 23 | ||||
-rw-r--r-- | rtemsbsd/rtems/rtems-bsd-log.c | 46 | ||||
-rw-r--r-- | rtemsbsd/rtems/rtems-bsd-vprintf.c | 119 | ||||
-rw-r--r-- | rtemsbsd/rtems/rtems-syslog-initialize.c | 21 | ||||
-rw-r--r-- | rtemsbsd/rtems/syslog.c | 125 |
6 files changed, 146 insertions, 190 deletions
diff --git a/rtemsbsd/include/machine/rtems-bsd-kernel-space.h b/rtemsbsd/include/machine/rtems-bsd-kernel-space.h index 3a73ed27..2d119887 100644 --- a/rtemsbsd/include/machine/rtems-bsd-kernel-space.h +++ b/rtemsbsd/include/machine/rtems-bsd-kernel-space.h @@ -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); diff --git a/rtemsbsd/include/rtems/bsd/bsd.h b/rtemsbsd/include/rtems/bsd/bsd.h index e955b9f3..8ab0fdaf 100644 --- a/rtemsbsd/include/rtems/bsd/bsd.h +++ b/rtemsbsd/include/rtems/bsd/bsd.h @@ -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 diff --git a/rtemsbsd/rtems/rtems-bsd-log.c b/rtemsbsd/rtems/rtems-bsd-log.c deleted file mode 100644 index ec5f705c..00000000 --- a/rtemsbsd/rtems/rtems-bsd-log.c +++ /dev/null @@ -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); -} diff --git a/rtemsbsd/rtems/rtems-bsd-vprintf.c b/rtemsbsd/rtems/rtems-bsd-vprintf.c new file mode 100644 index 00000000..36f0c59c --- /dev/null +++ b/rtemsbsd/rtems/rtems-bsd-vprintf.c @@ -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)); +} diff --git a/rtemsbsd/rtems/rtems-syslog-initialize.c b/rtemsbsd/rtems/rtems-syslog-initialize.c deleted file mode 100644 index c092d269..00000000 --- a/rtemsbsd/rtems/rtems-syslog-initialize.c +++ /dev/null @@ -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; -} diff --git a/rtemsbsd/rtems/syslog.c b/rtemsbsd/rtems/syslog.c index d8274e09..76162563 100644 --- a/rtemsbsd/rtems/syslog.c +++ b/rtemsbsd/rtems/syslog.c @@ -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 */ } |