log — functions and constants
Taking the SDK's own log lines into your program's log.
The SDK logs what the camera path is doing: calibration fetches, flag cycles, USB re-enumeration, NUC, reconnects. When a customer sends a diagnostics bundle those lines are usually the only record of what happened, and without a handler installed they go nowhere a Rust application can see.
The handler is process-wide. There is one, setting a second replaces the first, and lines are not attributed to a camera.
Threading, which is where this callback differs from every other
Every other callback in this crate arrives on an SDK thread, never on the thread that registered it. A log handler is the exception, because logging happens wherever the SDK happens to be running:
- Any thread, including yours. A call you make that logs
before it returns delivers the message on your thread, inside
your call. That is why the closure must be
Send + Synchere and onlySendelsewhere. - Possibly two at once. The SDK holds no lock across the
handler on purpose: serialising delivery would put every
logging thread behind the slowest handler, and one that blocked
would stall the frame path.
Fnrather thanFnMutfor the same reason: there is no&mutto hand out. - Promptly. The thread that logged waits for it. Queue the line; do not write to a socket from inside it.
- Nothing may escape. A panic is caught at the boundary and reported on stderr rather than unwinding into C, which would be undefined behaviour.
- Do not call
set_handlerorclear_handlerfrom inside the handler. Both wait for it to return, so it would wait for itself. The SDK refuses rather than deadlocking, which arrives here ascrate::Status::InvalidArgument. - A nested line is dropped. If the handler logs (directly, or by calling an SDK function that logs) that message is not delivered back into it.
Examples
# fn main() -> optris_sdk::Result<()> {
optris_sdk::log::set_handler(
|level, message| eprintln!("[optris {level:?}] {message}"),
optris_sdk::LogLevel::Info,
)?;
// ... work ...
optris_sdk::log::clear_handler()?;
# Ok(())
# }
pub fn clear_handler() -> Result<()>
Stop taking log lines.
When this returns the previous handler has finished and cannot be called again, so whatever it captured is safe to release. That is what makes freeing the closure here sound rather than a guess.
Clearing when nothing is installed succeeds and does nothing.
Errors
As set_handler.
pub fn has_handler() -> Result<bool>
Whether a log handler is installed.
Asks the SDK rather than reporting what this module last did, so it is also true of a handler installed by another binding or by C code in the same process: the registration is one per process, not one per language.
Errors
Returns whatever the SDK reports if it cannot answer.
pub fn set_handler<F>(handler: F, minimum: LogLevel) -> Result<()>
Send the SDK's log lines to handler(level, message).
message has no level prefix, no timestamp and no trailing
newline, so a caller formats it the way its own log does.
minimum drops anything less severe before the handler is called,
in the SDK, so a LogLevel::Warning floor costs nothing per
debug line.
Read this module's documentation first: the threading rules for this one callback are not the ones the rest of the crate has.
Errors
Returns crate::Status::InvalidArgument if called from inside
the handler: the SDK refuses rather than deadlocking.