dbg-macro icon indicating copy to clipboard operation
dbg-macro copied to clipboard

Allow specifying the output stream

Open alexeyr opened this issue 3 years ago • 9 comments

When using RapidCheck, I'd like the option to write to RC_LOG() instead of std::cerr:

Since the property function may be called many times, using classic debugging techniques such as printing to stdout or stderr can prove confusing. The RC_LOG macro provides a better way to log information during the execution of a test case. Information logged this way will only be printed on failure after the minimal test case has been found.

alexeyr avatar Mar 25 '21 10:03 alexeyr

This likely wouldn't be too hard. We could just add #define DBG_OUTPUT std::cerr and then users could override it.

DerekCresswell avatar Mar 25 '21 19:03 DerekCresswell

Will you make this change or should I make a PR?

alexeyr avatar Mar 26 '21 10:03 alexeyr

By all means, be my guest. We'd need that definition, with cerr as the default, and to add that information to the readme. As well we'd need a test case for this. I'll certainly review a PR if you make it.

I might mention, in the past the owner of this repo has expressed that they want as few as possible compile time options so it's possible they may reject this change. But seeing as it's fairly simple, it might not be a big issue.

DerekCresswell avatar Mar 26 '21 17:03 DerekCresswell

Indeed - I'd prefer a non-preprocessor way to make this work. Maybe this could be an argument to dbg::DebugOutput or to .print(…). This way, we would have

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

as the default, but users could define their own

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

Note: I haven't thought about this in detail. That approach might not work.

sharkdp avatar Mar 27 '21 09:03 sharkdp

What about a variant of dbg like

#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__)

and then

#define my_dbg(...) dbg_to(my_stream, __VA_ARGS__)

? It's still a preprocessor solution but less "magical", not a compile-time option, and the users don't need to know internal details (I also haven't yet checked if this works).

alexeyr avatar Apr 01 '21 13:04 alexeyr

@alexeyr I like that :+1:

sharkdp avatar Apr 01 '21 14:04 sharkdp

Then I'll try to make a PR when I have free time.

alexeyr avatar Apr 01 '21 14:04 alexeyr

What about output coloring? When should it be enabled? For example, cout would have it enabled if stdout is a tty and an ofstream would have it disabled. Is it possible to cover all cases?

thecaralice avatar Apr 08 '21 19:04 thecaralice

Good question. Maybe dbg_to should take a third (enum or bool) parameter that enables/disables colors.

sharkdp avatar Apr 13 '21 13:04 sharkdp