|
| 1 | +use crate::*; |
| 2 | +use lazy_static::lazy_static; |
| 3 | +use std::io::{self, Write}; |
| 4 | +use std::os::raw::c_char; |
| 5 | +use std::sync::Mutex; |
| 6 | + |
| 7 | +#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] |
| 8 | +#[repr(u32)] |
| 9 | +pub enum PrintLevel { |
| 10 | + Warn = libbpf_sys::LIBBPF_WARN, |
| 11 | + Info = libbpf_sys::LIBBPF_INFO, |
| 12 | + Debug = libbpf_sys::LIBBPF_DEBUG, |
| 13 | +} |
| 14 | + |
| 15 | +impl From<libbpf_sys::libbpf_print_level> for PrintLevel { |
| 16 | + fn from(level: libbpf_sys::libbpf_print_level) -> Self { |
| 17 | + match level { |
| 18 | + libbpf_sys::LIBBPF_WARN => Self::Warn, |
| 19 | + libbpf_sys::LIBBPF_INFO => Self::Info, |
| 20 | + libbpf_sys::LIBBPF_DEBUG => Self::Debug, |
| 21 | + // shouldn't happen, but anything unknown becomes the highest level |
| 22 | + _ => Self::Warn, |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +pub type PrintCallback = fn(PrintLevel, String); |
| 28 | + |
| 29 | +/// Mimic the default print functionality of libbpf. This way if the user calls `get_print` when no |
| 30 | +/// previous callback had been set, with the intention of restoring it, everything will behave as |
| 31 | +/// expected. |
| 32 | +fn default_callback(_lvl: PrintLevel, msg: String) { |
| 33 | + let _ = io::stderr().write(msg.as_bytes()); |
| 34 | +} |
| 35 | + |
| 36 | +// While we can't say that set_print is thread-safe, because we shouldn't assume that of |
| 37 | +// libbpf_set_print, we should still make sure that things are sane on the rust side of things. |
| 38 | +// Therefore we are using a lock to keep the log level and the callback in sync. |
| 39 | +// |
| 40 | +// We don't do anything that can panic with the lock held, so we'll unconditionally unwrap() when |
| 41 | +// locking the mutex. |
| 42 | +// |
| 43 | +// Note that default print behavior ignores debug messages. |
| 44 | +lazy_static! { |
| 45 | + static ref PRINT_CB: Mutex<Option<(PrintLevel, PrintCallback)>> = |
| 46 | + Mutex::new(Some((PrintLevel::Info, default_callback))); |
| 47 | +} |
| 48 | + |
| 49 | +extern "C" fn outer_print_cb( |
| 50 | + level: libbpf_sys::libbpf_print_level, |
| 51 | + fmtstr: *const c_char, |
| 52 | + va_list: *mut libbpf_sys::__va_list_tag, |
| 53 | +) -> i32 { |
| 54 | + let level = level.into(); |
| 55 | + if let Some((min_level, func)) = { *PRINT_CB.lock().unwrap() } { |
| 56 | + if level <= min_level { |
| 57 | + let msg = match unsafe { vsprintf::vsprintf(fmtstr, va_list) } { |
| 58 | + Ok(s) => s, |
| 59 | + Err(e) => format!("Failed to parse libbpf output: {}", e), |
| 60 | + }; |
| 61 | + func(level, msg); |
| 62 | + } |
| 63 | + } |
| 64 | + 1 // return value is ignored by libbpf |
| 65 | +} |
| 66 | + |
| 67 | +/// Set a callback to receive log messages from libbpf, instead of printing them to stderr. |
| 68 | +/// |
| 69 | +/// # Arguments |
| 70 | +/// |
| 71 | +/// * `callback` - Either a tuple `(min_level, function)` where `min_level` is the lowest priority |
| 72 | +/// log message to handle, or `None` to disable all printing. |
| 73 | +/// |
| 74 | +/// This overrides (and is overridden by) [`ObjectBuilder::debug`] |
| 75 | +/// |
| 76 | +/// # Examples |
| 77 | +/// |
| 78 | +/// To pass all messages to the `log` crate: |
| 79 | +/// |
| 80 | +/// ``` |
| 81 | +/// use log; |
| 82 | +/// use libbpf_rs::{PrintLevel, set_print}; |
| 83 | +/// |
| 84 | +/// fn print_to_log(level: PrintLevel, msg: String) { |
| 85 | +/// match level { |
| 86 | +/// PrintLevel::Debug => log::debug!("{}", msg), |
| 87 | +/// PrintLevel::Info => log::info!("{}", msg), |
| 88 | +/// PrintLevel::Warn => log::warn!("{}", msg), |
| 89 | +/// } |
| 90 | +/// } |
| 91 | +/// |
| 92 | +/// set_print(Some((PrintLevel::Debug, print_to_log))); |
| 93 | +/// ``` |
| 94 | +/// |
| 95 | +/// To disable printing completely: |
| 96 | +/// |
| 97 | +/// ``` |
| 98 | +/// use libbpf_rs::set_print; |
| 99 | +/// set_print(None); |
| 100 | +/// ``` |
| 101 | +/// |
| 102 | +/// To temporarliy suppress output: |
| 103 | +/// |
| 104 | +/// ``` |
| 105 | +/// use libbpf_rs::set_print; |
| 106 | +/// |
| 107 | +/// let prev = set_print(None); |
| 108 | +/// // do things quietly |
| 109 | +/// set_print(prev); |
| 110 | +/// ``` |
| 111 | +pub fn set_print( |
| 112 | + mut callback: Option<(PrintLevel, PrintCallback)>, |
| 113 | +) -> Option<(PrintLevel, PrintCallback)> { |
| 114 | + let real_cb: libbpf_sys::libbpf_print_fn_t; |
| 115 | + real_cb = callback.as_ref().and(Some(outer_print_cb)); |
| 116 | + std::mem::swap(&mut callback, &mut *PRINT_CB.lock().unwrap()); |
| 117 | + unsafe { libbpf_sys::libbpf_set_print(real_cb) }; |
| 118 | + callback |
| 119 | +} |
| 120 | + |
| 121 | +/// Return the current print callback and level. |
| 122 | +/// |
| 123 | +/// # Examples |
| 124 | +/// |
| 125 | +/// To temporarliy suppress output: |
| 126 | +/// |
| 127 | +/// ``` |
| 128 | +/// use libbpf_rs::{get_print, set_print}; |
| 129 | +/// |
| 130 | +/// let prev = get_print(); |
| 131 | +/// set_print(None); |
| 132 | +/// // do things quietly |
| 133 | +/// set_print(prev); |
| 134 | +/// ``` |
| 135 | +pub fn get_print() -> Option<(PrintLevel, PrintCallback)> { |
| 136 | + *PRINT_CB.lock().unwrap() |
| 137 | +} |
0 commit comments