-
-
Notifications
You must be signed in to change notification settings - Fork 246
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
added timestamp #91
added timestamp #91
Conversation
Thank you for your feedback and for your contribution! I would like to avoid adding more (compile time) configuration options to We could think about adding this by default. The problem I see here is that the current format (HH:MM:SS) is only going to be useful for scenarios where the user program runs for more than one second. If that is not the case, the user would probably like to see a (much) higher resolution in the time output. Going down to the microsecond level would lead to a We could also think about implementing this in a different way where we add an easy option to print the time with dbg(dbg::time()) or dbg(dbg::time) What do you think? What is your use case? |
Your thought is better. This is just a proposal for you guys. In fact the version with microsecond can be expected to be more useful, but I did also consider about the trade off between microsecond and characters, so I just came out with the configuration. I'd like to see a better version with your solution. |
With which one? The If yes, would you be interested to try an implementation? Otherwise, should we close this? |
Sorry I did misunderstand your meaning. |
I've implemented
How do you think about it? And I still haven't a good idea on how to test this function. |
Just a thought what if we had two functionalities : Then similar to hex we can do : If that sounds cool I'd be willing to give it a go. |
Hm. I think that's not quite the same as dbg(dbg::time(), x)
👍 |
I combined my commits into one, so my changes can be reviewed more clearly :) |
dbg.h
Outdated
namespace timestamp { | ||
|
||
// Provide a timestamp at the precision of microsecond. | ||
// Not exactly the current microsecond yet, still a useful reference. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, one option would be to make timestamp_t
an empty struct
and only determine the time when it's actually printed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This option sounds so genius! I cannot agree anymore. From your suggestion I've got another idea, how about just using struct time {};
?
dbg.h
Outdated
// Provide a timestamp at the precision of microsecond. | ||
// Not exactly the current microsecond yet, still a useful reference. | ||
|
||
struct timestamp_t { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we remove the namespace timestamp
and simply name this type timestamp
(it's still in the dbg
namespace).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just thought I could use this namespace to distinguish the relevant code for the timestamp, but I can remove it if unnecessary.
dbg.h
Outdated
@@ -626,6 +649,19 @@ class DebugOutput { | |||
return std::forward<T>(value); | |||
} | |||
|
|||
timestamp::timestamp_t&& print(const std::string& type, timestamp::timestamp_t value) const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of repeating the whole printing logic here, we could write an overload for pretty_print
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A specialized pretty_print
works neat and elegant indeed and this is how this project works, but this would prevent me from using ANSI_XXX
s. I just think printing timestamp with color might be better, but I can to forget about it if unnecessary.
tests/demo.cpp
Outdated
@@ -7,6 +7,8 @@ | |||
#include <string> | |||
#include <tuple> | |||
#include <vector> | |||
#include <ctime> | |||
#include <chrono> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These includes are not necessary, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I did forget about them after testing.
dbg.h
Outdated
template <> | ||
inline bool pretty_print(std::ostream& stream, const time&) { | ||
const auto ms = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() % 1000000; | ||
const auto hms = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use the same timestamp here as for the ms
computation. Otherwise, this could lead to inconsistencies.
I'll make the necessary changes.
dbg.h
Outdated
@@ -453,6 +457,16 @@ inline bool pretty_print(std::ostream& stream, const std::tuple<>&) { | |||
return true; | |||
} | |||
|
|||
template <> | |||
inline bool pretty_print(std::ostream& stream, const time&) { | |||
const auto ms = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count() % 1000000; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ms
is slightly confusing, as it usually describes milliseconds. I'll rename it to us
.
dbg.h
Outdated
const auto hms = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); | ||
const std::tm* tm = std::localtime(&hms); | ||
stream << std::put_time(tm, "%H:%M:%S") << '.' | ||
<< ms ; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will print a wrong value if ms
has a value smaller than 100000, because the leading zero(s) are missing. If ms = 12345
, it should print HH:MM:SS.012345
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much!
I should say thanks. Cause I never expect things could be this complicated, and thus I've learned a lot from this. |
Yeah, that happened on Windows because apparently I have switched to |
Looks like that works on both platforms 🎆 |
I'm glad it works, thanks again David! |
#90