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
//! ’syslog-c‘ is just a simple package of C library syslog, 
//! which is convenient for us to use in the process of 
//! rewriting C code to Rust

use std::ffi::CString;

/// C openlog, we use &str to replace *const i8.
/// We still use libc flags here
/// # Examples
/// ```
/// let ident = String::from("myprogram");
/// syslog_c::openlog(&ident, libc::LOG_CONS | libc::LOG_PID, libc::LOG_AUTHPRIV);
/// ```
pub fn openlog(ident: &str, option: libc::c_int, facility: libc::c_int) {
    let cstr = CString::new(ident).expect("String 2 CString Error when converting ident");
    let cstr_ptr = cstr.as_ptr();
    unsafe { libc::openlog(cstr_ptr, option, facility) };
}

/// the same as libc::closelog
pub fn closelog() {
    unsafe { libc::closelog() };
}

/// the same as libc::setlogmask
pub fn setlogmask(mask: libc::c_int) -> i32{
    unsafe { libc::setlogmask(mask) }
}

/// C syslog, we use &str to replace *const i8.
/// We still use libc flags here
/// # Examples
/// ```
/// let file_name = String::from("null.txt");
/// let msg = format!("{} {}", "No this file", file_name);
/// syslog_c::syslog(libc::LOG_ERR, &msg);
/// ```
pub fn syslog(pri: libc::c_int, fmt: &str) {
    let cstr = CString::new(fmt).expect("String 2 CString Error when syslog");
    let cstr_ptr = cstr.as_ptr();
    unsafe { libc::syslog(pri, cstr_ptr) };
}