blob: 592fc5533fc74b5ea3027e6dd7346ea359cb35d0 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#ifndef AOS_DIE_H_
2#define AOS_DIE_H_
brians343bc112013-02-10 01:53:46 +00003
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cstdarg>
brians343bc112013-02-10 01:53:46 +00005
John Park33858a32018-09-28 23:05:48 -07006#include "aos/libc/aos_strerror.h"
Tyler Chatowbf0609c2021-07-31 16:13:27 -07007#include "aos/macros.h"
Brian Silvermanf7986142014-04-21 17:42:35 -07008
brians343bc112013-02-10 01:53:46 +00009namespace aos {
10
11// Terminates the task/process and logs a message (without using the logging
12// framework). Designed for use in code that can't use the logging framework
13// (code that can should LOG(FATAL), which calls this).
Tyler Chatowbf0609c2021-07-31 16:13:27 -070014void Die(const char *format, ...) __attribute__((noreturn))
15__attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 1, 2)));
16void VDie(const char *format, va_list args) __attribute__((noreturn))
17__attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 1, 0)));
Brian Silverman01be0002014-05-10 15:44:38 -070018
19// The same as Die except appends " because of %d (%s)" (formatted with errno
20// and aos_strerror(errno)) to the message.
21#define PDie(format, args...) \
22 do { \
23 const int error = errno; \
24 ::aos::Die(format " because of %d (%s)", ##args, error, \
25 aos_strerror(error)); \
Brian Silvermanb20e06a2014-09-03 12:09:24 -040026 } while (false)
27
28// The same as Die except appends " because of %d (%s)" (formatted with error
29// and aos_strerror(error)) to the message.
30// PCHECK is to PDie as PRCHECK is to PRDie
31//
32// Example:
33// const int ret = pthread_mutex_lock(whatever);
34// if (ret != 0) PRDie(ret, "pthread_mutex_lock(%p) failed", whatever);
35#define PRDie(error, format, args...) \
36 do { \
37 ::aos::Die(format " because of %d (%s)", ##args, error, \
38 aos_strerror(error)); \
39 } while (false)
Brian Silverman01be0002014-05-10 15:44:38 -070040
Brian Silverman8d2e56e2013-09-23 17:55:03 -070041// Turns on (or off) "test mode", where (V)Die doesn't write out files and
42// doesn't print to stdout.
43// Test mode defaults to false.
44void SetDieTestMode(bool test_mode);
45
brians343bc112013-02-10 01:53:46 +000046} // namespace aos
47
John Park33858a32018-09-28 23:05:48 -070048#endif // AOS_DIE_H_