1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 /*
29  * Copyright (c) 1982, 1986, 1988, 1993
30  *	The Regents of the University of California.  All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions
34  * are met:
35  * 1. Redistributions of source code must retain the above copyright
36  *    notice, this list of conditions and the following disclaimer.
37  * 2. Redistributions in binary form must reproduce the above copyright
38  *    notice, this list of conditions and the following disclaimer in the
39  *    documentation and/or other materials provided with the distribution.
40  * 3. Neither the name of the University nor the names of its contributors
41  *    may be used to endorse or promote products derived from this software
42  *    without specific prior written permission.
43  *
44  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54  * SUCH DAMAGE.
55  */
56 
57 #pragma once
58 
59 #include <stdio.h>
60 #include <sys/cdefs.h>
61 #include <stdarg.h>
62 
63 __BEGIN_DECLS
64 
65 /** Corresponds to the Android ERROR log priority. */
66 #define LOG_EMERG 0
67 /** Corresponds to the Android ERROR log priority. */
68 #define LOG_ALERT 1
69 /** Corresponds to the Android ERROR log priority. */
70 #define LOG_CRIT 2
71 /** Corresponds to the Android ERROR log priority. */
72 #define LOG_ERR 3
73 /** Corresponds to the Android WARN log priority. */
74 #define LOG_WARNING 4
75 /** Corresponds to the Android INFO log priority. */
76 #define LOG_NOTICE 5
77 /** Corresponds to the Android INFO log priority. */
78 #define LOG_INFO 6
79 /** Corresponds to the Android DEBUG log priority. */
80 #define LOG_DEBUG 7
81 
82 #define LOG_PRIMASK 7
83 #define LOG_PRI(x) ((x) & LOG_PRIMASK)
84 #define LOG_MAKEPRI(fac, pri) ((fac) | (pri))
85 
86 /** Currently ignored on Android. */
87 #define LOG_KERN     (0<<3)
88 /** Currently ignored on Android. */
89 #define LOG_USER     (1<<3)
90 /** Currently ignored on Android. */
91 #define LOG_MAIL     (2<<3)
92 /** Currently ignored on Android. */
93 #define LOG_DAEMON   (3<<3)
94 /** Currently ignored on Android. */
95 #define LOG_AUTH     (4<<3)
96 /** Currently ignored on Android. */
97 #define LOG_SYSLOG   (5<<3)
98 /** Currently ignored on Android. */
99 #define LOG_LPR      (6<<3)
100 /** Currently ignored on Android. */
101 #define LOG_NEWS     (7<<3)
102 /** Currently ignored on Android. */
103 #define LOG_UUCP     (8<<3)
104 /** Currently ignored on Android. */
105 #define LOG_CRON     (9<<3)
106 /** Currently ignored on Android. */
107 #define LOG_AUTHPRIV (10<<3)
108 /** Currently ignored on Android. */
109 #define LOG_FTP      (11<<3)
110 /** Currently ignored on Android. */
111 #define LOG_LOCAL0   (16<<3)
112 /** Currently ignored on Android. */
113 #define LOG_LOCAL1   (17<<3)
114 /** Currently ignored on Android. */
115 #define LOG_LOCAL2   (18<<3)
116 /** Currently ignored on Android. */
117 #define LOG_LOCAL3   (19<<3)
118 /** Currently ignored on Android. */
119 #define LOG_LOCAL4   (20<<3)
120 /** Currently ignored on Android. */
121 #define LOG_LOCAL5   (21<<3)
122 /** Currently ignored on Android. */
123 #define LOG_LOCAL6   (22<<3)
124 /** Currently ignored on Android. */
125 #define LOG_LOCAL7   (23<<3)
126 
127 #define LOG_NFACILITIES 24
128 #define LOG_FACMASK 0x3f8
129 #define LOG_FAC(x) (((x) >> 3) & (LOG_FACMASK >> 3))
130 
131 /**
132  * Converts a log priority into a mask enabling that single priority,
133  * for use with setlogmask().
134  */
135 #define LOG_MASK(pri) (1 << (pri))
136 
137 /**
138  * Converts a log priority into a mask enabling that priority and all lower
139  * priorities, for use with setlogmask().
140  */
141 #define LOG_UPTO(pri) ((1 << ((pri)+1)) - 1)
142 
143 /** openlog() option ignored on Android. */
144 #define LOG_PID    0x01
145 /** openlog() option ignored on Android. */
146 #define LOG_CONS   0x02
147 /** openlog() option ignored on Android. */
148 #define LOG_ODELAY 0x04
149 /** openlog() option ignored on Android. */
150 #define LOG_NDELAY 0x08
151 /** openlog() option ignored on Android. */
152 #define LOG_NOWAIT 0x10
153 /**
154  * openlog() option to log to stderr as well as the system log.
155  *
156  * Available since API level 34 (ignored before then).
157  */
158 #define LOG_PERROR 0x20
159 
160 #if defined(SYSLOG_NAMES)
161 /** A mapping from name to value, used by `facilitynames` and `prioritynames`. */
162 typedef struct _code {
163   char* c_name;
164   int c_val;
165 } CODE;
166 /* A bogus facility value for "mark". */
167 #define INTERNAL_MARK LOG_MAKEPRI((LOG_NFACILITIES<<3), 0)
168 /** A table mapping facility names to values. */
169 static const CODE facilitynames[] = {
170   { "auth", LOG_AUTH, },
171   { "authpriv", LOG_AUTHPRIV, },
172   { "cron", LOG_CRON, },
173   { "daemon", LOG_DAEMON, },
174   { "ftp", LOG_FTP, },
175   { "kern", LOG_KERN, },
176   { "lpr", LOG_LPR, },
177   { "mail", LOG_MAIL, },
178   { "mark", INTERNAL_MARK, },
179   { "news", LOG_NEWS, },
180   { "security", LOG_AUTH, },
181   { "syslog", LOG_SYSLOG, },
182   { "user", LOG_USER, },
183   { "uucp", LOG_UUCP, },
184   { "local0", LOG_LOCAL0, },
185   { "local1", LOG_LOCAL1, },
186   { "local2", LOG_LOCAL2, },
187   { "local3", LOG_LOCAL3, },
188   { "local4", LOG_LOCAL4, },
189   { "local5", LOG_LOCAL5, },
190   { "local6", LOG_LOCAL6, },
191   { "local7", LOG_LOCAL7, },
192   { NULL, -1, },
193 };
194 /* A bogus priority value for "none". */
195 #define INTERNAL_NOPRI 8
196 /** A table mapping priority names to values. */
197 static const CODE prioritynames[] = {
198   { "alert", LOG_ALERT, },
199   { "crit", LOG_CRIT, },
200   { "debug", LOG_DEBUG, },
201   { "emerg", LOG_EMERG, },
202   { "err", LOG_ERR, },
203   { "error", LOG_ERR, },
204   { "info", LOG_INFO, },
205   { "none", INTERNAL_NOPRI, },
206   { "notice", LOG_NOTICE, },
207   { "panic", LOG_EMERG, },
208   { "warn", LOG_WARNING, },
209   { "warning", LOG_WARNING, },
210   { NULL, -1, },
211 };
212 #endif
213 
214 /**
215  * [closelog(3)](http://man7.org/linux/man-pages/man3/closelog.3.html) does
216  * nothing on Android.
217  */
218 void closelog(void);
219 
220 /**
221  * [openlog(3)](http://man7.org/linux/man-pages/man3/openlog.3.html) sets
222  * the log tag to `__prefix`, which can be NULL to return to the default of
223  * getprogname(). On Android, the other two arguments are ignored.
224  */
225 void openlog(const char* _Nullable __prefix, int __option, int __facility);
226 
227 /**
228  * [setlogmask(3)](http://man7.org/linux/man-pages/man3/setlogmask.3.html)
229  * sets which log priorities will actually be logged. See `LOG_MASK` and
230  * `LOG_UPTO`.
231  */
232 int setlogmask(int __mask);
233 
234 /**
235  * [syslog(3)](http://man7.org/linux/man-pages/man3/syslog.3.html) formats
236  * the printf()-like message and logs it with the given priority, unless
237  * suppressed by setlogmask(). On Android, the output goes to logcat.
238  */
239 void syslog(int __priority, const char* _Nonnull __fmt, ...) __printflike(2, 3);
240 
241 /**
242  * [vsyslog(3)](http://man7.org/linux/man-pages/man3/vsyslog.3.html) formats
243  * the vprintf()-like message and logs it with the given priority, unless
244  * suppressed by setlogmask(). On Android, the output goes to logcat.
245  */
246 void vsyslog(int __priority, const char* _Nonnull __fmt, va_list __args) __printflike(2, 0);
247 
248 __END_DECLS
249