Description
I want to suggest to add a function try_current (alternate name: get_current) to the task module with the following signature:
fn try_current() -> Option<Task>
Context:
For logging purposes, I name my tasks and then log the name via
task::current().name().unwrap_or("root")
Some logging is done outside the context of a task, hence the name "root".
However, since task::current panics outside the context of a task, I have to take care to disable logging for some crates like async_std itself, found during debugging. Here is an excerpt, using the logging framework "fern":
.level(log::LevelFilter::Warn)
.level_for(env!("CARGO_CRATE_NAME"), ARGS.log_level())
/*
The following modules cannot be logged,
otherwise task::current would panic.
Therefore, force them to be unlogged
*/
.level_for("async_io", log::LevelFilter::Off)
.level_for("async_std", log::LevelFilter::Off)
.level_for("polling", log::LevelFilter::Off)
This is fragile however, as future versions might add dependencies that would also panic when trying to log task::current()
.
If a try_current()
existed, I could rewrite the above as
task::try_current().map(|t| t.name()).unwrap_or("root")
thus avoiding any panic.
If this is ok, then I would like to work on this.