1 /* 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 */ 28 29 #ifndef _ASYNC_SAFE_LOG_LOG_H 30 #define _ASYNC_SAFE_LOG_LOG_H 31 32 #include <sys/cdefs.h> 33 #include <stdarg.h> 34 #include <stddef.h> 35 #include <stdint.h> 36 #include <stdlib.h> 37 38 // These functions do not allocate memory to send data to the log. 39 40 __BEGIN_DECLS 41 42 enum { 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 56 enum { 57 LOG_ID_MIN = 0, 58 59 LOG_ID_MAIN = 0, 60 LOG_ID_RADIO = 1, 61 LOG_ID_EVENTS = 2, 62 LOG_ID_SYSTEM = 3, 63 LOG_ID_CRASH = 4, 64 65 LOG_ID_MAX 66 }; 67 68 // Formats a message to the log (priority 'fatal'), then aborts. 69 // 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) \ 77 78 79 // These functions do return, so callers that want to abort, must do so themselves, 80 // or use the macro above. 81 void async_safe_fatal_no_abort(const char* _Nonnull fmt, ...) __printflike(1, 2); 82 #if defined(__arm__) || defined(__aarch64__) || defined(__x86_64__) 83 void async_safe_fatal_va_list( 84 const char* _Nullable prefix, const char* _Nonnull fmt, va_list); 85 #else // defined(__mips__) || defined(__i386__) 86 void async_safe_fatal_va_list( 87 const char* _Nullable prefix, const char* _Nonnull fmt, va_list _Nonnull); 88 #endif 89 90 // 91 // Formatting routines for the C library's internal debugging. 92 // Unlike the usual alternatives, these don't allocate, and they don't drag in all of stdio. 93 // These are async signal safe, so they can be called from signal handlers. 94 // 95 96 int async_safe_format_buffer(char* _Nonnull buf, size_t size, const char* _Nonnull fmt, ...) __printflike(3, 4); 97 98 #if defined(__arm__) || defined(__aarch64__) || defined(__x86_64__) 99 int async_safe_format_buffer_va_list( 100 char* _Nonnull buffer, size_t buffer_size, const char* _Nonnull format, va_list args); 101 #else // defined(__mips__) || defined(__i386__) 102 int async_safe_format_buffer_va_list( 103 char* _Nonnull buffer, size_t buffer_size, const char* _Nonnull format, va_list _Nonnull args); 104 #endif 105 106 int async_safe_format_fd(int fd, const char* _Nonnull format , ...) __printflike(2, 3); 107 int async_safe_format_log(int pri, const char* _Nonnull tag, const char* _Nonnull fmt, ...) __printflike(3, 4); 108 #if defined(__arm__) || defined(__aarch64__) || defined(__x86_64__) 109 int async_safe_format_log_va_list( 110 int pri, const char* _Nonnull tag, const char* _Nonnull fmt, va_list ap); 111 #else // defined(__mips__) || defined(__i386__) 112 int async_safe_format_log_va_list( 113 int pri, const char* _Nonnull tag, const char* _Nonnull fmt, va_list _Nonnull ap); 114 #endif 115 int async_safe_write_log(int pri, const char* _Nonnull tag, const char* _Nonnull msg); 116 117 #define CHECK(predicate) \ 118 do { \ 119 if (!(predicate)) { \ 120 async_safe_fatal("%s:%d: %s CHECK '" #predicate "' failed", \ 121 __FILE__, __LINE__, __FUNCTION__); \ 122 } \ 123 } while(0) 124 125 __END_DECLS 126 127 #endif 128