diff --git a/dbg.h b/dbg.h index 1c0d24f..6ebcd70 100644 --- a/dbg.h +++ b/dbg.h @@ -653,8 +653,12 @@ class DebugOutput { // Helper alias to avoid obscure type `const char* const*` in signature. using expr_t = const char*; - DebugOutput(const char* filepath, int line, const char* function_name) - : m_use_colorized_output(isColorizedOutputEnabled()) { + DebugOutput(std::ostream& os, + bool colorized_output, + const char* filepath, + int line, + const char* function_name) + : m_use_colorized_output(colorized_output), stream(os) { std::string path = filepath; const std::size_t path_length = path.length(); if (path_length > MAX_PATH_LENGTH) { @@ -696,7 +700,7 @@ class DebugOutput { output << " (" << ansi(ANSI_TYPE) << *type << ansi(ANSI_RESET) << ")"; } output << std::endl; - std::cerr << output.str(); + stream << output.str(); return std::forward(value); } @@ -719,6 +723,7 @@ class DebugOutput { } const bool m_use_colorized_output; + std::ostream& stream; std::string m_location; @@ -802,10 +807,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, colorized_output, ...) \ + dbg::DebugOutput(stream, colorized_output, __FILE__, __LINE__, __func__) \ + .print({DBG_MAP(DBG_STRINGIFY, __VA_ARGS__)}, \ {DBG_MAP(DBG_TYPE_NAME, __VA_ARGS__)}, __VA_ARGS__) + +#define dbg(...) dbg_to(std::cerr, dbg::isColorizedOutputEnabled(), __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..10d26f0 100644 --- a/tests/basic.cpp +++ b/tests/basic.cpp @@ -25,8 +25,9 @@ std::string pretty_print(T&& value) { return stream.str(); } -#define dbg_def(def) \ - dbg::DebugOutput(__FILE__, __LINE__, __func__) \ +#define dbg_def(def) \ + dbg::DebugOutput(std::cerr, dbg::isColorizedOutputEnabled(), __FILE__, \ + __LINE__, __func__) \ .print({#def}, {"definition"}, def) TEST_CASE("Environment information") {