syslog_c/
lib.rs

1//! ’syslog-c‘ is just a simple package of C library syslog, 
2//! which is convenient for us to use in the process of 
3//! rewriting C code to Rust
4
5use std::ffi::CString;
6
7/// C openlog, we use &str to replace *const i8.
8/// We still use libc flags here
9/// # Examples
10/// ```
11/// let ident = String::from("myprogram");
12/// syslog_c::openlog(&ident, libc::LOG_CONS | libc::LOG_PID, libc::LOG_AUTHPRIV);
13/// ```
14pub fn openlog(ident: &str, option: libc::c_int, facility: libc::c_int) {
15    let cstr = CString::new(ident).expect("String 2 CString Error when converting ident");
16    let cstr_ptr = cstr.as_ptr();
17    unsafe { libc::openlog(cstr_ptr, option, facility) };
18}
19
20/// the same as libc::closelog
21pub fn closelog() {
22    unsafe { libc::closelog() };
23}
24
25/// the same as libc::setlogmask
26pub fn setlogmask(mask: libc::c_int) -> i32{
27    unsafe { libc::setlogmask(mask) }
28}
29
30/// C syslog, we use &str to replace *const i8.
31/// We still use libc flags here
32/// # Examples
33/// ```
34/// let file_name = String::from("null.txt");
35/// let msg = format!("{} {}", "No this file", file_name);
36/// syslog_c::syslog(libc::LOG_ERR, &msg);
37/// ```
38pub fn syslog(pri: libc::c_int, fmt: &str) {
39    let cstr = CString::new(fmt).expect("String 2 CString Error when syslog");
40    let cstr_ptr = cstr.as_ptr();
41    unsafe { libc::syslog(pri, cstr_ptr) };
42}
43
44
45