source: rtems-libbsd/rtemsbsd/rtems/rtems-kernel-vprintf.c @ 91327bd

55-freebsd-126-freebsd-12
Last change on this file since 91327bd was 91327bd, checked in by Sebastian Huber <sebastian.huber@…>, on 10/10/18 at 10:12:08

Use global stdout for kernel output and syslog()

  • Property mode set to 100644
File size: 3.5 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup rtems_bsd_rtems
5 *
6 * @brief TODO.
7 */
8
9/*
10 * Copyright (c) 2014, 2016 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Dornierstr. 4
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40#include <machine/rtems-bsd-kernel-space.h>
41
42#include <sys/types.h>
43#include <sys/lock.h>
44#include <sys/reent.h>
45#include <sys/systm.h>
46#include <sys/syslog.h>
47
48#include <stdio.h>
49
50#include <rtems/bsd/bsd.h>
51
52#define VPRINTF_LOCK() _Mutex_Acquire(&vprintf_mtx)
53#define VPRINTF_UNLOCK() _Mutex_Release(&vprintf_mtx)
54
55static const char * const log_priorities[] = {
56        [LOG_EMERG] = "emerg",
57        [LOG_ALERT] = "alert",
58        [LOG_CRIT] = "crit",
59        [LOG_ERR] = "err",
60        [LOG_WARNING] = "warning",
61        [LOG_NOTICE] = "notice",
62        [LOG_INFO] = "info",
63        [LOG_DEBUG] = "debug"
64};
65
66/* Use a <sys/lock.h> mutex due to the static initialization capability */
67static struct _Mutex_Control vprintf_mtx = _MUTEX_INITIALIZER;
68
69static void
70default_putchar(int c)
71{
72
73        /*
74         * Output to the global stdout FILE object and not to the thread-local
75         * stdout FILE object.
76         */
77        fputc(c, &__sf[1]);
78}
79
80static void
81kvprintf_putchar(int c, void *arg)
82{
83        int *last;
84
85        last = arg;
86        *last = c;
87        default_putchar(c);
88}
89
90static int
91default_vprintf_handler(int level, const char *fmt, va_list ap)
92{
93        int n;
94        int last;
95
96        VPRINTF_LOCK();
97
98        if (level != LOG_PRINTF) {
99                const char *p;
100
101                p = log_priorities[LOG_PRI(level)];
102
103                while (*p != '\0') {
104                        default_putchar(*p);
105                        ++p;
106                }
107
108                default_putchar(':');
109                default_putchar(' ');
110        }
111
112        last = -1;
113        n = kvprintf(fmt, kvprintf_putchar, &last, 10, ap);
114
115        if (level != LOG_PRINTF && last != '\n') {
116                default_putchar('\n');
117        }
118
119        VPRINTF_UNLOCK();
120        return (n);
121}
122
123static int (*vprintf_handler)(int, const char *, va_list) =
124    default_vprintf_handler;
125
126int
127rtems_bsd_vprintf_handler_mute(int level, const char *fmt, va_list ap)
128{
129        (void) level;
130        (void) fmt;
131        (void) ap;
132
133        return 0;
134}
135
136rtems_bsd_vprintf_handler
137rtems_bsd_set_vprintf_handler(rtems_bsd_vprintf_handler new_handler)
138{
139        rtems_bsd_vprintf_handler old_handler;
140
141        VPRINTF_LOCK();
142        old_handler = vprintf_handler;
143        vprintf_handler = new_handler;
144        VPRINTF_UNLOCK();
145        return (old_handler);
146}
147
148int
149rtems_bsd_vprintf(int level, const char *fmt, va_list ap)
150{
151
152        return ((*vprintf_handler)(level, fmt, ap));
153}
Note: See TracBrowser for help on using the repository browser.