Skip to content

Conversation

@maxicarlos08
Copy link
Owner

There currently are some limitations by the tracing framework, most notably tokio-rs/tracing#372 which doesn't allow the creation of events that have a level or target set at runtime.

@maxicarlos08 maxicarlos08 requested a review from RReverser July 10, 2023 23:17
@maxicarlos08 maxicarlos08 linked an issue Jul 10, 2023 that may be closed by this pull request

pub fn setup() {
tracing_subscriber::registry()
.with(EnvFilter::from_default_env())
Copy link
Collaborator

@RReverser RReverser Jul 10, 2023

Choose a reason for hiding this comment

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

I believe just tracing_subscriber::fmt::init() is enough to do all of this. So you don't even need a separate module.

https://docs.rs/tracing-subscriber/latest/tracing_subscriber/fmt/index.html#filtering-events-with-environment-variables:

The default subscriber installed by init enables you to filter events at runtime using environment variables (using the EnvFilter).

At least that's how I always use it.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Does the same apply to src/lib.rs:45 ?

@RReverser
Copy link
Collaborator

One more thing that I'd like to see - which was actually initial motivation - is instrumentation of public API functions too, so that I could just do ops like

let camera_file = fs.download(folder, filename).await?;

fs.delete_file(folder, filename).await?;

without having to attach manual debug spans to each of those calls. As mentioned in the issue, it's probably going to be a bit tricky on tasks due to custom sync&async struct, but can be done by e.g. passing function name to Task::new.

If you want, I can do this myself at some point.

@maxicarlos08
Copy link
Owner Author

If you want, I can do this myself at some point.

I am not yet very familiar with the instrumentation part of tracing (sadly), if you could do this that would be very nice 🙏

@RReverser
Copy link
Collaborator

I am not yet very familiar with the instrumentation part of tracing (sadly), if you could do this that would be very nice 🙏

Cool. I'm actually a bit busy this week and travelling the next 2, so it will take some time for me to get back to this, but it's not very urgent I suppose - happy to do it once I'm back :)

@maxicarlos08
Copy link
Owner Author

Have fun on your holidays! I will also see if I can figure out this myself 😉

HOOK_LOG_FUNCTION.call_once(|| unsafe {
libgphoto2_sys::gp_log_add_func(max_log_level, Some(log_function), std::ptr::null_mut());
libgphoto2_sys::gp_log_add_func(
GPLogLevel::GP_LOG_DEBUG,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should probably use https://docs.rs/tracing/latest/tracing/level_filters/constant.STATIC_MAX_LEVEL.html to match previous behaviour / optimisation that used log::STATIC_MAX_LEVEL.

unsafe extern "C" fn log_function(
level: libgphoto2_sys::GPLogLevel,
domain: *const std::os::raw::c_char,
_domain: *const std::os::raw::c_char,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wait dynamic domain also can't be passed to tracing? 😢

Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess you could at least pass it as extra variable using tracing's structured logging like

tracing::event!(target: "gphoto2", Level::TRACE, scope, message)

@RReverser
Copy link
Collaborator

tokio-rs/tracing#372 (comment) brings an interesting point: tracing crate translates log events just fine, so to support dynamic levels and targets it might be easier to keep log for log messages, but use tracing::instrument and other tools for API spans. In theory that should give best of both worlds.

That would amount to reverting most of this PR except the env-logger -> tracing-subscriber transition though, sorry :(

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.

Switch from log to tracing?

3 participants