Skip to content
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

Add a log callback that takes a formatted message. #802

Closed

Conversation

mutexlox-signal
Copy link
Contributor

In some contexts (e.g. cubeb-rs), it is not possible to pass a method that formats a string (for instance, because c_variadic is not stable in rust, and because it only supports compile-time constant format strings).

So, add a callback method that takes a const char* that has already been printf-style formatted.

In some contexts (e.g. cubeb-rs), it is not possible to pass a method
that formats a string (for instance, because `c_variadic` is not stable
in rust, and because it only supports compile-time constant format
strings).

So, add a callback method that takes a `const char*` that has already
been printf-style formatted.
@mutexlox-signal mutexlox-signal marked this pull request as ready for review October 11, 2024 15:26
@mutexlox-signal
Copy link
Contributor Author

cc @padenot :)

mutexlox-signal added a commit to mutexlox-signal/cubeb-rs that referenced this pull request Oct 15, 2024
@mutexlox-signal
Copy link
Contributor Author

gentle ping on this :)

@padenot
Copy link
Collaborator

padenot commented Oct 16, 2024

I don't remember exactly how we set things up, aren't the macros exposed on the rust side as well? There's a bit of custom code for rust.

@mutexlox-signal
Copy link
Contributor Author

This is for capturing logs and saving them to output, rather than adding to logs. I do not see anything in cubeb-rs right now that turns on logs (https://github.com/search?q=repo%3Amozilla%2Fcubeb-rs%20cubeb_set_log_callback&type=code or https://docs.rs/cubeb/latest/cubeb/), but if I'm missing something pointers would be appreciated!

@mutexlox-signal
Copy link
Contributor Author

mutexlox-signal commented Oct 16, 2024

The problem I'm trying to solve is that https://docs.rs/cubeb/latest/cubeb/ffi/fn.cubeb_set_log_callback.html is not really usable for the reasons mentioned in the original comment: the lack of c_variadic, the requirement for compile-time constant format strings, plus also the fact that rust does not use C format string syntax.

For additional context, see the proposed usage of this in cubeb-rs here: https://github.com/mozilla/cubeb-rs/pull/98/files

@ChunMinChang
Copy link
Member

If you'd like to have a log with variadic parameters, something like this should work: mozilla/cubeb-coreaudio-rs@1524600

@mutexlox-signal
Copy link
Contributor Author

I don't believe that that does what I want, as my ultimate goal is to be able to write the logs from cubeb to the same file as those for the rest of my library, using rust's logging macros, rather than just to write them to stdout (or even any specific fd). If I just wrote the logs to an fd, they would be out-of-order with the messages that go through rust's logging macros.

If it were possible to use something like mozilla/cubeb-coreaudio-rs@1524600 to generate a char* that I could then pass to rust's logging macros, that would work, but I do not believe that's possible.

@mutexlox-signal
Copy link
Contributor Author

though, I suppose i could write an extern C function in rust that takes a char* and call that from the callback... hmm, let me try that.

@mutexlox-signal
Copy link
Contributor Author

Thanks for the pointer, I was able to make this work in mozilla/cubeb-rs#98 without any changes to cubeb.

@mutexlox-signal mutexlox-signal deleted the mutexlox/cubeb-log branch October 17, 2024 17:32
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.

3 participants