Skip to content

Conversation

@tarasko
Copy link

@tarasko tarasko commented Jul 23, 2024

Closes #184

Since libc strftime doesn't support %f specifier the implementation works it around by

  1. first, creating a temporary format string for strftime where %f is replaced with actual microseconds.
  2. passing this temporary format string to strftime

The first step is optional, it will be only invoked if %f is detected by the Formatter constructor

import picologging as logging

logging.basicConfig(format="%(levelname)s - %(asctime)s - %(name)s - %(module)s - %(message)s", datefmt="%F %T.%f")
logger = logging.getLogger()
logger.setLevel(logging.INFO)

if __name__ == '__main__':
    for i in range(5):
        logger.info("Hello world! %d", i)
INFO - 2024-07-23 04:15:56.170199 - root - <unknown> - Hello world! 0
INFO - 2024-07-23 04:15:56.170242 - root - <unknown> - Hello world! 1
INFO - 2024-07-23 04:15:56.170255 - root - <unknown> - Hello world! 2
INFO - 2024-07-23 04:15:56.170265 - root - <unknown> - Hello world! 3
INFO - 2024-07-23 04:15:56.170274 - root - <unknown> - Hello world! 4

@tarasko
Copy link
Author

tarasko commented Jul 23, 2024

@microsoft-github-policy-service agree

@microsoft-github-policy-service agree

@tarasko tarasko changed the title Feature/microseconds formatter support Add support for %f (microseconds) to datefmt Jul 23, 2024
@tarasko tarasko changed the title Add support for %f (microseconds) to datefmt Add support for %f (format microseconds) to datefmt Jul 23, 2024
@tonybaloney tonybaloney self-requested a review July 26, 2024 20:22
@tarasko tarasko force-pushed the feature/microseconds_formatter_support branch from 54f2817 to 87c048a Compare September 12, 2024 10:45
@tarasko
Copy link
Author

tarasko commented Sep 12, 2024

Hi @tonybaloney. Would you be able to take a look at my PRs please?
Do you think you will a make new release any time soon?

@tonybaloney
Copy link
Collaborator

The other PR is merged so I think this one can be closed?

@tarasko
Copy link
Author

tarasko commented Sep 14, 2024

The other PR is merged so I think this one can be closed?

Sorry for confusion.

The other PR was just fixing default asctime formatting.
This one adds support for %f specifier.

I see some merge conflicts, I'll resolve them and add tests.
For now I'm converting this PR to draft. I'll restore it as soon as it's ready

@tarasko tarasko marked this pull request as draft September 14, 2024 08:25
@tarasko tarasko marked this pull request as ready for review September 14, 2024 09:31
@tarasko
Copy link
Author

tarasko commented Sep 14, 2024

@tonybaloney
It is ready

@tonybaloney
Copy link
Collaborator

@tonybaloney

It is ready

Ah I understand now. I was looking at 3.13 support as well in another PR since it seems that the API for getting times changed and the old function for getting microseconds has been removed. So I'll consider that in this PR

@tarasko
Copy link
Author

tarasko commented Sep 15, 2024

@tonybaloney
It is ready

Ah I understand now. I was looking at 3.13 support as well in another PR since it seems that the API for getting times changed and the old function for getting microseconds has been removed. So I'll consider that in this PR

Well this PR doesn't touch PyTime, but it seems like we just need to call PyTime_Time instead of _PyTime_GetSystemClockWithInfo for python 3.13.
Something like this:

#if PY_VERSION_HEX < 0x03130000
#define py_time _PyTime_t
int py_time_get_time(py_time* t) { return _PyTime_GetSystemClockWithInfo(&t, NULL); } 
#else
#define py_time PyTime_t
int py_time_get_time(py_time* t) { return PyTime_Time(&t); }
#endif

@tonybaloney
Copy link
Collaborator

@tonybaloney

It is ready

Ah I understand now. I was looking at 3.13 support as well in another PR since it seems that the API for getting times changed and the old function for getting microseconds has been removed. So I'll consider that in this PR

Well this PR doesn't touch PyTime, but it seems like we just need to call PyTime_Time instead of _PyTime_GetSystemClockWithInfo for python 3.13.

Something like this:


#if PY_VERSION_HEX < 0x03130000

#define py_time _PyTime_t

int py_time_get_time(py_time* t) { return _PyTime_GetSystemClockWithInfo(&t, NULL); } 

#else

#define py_time PyTime_t

int py_time_get_time(py_time* t) { return PyTime_Time(&t); }

#endif

Yes that's in #212

the API that's missing is the one to fetch milliseconds.

I've set it to zero until I can find a solution

@tarasko
Copy link
Author

tarasko commented Sep 15, 2024

@tonybaloney

It is ready

Ah I understand now. I was looking at 3.13 support as well in another PR since it seems that the API for getting times changed and the old function for getting microseconds has been removed. So I'll consider that in this PR

Well this PR doesn't touch PyTime, but it seems like we just need to call PyTime_Time instead of _PyTime_GetSystemClockWithInfo for python 3.13.
Something like this:


#if PY_VERSION_HEX < 0x03130000

#define py_time _PyTime_t

int py_time_get_time(py_time* t) { return _PyTime_GetSystemClockWithInfo(&t, NULL); } 

#else

#define py_time PyTime_t

int py_time_get_time(py_time* t) { return PyTime_Time(&t); }

#endif

Yes that's in #212

the API that's missing is the one to fetch milliseconds.

I've set it to zero until I can find a solution

Ah I see.
I think PyTime_t is just nanoseconds. Perhaps this will just work:

long msec = std::ceil(static_cast<double>(t % 1000000000) / 1000000);

@Revolution1
Copy link

any update?

@DenchPokepon
Copy link

Looking forward to using your library with this feature :)

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.

Python code crashes if I use %f in datefmt

4 participants