Function libbpf_rs::set_print

source ·
pub fn set_print(
    callback: Option<(PrintLevel, PrintCallback)>,
) -> Option<(PrintLevel, PrintCallback)>
Expand description

Set a callback to receive log messages from libbpf, instead of printing them to stderr.

§Arguments

  • callback - Either a tuple (min_level, function) where min_level is the lowest priority log message to handle, or None to disable all printing.

This overrides (and is overridden by) ObjectBuilder::debug

§Examples

To pass all messages to the log crate:

use libbpf_rs::{PrintLevel, set_print};

fn print_to_log(level: PrintLevel, msg: String) {
    match level {
        PrintLevel::Debug => log::debug!("{}", msg),
        PrintLevel::Info => log::info!("{}", msg),
        PrintLevel::Warn => log::warn!("{}", msg),
    }
}

set_print(Some((PrintLevel::Debug, print_to_log)));

To disable printing completely:

use libbpf_rs::set_print;
set_print(None);

To temporarliy suppress output:

use libbpf_rs::set_print;

let prev = set_print(None);
// do things quietly
set_print(prev);