1 /*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 /**
20 * @addtogroup Logging
21 * @{
22 */
23
24 /**
25 * \file
26 *
27 * Support routines to send messages to the Android log buffer,
28 * which can later be accessed through the `logcat` utility.
29 *
30 * Each log message must have
31 * - a priority
32 * - a log tag
33 * - some text
34 *
35 * The tag normally corresponds to the component that emits the log message,
36 * and should be reasonably small.
37 *
38 * Log message text may be truncated to less than an implementation-specific
39 * limit (1023 bytes).
40 *
41 * Note that a newline character ("\n") will be appended automatically to your
42 * log message, if not already there. It is not possible to send several
43 * messages and have them appear on a single line in logcat.
44 *
45 * Please use logging in moderation:
46 *
47 * - Sending log messages eats CPU and slow down your application and the
48 * system.
49 *
50 * - The circular log buffer is pretty small, so sending many messages
51 * will hide other important log messages.
52 *
53 * - In release builds, only send log messages to account for exceptional
54 * conditions.
55 */
56
57 #include <stdarg.h>
58 #include <stdbool.h>
59 #include <stddef.h>
60 #include <stdint.h>
61 #include <sys/cdefs.h>
62
63 #if !defined(__BIONIC__) && !defined(__INTRODUCED_IN)
64 #define __INTRODUCED_IN(x)
65 #endif
66
67 #ifdef __cplusplus
68 extern "C" {
69 #endif
70
71 /**
72 * Android log priority values, in increasing order of priority.
73 */
74 typedef enum android_LogPriority {
75 /** For internal use only. */
76 ANDROID_LOG_UNKNOWN = 0,
77 /** The default priority, for internal use only. */
78 ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
79 /** Verbose logging. Should typically be disabled for a release apk. */
80 ANDROID_LOG_VERBOSE,
81 /** Debug logging. Should typically be disabled for a release apk. */
82 ANDROID_LOG_DEBUG,
83 /** Informational logging. Should typically be disabled for a release apk. */
84 ANDROID_LOG_INFO,
85 /** Warning logging. For use with recoverable failures. */
86 ANDROID_LOG_WARN,
87 /** Error logging. For use with unrecoverable failures. */
88 ANDROID_LOG_ERROR,
89 /** Fatal logging. For use when aborting. */
90 ANDROID_LOG_FATAL,
91 /** For internal use only. */
92 ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */
93 } android_LogPriority;
94
95 /**
96 * Writes the constant string `text` to the log, with priority `prio` and tag
97 * `tag`.
98 *
99 * @return 1 if the message was written to the log, or -EPERM if it was not; see
100 * __android_log_is_loggable().
101 */
102 int __android_log_write(int prio, const char* tag, const char* text);
103
104 /**
105 * Writes a formatted string to the log, with priority `prio` and tag `tag`.
106 * The details of formatting are the same as for
107 * [printf(3)](http://man7.org/linux/man-pages/man3/printf.3.html).
108 *
109 * @return 1 if the message was written to the log, or -EPERM if it was not; see
110 * __android_log_is_loggable().
111 */
112 int __android_log_print(int prio, const char* tag, const char* fmt, ...)
113 __attribute__((__format__(printf, 3, 4)));
114
115 /**
116 * Equivalent to __android_log_print(), but taking a `va_list`.
117 * (If __android_log_print() is like printf(), this is like vprintf().)
118 *
119 * @return 1 if the message was written to the log, or -EPERM if it was not; see
120 * __android_log_is_loggable().
121 */
122 int __android_log_vprint(int prio, const char* tag, const char* fmt, va_list ap)
123 __attribute__((__format__(printf, 3, 0)));
124
125 /**
126 * Writes an assertion failure to the log (as `ANDROID_LOG_FATAL`) and to
127 * stderr, before calling
128 * [abort(3)](http://man7.org/linux/man-pages/man3/abort.3.html).
129 *
130 * If `fmt` is non-null, `cond` is unused. If `fmt` is null, the string
131 * `Assertion failed: %s` is used with `cond` as the string argument.
132 * If both `fmt` and `cond` are null, a default string is provided.
133 *
134 * Most callers should use
135 * [assert(3)](http://man7.org/linux/man-pages/man3/assert.3.html) from
136 * `<assert.h>` instead, or the `__assert` and `__assert2` functions
137 * provided by bionic if more control is needed. They support automatically
138 * including the source filename and line number more conveniently than this
139 * function.
140 */
141 void __android_log_assert(const char* cond, const char* tag, const char* fmt, ...)
142 __attribute__((__noreturn__)) __attribute__((__format__(printf, 3, 4)));
143
144 /**
145 * Identifies a specific log buffer for __android_log_buf_write()
146 * and __android_log_buf_print().
147 */
148 typedef enum log_id {
149 LOG_ID_MIN = 0,
150
151 /** The main log buffer. This is the only log buffer available to apps. */
152 LOG_ID_MAIN = 0,
153 /** The radio log buffer. */
154 LOG_ID_RADIO = 1,
155 /** The event log buffer. */
156 LOG_ID_EVENTS = 2,
157 /** The system log buffer. */
158 LOG_ID_SYSTEM = 3,
159 /** The crash log buffer. */
160 LOG_ID_CRASH = 4,
161 /** The statistics log buffer. */
162 LOG_ID_STATS = 5,
163 /** The security log buffer. */
164 LOG_ID_SECURITY = 6,
165 /** The kernel log buffer. */
166 LOG_ID_KERNEL = 7,
167
168 LOG_ID_MAX,
169
170 /** Let the logging function choose the best log target. */
171 LOG_ID_DEFAULT = 0x7FFFFFFF
172 } log_id_t;
173
__android_log_id_is_valid(log_id_t id)174 static inline bool __android_log_id_is_valid(log_id_t id) {
175 return id >= LOG_ID_MIN && id < LOG_ID_MAX;
176 }
177
178 /**
179 * Writes the constant string `text` to the log buffer `id`,
180 * with priority `prio` and tag `tag`.
181 *
182 * Apps should use __android_log_write() instead.
183 *
184 * @return 1 if the message was written to the log, or -EPERM if it was not; see
185 * __android_log_is_loggable().
186 */
187 int __android_log_buf_write(int bufID, int prio, const char* tag, const char* text);
188
189 /**
190 * Writes a formatted string to log buffer `id`,
191 * with priority `prio` and tag `tag`.
192 * The details of formatting are the same as for
193 * [printf(3)](http://man7.org/linux/man-pages/man3/printf.3.html).
194 *
195 * Apps should use __android_log_print() instead.
196 *
197 * @return 1 if the message was written to the log, or -EPERM if it was not; see
198 * __android_log_is_loggable().
199 */
200 int __android_log_buf_print(int bufID, int prio, const char* tag, const char* fmt, ...)
201 __attribute__((__format__(printf, 4, 5)));
202
203 /**
204 * Logger data struct used for writing log messages to liblog via __android_log_write_logger_data()
205 * and sending log messages to user defined loggers specified in __android_log_set_logger().
206 */
207 struct __android_log_message {
208 /** Must be set to `sizeof(__android_log_message)` and is used for versioning. */
209 size_t struct_size;
210
211 /** {@link log_id_t} values. */
212 int32_t buffer_id;
213
214 /** {@link android_LogPriority} values. */
215 int32_t priority;
216
217 /** The tag for the log message. */
218 const char* tag;
219
220 /** Optional file name, may be set to nullptr. */
221 const char* file;
222
223 /** Optional line number, ignore if file is nullptr. */
224 uint32_t line;
225
226 /** The log message itself. */
227 const char* message;
228 };
229
230 /**
231 * Prototype for the 'logger' function that is called for every log message.
232 */
233 typedef void (*__android_logger_function)(const struct __android_log_message* log_message);
234 /**
235 * Prototype for the 'abort' function that is called when liblog will abort due to
236 * __android_log_assert() failures.
237 */
238 typedef void (*__android_aborter_function)(const char* abort_message);
239
240 /**
241 * Writes the log message specified by log_message. log_message includes additional file name and
242 * line number information that a logger may use. log_message is versioned for backwards
243 * compatibility.
244 * This assumes that loggability has already been checked through __android_log_is_loggable().
245 * Higher level logging libraries, such as libbase, first check loggability, then format their
246 * buffers, then pass the message to liblog via this function, and therefore we do not want to
247 * duplicate the loggability check here.
248 *
249 * @param log_message the log message itself, see {@link __android_log_message}.
250 *
251 * Available since API level 30.
252 */
253 void __android_log_write_log_message(struct __android_log_message* log_message) __INTRODUCED_IN(30);
254
255 /**
256 * Sets a user defined logger function. All log messages sent to liblog will be set to the
257 * function pointer specified by logger for processing. It is not expected that log messages are
258 * already terminated with a new line. This function should add new lines if required for line
259 * separation.
260 *
261 * @param logger the new function that will handle log messages.
262 *
263 * Available since API level 30.
264 */
265 void __android_log_set_logger(__android_logger_function logger) __INTRODUCED_IN(30);
266
267 /**
268 * Writes the log message to logd. This is an {@link __android_logger_function} and can be provided to
269 * __android_log_set_logger(). It is the default logger when running liblog on a device.
270 *
271 * @param log_message the log message to write, see {@link __android_log_message}.
272 *
273 * Available since API level 30.
274 */
275 void __android_log_logd_logger(const struct __android_log_message* log_message) __INTRODUCED_IN(30);
276
277 /**
278 * Writes the log message to stderr. This is an {@link __android_logger_function} and can be provided to
279 * __android_log_set_logger(). It is the default logger when running liblog on host.
280 *
281 * @param log_message the log message to write, see {@link __android_log_message}.
282 *
283 * Available since API level 30.
284 */
285 void __android_log_stderr_logger(const struct __android_log_message* log_message)
286 __INTRODUCED_IN(30);
287
288 /**
289 * Sets a user defined aborter function that is called for __android_log_assert() failures. This
290 * user defined aborter function is highly recommended to abort and be noreturn, but is not strictly
291 * required to.
292 *
293 * @param aborter the new aborter function, see {@link __android_aborter_function}.
294 *
295 * Available since API level 30.
296 */
297 void __android_log_set_aborter(__android_aborter_function aborter) __INTRODUCED_IN(30);
298
299 /**
300 * Calls the stored aborter function. This allows for other logging libraries to use the same
301 * aborter function by calling this function in liblog.
302 *
303 * @param abort_message an additional message supplied when aborting, for example this is used to
304 * call android_set_abort_message() in __android_log_default_aborter().
305 *
306 * Available since API level 30.
307 */
308 void __android_log_call_aborter(const char* abort_message) __INTRODUCED_IN(30);
309
310 /**
311 * Sets android_set_abort_message() on device then aborts(). This is the default aborter.
312 *
313 * @param abort_message an additional message supplied when aborting. This functions calls
314 * android_set_abort_message() with its contents.
315 *
316 * Available since API level 30.
317 */
318 void __android_log_default_aborter(const char* abort_message) __attribute__((noreturn))
319 __INTRODUCED_IN(30);
320
321 /**
322 * Use the per-tag properties "log.tag.<tagname>" along with the minimum priority from
323 * __android_log_set_minimum_priority() to determine if a log message with a given prio and tag will
324 * be printed. A non-zero result indicates yes, zero indicates false.
325 *
326 * If both a priority for a tag and a minimum priority are set by
327 * __android_log_set_minimum_priority(), then the lowest of the two values are to determine the
328 * minimum priority needed to log. If only one is set, then that value is used to determine the
329 * minimum priority needed. If none are set, then default_priority is used.
330 *
331 * @param prio the priority to test, takes {@link android_LogPriority} values.
332 * @param tag the tag to test.
333 * @param default_prio the default priority to use if no properties or minimum priority are set.
334 * @return an integer where 1 indicates that the message is loggable and 0 indicates that it is not.
335 *
336 * Available since API level 30.
337 */
338 int __android_log_is_loggable(int prio, const char* tag, int default_prio) __INTRODUCED_IN(30);
339
340 /**
341 * Use the per-tag properties "log.tag.<tagname>" along with the minimum priority from
342 * __android_log_set_minimum_priority() to determine if a log message with a given prio and tag will
343 * be printed. A non-zero result indicates yes, zero indicates false.
344 *
345 * If both a priority for a tag and a minimum priority are set by
346 * __android_log_set_minimum_priority(), then the lowest of the two values are to determine the
347 * minimum priority needed to log. If only one is set, then that value is used to determine the
348 * minimum priority needed. If none are set, then default_priority is used.
349 *
350 * @param prio the priority to test, takes {@link android_LogPriority} values.
351 * @param tag the tag to test.
352 * @param len the length of the tag.
353 * @param default_prio the default priority to use if no properties or minimum priority are set.
354 * @return an integer where 1 indicates that the message is loggable and 0 indicates that it is not.
355 *
356 * Available since API level 30.
357 */
358 int __android_log_is_loggable_len(int prio, const char* tag, size_t len, int default_prio)
359 __INTRODUCED_IN(30);
360
361 /**
362 * Sets the minimum priority that will be logged for this process.
363 *
364 * @param priority the new minimum priority to set, takes {@link android_LogPriority} values.
365 * @return the previous set minimum priority, or `ANDROID_LOG_DEFAULT` if none was set.
366 *
367 * Available since API level 30.
368 */
369 int32_t __android_log_set_minimum_priority(int32_t priority) __INTRODUCED_IN(30);
370
371 /**
372 * Gets the minimum priority that will be logged for this process.
373 *
374 * @return the current minimum priority, or `ANDROID_LOG_DEFAULT` if none is set.
375 *
376 * Available since API level 30.
377 */
378 int32_t __android_log_get_minimum_priority(void) __INTRODUCED_IN(30);
379
380 /**
381 * Sets the default tag if no tag is provided when writing a log message. Defaults to
382 * getprogname(). This truncates tag to the maximum log message size, though appropriate tags
383 * should be much smaller.
384 *
385 * @param tag the new log tag.
386 *
387 * Available since API level 30.
388 */
389 void __android_log_set_default_tag(const char* tag) __INTRODUCED_IN(30);
390
391 #ifdef __cplusplus
392 }
393 #endif
394
395 /** @} */
396