David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 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 | */ |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 28 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 29 | #ifndef _ASYNC_SAFE_LOG_LOG_H |
| 30 | #define _ASYNC_SAFE_LOG_LOG_H |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 31 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 32 | #include <sys/cdefs.h> |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 33 | #include <stdarg.h> |
| 34 | #include <stddef.h> |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 35 | #include <stdint.h> |
Elliott Hughes | 695713e | 2017-06-20 17:28:42 -0700 | [diff] [blame] | 36 | #include <stdlib.h> |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 37 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 38 | // These functions do not allocate memory to send data to the log. |
| 39 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 40 | __BEGIN_DECLS |
| 41 | |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 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 | |
Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 56 | enum { |
Elliott Hughes | ae0a62b | 2014-05-07 17:12:40 -0700 | [diff] [blame] | 57 | LOG_ID_MIN = 0, |
Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 58 | |
Elliott Hughes | ae0a62b | 2014-05-07 17:12:40 -0700 | [diff] [blame] | 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, |
Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 64 | |
Elliott Hughes | ae0a62b | 2014-05-07 17:12:40 -0700 | [diff] [blame] | 65 | LOG_ID_MAX |
Mark Salyzyn | 0336e35 | 2013-11-08 06:58:01 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 68 | // Formats a message to the log (priority 'fatal'), then aborts. |
Elliott Hughes | 695713e | 2017-06-20 17:28:42 -0700 | [diff] [blame] | 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) \ |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 77 | |
Elliott Hughes | 695713e | 2017-06-20 17:28:42 -0700 | [diff] [blame] | 78 | |
| 79 | // These functions do return, so callers that want to abort, must do so themselves, |
| 80 | // or use the macro above. |
Elliott Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 81 | void async_safe_fatal_no_abort(const char* fmt, ...) __printflike(1, 2); |
| 82 | void async_safe_fatal_va_list(const char* prefix, const char* fmt, va_list args); |
Elliott Hughes | 61e699a | 2013-06-12 14:05:46 -0700 | [diff] [blame] | 83 | |
| 84 | // |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 85 | // Formatting routines for the C library's internal debugging. |
Elliott Hughes | 91570ce | 2014-07-10 12:34:23 -0700 | [diff] [blame] | 86 | // Unlike the usual alternatives, these don't allocate, and they don't drag in all of stdio. |
Josh Gao | c17e5a4 | 2017-03-08 21:31:48 -0800 | [diff] [blame] | 87 | // These are async signal safe, so they can be called from signal handlers. |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 88 | // |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 89 | |
Elliott Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 90 | int async_safe_format_buffer(char* buf, size_t size, const char* fmt, ...) __printflike(3, 4); |
| 91 | int async_safe_format_buffer_va_list(char* buffer, size_t buffer_size, const char* format, va_list args); |
Josh Gao | 273991c | 2017-02-15 11:46:55 -0800 | [diff] [blame] | 92 | |
Elliott Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 93 | int async_safe_format_fd(int fd, const char* format , ...) __printflike(2, 3); |
| 94 | int async_safe_format_log(int pri, const char* tag, const char* fmt, ...) __printflike(3, 4); |
Josh Gao | a0f6dc5 | 2017-10-12 13:34:12 -0700 | [diff] [blame] | 95 | int async_safe_format_log_va_list(int pri, const char* tag, const char* fmt, va_list ap); |
Elliott Hughes | 3f66e74 | 2017-08-01 13:24:40 -0700 | [diff] [blame] | 96 | int async_safe_write_log(int pri, const char* tag, const char* msg); |
Colin Cross | 2c75991 | 2016-02-05 16:17:39 -0800 | [diff] [blame] | 97 | |
Dimitry Ivanov | 6391e1a | 2017-02-23 17:57:14 -0800 | [diff] [blame] | 98 | #define CHECK(predicate) \ |
| 99 | do { \ |
| 100 | if (!(predicate)) { \ |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 101 | async_safe_fatal("%s:%d: %s CHECK '" #predicate "' failed", \ |
Dimitry Ivanov | 6391e1a | 2017-02-23 17:57:14 -0800 | [diff] [blame] | 102 | __FILE__, __LINE__, __FUNCTION__); \ |
| 103 | } \ |
| 104 | } while(0) |
| 105 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 106 | __END_DECLS |
| 107 | |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 108 | #endif |