Skip to content

LogLocation lifetimes. #94

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

Closed
dpc opened this issue Aug 29, 2016 · 7 comments
Closed

LogLocation lifetimes. #94

dpc opened this issue Aug 29, 2016 · 7 comments

Comments

@dpc
Copy link

dpc commented Aug 29, 2016

Could LogLocation::module_path() and LogLocation::file return 'static str, since they own them as &'static str? I am actually hitting this, and I need to use hidden variables directly instead.

@sfackler
Copy link
Member

I think extending these lifetimes would be fine - thoughts @alexcrichton?

@alexcrichton
Copy link
Member

It may make it harder to dynamically generate these records in the future, but I'm not sure if there's even a case for that?

@dpc
Copy link
Author

dpc commented Aug 29, 2016

LogLocation expects &'static str to be created, so... either LogLocation becomes LogLocation<'a> and takes &'a str, or LogLocation::module_path() can always return &'static str.

@dpc
Copy link
Author

dpc commented Sep 11, 2016

Actually. Is there any reason why LogLocation couldn't be LogLocation<'a> and hold &'a str only? Even though obviously function! etc. will always be 'static I can already see that accepting &'a would give more flexibility.

The drawback is, that logging backend would not be able to save the &'static str directly (but it can clone if has to). Considering that accessors functions return &'a str noone is depending on it.

@stephaneyfx
Copy link

I would like to suggest a different idea. As mentioned in this other post, an important use case is integrating a foreign logging system, so that messages from this system (e.g. a C library) can be forwarded to the Rust logger. The location of such foreign log messages is not necessarily a static string, or may not even be in UTF-8, requiring a dynamic conversion. To accommodate for this, wouldn't it be better to define Location as follows?

pub struct Location {
    pub __module_path: Cow<'static, str>,
    pub __file: Cow<'static, str>,
    pub __line: u32,
}

This avoids the cost of dynamic allocation for logging originating from Rust code while allowing locations not defined statically (e.g. because they come from a C library). One potential drawback is that Location is not Copy anymore.

Of course the use case of integrating a foreign logging system depends on #95 to define a LogBuilder or some other API to officially allow user code to create Location instances and pass them to the logger.

@whitequark
Copy link
Member

This would be great but unfortunately there is no Cow with #![no_std], unless you opt into liballoc.

@stephaneyfx
Copy link

Thank you. I opened #206 to track the integration with foreign loggers and the no_std issue is mentioned in #206 (comment).

sfackler added a commit to sfackler/log that referenced this issue Sep 26, 2017
EFanZh pushed a commit to EFanZh/log that referenced this issue Jul 23, 2023
See this issue: cargo-bins/cargo-quickinstall#27

Quick Install is a hosted repo of built crates, essentially. The approach I've taken here is
a list of strategies:

1. First, we check the crate meta or default and build the URL to the repo. Once we have
   that, we perform a `HEAD` request to the URL to see if it's available.
2. If it's not, we build the URL to the quickinstall repo, and perform a `HEAD` to there.

As soon as we've got a hit, we use that. I've built it so it's extensible with more strategies.
This could be useful for rust-lang#4.

This also adds a prompt before downloading from third-party sources, and logs a short
name for a source, which is easier to glance than a full URL, and includes a quick refactor
of the install/link machinery.
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

No branches or pull requests

5 participants