From 4a98e5d75ecaef58f6a878574947fb5259ce3e43 Mon Sep 17 00:00:00 2001 From: Alice Carroll Date: Wed, 7 Apr 2021 18:31:39 -0300 Subject: [PATCH] Implement dbg_to --- dbg.h | 28 ++++++++++++++++++---------- tests/basic.cpp | 2 +- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/dbg.h b/dbg.h index 1c0d24f..daff46e 100644 --- a/dbg.h +++ b/dbg.h @@ -667,7 +667,8 @@ class DebugOutput { } template - auto print(std::initializer_list exprs, + auto print(std::ostream& os, + std::initializer_list exprs, std::initializer_list types, T&&... values) -> last_t { if (exprs.size() != sizeof...(values)) { @@ -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(values)...); + return print_impl(os, exprs.begin(), types.begin(), + std::forward(values)...); } private: template - 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); @@ -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(value); } template - 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 { - print_impl(exprs, types, std::forward(value)); - return print_impl(exprs + 1, types + 1, std::forward(rest)...); + print_impl(os, exprs, types, std::forward(value)); + return print_impl(os, exprs + 1, types + 1, std::forward(rest)...); } const char* ansi(const char* code) const { @@ -802,10 +808,12 @@ auto identity(T&&, U&&... u) -> last_t { #define DBG_TYPE_NAME(x) dbg::type_name() -#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 diff --git a/tests/basic.cpp b/tests/basic.cpp index 4c312e7..668eed2 100644 --- a/tests/basic.cpp +++ b/tests/basic.cpp @@ -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__)