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 4a98e5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
28 changes: 18 additions & 10 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 @@ -676,12 +677,16 @@ class DebugOutput {
<< "The number of arguments mismatch, please check unprotected comma"
<< ansi(ANSI_RESET) << std::endl;
}
return print_impl(exprs.begin(), types.begin(), std::forward<T>(values)...);
return print_impl(os, exprs.begin(), types.begin(),
std::forward<T>(values)...);
}

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 +701,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 +808,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
2 changes: 1 addition & 1 deletion tests/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ std::string pretty_print(T&& value) {

#define dbg_def(def) \
dbg::DebugOutput(__FILE__, __LINE__, __func__) \
.print({#def}, {"definition"}, def)
.print(std::cerr, {#def}, {"definition"}, def)

TEST_CASE("Environment information") {
#if defined(__GNUC__)
Expand Down

0 comments on commit 4a98e5d

Please sign in to comment.