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

<ios> and <iomanip> flags #61

Closed
mhammerc opened this issue Sep 24, 2019 · 8 comments
Closed

<ios> and <iomanip> flags #61

mhammerc opened this issue Sep 24, 2019 · 8 comments

Comments

@mhammerc
Copy link

mhammerc commented Sep 24, 2019

Hello!

This project is very great, thank you!

I often need to print values as hexadecimal, and bonus point as binary. It would be nice if this library support it.

Maybe dbg(std::hex, number)? This does break the implicit rull that arguments are all "dbged" but it could work.
Maybe dbg::dbg(dbg::hex(number))?

If you do not have time, I can PR this feature if you are ok on the concept and the way of doing it!

EDIT: maybe I missed the already-existing feature to print as hexadecimal!

@mhammerc
Copy link
Author

mhammerc commented Sep 24, 2019

Implementation idea:

dbg(dbg_hex(number))

dbg_hex is a simple templated struct which hold the value. std::ostream& operator<< would be specialized to print as hex like you shown in the README.

@sharkdp
Copy link
Owner

sharkdp commented Sep 25, 2019

Thank you for your feedback!

I see how this could be useful and I kind of like the idea. The issue I see is that this could lead to feature creep.

For now, this header has a very simple "API": the dbg(…) macro - nothing more. If we decide to add custom formatting options, I'm afraid it would lead to a whole bunch of things that could potentially be added.

Also, I would like to avoid adding more names to the global namespace. If at all, it should probably be dbg(dbg_macro::hex(…)). If this could somehow nicely integrate with the I/O manipulators from <ios> and <iomanip>, that would be an option. But I don't want dbg(…) to take a second optional argument (see #2).

@mhammerc
Copy link
Author

mhammerc commented Sep 25, 2019

I agree to not polluting the global namespace. But to keep consistency, maybe we should also move the dbg(...) macro to that new namespace? Or name the namespace dbg!

There are lots of flag from <ios> header. Best case would be supporting all of them.
When debugging, we want to be fast. No time to write a complex line like std::cout << std::hex << value << std::endl. Especially if we have more than one value to print (dbg() already solve this problem).

One problem is that std::ostream (cout, cerr and more) save flags that were set.

std::cout << std::hex;
std::cout << 15 << std::endl;

This code effectively print f (https://godbolt.org/z/caMvrD). If we apply flags from <ios> we have to duplicate std::cerr to not break user-defined flags (std::ostream stream(std::cerr.rdbuf())). Example: https://godbolt.org/z/J0GnAr

I suppose that debugging is consistent enough on a project lifetime so we can do the following (it is not a naming proposition):

 dbg::set(std::hex);
 dbg::set(std::boolalpha);
 ...
 dbg(true);
 dbg(15);

We keep the fast use of dbg() for all values, with an added optional set(), which could be project-scoped (example: set at top of the main()).

@mhammerc mhammerc changed the title Hexadecimal numbers <ios> and <iomanip> flags Sep 25, 2019
@sharkdp
Copy link
Owner

sharkdp commented Dec 20, 2019

I didn't know that we could use dbg as a namespace name in addition to the macro name. Neat!

I started an initial implementation here: #68
Glad for any feedback.

@mhammerc
Copy link
Author

Congrats!

I'll take a deep look in a day or two. Promise I'll feedback!

@mhammerc
Copy link
Author

I didn't know that we could use dbg as a namespace name in addition to the macro name. Neat!

A macro is a preprocessor! That mean it gets translated in pre-processing which happen before compilation 😋

@sharkdp
Copy link
Owner

sharkdp commented Dec 22, 2019

A macro is a preprocessor! That mean it gets translated in pre-processing which happen before compilation yum

I was afraid that the preprocessor (because it does not know anything about the syntax) would maybe affect a string like namespace dbg { when a #define dbg(…) … was around.

@mhammerc
Copy link
Author

I left a review! Great work :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants