Skip to content

Commit

Permalink
Add stream-style logging macros
Browse files Browse the repository at this point in the history
  • Loading branch information
dentiny committed Dec 11, 2024
1 parent 5705473 commit 34d5495
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/include/common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,47 @@

#include <cassert>
#include <exception>
#include <iostream>
#include <sstream>
#include <stdexcept>

namespace bustub {

#define BUSTUB_ASSERT(expr, message) assert((expr) && (message))

namespace internal {

// An internal stream, used to take C++ stream-style parameters for display, and exit the program with `std::abort`.
class LogFatalStream {
public:
LogFatalStream(const char *file, int line) : file_(file), line_(line) {}

~LogFatalStream() {
std::cerr << file_ << ":" << line_ << ": " << log_stream_.str() << std::endl;
std::abort();
}

template <typename T>
LogFatalStream &operator<<(const T &val) {
log_stream_ << val;
return *this;
}

private:
const char *file_;
int line_;
std::ostringstream log_stream_;
};

} // namespace internal

// A macro which checks `expr` value and performs assert.
// Different from `BUSTUB_ASSERT`, it takes stream-style parameters.
#define BUSTUB_ASSERT_AND_LOG(expr) \
if (bool val = (expr); !val) internal::LogFatalStream { \
__FILE__, __LINE__ \
}

#define UNIMPLEMENTED(message) throw std::logic_error(message)

#define BUSTUB_ENSURE(expr, message) \
Expand Down

0 comments on commit 34d5495

Please sign in to comment.