File tree 1 file changed +34
-0
lines changed
1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 14
14
15
15
#include < cassert>
16
16
#include < exception>
17
+ #include < sstream>
17
18
#include < stdexcept>
18
19
19
20
namespace bustub {
20
21
21
22
#define BUSTUB_ASSERT (expr, message ) assert((expr) && (message))
22
23
24
+ namespace internal {
25
+
26
+ // An internal stream, used to take C++ stream-style parameters for display, and exit the program with `std::abort`.
27
+ class LogFatalStream {
28
+ public:
29
+ LogFatalStream (const char *file, int line) : file_(file), line_(line) {}
30
+
31
+ ~LogFatalStream () {
32
+ std::cerr << file_ << " :" << line_ << " : " << log_stream_.str () << std::endl;
33
+ std::abort ();
34
+ }
35
+
36
+ template <typename T>
37
+ LogFatalStream &operator <<(const T &val) {
38
+ log_stream_ << val;
39
+ return *this ;
40
+ }
41
+
42
+ private:
43
+ const char *file_;
44
+ int line_;
45
+ std::ostringstream log_stream_;
46
+ };
47
+
48
+ } // namespace internal
49
+
50
+ // A macro which checks `expr` value and performs assert.
51
+ // Different from `BUSTUB_ASSERT`, it takes stream-style parameters.
52
+ #define BUSTUB_ASSERT_AND_LOG (expr ) \
53
+ if (bool val = (expr); !val) LogFatalStream { \
54
+ __FILE__, __LINE__ \
55
+ }
56
+
23
57
#define UNIMPLEMENTED (message ) throw std::logic_error (message)
24
58
25
59
#define BUSTUB_ENSURE (expr, message ) \
You can’t perform that action at this time.
0 commit comments