Skip to content

Instantly share code, notes, and snippets.

@faithandbrave
Created March 26, 2021 05:22
Show Gist options
  • Save faithandbrave/2eefe7fc78b883d668e85490343c71ae to your computer and use it in GitHub Desktop.
Save faithandbrave/2eefe7fc78b883d668e85490343c71ae to your computer and use it in GitHub Desktop.
#include <sstream>
#include <stdexcept>
class MyAssertionFailed : public std::runtime_error {
public:
explicit MyAssertionFailed(const std::string& what)
: std::runtime_error(what) {}
};
class LocalLogStream {
std::ostringstream stream_;
public:
LocalLogStream(const char* head_message, const char* func, const char* file, int line) {
stream_ << head_message << ' ' << func << " at" << file << ':' << line << ' ';
}
~LocalLogStream() noexcept(false) {
throw MyAssertionFailed(stream_.str());
}
template <class T>
LocalLogStream& operator<<(const T& x) {
stream_ << x;
return *this;
}
};
#define MY_ASSERT(cond) if (cond) {} else LocalLogStream{"assertion failed " # cond, __func__, __FILE__, __LINE__}
int main() {
bool my_cond = false;
MY_ASSERT(my_cond) << "invalid argument";
MY_ASSERT(my_cond);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment