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 #include <sys/time.h>
63
64 #if !defined(__BIONIC__) && !defined(__INTRODUCED_IN)
65 #define __INTRODUCED_IN(x)
66 #endif
67
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71
72 /**
73 * Android log priority values, in increasing order of priority.
74 */
75 typedef enum android_LogPriority {
76 /** For internal use only. */
77 ANDROID_LOG_UNKNOWN = 0,
78 /** The default priority, for internal use only. */
79 ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
80 /** Verbose logging. Should typically be disabled for a release apk. */
81 ANDROID_LOG_VERBOSE,
82 /** Debug logging. Should typically be disabled for a release apk. */
83 ANDROID_LOG_DEBUG,
84 /** Informational logging. Should typically be disabled for a release apk. */
85 ANDROID_LOG_INFO,
86 /** Warning logging. For use with recoverable failures. */
87 ANDROID_LOG_WARN,
88 /** Error logging. For use with unrecoverable failures. */
89 ANDROID_LOG_ERROR,
90 /** Fatal logging. For use when aborting. */
91 ANDROID_LOG_FATAL,
92 /** For internal use only. */
93 ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */
94 } android_LogPriority;
95
96 /**
97 * Writes the constant string `text` to the log,
98 * with priority `prio` (one of the `android_LogPriority` values) and tag `tag`.
99 *
100 * @return 1 if the message was written to the log, or -EPERM if it was not; see
101 * __android_log_is_loggable().
102 */
103 int __android_log_write(int prio, const char* tag, const char* text);
104
105 /**
106 * Writes a formatted string to the log,
107 * with priority `prio` (one of the `android_LogPriority` values) and tag `tag`.
108 *
109 * The details of formatting are the same as for
110 * [printf(3)](http://man7.org/linux/man-pages/man3/printf.3.html).
111 *
112 * @return 1 if the message was written to the log, or -EPERM if it was not; see
113 * __android_log_is_loggable().
114 */
115 int __android_log_print(int prio, const char* tag, const char* fmt, ...)
116 __attribute__((__format__(printf, 3, 4)));
117
118 /**
119 * Equivalent to __android_log_print(), but taking a `va_list`.
120 * (If __android_log_print() is like printf(), this is like vprintf().)
121 *
122 * @return 1 if the message was written to the log, or -EPERM if it was not; see
123 * __android_log_is_loggable().
124 */
125 int __android_log_vprint(int prio, const char* tag, const char* fmt, va_list ap)
126 __attribute__((__format__(printf, 3, 0)));
127
128 /**
129 * Writes an assertion failure to the log (as `ANDROID_LOG_FATAL`) and to
130 * stderr, before calling
131 * [abort(3)](http://man7.org/linux/man-pages/man3/abort.3.html).
132 *
133 * If `fmt` is non-null, `cond` is unused. If `fmt` is null, the string
134 * `Assertion failed: %s` is used with `cond` as the string argument.
135 * If both `fmt` and `cond` are null, a default string is provided.
136 *
137 * Most callers should use
138 * [assert(3)](http://man7.org/linux/man-pages/man3/assert.3.html) from
139 * `<assert.h>` instead, or the `__assert` and `__assert2` functions
140 * provided by bionic if more control is needed. They support automatically
141 * including the source filename and line number more conveniently than this
142 * function.
143 */
144 void __android_log_assert(const char* cond, const char* tag, const char* fmt, ...)
145 __attribute__((__noreturn__)) __attribute__((__format__(printf, 3, 4)));
146
147 /**
148 * Identifies a specific log buffer for __android_log_buf_write()
149 * and __android_log_buf_print().
150 */
151 typedef enum log_id {
152 /** For internal use only. */
153 LOG_ID_MIN = 0,
154
155 /** The main log buffer. This is the only log buffer available to apps. */
156 LOG_ID_MAIN = 0,
157 /** The radio log buffer. */
158 LOG_ID_RADIO = 1,
159 /** The event log buffer. */
160 LOG_ID_EVENTS = 2,
161 /** The system log buffer. */
162 LOG_ID_SYSTEM = 3,
163 /** The crash log buffer. */
164 LOG_ID_CRASH = 4,
165 /** The statistics log buffer. */
166 LOG_ID_STATS = 5,
167 /** The security log buffer. */
168 LOG_ID_SECURITY = 6,
169 /** The kernel log buffer. */
170 LOG_ID_KERNEL = 7,
171
172 /** For internal use only. */
173 LOG_ID_MAX,
174
175 /**
176 * Let the logging library choose the best log target in cases where it's
177 * unclear. This is useful if you're generic library code that can't know
178 * which log your caller should use.
179 */
180 LOG_ID_DEFAULT = 0x7FFFFFFF
181 } log_id_t;
182
__android_log_id_is_valid(log_id_t log_id)183 static inline bool __android_log_id_is_valid(log_id_t log_id) {
184 return log_id >= LOG_ID_MIN && log_id < LOG_ID_MAX;
185 }
186
187 /**
188 * Writes the string `text` to the log buffer `log_id` (one of the `log_id_t` values),
189 * with priority `prio` (one of the `android_LogPriority` values) and tag `tag`.
190 *
191 * Apps should use __android_log_write() instead because LOG_ID_MAIN is the
192 * only log buffer available to them.
193 *
194 * @return 1 if the message was written to the log, or -EPERM if it was not; see
195 * __android_log_is_loggable().
196 */
197 int __android_log_buf_write(int log_id, int prio, const char* tag, const char* text);
198
199 /**
200 * Writes a formatted string to the log buffer `log_id` (one of the `log_id_t` values),
201 * with priority `prio` (one of the `android_LogPriority` values) and tag `tag`.
202 *
203 * The details of formatting are the same as for
204 * [printf(3)](http://man7.org/linux/man-pages/man3/printf.3.html).
205 *
206 * Apps should use __android_log_print() instead because LOG_ID_MAIN is the
207 * only log buffer available to them.
208 *
209 * @return 1 if the message was written to the log, or -EPERM if it was not; see
210 * __android_log_is_loggable().
211 */
212 int __android_log_buf_print(int log_id, int prio, const char* tag, const char* fmt, ...)
213 __attribute__((__format__(printf, 4, 5)));
214
215 /**
216 * Logger data struct used for writing log messages to liblog via __android_log_write_logger_data()
217 * and sending log messages to user defined loggers specified in __android_log_set_logger().
218 */
219 struct __android_log_message {
220 /** Must be set to `sizeof(__android_log_message)` and is used for versioning. */
221 size_t struct_size;
222
223 /** {@link log_id_t} values. */
224 int32_t buffer_id;
225
226 /** {@link android_LogPriority} values. */
227 int32_t priority;
228
229 /** The tag for the log message. */
230 const char* tag;
231
232 /** Optional file name, may be set to nullptr. */
233 const char* file;
234
235 /** Optional line number, ignore if file is nullptr. */
236 uint32_t line;
237
238 /** The log message itself. */
239 const char* message;
240 };
241
242 /**
243 * Prototype for the 'logger' function that is called for every log message.
244 */
245 typedef void (*__android_logger_function)(const struct __android_log_message* log_message);
246 /**
247 * Prototype for the 'abort' function that is called when liblog will abort due to
248 * __android_log_assert() failures.
249 */
250 typedef void (*__android_aborter_function)(const char* abort_message);
251
252 /**
253 * Writes the log message specified by log_message. log_message includes additional file name and
254 * line number information that a logger may use. log_message is versioned for backwards
255 * compatibility.
256 * This assumes that loggability has already been checked through __android_log_is_loggable().
257 * Higher level logging libraries, such as libbase, first check loggability, then format their
258 * buffers, then pass the message to liblog via this function, and therefore we do not want to
259 * duplicate the loggability check here.
260 *
261 * @param log_message the log message itself, see {@link __android_log_message}.
262 *
263 * Available since API level 30.
264 */
265 void __android_log_write_log_message(struct __android_log_message* log_message) __INTRODUCED_IN(30);
266
267 /**
268 * Sets a user defined logger function. All log messages sent to liblog will be set to the
269 * function pointer specified by logger for processing. It is not expected that log messages are
270 * already terminated with a new line. This function should add new lines if required for line
271 * separation.
272 *
273 * @param logger the new function that will handle log messages.
274 *
275 * Available since API level 30.
276 */
277 void __android_log_set_logger(__android_logger_function logger) __INTRODUCED_IN(30);
278
279 /**
280 * Writes the log message to logd. This is an {@link __android_logger_function} and can be provided to
281 * __android_log_set_logger(). It is the default logger when running liblog on a device.
282 *
283 * @param log_message the log message to write, see {@link __android_log_message}.
284 *
285 * Available since API level 30.
286 */
287 void __android_log_logd_logger(const struct __android_log_message* log_message) __INTRODUCED_IN(30);
288
289 /**
290 * Writes the log message to logd using the passed in timestamp. The messages are stored
291 * in logd in the order received not in order by timestamp. When displaying the log, there is no
292 * guarantee that messages are in timestamp order and might cause messages with different times to
293 * be interleaved. Filtering the log using a timestamp will work properly even if out of time
294 * order messages are present.
295 *
296 * @param log_message the log message to write, see {@link __android_log_message}.
297 * @param timestamp the time to use for this log message. The value is interpreted as a
298 * CLOCK_REALTIME value.
299 *
300 * Available since API level 37.
301 */
302 void __android_log_logd_logger_with_timestamp(const struct __android_log_message* log_message,
303 const struct timespec* timestamp) __INTRODUCED_IN(37);
304
305 /**
306 * Writes the log message to stderr. This is an {@link __android_logger_function} and can be provided to
307 * __android_log_set_logger(). It is the default logger when running liblog on host.
308 *
309 * @param log_message the log message to write, see {@link __android_log_message}.
310 *
311 * Available since API level 30.
312 */
313 void __android_log_stderr_logger(const struct __android_log_message* log_message)
314 __INTRODUCED_IN(30);
315
316 /**
317 * Sets a user defined aborter function that is called for __android_log_assert() failures. This
318 * user defined aborter function is highly recommended to abort and be noreturn, but is not strictly
319 * required to.
320 *
321 * @param aborter the new aborter function, see {@link __android_aborter_function}.
322 *
323 * Available since API level 30.
324 */
325 void __android_log_set_aborter(__android_aborter_function aborter) __INTRODUCED_IN(30);
326
327 /**
328 * Calls the stored aborter function. This allows for other logging libraries to use the same
329 * aborter function by calling this function in liblog.
330 *
331 * @param abort_message an additional message supplied when aborting, for example this is used to
332 * call android_set_abort_message() in __android_log_default_aborter().
333 *
334 * Available since API level 30.
335 */
336 void __android_log_call_aborter(const char* abort_message) __INTRODUCED_IN(30);
337
338 /**
339 * Sets android_set_abort_message() on device then aborts(). This is the default aborter.
340 *
341 * @param abort_message an additional message supplied when aborting. This functions calls
342 * android_set_abort_message() with its contents.
343 *
344 * Available since API level 30.
345 */
346 void __android_log_default_aborter(const char* abort_message) __attribute__((noreturn))
347 __INTRODUCED_IN(30);
348
349 /**
350 * Use the per-tag properties "log.tag.<tagname>" along with the minimum priority from
351 * __android_log_set_minimum_priority() to determine if a log message with a given prio and tag will
352 * be printed. A non-zero result indicates yes, zero indicates false.
353 *
354 * If both a priority for a tag and a minimum priority are set by
355 * __android_log_set_minimum_priority(), then the lowest of the two values are to determine the
356 * minimum priority needed to log. If only one is set, then that value is used to determine the
357 * minimum priority needed. If none are set, then default_priority is used.
358 *
359 * @param prio the priority to test, takes {@link android_LogPriority} values.
360 * @param tag the tag to test.
361 * @param default_prio the default priority to use if no properties or minimum priority are set.
362 * @return an integer where 1 indicates that the message is loggable and 0 indicates that it is not.
363 *
364 * Available since API level 30.
365 */
366 int __android_log_is_loggable(int prio, const char* tag, int default_prio) __INTRODUCED_IN(30);
367
368 /**
369 * Use the per-tag properties "log.tag.<tagname>" along with the minimum priority from
370 * __android_log_set_minimum_priority() to determine if a log message with a given prio and tag will
371 * be printed. A non-zero result indicates yes, zero indicates false.
372 *
373 * If both a priority for a tag and a minimum priority are set by
374 * __android_log_set_minimum_priority(), then the lowest of the two values are to determine the
375 * minimum priority needed to log. If only one is set, then that value is used to determine the
376 * minimum priority needed. If none are set, then default_priority is used.
377 *
378 * @param prio the priority to test, takes {@link android_LogPriority} values.
379 * @param tag the tag to test.
380 * @param len the length of the tag.
381 * @param default_prio the default priority to use if no properties or minimum priority are set.
382 * @return an integer where 1 indicates that the message is loggable and 0 indicates that it is not.
383 *
384 * Available since API level 30.
385 */
386 int __android_log_is_loggable_len(int prio, const char* tag, size_t len, int default_prio)
387 __INTRODUCED_IN(30);
388
389 /**
390 * Sets the minimum priority that will be logged for this process.
391 *
392 * @param priority the new minimum priority to set, takes {@link android_LogPriority} values.
393 * @return the previous set minimum priority, or `ANDROID_LOG_DEFAULT` if none was set.
394 *
395 * Available since API level 30.
396 */
397 int32_t __android_log_set_minimum_priority(int32_t priority) __INTRODUCED_IN(30);
398
399 /**
400 * Gets the minimum priority that will be logged for this process.
401 *
402 * @return the current minimum priority, or `ANDROID_LOG_DEFAULT` if none is set.
403 *
404 * Available since API level 30.
405 */
406 int32_t __android_log_get_minimum_priority(void) __INTRODUCED_IN(30);
407
408 /**
409 * Sets the default tag if no tag is provided when writing a log message. Defaults to
410 * getprogname(). This truncates tag to the maximum log message size, though appropriate tags
411 * should be much smaller.
412 *
413 * @param tag the new log tag.
414 *
415 * Available since API level 30.
416 */
417 void __android_log_set_default_tag(const char* tag) __INTRODUCED_IN(30);
418
419 #ifdef __cplusplus
420 }
421 #endif
422
423 /** @} */
424