extern crate libc;
extern crate log;
extern crate libsystemd_sys as ffi;
extern crate cstr_argument;
use libc::{c_char, c_void, free, strlen};
pub use std::io::{Result, Error};
pub fn ffi_result(ret: ffi::c_int) -> Result<ffi::c_int>
{
if ret < 0 {
Err(Error::from_raw_os_error(-ret))
} else {
Ok(ret)
}
}
fn free_cstring(ptr: *mut c_char) -> Option<String> {
if ptr.is_null() {
return None;
}
unsafe {
let len = strlen(ptr);
let char_slice = std::slice::from_raw_parts(ptr as *mut u8, len);
let s = String::from_utf8_lossy(&char_slice).into_owned();
free(ptr as *mut c_void);
Some(s)
}
}
#[macro_export]
macro_rules! sd_try {
($e:expr) => ({
try!($crate::ffi_result(unsafe{ $e}))
})
}
pub mod journal;
#[macro_export]
macro_rules! log_with{
($func:expr, $lvl:expr, $($arg:tt),+) => ({
$func(&::log::Record::builder()
.args(format_args!($($arg),+))
.level($lvl)
.file(Some(file!()))
.line(Some(line!()))
.module_path(Some(module_path!()))
.build())
});
(@raw $func:expr, $lvl:expr, $($arg:tt),+) => ({
$func($lvl, file!(), line!(), module_path!(), &format_args!($($arg),+))
});
(@target $tgt:expr, $func:expr, $lvl:expr, $($arg:tt),+) => ({
$func(&::log::Record::builder()
.args(format_args!($($arg),+))
.level($lvl)
.target($tgt)
.file(Some(file!()))
.line(Some(line!()))
.module_path(Some(module_path!()))
.build())
})
}
#[macro_export]
macro_rules! sd_journal_log{
($lvl:expr, $($arg:tt)+) => (log_with!(@raw ::systemd::journal::log, $lvl, $($arg)+))
}
pub mod daemon;
pub mod id128;
pub mod login;
#[cfg(feature = "bus")]
pub mod bus;