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 <sys/cdefs.h>
60  
61  #include <stdio.h>
62  #include <stdarg.h>
63  
64  __BEGIN_DECLS
65  
66  /** Corresponds to the Android ERROR log priority. */
67  #define LOG_EMERG 0
68  /** Corresponds to the Android ERROR log priority. */
69  #define LOG_ALERT 1
70  /** Corresponds to the Android ERROR log priority. */
71  #define LOG_CRIT 2
72  /** Corresponds to the Android ERROR log priority. */
73  #define LOG_ERR 3
74  /** Corresponds to the Android WARN log priority. */
75  #define LOG_WARNING 4
76  /** Corresponds to the Android INFO log priority. */
77  #define LOG_NOTICE 5
78  /** Corresponds to the Android INFO log priority. */
79  #define LOG_INFO 6
80  /** Corresponds to the Android DEBUG log priority. */
81  #define LOG_DEBUG 7
82  
83  #define LOG_PRIMASK 7
84  #define LOG_PRI(x) ((x) & LOG_PRIMASK)
85  #define LOG_MAKEPRI(fac, pri) ((fac) | (pri))
86  
87  /** Currently ignored on Android. */
88  #define LOG_KERN     (0<<3)
89  /** Currently ignored on Android. */
90  #define LOG_USER     (1<<3)
91  /** Currently ignored on Android. */
92  #define LOG_MAIL     (2<<3)
93  /** Currently ignored on Android. */
94  #define LOG_DAEMON   (3<<3)
95  /** Currently ignored on Android. */
96  #define LOG_AUTH     (4<<3)
97  /** Currently ignored on Android. */
98  #define LOG_SYSLOG   (5<<3)
99  /** Currently ignored on Android. */
100  #define LOG_LPR      (6<<3)
101  /** Currently ignored on Android. */
102  #define LOG_NEWS     (7<<3)
103  /** Currently ignored on Android. */
104  #define LOG_UUCP     (8<<3)
105  /** Currently ignored on Android. */
106  #define LOG_CRON     (9<<3)
107  /** Currently ignored on Android. */
108  #define LOG_AUTHPRIV (10<<3)
109  /** Currently ignored on Android. */
110  #define LOG_FTP      (11<<3)
111  /** Currently ignored on Android. */
112  #define LOG_LOCAL0   (16<<3)
113  /** Currently ignored on Android. */
114  #define LOG_LOCAL1   (17<<3)
115  /** Currently ignored on Android. */
116  #define LOG_LOCAL2   (18<<3)
117  /** Currently ignored on Android. */
118  #define LOG_LOCAL3   (19<<3)
119  /** Currently ignored on Android. */
120  #define LOG_LOCAL4   (20<<3)
121  /** Currently ignored on Android. */
122  #define LOG_LOCAL5   (21<<3)
123  /** Currently ignored on Android. */
124  #define LOG_LOCAL6   (22<<3)
125  /** Currently ignored on Android. */
126  #define LOG_LOCAL7   (23<<3)
127  
128  #define LOG_NFACILITIES 24
129  #define LOG_FACMASK 0x3f8
130  #define LOG_FAC(x) (((x) >> 3) & (LOG_FACMASK >> 3))
131  
132  /**
133   * Converts a log priority into a mask enabling that single priority,
134   * for use with setlogmask().
135   */
136  #define LOG_MASK(pri) (1 << (pri))
137  
138  /**
139   * Converts a log priority into a mask enabling that priority and all lower
140   * priorities, for use with setlogmask().
141   */
142  #define LOG_UPTO(pri) ((1 << ((pri)+1)) - 1)
143  
144  /** openlog() option ignored on Android. */
145  #define LOG_PID    0x01
146  /** openlog() option ignored on Android. */
147  #define LOG_CONS   0x02
148  /** openlog() option ignored on Android. */
149  #define LOG_ODELAY 0x04
150  /** openlog() option ignored on Android. */
151  #define LOG_NDELAY 0x08
152  /** openlog() option ignored on Android. */
153  #define LOG_NOWAIT 0x10
154  /**
155   * openlog() option to log to stderr as well as the system log.
156   *
157   * Available since API level 34 (ignored before then).
158   */
159  #define LOG_PERROR 0x20
160  
161  #if defined(SYSLOG_NAMES)
162  /** A mapping from name to value, used by `facilitynames` and `prioritynames`. */
163  typedef struct _code {
164    char* c_name;
165    int c_val;
166  } CODE;
167  /* A bogus facility value for "mark". */
168  #define INTERNAL_MARK LOG_MAKEPRI((LOG_NFACILITIES<<3), 0)
169  /** A table mapping facility names to values. */
170  static const CODE facilitynames[] = {
171    { "auth", LOG_AUTH, },
172    { "authpriv", LOG_AUTHPRIV, },
173    { "cron", LOG_CRON, },
174    { "daemon", LOG_DAEMON, },
175    { "ftp", LOG_FTP, },
176    { "kern", LOG_KERN, },
177    { "lpr", LOG_LPR, },
178    { "mail", LOG_MAIL, },
179    { "mark", INTERNAL_MARK, },
180    { "news", LOG_NEWS, },
181    { "security", LOG_AUTH, },
182    { "syslog", LOG_SYSLOG, },
183    { "user", LOG_USER, },
184    { "uucp", LOG_UUCP, },
185    { "local0", LOG_LOCAL0, },
186    { "local1", LOG_LOCAL1, },
187    { "local2", LOG_LOCAL2, },
188    { "local3", LOG_LOCAL3, },
189    { "local4", LOG_LOCAL4, },
190    { "local5", LOG_LOCAL5, },
191    { "local6", LOG_LOCAL6, },
192    { "local7", LOG_LOCAL7, },
193    { NULL, -1, },
194  };
195  /* A bogus priority value for "none". */
196  #define INTERNAL_NOPRI 8
197  /** A table mapping priority names to values. */
198  static const CODE prioritynames[] = {
199    { "alert", LOG_ALERT, },
200    { "crit", LOG_CRIT, },
201    { "debug", LOG_DEBUG, },
202    { "emerg", LOG_EMERG, },
203    { "err", LOG_ERR, },
204    { "error", LOG_ERR, },
205    { "info", LOG_INFO, },
206    { "none", INTERNAL_NOPRI, },
207    { "notice", LOG_NOTICE, },
208    { "panic", LOG_EMERG, },
209    { "warn", LOG_WARNING, },
210    { "warning", LOG_WARNING, },
211    { NULL, -1, },
212  };
213  #endif
214  
215  /**
216   * [closelog(3)](https://man7.org/linux/man-pages/man3/closelog.3.html) does
217   * nothing on Android.
218   */
219  void closelog(void);
220  
221  /**
222   * [openlog(3)](https://man7.org/linux/man-pages/man3/openlog.3.html) sets
223   * the log tag to `__prefix`, which can be NULL to return to the default of
224   * getprogname(). On Android, the other two arguments are ignored.
225   */
226  void openlog(const char* _Nullable __prefix, int __option, int __facility);
227  
228  /**
229   * [setlogmask(3)](https://man7.org/linux/man-pages/man3/setlogmask.3.html)
230   * sets which log priorities will actually be logged. See `LOG_MASK` and
231   * `LOG_UPTO`.
232   */
233  int setlogmask(int __mask);
234  
235  /**
236   * [syslog(3)](https://man7.org/linux/man-pages/man3/syslog.3.html) formats
237   * the printf()-like message and logs it with the given priority, unless
238   * suppressed by setlogmask(). On Android, the output goes to logcat.
239   */
240  void syslog(int __priority, const char* _Nonnull __fmt, ...) __printflike(2, 3);
241  
242  /**
243   * [vsyslog(3)](https://man7.org/linux/man-pages/man3/vsyslog.3.html) formats
244   * the vprintf()-like message and logs it with the given priority, unless
245   * suppressed by setlogmask(). On Android, the output goes to logcat.
246   */
247  void vsyslog(int __priority, const char* _Nonnull __fmt, va_list __args) __printflike(2, 0);
248  
249  __END_DECLS
250