Created
March 26, 2021 05:22
-
-
Save faithandbrave/2eefe7fc78b883d668e85490343c71ae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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