Skip to content

Add option with_short_file_names #13

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
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

petm-dec
Copy link

@petm-dec petm-dec commented Mar 8, 2023

Hello, we use very long paths.
Our other glog libraries (go and c++) log only the file name and not the full path.
I'd like to have the same for our rust logs, so I made this small addition.

src/format.rs Outdated
Comment on lines 221 to 230
.map(|f| {
if self.with_short_file_names {
f.rsplit_once('/')
.map(|(_, file_name)| file_name)
.unwrap_or_default()
} else {
f
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not opposed to this change, but I'd prefer an approach like this instead:

Suggested change
.map(|f| {
if self.with_short_file_names {
f.rsplit_once('/')
.map(|(_, file_name)| file_name)
.unwrap_or_default()
} else {
f
}
.map(|f| {
if self.with_short_file_names {
let f = Path::new(f);
f.file_name().map(OsStr::to_str);
} else {
f
}

I haven't compiled this, so it might fail, but I'd rather be operating in terms of Paths instead of string manipulation.

src/lib.rs Outdated
@@ -185,6 +187,13 @@ impl<T> Glog<T> {
}
}

pub fn with_short_file_names(self, with_short_file_names: bool) -> Glog<T> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that this behavior is meant to match glog's, but I can see "short" being somewhat ambiguous (e.g., what qualifies as "short"? A single directory preceding it?).

How would you feel about renaming this to with_trimmed_directory and maybe adding a seperate builder option that exposes Path::strip_prefix, allowing for more fine-grained rendering of paths?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'll use Path and rename.

Not sure about using Path::strip_prefix. Could be useful, though with the module shown (with_target(true)) we would never use it. Either the full path or just the filename, I doubt that stripping away just a part makes much sense.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Rust there's a very high likelihood that the source will be called lib.rs (or maybe mod.rs) and even with one directory element you'll get src/lib.rs. Are you sure that this change will actually give you useful logs?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In combination with with_target(true), otherwise not.

This option strips the directory from the file path, like some
other glog libraries do by default.
@petm-dec
Copy link
Author

petm-dec commented Mar 8, 2023

I renamed the option and used Path to strip the directory.
I think an option to only strip a prefix could be added later, not sure if that would be useful for anyone (not to me I think).
Thanks for looking at this.

src/format.rs Outdated
Comment on lines 226 to 227
.unwrap_or_default()
.unwrap_or_default()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to use the original full filename if something goes wrong here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is better. I added a fixup that does this.

plm and others added 3 commits March 9, 2023 22:28
This option strips a prefix from the target file path.
If the prefix doesn't match, the full path is logged.
@petm-dec
Copy link
Author

I also added with_strip_prefix option as suggested.

I added a commit on a test branch, with a little example that shows the resulting logs of various cases:
petm-dec@adc5835

@davidbarsky
Copy link
Owner

Sorry, I should've mentioned this last week when I first saw this PR, but I only thought of this approach last night. How would you feel about a trait called FormatFile (inspired by tracing-subscriber's FormatTime, with the self corresponding to the struct is required to contain the file name, line number, and module path. You'd then be able to define a formatter for how files/targets/modules are rendered without needing to figuring how the setting should be named.

If you're interested, I can make a PR with the above?

@petm-dec
Copy link
Author

petm-dec commented Mar 15, 2023

If you're interested, I can make a PR with the above?

Sure, you modify this as you wish.
FormatFile seems like a nice and flexible approach.

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.

3 participants