Add {short_file_name} as log format option#541
Add {short_file_name} as log format option#541tonynajjar wants to merge 1 commit intoros2:rollingfrom
Conversation
Signed-off-by: Tony Najjar <tony.najjar@dexory.com>
|
I'd love to have it backported to Kilted when merged, it doesn't break ABI |
|
@tonynajjar this slaps, thanks so much. Let me see what I can cook up when I make the social media rounds tomorrow. We haven't had a "what's been merged recently post" perhaps we're past due. |
fujitatomoya
left a comment
There was a problem hiding this comment.
@tonynajjar thanks for the PR.
i have a question about this approach. what is the major background and requirement for this PR?
assuming that full absolute paths in log output can be noisy and hurt readability.
however, I'm concerned that showing only the basename isn't quite enough. in a typical ROS 2 workspace, many packages can have identically named files (main.cpp, node.cpp, etc.), so {short_file_name} alone would make it hard to tell where a log line is actually coming from.
would it be possible to instead show the path relative to the package root? for example, my_package/src/my_node.cpp instead of just my_node.cpp. that would cut out the workspace-specific noise while still keeping enough context to identify the package. something like {relative_file_name} or {package_file_name} might be a better middle ground.
curious to hear your thoughts — what was the use case that motivated going with just the basename?
|
@fujitatomoya you're right, the motivation is to reduce the unnecessary noise and cluttering from the full path (in our case it's quite long)
I looked into this and although I first though you have a very good point, it turns out that Take this example from Nav2: with I get although as you can see here, the Additionally when I look at our full stack of 100+ nodes, I never see |
|
thanks for the explanation! i was just curious about the background for this additional token. but i am not sure how much this is useful against full file path including performance overhead. l would like to see more feedback on this 👂 |
For us here is a before and after: With one line it might not seem like a big deal but with thousands it compounds pretty badly and makes the logs unreadable |
|
that is good example 👍 |
fujitatomoya
left a comment
There was a problem hiding this comment.
overall lgtm, i got a few comments.
@tonynajjar i can see @kscottz 's approval including doc support, so let's proceed !!! thanks for waiting 🙇
| {.token = "message", .handler = expand_message}, | ||
| {.token = "function_name", .handler = expand_function_name}, | ||
| {.token = "file_name", .handler = expand_file_name}, | ||
| {.token = "short_file_name", .handler = expand_short_file_name}, |
There was a problem hiding this comment.
i would register "short_file_name" before "file_name", because if it does prefix matching with the shorter token's length, "short_file_name" could match "file_name" first. but i do not think this is a real issue here, because it uses strcmp to complete match check.
| (void)end_offset; | ||
|
|
||
| if (logging_input->location) { | ||
| const char * file_name = logging_input->location->file_name; |
There was a problem hiding this comment.
should we check if this is not NULL to avoid undefined behavior?
| } | ||
| #ifdef _WIN32 | ||
| const char * last_backslash = strrchr(file_name, '\\'); | ||
| if (last_backslash != NULL && last_backslash > last_sep) { |
There was a problem hiding this comment.
last_sep could be null?
| if (last_backslash != NULL && last_backslash > last_sep) { | |
| if (last_backslash != NULL && (last_sep == NULL || last_backslash > last_sep)) { |
Description
Add a new
{short_file_name}format token to the rcutils logging system that outputs only the basename of the source file (e.g.,my_node.cpp) instead of the full path (e.g.,/opt/ros_ws/src/my_package/src/my_node.cpp).The existing
{file_name}token continues to output the full path as before. The new token usesstrrchrto find the last path separator (/on POSIX,\on Windows) and returns everything after it.Changed files
src/logging.c— Addedexpand_short_file_name()handler and registered the{short_file_name}token in thetokens[]arrayinclude/rcutils/logging.h— Addedshort_file_nameto the documented list of available format tokenstest/test_logging_console_output_handler.cpp— Added unit tests verifying basename extraction from full paths and passthrough for bare filenamestest/test_logging_output_format.py— Added integration test launching with{short_file_name}:{line_number}format and validating outputtest/test_logging_output_format_short_file_name.txt— Expected output fixture for the integration testIs this user-facing behavior change?
Yes. Users can now use
{short_file_name}in theRCUTILS_CONSOLE_OUTPUT_FORMATenvironment variable to display only the source file basename in log output, reducing log line length and improving readability.Did you use Generative AI?
Yes — GitHub Copilot (Claude Opus 4.6)
Additional Information
/) and Windows (\) path separators via#ifdef _WIN32. Tested on Ubuntu Linux but I don't have a way to test on Windows.rcutils_log_location_tstruct — the basename is extracted at format-expansion time