Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Created June 1, 2021 13:44
Show Gist options
  • Save HalCanary/c35499751a6c1712b5d05f7bfbf05573 to your computer and use it in GitHub Desktop.
Save HalCanary/c35499751a6c1712b5d05f7bfbf05573 to your computer and use it in GitHub Desktop.
do_or_fail.h
#ifndef DO_OR_FAIL_H
#define DO_OR_FAIL_H
extern void fail_now(const char* file, int line, const char* msg, int err);
#define DO_OR_FAIL(SUCCESS, MSG) \
do { if (!(SUCCESS)) { fail_now(__FILE__, __LINE__, (MSG), errno); } } while (false)
#endif // DO_OR_FAIL_H
////////////////////////////////////////////////////////////////////////////////
void fail_now(const char* file, int line, const char* msg, int err) {
std::fprintf(stderr, "%s: %d: %s: %s\n", file, line, msg, err ? std::strerror(err) : "");
std::exit(EXIT_FAILURE);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment