morganstanley / binlog

A high performance C++ log library, producing structured binary logs
http://opensource.morganstanley.com/binlog/
Apache License 2.0
608 stars 72 forks source link

Make std::chrono::duration loggable with pretty printing #129

Closed andrewkcorcoran closed 3 years ago

andrewkcorcoran commented 3 years ago

Currently it seems like the easiest way for users to output a std::chrono::duration object is to call ::count() on it (and remember to std::chrono::duration_cast it to a consistent ratio). It would be great if binlog had native support for durations in a similar fashion to the support added for std::chrono::time_point. It could reuse the time portion of the datetime format string for logging purposes by default and possibly add an additional override just for times/durations.

erenon commented 3 years ago

Do you mean something like this:

BINLOG_INFO("{} {} {}", std::chrono::minutes{1}, std::chrono::seconds{2}, std::chrono::milliseconds{3});
// Outputs: 1m 2s 3ms

For hours, minutes, seconds, milliseconds, microseconds, nanoseconds.

It could reuse the time portion of the datetime format string for logging purposes by default and possibly add an additional override just for times/durations.

I do not get this part: how would the timestamp format affect the formatting of individual duration objects?

andrewkcorcoran commented 3 years ago

Yea pretty much. I had thought the outputs could be 00:01:00 00:00:02 00:00:00.003 (hence the talk about formatting strings) but perhaps that's overcomplicating things?

On Fri, 16 Jul 2021, 11:17 am Thaler Benedek, @.***> wrote:

Do you mean something like this:

BINLOG_INFO("{} {} {}", std::chrono::minutes{1}, std::chrono::seconds{2}, std::chrono::milliseconds{3}); // Outputs: 1m 2s 3ms

For hours, minutes, seconds, milliseconds, microseconds, nanoseconds.

It could reuse the time portion of the datetime format string for logging purposes by default and possibly add an additional override just for times/durations.

I do not get this part: how would the timestamp format affect the formatting of individual duration objects?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/morganstanley/binlog/issues/129#issuecomment-881232608, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL5LS3AHESFOKTWLMH735WLTX7MJBANCNFSM5AM3OBKA .

erenon commented 3 years ago

Well, if the format string is "%H:%M:%S", representing milliseconds in a useful way would be challenging. Let's aim for a suffix notation for now. (e.g: 123ms)

andrewkcorcoran commented 3 years ago

To be generic and forward looking (e.g. for std::chrono::years etc. maybe we should look at serializing std::chrono::duration<Rep, Period> instead of individual serializers forstd::chrono::seconds/milliseconds etc. We could add overloads in the pretty printer to handle known std::ratios to print a suffix such as s/ms/ns etc. and have a catch all which simply prints the std::ratio values for unspecified overloads (perhaps using the names already defined in https://en.cppreference.com/w/cpp/header/ratio e.g. picoseconds, gigaseconds etc.)

On Fri, 16 Jul 2021, 4:56 pm Thaler Benedek, @.***> wrote:

Well, if the format string is "%H:%M:%S", representing milliseconds in a useful way would be challenging. Let's aim for a suffix notation for now. (e.g: 123ms)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/morganstanley/binlog/issues/129#issuecomment-881426711, or unsubscribe https://github.com/notifications/unsubscribe-auth/AL5LS3BPISIABLRWSYYKQELTYAT6BANCNFSM5AM3OBKA .

erenon commented 3 years ago

@andrewkcorcoran see https://github.com/morganstanley/binlog/pull/135