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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
mod level;

#[cfg(feature = "v2_1")]
use crate::cstr;

pub use self::level::Level;

pub struct Log {
    pub name: String,
    pub lxcpath: String,
    pub file: String,
    pub level: self::Level,
    pub prefix: String,
    pub quiet: bool,
}

#[cfg(feature = "v2_1")]
impl std::convert::Into<lxc_sys::lxc_log> for Log {
    fn into(self) -> lxc_sys::lxc_log {
        let level: String = self.level.into();

        lxc_sys::lxc_log {
            name: cstr!(&self.name),
            lxcpath: cstr!(&self.lxcpath),
            file: cstr!(&self.file),
            level: cstr!(&level),
            prefix: cstr!(&self.prefix),
            quiet: self.quiet,
        }
    }
}

impl Log {
    /**
     * Initialize the log.
     */
    pub fn init(self) -> Result<(), ()> {
        if self.log_init() == 0 {
            Ok(())
        } else {
            Err(())
        }
    }

    #[cfg(not(feature = "v2_1"))]
    fn log_init(self) -> i32 {
        -1
    }

    #[cfg(feature = "v2_1")]
    fn log_init(self) -> i32 {
        let mut info: lxc_sys::lxc_log = self.into();

        unsafe { lxc_sys::lxc_log_init(&mut info) }
    }

    /**
     * Close log file.
     */
    pub fn close() {
        #[cfg(feature = "v2_0")]
        unsafe {
            lxc_sys::lxc_log_close()
        }
    }
}