1#![cfg_attr(feature = "unstable-doc-cfg", feature(doc_cfg))]
2#![warn(rust_2018_idioms)]
3
4extern crate libsystemd_sys as ffi;
5
6#[cfg(feature = "journal")]
13#[allow(deprecated)]
14pub use journal::JournalFiles;
15#[cfg(feature = "journal")]
16pub use journal::{Journal, JournalLog, JournalRecord, JournalSeek, JournalWaitResult};
17use libc::{c_char, c_void, free, strlen};
18pub use std::io::{Error, Result};
19
20#[cfg(any(feature = "journal", feature = "bus"))]
21fn usec_from_duration(duration: std::time::Duration) -> u64 {
22 let sub_usecs = duration.subsec_micros() as u64;
23 duration.as_secs() * 1_000_000 + sub_usecs
24}
25
26pub fn ffi_result(ret: ffi::c_int) -> Result<ffi::c_int> {
28 if ret < 0 {
29 Err(Error::from_raw_os_error(-ret))
30 } else {
31 Ok(ret)
32 }
33}
34
35unsafe fn free_cstring(ptr: *mut c_char) -> Option<String> {
38 if ptr.is_null() {
39 return None;
40 }
41 let len = strlen(ptr);
42 let char_slice = std::slice::from_raw_parts(ptr as *mut u8, len);
43 let s = String::from_utf8_lossy(char_slice).into_owned();
44 free(ptr as *mut c_void);
45 Some(s)
46}
47
48#[macro_export]
56macro_rules! sd_try {
57 ($e:expr) => {{
58 $crate::ffi_result(unsafe { $e })?
59 }};
60}
61
62#[cfg(feature = "journal")]
67pub mod journal;
68
69#[macro_export]
72macro_rules! log_with{
73 ($func:expr, $lvl:expr, $($arg:tt),+) => ({
74 $func(&::log::Record::builder()
75 .args(format_args!($($arg),+))
76 .level($lvl)
77 .file(Some(file!()))
78 .line(Some(line!()))
79 .module_path(Some(module_path!()))
80 .build())
81 });
82 (@raw $func:expr, $lvl:expr, $($arg:tt),+) => ({
83 $func($lvl, file!(), line!(), module_path!(), &format_args!($($arg),+))
84 });
85 (@target $tgt:expr, $func:expr, $lvl:expr, $($arg:tt),+) => ({
86 $func(&::log::Record::builder()
87 .args(format_args!($($arg),+))
88 .level($lvl)
89 .target($tgt)
90 .file(Some(file!()))
91 .line(Some(line!()))
92 .module_path(Some(module_path!()))
93 .build())
94 })
95}
96
97#[cfg(feature = "journal")]
98#[macro_export]
99macro_rules! sd_journal_log{
100 ($lvl:expr, $($arg:tt)+) => ($crate::log_with!(@raw ::systemd::journal::log, $lvl, $($arg)+))
101}
102
103pub mod daemon;
104
105pub mod id128;
106
107pub mod login;
109
110#[cfg(feature = "bus")]
113pub mod bus;
114
115pub mod unit;