paranoid_android/
logging.rs

1use ndk_sys::{android_LogPriority, log_id};
2use tracing_core::Level;
3
4#[repr(u32)]
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
6pub enum Priority {
7    Verbose = android_LogPriority::ANDROID_LOG_VERBOSE.0,
8    Debug = android_LogPriority::ANDROID_LOG_DEBUG.0,
9    Info = android_LogPriority::ANDROID_LOG_INFO.0,
10    Warn = android_LogPriority::ANDROID_LOG_WARN.0,
11    Error = android_LogPriority::ANDROID_LOG_ERROR.0,
12    Fatal = android_LogPriority::ANDROID_LOG_FATAL.0,
13}
14
15/// An [Android log buffer](https://developer.android.com/ndk/reference/group/logging#log_id).
16#[repr(u32)]
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum Buffer {
19    /// Let the logging function choose the best log target.
20    Default = log_id::LOG_ID_DEFAULT.0,
21
22    /// The main log buffer.
23    ///
24    /// This is the only log buffer available to apps.
25    Main = log_id::LOG_ID_MAIN.0,
26
27    /// The crash log buffer.
28    Crash = log_id::LOG_ID_CRASH.0,
29    /// The statistics log buffer.
30    Stats = log_id::LOG_ID_STATS.0,
31    /// The event log buffer.
32    Events = log_id::LOG_ID_EVENTS.0,
33    /// The security log buffer.
34    Security = log_id::LOG_ID_SECURITY.0,
35    /// The system log buffer.
36    System = log_id::LOG_ID_SYSTEM.0,
37    /// The kernel log buffer.
38    Kernel = log_id::LOG_ID_KERNEL.0,
39    /// The radio log buffer.
40    Radio = log_id::LOG_ID_RADIO.0,
41}
42
43impl Priority {
44    pub fn as_raw(self) -> android_LogPriority {
45        android_LogPriority(self as u32)
46    }
47}
48
49impl From<Level> for Priority {
50    fn from(l: Level) -> Self {
51        match l {
52            Level::TRACE => Priority::Verbose,
53            Level::DEBUG => Priority::Debug,
54            Level::INFO => Priority::Info,
55            Level::WARN => Priority::Warn,
56            Level::ERROR => Priority::Error,
57        }
58    }
59}
60
61impl From<Priority> for Level {
62    fn from(p: Priority) -> Self {
63        match p {
64            Priority::Verbose => Level::TRACE,
65            Priority::Debug => Level::DEBUG,
66            Priority::Info => Level::INFO,
67            Priority::Warn => Level::WARN,
68            Priority::Error | Priority::Fatal => Level::ERROR,
69        }
70    }
71}
72
73impl Buffer {
74    pub(crate) fn as_raw(self) -> log_id {
75        log_id(self as u32)
76    }
77}
78
79impl Default for Buffer {
80    fn default() -> Self {
81        Self::Default
82    }
83}