libcryptsetup_rs/
log.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use std::{
6    os::raw::{c_char, c_int, c_void},
7    ptr,
8};
9
10use crate::{consts::vals::CryptLogLevel, err::LibcryptErr};
11
12type LoggingCallback = unsafe extern "C" fn(level: c_int, msg: *const c_char, usrptr: *mut c_void);
13
14/// Generate a log entry
15pub fn log(level: CryptLogLevel, msg: &str) -> Result<(), LibcryptErr> {
16    let msg_cstring = to_cstring!(msg)?;
17    mutex!(libcryptsetup_rs_sys::crypt_log(
18        ptr::null_mut(),
19        level as c_int,
20        msg_cstring.as_ptr(),
21    ));
22    Ok(())
23}
24
25/// Set the callback to be executed on logging events
26pub fn set_log_callback<T>(callback: Option<LoggingCallback>, usrdata: Option<&mut T>) {
27    mutex!(libcryptsetup_rs_sys::crypt_set_log_callback(
28        ptr::null_mut(),
29        callback,
30        match usrdata {
31            Some(ud) => (ud as *mut T).cast::<c_void>(),
32            None => ptr::null_mut(),
33        },
34    ))
35}