Skip to main content

apple_log/
os_log_entry_log.rs

1use core::ffi::c_void;
2use std::ptr::NonNull;
3
4use crate::ffi;
5use crate::os_log_store::{OSLogEntryCommon, OSLogEntryFromProcess, OSLogEntryWithPayload};
6
7/// The severity of an `OSLogEntryLog`.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub enum OSLogEntryLogLevel {
10    Undefined,
11    Debug,
12    Info,
13    Notice,
14    Error,
15    Fault,
16}
17
18impl OSLogEntryLogLevel {
19    pub(crate) const fn from_raw(raw: i32) -> Self {
20        match raw {
21            1 => Self::Debug,
22            2 => Self::Info,
23            3 => Self::Notice,
24            4 => Self::Error,
25            5 => Self::Fault,
26            _ => Self::Undefined,
27        }
28    }
29}
30
31/// Snapshot of an `OSLogEntryLog` returned from `OSLogStore`.
32pub struct OSLogEntryLog {
33    ptr: NonNull<c_void>,
34}
35
36impl OSLogEntryLog {
37    pub(crate) const fn from_raw(ptr: NonNull<c_void>) -> Self {
38        Self { ptr }
39    }
40
41    #[must_use]
42    pub fn level(&self) -> OSLogEntryLogLevel {
43        OSLogEntryLogLevel::from_raw(unsafe { ffi::apple_log_os_log_entry_level(self.ptr.as_ptr()) })
44    }
45}
46
47impl OSLogEntryCommon for OSLogEntryLog {
48    fn raw_entry_ptr(&self) -> *mut c_void {
49        self.ptr.as_ptr()
50    }
51}
52
53impl OSLogEntryFromProcess for OSLogEntryLog {}
54impl OSLogEntryWithPayload for OSLogEntryLog {}
55
56impl Drop for OSLogEntryLog {
57    fn drop(&mut self) {
58        unsafe { ffi::apple_log_os_log_entry_release(self.ptr.as_ptr()) };
59    }
60}