Skip to content

Add stream-style logging macros #786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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