I'm writing a small wrapper lib around log4rs, which aims to suit the needs of multiple, different binaries (e.g. foo, bar).
I wanted to create a rather simple function, like env_logger::init(); which will automatically know the name of the package / bin without the user having to explicitly specify it, if possible (and in turn, create the relevant log file such as foo.log, bar.log).
Unfortunately, I didn't figure out how env_logger/log knows the app name without explicit user intervention - I tried going for the source code and couldn't find what I was looking for. I know they do know about the app name because specifying RUST_LOG=foo=debug will only display debug info related to my app.
I thought this was done with env!("CARGO_PKG_NAME") or the likes, but I realized this value is evaluated in compile time for the library - not for the binary using it. I saw this reference to clap, which iiuc is trying to do the same thing, but I couldn't pull it off myself. Also, env_logger doesn't use a macro so that's slightly nicer.
I just tried using it, but this var is still being evaluated in compile-time, right?
Meaning - I can't compile the library as a stand-alone because it doesn't have this value. Unless I use option_env which turns out empty.
It doesn't seem to be re-evaluated or compiled for the binary using this library.
Maybe I failed to mention - but I'm trying to use this library as an external crate.
Am I doing something wrong?
Edit: I just realized my mistake - will publish the solution and explabation in a bit
Yes, 100%. I was assuming that you wanted this based on the question's title – also if you obtain the value once at compile-time, you should be able to use it as if it were a hard-coded string.
In fact, I don't think trying to do something like this at run-time would make sense – your binary's name doesn't change during runtime, nor does anything in any libraries it uses, since libraries are compiled into the binary. So I don't get what you want to shift from compile-time to runtime.