John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #ifndef AOS_DIE_H_ |
| 2 | #define AOS_DIE_H_ |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <cstdarg> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 5 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 6 | #include "aos/libc/aos_strerror.h" |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 7 | #include "aos/macros.h" |
Brian Silverman | f798614 | 2014-04-21 17:42:35 -0700 | [diff] [blame] | 8 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | namespace 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 Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 14 | void Die(const char *format, ...) __attribute__((noreturn)) |
| 15 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 1, 2))); |
| 16 | void VDie(const char *format, va_list args) __attribute__((noreturn)) |
| 17 | __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 1, 0))); |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 18 | |
| 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 Silverman | b20e06a | 2014-09-03 12:09:24 -0400 | [diff] [blame] | 26 | } 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 Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 40 | |
Brian Silverman | 8d2e56e | 2013-09-23 17:55:03 -0700 | [diff] [blame] | 41 | // 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. |
| 44 | void SetDieTestMode(bool test_mode); |
| 45 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 46 | } // namespace aos |
| 47 | |
John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 48 | #endif // AOS_DIE_H_ |