Skip to content

Commit

Permalink
Implement dbg_to
Browse files Browse the repository at this point in the history
  • Loading branch information
thecaralice committed Apr 7, 2021
1 parent a6200c5 commit 13d1864
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions dbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,8 @@ class DebugOutput {
}

template <typename... T>
auto print(std::initializer_list<expr_t> exprs,
auto print(std::ostream& os,
std::initializer_list<expr_t> exprs,
std::initializer_list<std::string> types,
T&&... values) -> last_t<T...> {
if (exprs.size() != sizeof...(values)) {
Expand All @@ -681,7 +682,10 @@ class DebugOutput {

private:
template <typename T>
T&& print_impl(const expr_t* expr, const std::string* type, T&& value) {
T&& print_impl(std::ostream& os,
const expr_t* expr,
const std::string* type,
T&& value) {
const T& ref = value;
std::stringstream stream_value;
const bool print_expr_and_type = pretty_print(stream_value, ref);
Expand All @@ -696,18 +700,19 @@ class DebugOutput {
output << " (" << ansi(ANSI_TYPE) << *type << ansi(ANSI_RESET) << ")";
}
output << std::endl;
std::cerr << output.str();
os << output.str();

return std::forward<T>(value);
}

template <typename T, typename... U>
auto print_impl(const expr_t* exprs,
auto print_impl(std::ostream& os,
const expr_t* exprs,
const std::string* types,
T&& value,
U&&... rest) -> last_t<T, U...> {
print_impl(exprs, types, std::forward<T>(value));
return print_impl(exprs + 1, types + 1, std::forward<U>(rest)...);
print_impl(os, exprs, types, std::forward<T>(value));
return print_impl(os, exprs + 1, types + 1, std::forward<U>(rest)...);
}

const char* ansi(const char* code) const {
Expand Down Expand Up @@ -802,10 +807,12 @@ auto identity(T&&, U&&... u) -> last_t<U...> {

#define DBG_TYPE_NAME(x) dbg::type_name<decltype(x)>()

#define dbg(...) \
dbg::DebugOutput(__FILE__, __LINE__, __func__) \
.print({DBG_MAP(DBG_STRINGIFY, __VA_ARGS__)}, \
#define dbg_to(stream, ...) \
dbg::DebugOutput(__FILE__, __LINE__, __func__) \
.print(stream, {DBG_MAP(DBG_STRINGIFY, __VA_ARGS__)}, \
{DBG_MAP(DBG_TYPE_NAME, __VA_ARGS__)}, __VA_ARGS__)

#define dbg(...) dbg_to(std::cerr, __VA_ARGS__)
#else
#define dbg(...) dbg::identity(__VA_ARGS__)
#endif // DBG_MACRO_DISABLE
Expand Down

0 comments on commit 13d1864

Please sign in to comment.