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