log4rs icon indicating copy to clipboard operation
log4rs copied to clipboard

Fixed window roller expand env var

Open Caleb-Hill opened this issue 2 years ago • 2 comments

This PR implements expanding env vars in file patterns in FixedWindowRoller as requested here https://github.com/estk/log4rs/issues/280 .

Caleb-Hill avatar Oct 18 '22 18:10 Caleb-Hill

@Caleb-Hill Horay! Thanks for opening a PR. I really like the change from taking a PathBuf to String, and after looking it over I think we can take this to 11. I suggest the following type for expand_env_vars.

    pub fn expand_env_vars<'str, Str>(s: Str) -> Cow<'str, str>
    where
        Str: Into<Cow<'str, str>>,
    {
        let orig_path: Cow<str> = s.into();
        let mut out_path = orig_path.clone();

The nice thing about this is that the ref gets passed all the way through so that in many cases there may be zero allocations. I know its a bit of a funny optimization, but...

There will be a few other resulting changes such as:

        let src_str = expand_env_vars(pattern.replace("{}", &i.to_string()));
        let src = Path::new(src_str.as_ref());

        let dst_str = expand_env_vars(pattern.replace("{}", &(i + 1).to_string()));
        let dst = Path::new(dst_str.as_ref());

I really appreciate your time, and thanks again!

estk avatar Oct 18 '22 19:10 estk

Additionally, as a note, i've been thinking about switching over to camino

estk avatar Oct 18 '22 21:10 estk

Updated it to use Cow, sorry I just got around to it. Somehow the github email ended up in my spam folder.

Caleb-Hill avatar Oct 24 '22 23:10 Caleb-Hill

Additionally, as a note, i've been thinking about switching over to camino

This seems like a good idea but probably a separate PR at least to my eyes.

Caleb-Hill avatar Oct 24 '22 23:10 Caleb-Hill

@estk Could you run the workflow and or re review my changes please?

Caleb-Hill avatar Oct 28 '22 14:10 Caleb-Hill

Fixed redundant clone lint issue on previous version.

Caleb-Hill avatar Oct 30 '22 17:10 Caleb-Hill