blob: f9ff457311d434fdb58c7e0df657f49493c44592 [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#ifndef AOS_COMMON_DIE_H_
2#define AOS_COMMON_DIE_H_
3
4#include <stdarg.h>
5
Brian Silvermanf7986142014-04-21 17:42:35 -07006#include "aos/common/macros.h"
Brian Silvermanaf784862014-05-13 08:14:55 -07007#include "aos/common/libc/aos_strerror.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).
14void Die(const char *format, ...)
15 __attribute__((noreturn))
Brian Silvermanf7986142014-04-21 17:42:35 -070016 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 1, 2)));
brians343bc112013-02-10 01:53:46 +000017void VDie(const char *format, va_list args)
18 __attribute__((noreturn))
Brian Silvermanf7986142014-04-21 17:42:35 -070019 __attribute__((format(GOOD_PRINTF_FORMAT_TYPE, 1, 0)));
brians343bc112013-02-10 01:53:46 +000020
Brian Silverman01be0002014-05-10 15:44:38 -070021
22// The same as Die except appends " because of %d (%s)" (formatted with errno
23// and aos_strerror(errno)) to the message.
24#define PDie(format, args...) \
25 do { \
26 const int error = errno; \
27 ::aos::Die(format " because of %d (%s)", ##args, error, \
28 aos_strerror(error)); \
Brian Silvermanb20e06a2014-09-03 12:09:24 -040029 } while (false)
30
31// The same as Die except appends " because of %d (%s)" (formatted with error
32// and aos_strerror(error)) to the message.
33// PCHECK is to PDie as PRCHECK is to PRDie
34//
35// Example:
36// const int ret = pthread_mutex_lock(whatever);
37// if (ret != 0) PRDie(ret, "pthread_mutex_lock(%p) failed", whatever);
38#define PRDie(error, format, args...) \
39 do { \
40 ::aos::Die(format " because of %d (%s)", ##args, error, \
41 aos_strerror(error)); \
42 } while (false)
Brian Silverman01be0002014-05-10 15:44:38 -070043
Brian Silverman8d2e56e2013-09-23 17:55:03 -070044// Turns on (or off) "test mode", where (V)Die doesn't write out files and
45// doesn't print to stdout.
46// Test mode defaults to false.
47void SetDieTestMode(bool test_mode);
48
brians343bc112013-02-10 01:53:46 +000049} // namespace aos
50
51#endif // AOS_COMMON_DIE_H_