add PRCHECK and PRDie
Change-Id: I1b1e8f54d24d0ba956f0884c3fcf6db367bf137f
diff --git a/aos/common/die.h b/aos/common/die.h
index 80e8450..f9ff457 100644
--- a/aos/common/die.h
+++ b/aos/common/die.h
@@ -26,7 +26,20 @@
const int error = errno; \
::aos::Die(format " because of %d (%s)", ##args, error, \
aos_strerror(error)); \
- } while (false);
+ } while (false)
+
+// The same as Die except appends " because of %d (%s)" (formatted with error
+// and aos_strerror(error)) to the message.
+// PCHECK is to PDie as PRCHECK is to PRDie
+//
+// Example:
+// const int ret = pthread_mutex_lock(whatever);
+// if (ret != 0) PRDie(ret, "pthread_mutex_lock(%p) failed", whatever);
+#define PRDie(error, format, args...) \
+ do { \
+ ::aos::Die(format " because of %d (%s)", ##args, error, \
+ aos_strerror(error)); \
+ } while (false)
// Turns on (or off) "test mode", where (V)Die doesn't write out files and
// doesn't print to stdout.
diff --git a/aos/common/logging/logging.h b/aos/common/logging/logging.h
index a149d39..66b88c7 100644
--- a/aos/common/logging/logging.h
+++ b/aos/common/logging/logging.h
@@ -233,11 +233,27 @@
return value;
}
+inline void CheckSyscallReturn(const char *syscall_string, int value) {
+ if (__builtin_expect(value != 0, false)) {
+ PELOG(FATAL, value, "%s failed", syscall_string);
+ }
+}
+
// Check that syscall does not return -1. If it does, PLOG(FATAL)s. This is
// useful for quickly checking syscalls where it's not very useful to print out
-// the values of any of the arguments.
+// the values of any of the arguments. Returns the result otherwise.
+//
+// Example: const int fd = PCHECK(open("/tmp/whatever", O_WRONLY))
#define PCHECK(syscall) ::aos::CheckSyscall(STRINGIFY(syscall), syscall)
+// PELOG(FATAL)s with the result of syscall if it returns anything other than 0.
+// This is useful for quickly checking things like many of the pthreads
+// functions where it's not very useful to print out the values of any of the
+// arguments.
+//
+// Example: PRCHECK(munmap(address, length))
+#define PRCHECK(syscall) ::aos::CheckSyscallReturn(STRINGIFY(syscall), syscall)
+
} // namespace aos
#endif // __cplusplus