blob: 6fdb84f0dd86fe26b611fb6e74a02f4becc60718 [file] [log] [blame]
David 'Digit' Turner5c734642010-01-20 12:36:51 -08001/*
2 * Copyright (C) 2010 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 */
Elliott Hughes18a206c2012-10-29 17:37:13 -070028
Christopher Ferris7a3681e2017-04-24 17:48:32 -070029#ifndef _ASYNC_SAFE_LOG_LOG_H
30#define _ASYNC_SAFE_LOG_LOG_H
David 'Digit' Turner5c734642010-01-20 12:36:51 -080031
Elliott Hughes1e980b62013-01-17 18:36:06 -080032#include <sys/cdefs.h>
David 'Digit' Turner5c734642010-01-20 12:36:51 -080033#include <stdarg.h>
34#include <stddef.h>
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070035#include <stdint.h>
Elliott Hughes695713e2017-06-20 17:28:42 -070036#include <stdlib.h>
David 'Digit' Turner5c734642010-01-20 12:36:51 -080037
Christopher Ferris7a3681e2017-04-24 17:48:32 -070038// These functions do not allocate memory to send data to the log.
39
Elliott Hughes1e980b62013-01-17 18:36:06 -080040__BEGIN_DECLS
41
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070042enum {
43 ANDROID_LOG_UNKNOWN = 0,
44 ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
45
46 ANDROID_LOG_VERBOSE,
47 ANDROID_LOG_DEBUG,
48 ANDROID_LOG_INFO,
49 ANDROID_LOG_WARN,
50 ANDROID_LOG_ERROR,
51 ANDROID_LOG_FATAL,
52
53 ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */
54};
55
Mark Salyzyn0336e352013-11-08 06:58:01 -080056enum {
Elliott Hughesae0a62b2014-05-07 17:12:40 -070057 LOG_ID_MIN = 0,
Mark Salyzyn0336e352013-11-08 06:58:01 -080058
Elliott Hughesae0a62b2014-05-07 17:12:40 -070059 LOG_ID_MAIN = 0,
60 LOG_ID_RADIO = 1,
61 LOG_ID_EVENTS = 2,
62 LOG_ID_SYSTEM = 3,
63 LOG_ID_CRASH = 4,
Mark Salyzyn0336e352013-11-08 06:58:01 -080064
Elliott Hughesae0a62b2014-05-07 17:12:40 -070065 LOG_ID_MAX
Mark Salyzyn0336e352013-11-08 06:58:01 -080066};
67
Elliott Hughes0d787c12013-04-04 13:46:46 -070068// Formats a message to the log (priority 'fatal'), then aborts.
Elliott Hughes695713e2017-06-20 17:28:42 -070069// Implemented as a macro so that async_safe_fatal isn't on the stack when we crash:
70// we appear to go straight from the caller to abort, saving an uninteresting stack
71// frame.
72#define async_safe_fatal(...) \
73 do { \
74 async_safe_fatal_no_abort(__VA_ARGS__); \
75 abort(); \
76 } while (0) \
Elliott Hughes0d787c12013-04-04 13:46:46 -070077
Elliott Hughes695713e2017-06-20 17:28:42 -070078
79// These functions do return, so callers that want to abort, must do so themselves,
80// or use the macro above.
81void async_safe_fatal_no_abort(const char* _Nonnull fmt, ...) __printflike(1, 2);
Christopher Ferris7a3681e2017-04-24 17:48:32 -070082#if defined(__arm__) || defined(__aarch64__) || defined(__x86_64__)
83void async_safe_fatal_va_list(
84 const char* _Nullable prefix, const char* _Nonnull fmt, va_list);
85#else // defined(__mips__) || defined(__i386__)
86void async_safe_fatal_va_list(
87 const char* _Nullable prefix, const char* _Nonnull fmt, va_list _Nonnull);
88#endif
Elliott Hughes61e699a2013-06-12 14:05:46 -070089
90//
Elliott Hughes1e980b62013-01-17 18:36:06 -080091// Formatting routines for the C library's internal debugging.
Elliott Hughes91570ce2014-07-10 12:34:23 -070092// Unlike the usual alternatives, these don't allocate, and they don't drag in all of stdio.
Josh Gaoc17e5a42017-03-08 21:31:48 -080093// These are async signal safe, so they can be called from signal handlers.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070094//
Elliott Hughes1e980b62013-01-17 18:36:06 -080095
Christopher Ferris7a3681e2017-04-24 17:48:32 -070096int async_safe_format_buffer(char* _Nonnull buf, size_t size, const char* _Nonnull fmt, ...) __printflike(3, 4);
Josh Gao273991c2017-02-15 11:46:55 -080097
98#if defined(__arm__) || defined(__aarch64__) || defined(__x86_64__)
Christopher Ferris7a3681e2017-04-24 17:48:32 -070099int async_safe_format_buffer_va_list(
100 char* _Nonnull buffer, size_t buffer_size, const char* _Nonnull format, va_list args);
Josh Gao273991c2017-02-15 11:46:55 -0800101#else // defined(__mips__) || defined(__i386__)
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700102int async_safe_format_buffer_va_list(
103 char* _Nonnull buffer, size_t buffer_size, const char* _Nonnull format, va_list _Nonnull args);
Josh Gao273991c2017-02-15 11:46:55 -0800104#endif
105
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700106int async_safe_format_fd(int fd, const char* _Nonnull format , ...) __printflike(2, 3);
107int async_safe_format_log(int pri, const char* _Nonnull tag, const char* _Nonnull fmt, ...) __printflike(3, 4);
Elliott Hughes53420fb2016-06-30 13:47:16 -0700108#if defined(__arm__) || defined(__aarch64__) || defined(__x86_64__)
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700109int async_safe_format_log_va_list(
110 int pri, const char* _Nonnull tag, const char* _Nonnull fmt, va_list ap);
Elliott Hughes53420fb2016-06-30 13:47:16 -0700111#else // defined(__mips__) || defined(__i386__)
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700112int async_safe_format_log_va_list(
113 int pri, const char* _Nonnull tag, const char* _Nonnull fmt, va_list _Nonnull ap);
Elliott Hughes8ed118d2016-06-30 12:46:21 -0700114#endif
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700115int async_safe_write_log(int pri, const char* _Nonnull tag, const char* _Nonnull msg);
Colin Cross2c759912016-02-05 16:17:39 -0800116
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800117#define CHECK(predicate) \
118 do { \
119 if (!(predicate)) { \
Christopher Ferris7a3681e2017-04-24 17:48:32 -0700120 async_safe_fatal("%s:%d: %s CHECK '" #predicate "' failed", \
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -0800121 __FILE__, __LINE__, __FUNCTION__); \
122 } \
123 } while(0)
124
Elliott Hughes1e980b62013-01-17 18:36:06 -0800125__END_DECLS
126
Elliott Hughes8f2a5a02013-03-15 15:30:25 -0700127#endif