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/systm.h> |
---|
45 | #include <sys/syslog.h> |
---|
46 | |
---|
47 | #include <stdio.h> |
---|
48 | |
---|
49 | #include <rtems/bsd/bsd.h> |
---|
50 | |
---|
51 | #undef printf |
---|
52 | |
---|
53 | #define VPRINTF_LOCK() _Mutex_Acquire(&vprintf_mtx) |
---|
54 | #define VPRINTF_UNLOCK() _Mutex_Release(&vprintf_mtx) |
---|
55 | |
---|
56 | static const char * const log_priorities[] = { |
---|
57 | [LOG_EMERG] = "emerg", |
---|
58 | [LOG_ALERT] = "alert", |
---|
59 | [LOG_CRIT] = "crit", |
---|
60 | [LOG_ERR] = "err", |
---|
61 | [LOG_WARNING] = "warning", |
---|
62 | [LOG_NOTICE] = "notice", |
---|
63 | [LOG_INFO] = "info", |
---|
64 | [LOG_DEBUG] = "debug" |
---|
65 | }; |
---|
66 | |
---|
67 | /* Use a <sys/lock.h> mutex due to the static initialization capability */ |
---|
68 | static struct _Mutex_Control vprintf_mtx = _MUTEX_INITIALIZER; |
---|
69 | |
---|
70 | static void |
---|
71 | vprintf_putchar(int c, void *arg) |
---|
72 | { |
---|
73 | int *last; |
---|
74 | |
---|
75 | last = arg; |
---|
76 | *last = c; |
---|
77 | putchar(c); |
---|
78 | } |
---|
79 | |
---|
80 | static int |
---|
81 | default_vprintf_handler(int level, const char *fmt, va_list ap) |
---|
82 | { |
---|
83 | int n; |
---|
84 | int last; |
---|
85 | |
---|
86 | VPRINTF_LOCK(); |
---|
87 | |
---|
88 | if (level != LOG_PRINTF) { |
---|
89 | printf("%s: ", log_priorities[LOG_PRI(level)]); |
---|
90 | } |
---|
91 | |
---|
92 | last = -1; |
---|
93 | n = kvprintf(fmt, vprintf_putchar, &last, 10, ap); |
---|
94 | |
---|
95 | if (level != LOG_PRINTF && last != '\n') { |
---|
96 | putchar('\n'); |
---|
97 | } |
---|
98 | |
---|
99 | VPRINTF_UNLOCK(); |
---|
100 | return (n); |
---|
101 | } |
---|
102 | |
---|
103 | static int (*vprintf_handler)(int, const char *, va_list) = |
---|
104 | default_vprintf_handler; |
---|
105 | |
---|
106 | void |
---|
107 | rtems_bsd_set_vprintf_handler(int (*new_vprintf_handler) |
---|
108 | (int, const char *, va_list)) |
---|
109 | { |
---|
110 | |
---|
111 | vprintf_handler = new_vprintf_handler; |
---|
112 | } |
---|
113 | |
---|
114 | int |
---|
115 | rtems_bsd_vprintf(int level, const char *fmt, va_list ap) |
---|
116 | { |
---|
117 | |
---|
118 | return ((*vprintf_handler)(level, fmt, ap)); |
---|
119 | } |
---|