-
Couldn't load subscription status.
- Fork 28
Timestamped logging macro for elapsed physical time #546
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
Open
adi-65-ray
wants to merge
8
commits into
lf-lang:main
Choose a base branch
from
adi-65-ray:logging-timestamped
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4f51612
Timestamped logging macro
adi-65-ray 2e60dc9
Added unit test
adi-65-ray 7aa0ff7
unit test for logging clang-format fix
adi-65-ray 995e05e
logging_macros format fix
adi-65-ray 9564112
Update test/general/logging_test.c
hokeun e5ccae5
Merge branch 'main' into logging-timestamped
hokeun eb05623
Remove inclusion of tag.h
hokeun 578111d
Enable LOG_LEVEL_DEBUG for log UT
adi-65-ray File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| #include "logging_macros.h" | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <assert.h> | ||
| #include <unistd.h> | ||
| #include <regex.h> | ||
|
|
||
| #define TIMEED_DEBUG_CHAR_LEN 30 | ||
| #define SOME_EXTRA_SPACE 10 | ||
| /** | ||
| * @brief unit for LF_PRINT_DEBUG macro | ||
| * must be in LOG_LEVEL LOG_LEVEL_DEBUG | ||
| */ | ||
| void test_logging_macro(const char* expected, int st_len) { | ||
|
|
||
| FILE* tmp = tmpfile(); // auto-deletes when closed | ||
| char* buffer; | ||
| char pattern[256]; | ||
| regex_t re; | ||
| int result; | ||
|
|
||
| // strlen("DEBUG: ") + null character + new line character + some extra space | ||
| int buffer_size = st_len + TIMEED_DEBUG_CHAR_LEN + SOME_EXTRA_SPACE; | ||
|
|
||
| if (!tmp) { | ||
| perror("tmpfile"); | ||
| exit(1); | ||
| } | ||
|
|
||
| // Redirect stdout -> tmp | ||
| fflush(stdout); | ||
| int fd = fileno(tmp); | ||
| if (fd == -1 || dup2(fd, STDOUT_FILENO) == -1) { | ||
| perror("redirect stdout"); | ||
| exit(1); | ||
| } | ||
|
|
||
| // Call code under test | ||
| LF_PRINT_DEBUG("%s", expected); | ||
|
|
||
| fflush(stdout); // flush so data goes into tmp | ||
| rewind(tmp); // reset read position | ||
|
|
||
| // Read back | ||
| buffer = (char*)malloc(buffer_size); | ||
| size_t n = fread(buffer, 1, buffer_size - 1, tmp); | ||
| buffer[n] = '\0'; | ||
|
|
||
| // Regex to check format: DEBUG: [number]expected\n | ||
| snprintf(pattern, sizeof(pattern), "^DEBUG: \\[-?[0-9]+\\]%s\\n?$", expected); | ||
|
|
||
| if (regcomp(&re, pattern, REG_EXTENDED | REG_NEWLINE) != 0) { | ||
| perror("regcomp"); | ||
| exit(1); | ||
| } | ||
|
|
||
| result = regexec(&re, buffer, 0, NULL, 0); | ||
| regfree(&re); | ||
|
|
||
| assert(result == 0); // match succeeded | ||
|
|
||
| fclose(tmp); // deletes the file | ||
| free(buffer); | ||
| } | ||
|
|
||
| int main() { | ||
| char* str_test = "Hello World"; | ||
| test_logging_macro(str_test, strlen(str_test)); | ||
| return 0; | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.