Adding sub seconds to logging - #133
Open
kanjoe24 wants to merge 1 commit into
Open
Conversation
Contributor
Author
|
test logs: |
There was a problem hiding this comment.
Pull request overview
This PR updates the C logging implementation to include sub-second (microsecond) precision timestamps in log output.
Changes:
- Increased the maximum timestamp buffer size (
UT_MAX_TIME_STRING) to accommodate fractional seconds. - Switched from
time()toclock_gettime(CLOCK_REALTIME, ...)and appended.%06ld(microseconds) to the formatted timestamp inUT_log()andUT_logPrefix().
Suppressed comments (1)
src/ut_log.c:143
clock_gettime()/localtime()/strftime()results aren’t checked here. On failure, the timestamp string can be incorrect orlocaltime()can return NULL and be dereferenced bystrftime(). Handle failures and fall back to a safe timestamp string.
clock_gettime(CLOCK_REALTIME, &ts);
if ( gLogInit == false )
{
/* If ut-core has never called to set the log to a fixed location then let's set it */
UT_log_setLogFilePath(UT_LOG_DEFAULT_PATH);
}
/* #FIXME : This will need rework, we shouldn't be opening logs constantly */
fp = fopen(gLogFileName, "a");
if (fp == NULL)
{
printf("\nUnable to open file for logging...");
return;
}
tmp = localtime(&ts.tv_sec);
strftime(time_now, sizeof(time_now), "%Y-%m-%d %H:%M:%S", tmp); // example: 2023-06-19 14:30:45
snprintf(time_now + strlen(time_now), sizeof(time_now) - strlen(time_now), ".%06ld", ts.tv_nsec / 1000); // example: 2023-06-19 14:30:45.123456
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
kanjoe24
force-pushed
the
feature/gh132-add-subsec-to-utlog
branch
from
August 7, 2026 15:31
b921542 to
06ba226
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
Suppressed comments (6)
src/ut_log.c:79
- clock_gettime(CLOCK_REALTIME, ...) return value is ignored. On failure, ts may contain uninitialized data, and subsequent localtime_r/formatting can crash or log garbage. Add a fallback when clock_gettime fails.
clock_gettime(CLOCK_REALTIME, &ts);
src/ut_log.c:97
- localtime_r()/strftime() results are not checked before using strlen(time_now) and appending subseconds. If localtime_r returns NULL or strftime returns 0, this can result in undefined behavior or a crash. Guard the formatting and ensure time_now is always NUL-terminated before appending.
tmp = localtime_r(&ts.tv_sec, &tm_buf);
strftime(time_now, sizeof(time_now), "%Y-%m-%d %H:%M:%S", tmp); // example: 2023-06-19 14:30:45
snprintf(time_now + strlen(time_now), sizeof(time_now) - strlen(time_now), ".%06ld", ts.tv_nsec / 1000); // example: 2023-06-19 14:30:45.123456
src/ut_log.c:128
- clock_gettime(CLOCK_REALTIME, ...) return value is ignored. On failure, ts may contain uninitialized data, and subsequent localtime_r/formatting can crash or log garbage. Add a fallback when clock_gettime fails.
clock_gettime(CLOCK_REALTIME, &ts);
src/ut_log.c:146
- localtime_r()/strftime() results are not checked before using strlen(time_now) and appending subseconds. If localtime_r returns NULL or strftime returns 0, this can result in undefined behavior or a crash. Guard the formatting and ensure time_now is always NUL-terminated before appending.
tmp = localtime_r(&ts.tv_sec, &tm_buf);
strftime(time_now, sizeof(time_now), "%Y-%m-%d %H:%M:%S", tmp); // example: 2023-06-19 14:30:45
snprintf(time_now + strlen(time_now), sizeof(time_now) - strlen(time_now), ".%06ld", ts.tv_nsec / 1000); // example: 2023-06-19 14:30:45.123456
src/ut_log.c:49
- localtime_r()/strftime() results are not checked. If localtime_r returns NULL (or strftime returns 0), this will pass an invalid tm pointer to strftime or leave time_now in an unspecified state, which can lead to crashes or malformed filenames.
This issue also appears in the following locations of the same file:
- line 95
- line 144
time(&now);
tmp = localtime_r(&now, &tm_buf);
strftime(time_now, sizeof(time_now), "%F_%H%M%S", tmp);
src/ut_log.c:97
- Sub-second timestamp formatting is newly introduced here, but there is no automated test asserting the log prefix contains "." followed by exactly 6 digits (and that the overall timestamp remains stable). Adding a small unit/integration test would prevent regressions in log parsing/format expectations.
strftime(time_now, sizeof(time_now), "%Y-%m-%d %H:%M:%S", tmp); // example: 2023-06-19 14:30:45
snprintf(time_now + strlen(time_now), sizeof(time_now) - strlen(time_now), ".%06ld", ts.tv_nsec / 1000); // example: 2023-06-19 14:30:45.123456
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull request overview
This PR updates the C logging implementation to include sub-second (microsecond) precision timestamps in log output.
Changes:
UT_MAX_TIME_STRING) to accommodate fractional seconds.time()toclock_gettime(CLOCK_REALTIME, ...)and appended.%06ld(microseconds) to the formatted timestamp inUT_log()andUT_logPrefix().localtime_r()instead oflocaltime()as it returns a pointer to a shared static buffer — not thread-safe.