Skip to content
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

Allow specifying the output stream #106

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
19 changes: 13 additions & 6 deletions dbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<T>(value);
}
Expand All @@ -719,6 +723,7 @@ class DebugOutput {
}

const bool m_use_colorized_output;
std::ostream& stream;

std::string m_location;

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, 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
Expand Down
5 changes: 3 additions & 2 deletions tests/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down