Skip to content

Commit

Permalink
Add custom logger API.
Browse files Browse the repository at this point in the history
  • Loading branch information
wmedrano committed Sep 13, 2024
1 parent b25d6d4 commit fc18d00
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 13 deletions.
34 changes: 34 additions & 0 deletions docs/logging.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,37 @@ If the `log` feature is not enabled, then `jack` will log info messages to
```rust
jack::set_logger(jack::LoggerType::Stdio);
```

## Custom

`jack::LoggerType::Custom` can be used to set a custom logger. Here is
stdout/stderr implemented as a custom logger:

```rust
fn main() {
jack::set_logger(jack::LoggerType::Custom{info: stdout_handler, error: stderr_handler});
...
}

unsafe extern "C" fn stdout_handler(msg: *const libc::c_char) {
let res = std::panic::catch_unwind(|| match std::ffi::CStr::from_ptr(msg).to_str() {
Ok(msg) => println!("{}", msg),
Err(err) => println!("failed to log to JACK info: {:?}", err),
});
if let Err(err) = res {
eprintln!("{err:?}");
std::mem::forget(err); // Prevent from rethrowing panic.
}
}

unsafe extern "C" fn stderr_handler(msg: *const libc::c_char) {
let res = std::panic::catch_unwind(|| match std::ffi::CStr::from_ptr(msg).to_str() {
Ok(msg) => eprintln!("{}", msg),
Err(err) => eprintln!("failed to log to JACK error: {:?}", err),
});
if let Err(err) = res {
eprintln!("{err:?}");
std::mem::forget(err); // Prevent from rethrowing panic.
}
}
```
36 changes: 23 additions & 13 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ pub enum LoggerType {
/// Use the info! and error! macro from the [log crate](https://crates.io/crates/log).
#[cfg(feature = "log")]
Log,
/// Use custom functions to handle logging.
Custom {
/// The function to call for info messages. Must not panic.
info: unsafe extern "C" fn(*const ::libc::c_char),
/// The function to call for error messages. Must not panic.
error: unsafe extern "C" fn(*const ::libc::c_char),
},
}

impl Default for LoggerType {
Expand All @@ -45,20 +52,23 @@ pub fn set_logger(logger: LoggerType) {
}

fn set_logger_impl(logger: LoggerType) {
match logger {
LoggerType::None => unsafe {
jack_sys::jack_set_error_function(Some(silent_handler));
jack_sys::jack_set_info_function(Some(silent_handler));
},
LoggerType::Stdio => unsafe {
jack_sys::jack_set_error_function(Some(stderr_handler));
jack_sys::jack_set_info_function(Some(stdout_handler));
},
let info_fn = match logger {
LoggerType::None => silent_handler,
LoggerType::Stdio => stdout_handler,
#[cfg(feature = "log")]
LoggerType::Log => unsafe {
jack_sys::jack_set_error_function(Some(error_handler));
jack_sys::jack_set_info_function(Some(info_handler));
},
LoggerType::Log => info_handler,
LoggerType::Custom { info, .. } => info,
};
let error_fn = match logger {
LoggerType::None => silent_handler,
LoggerType::Stdio => stderr_handler,
#[cfg(feature = "log")]
LoggerType::Log => error_handler,
LoggerType::Custom { error, .. } => error,
};
unsafe {
jack_sys::jack_set_error_function(Some(error_fn));
jack_sys::jack_set_info_function(Some(info_fn));
}
}

Expand Down

0 comments on commit fc18d00

Please sign in to comment.