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

added timestamp #91

Merged
merged 6 commits into from
Mar 9, 2020
Merged

added timestamp #91

merged 6 commits into from
Mar 9, 2020

Conversation

MrHate
Copy link
Contributor

@MrHate MrHate commented Feb 23, 2020

#90

@sharkdp
Copy link
Owner

sharkdp commented Feb 25, 2020

Thank you for your feedback and for your contribution!

I would like to avoid adding more (compile time) configuration options to dbg-macro because the number of scenarios that have to be tested increases a lot (exponentially).

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 HH:MM:SS.SSSSSS format that takes away 15 characters (plus padding) of the valuable horizontal screen width.

We could also think about implementing this in a different way where we add an easy option to print the time with dbg(…). Maybe something like

dbg(dbg::time())

or

dbg(dbg::time)

What do you think? What is your use case?

@MrHate
Copy link
Contributor Author

MrHate commented Feb 26, 2020

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.

@sharkdp
Copy link
Owner

sharkdp commented Feb 27, 2020

I'd like to see a better version with your solution.

With which one? The dbg(dbg::time()) suggestion?

If yes, would you be interested to try an implementation? Otherwise, should we close this?

@MrHate
Copy link
Contributor Author

MrHate commented Feb 28, 2020

Sorry I did misunderstand your meaning.
I will try to implement the the dbg(dbg::time()) version.

@MrHate
Copy link
Contributor Author

MrHate commented Feb 28, 2020

I've implemented dbg(dbg::time()) version here, now it works like this:

[..p/dbg/tests/demo.cpp:140 (main)] 13:37:03.745746

How do you think about it?

And I still haven't a good idea on how to test this function.

@DerekCresswell
Copy link
Contributor

DerekCresswell commented Mar 3, 2020

Just a thought what if we had two functionalities :
dbg::time() prints out a time stamp

Then similar to hex we can do :
dbg(dbg::time(x)) to print x with a timestamp as well.

If that sounds cool I'd be willing to give it a go.

@sharkdp
Copy link
Owner

sharkdp commented Mar 3, 2020

Then similar to hex we can do :
dbg(dbg::time(x)) to print x with a timestamp as well.

Hm. I think that's not quite the same as dbg::hex(…). I'd rather stick with an argument-less dbg::time() and work on #2 in order to support things like

dbg(dbg::time(), x)

If that sounds cool I'd be willing to give it a go.

👍

@MrHate
Copy link
Contributor Author

MrHate commented Mar 3, 2020

I combined my commits into one, so my changes can be reviewed more clearly :)
So how about my pr now, do I need revision or something else?

dbg.h Outdated
namespace timestamp {

// Provide a timestamp at the precision of microsecond.
// Not exactly the current microsecond yet, still a useful reference.
Copy link
Owner

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.

Copy link
Contributor Author

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 {
Copy link
Owner

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).

Copy link
Contributor Author

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 {
Copy link
Owner

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.

Copy link
Contributor Author

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_XXXs. 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>
Copy link
Owner

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?

Copy link
Contributor Author

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());
Copy link
Owner

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;
Copy link
Owner

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 ;
Copy link
Owner

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

Copy link
Owner

@sharkdp sharkdp left a 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!

@MrHate
Copy link
Contributor Author

MrHate commented Mar 9, 2020

I should say thanks. Cause I never expect things could be this complicated, and thus I've learned a lot from this.
But another question is, there is a error here that high_resolution_clock cannot be converted into system_clock, and there is no member named to_time_t in high_resolution_clock. My idea is just make the microsecond as a reference but not exactly the current microsecond cause it cannot be such precise itself, so maybe it's not a bad thing to divide the microsecond part out.

@sharkdp
Copy link
Owner

sharkdp commented Mar 9, 2020

But another question is, there is a error here that high_resolution_clock cannot be converted into system_clock, and there is no member named to_time_t in high_resolution_clock.

Yeah, that happened on Windows because apparently high_resolution_clock refers to steady_clock there.

I have switched to system_clock. Let's see if that works.

@sharkdp sharkdp merged commit 1a1b798 into sharkdp:master Mar 9, 2020
@sharkdp
Copy link
Owner

sharkdp commented Mar 9, 2020

Looks like that works on both platforms 🎆

@MrHate
Copy link
Contributor Author

MrHate commented Mar 9, 2020

I'm glad it works, thanks again David!

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

Successfully merging this pull request may close these issues.

None yet

3 participants