1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
use crate::{Alpm, Error};
use alpm_sys_ll::*;
use std::ffi::CString;
impl Alpm {
pub fn log_action<S1: Into<Vec<u8>>, S2: Into<Vec<u8>>>(
&self,
prefix: S1,
msg: S2,
) -> Result<(), Error> {
let s = CString::new(msg).unwrap();
let p = CString::new(prefix).unwrap();
let ret = unsafe { (self.lib.alpm_logaction)(self.as_ptr(), p.as_ptr(), s.as_ptr()) };
self.check_ret(ret)
}
}
/// Logs a formatted message.
///
/// A wrapper around the [`Alpm::log_action`] function that acts similar to writeln!.
///
/// Unline the [`Alpm::log_action`] function this macro automatically appends a newline to the message.
///
/// The first argument is a handle to alpm.
///
/// The second is the message prefix. This is usually the name of your program doing the logging.
///
/// The third and following arguments are the same as [`format!`]. The third argument must be a string
/// literal.
///
/// Like [Alpm::log_action`] this returns a Result.
///
/// # Examples
///
/// ```
/// # use alpm::{Alpm, log_action};
/// # let handle = Alpm::new("/", "tests/db").unwrap();
/// log_action!(handle, "coolprogram", "starting transaction").unwrap();
/// log_action!(handle, "coolprogram", "installing package {}", "xorg").unwrap();
/// log_action!(handle, "coolprogram", "installing packages {pkg1} {pkg2}", pkg1 = "xorg", pkg2 = "git").unwrap();
/// ```
#[macro_export]
macro_rules! log_action {
($handle:tt, $prefix:tt, $($arg:tt)*) => ({
let mut s = format!($($arg)*);
s.reserve_exact(1);
s.push('\n');
$handle.log_action($prefix, s)
})
}