blob: 0e02ea76ef3ede241de89b8f0756cbbc902f3535 [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
Elliott Hughescbc80ba2018-02-13 14:26:29 -080029#pragma once
David 'Digit' Turner5c734642010-01-20 12:36:51 -080030
Elliott Hughes1e980b62013-01-17 18:36:06 -080031#include <sys/cdefs.h>
David 'Digit' Turner5c734642010-01-20 12:36:51 -080032#include <stdarg.h>
33#include <stddef.h>
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070034#include <stdint.h>
Elliott Hughes695713e2017-06-20 17:28:42 -070035#include <stdlib.h>
David 'Digit' Turner5c734642010-01-20 12:36:51 -080036
Elliott Hughes3019d782019-02-13 12:39:07 -080037// This file is an alternative to <android/log.h>, but reuses
38// `android_LogPriority` and should not have conflicting identifiers.
39#include <android/log.h>
40
Christopher Ferris7a3681e2017-04-24 17:48:32 -070041// These functions do not allocate memory to send data to the log.
42
Elliott Hughes1e980b62013-01-17 18:36:06 -080043__BEGIN_DECLS
44
Elliott Hughes0d787c12013-04-04 13:46:46 -070045// Formats a message to the log (priority 'fatal'), then aborts.
Elliott Hughes695713e2017-06-20 17:28:42 -070046// Implemented as a macro so that async_safe_fatal isn't on the stack when we crash:
47// we appear to go straight from the caller to abort, saving an uninteresting stack
48// frame.
49#define async_safe_fatal(...) \
50 do { \
51 async_safe_fatal_no_abort(__VA_ARGS__); \
52 abort(); \
53 } while (0) \
Elliott Hughes0d787c12013-04-04 13:46:46 -070054
Elliott Hughes695713e2017-06-20 17:28:42 -070055
56// These functions do return, so callers that want to abort, must do so themselves,
57// or use the macro above.
Elliott Hughes3f66e742017-08-01 13:24:40 -070058void async_safe_fatal_no_abort(const char* fmt, ...) __printflike(1, 2);
59void async_safe_fatal_va_list(const char* prefix, const char* fmt, va_list args);
Elliott Hughes61e699a2013-06-12 14:05:46 -070060
61//
Elliott Hughes1e980b62013-01-17 18:36:06 -080062// Formatting routines for the C library's internal debugging.
Elliott Hughes91570ce2014-07-10 12:34:23 -070063// Unlike the usual alternatives, these don't allocate, and they don't drag in all of stdio.
Josh Gaoc17e5a42017-03-08 21:31:48 -080064// These are async signal safe, so they can be called from signal handlers.
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070065//
Elliott Hughes1e980b62013-01-17 18:36:06 -080066
Elliott Hughes3f66e742017-08-01 13:24:40 -070067int async_safe_format_buffer(char* buf, size_t size, const char* fmt, ...) __printflike(3, 4);
68int async_safe_format_buffer_va_list(char* buffer, size_t buffer_size, const char* format, va_list args);
Josh Gao273991c2017-02-15 11:46:55 -080069
Elliott Hughes3f66e742017-08-01 13:24:40 -070070int async_safe_format_fd(int fd, const char* format , ...) __printflike(2, 3);
Ryan Prichard5de9a31c2018-10-02 18:13:28 -070071int async_safe_format_fd_va_list(int fd, const char* format, va_list args);
Elliott Hughes3019d782019-02-13 12:39:07 -080072int async_safe_format_log(int priority, const char* tag, const char* fmt, ...) __printflike(3, 4);
73int async_safe_format_log_va_list(int priority, const char* tag, const char* fmt, va_list ap);
74int async_safe_write_log(int priority, const char* tag, const char* msg);
Dimitry Ivanov6391e1a2017-02-23 17:57:14 -080075
Elliott Hughes1e980b62013-01-17 18:36:06 -080076__END_DECLS