aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs138
1 files changed, 87 insertions, 51 deletions
diff --git a/src/lib.rs b/src/lib.rs
index b35db3f..cfd2bb9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -100,43 +100,85 @@ pub use env_logger::fmt::Formatter;
pub(crate) type FormatFn = Box<dyn Fn(&mut dyn fmt::Write, &Record) -> fmt::Result + Sync + Send>;
-#[derive(Copy, Clone, Eq, PartialEq, Debug)]
+/// Possible identifiers of a specific buffer of Android logging system for
+/// logging a message.
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LogId {
+ /// Main log buffer.
+ ///
+ /// This is the only log buffer available to apps.
Main,
+
+ /// Radio log buffer.
Radio,
+
+ /// Event log buffer.
Events,
+
+ /// System log buffer.
System,
- Crash
+
+ /// Crash log buffer.
+ Crash,
+
+ /// Kernel log buffer.
+ Kernel,
+
+ /// Security log buffer.
+ Security,
+
+ /// Statistics log buffer.
+ Stats,
}
+#[cfg(target_os = "android")]
impl LogId {
- #[cfg(target_os = "android")]
- fn to_native(log_id: Option<Self>) -> log_ffi::log_id_t {
+ const fn to_native(log_id: Option<Self>) -> Option<log_ffi::log_id_t> {
match log_id {
- Some(LogId::Main) => log_ffi::log_id_t::MAIN,
- Some(LogId::Radio) => log_ffi::log_id_t::RADIO,
- Some(LogId::Events) => log_ffi::log_id_t::EVENTS,
- Some(LogId::System) => log_ffi::log_id_t::SYSTEM,
- Some(LogId::Crash) => log_ffi::log_id_t::CRASH,
- None => log_ffi::log_id_t::DEFAULT,
+ Some(Self::Main) => Some(log_ffi::log_id_t::MAIN),
+ Some(Self::Radio) => Some(log_ffi::log_id_t::RADIO),
+ Some(Self::Events) => Some(log_ffi::log_id_t::EVENTS),
+ Some(Self::System) => Some(log_ffi::log_id_t::SYSTEM),
+ Some(Self::Crash) => Some(log_ffi::log_id_t::CRASH),
+ Some(Self::Kernel) => Some(log_ffi::log_id_t::KERNEL),
+ Some(Self::Security) => Some(log_ffi::log_id_t::SECURITY),
+ Some(Self::Stats) => Some(log_ffi::log_id_t::STATS),
+ None => None,
}
}
}
-/// Output log to android system.
+/// Outputs log to Android system.
#[cfg(target_os = "android")]
-fn android_log(log_id: log_ffi::log_id_t, prio: log_ffi::LogPriority, tag: &CStr, msg: &CStr) {
- unsafe {
- log_ffi::__android_log_buf_write(log_id as i32,
- prio as i32,
- tag.as_ptr() as *const log_ffi::c_char,
- msg.as_ptr() as *const log_ffi::c_char);
- };
+fn android_log(
+ buf_id: Option<log_ffi::log_id_t>,
+ prio: log_ffi::LogPriority,
+ tag: &CStr,
+ msg: &CStr,
+) {
+ if let Some(buf_id) = buf_id {
+ unsafe {
+ log_ffi::__android_log_buf_write(
+ buf_id as log_ffi::c_int,
+ prio as log_ffi::c_int,
+ tag.as_ptr() as *const log_ffi::c_char,
+ msg.as_ptr() as *const log_ffi::c_char,
+ );
+ };
+ } else {
+ unsafe {
+ log_ffi::__android_log_write(
+ prio as log_ffi::c_int,
+ tag.as_ptr() as *const log_ffi::c_char,
+ msg.as_ptr() as *const log_ffi::c_char,
+ );
+ };
+ }
}
/// Dummy output placeholder for tests.
#[cfg(not(target_os = "android"))]
-fn android_log(_log_id: Option<LogId>, _priority: Level, _tag: &CStr, _msg: &CStr) {}
+fn android_log(_buf_id: Option<LogId>, _priority: Level, _tag: &CStr, _msg: &CStr) {}
/// Underlying android logger backend
pub struct AndroidLogger {
@@ -209,7 +251,7 @@ impl Log for AndroidLogger {
// message must not exceed LOGGING_MSG_MAX_LEN
// therefore split log message into multiple log calls
- let mut writer = PlatformLogWriter::new(config.log_id, record.level(), tag);
+ let mut writer = PlatformLogWriter::new(config.buf_id, record.level(), tag);
// If a custom tag is used, add the module path to the message.
// Use PlatformLogWriter to output chunks if they exceed max size.
@@ -252,20 +294,13 @@ impl AndroidLogger {
#[derive(Default)]
pub struct Config {
log_level: Option<LevelFilter>,
- log_id: Option<LogId>,
+ buf_id: Option<LogId>,
filter: Option<env_logger::filter::Filter>,
tag: Option<CString>,
custom_format: Option<FormatFn>,
}
impl Config {
- // TODO: Remove on 0.13 version release.
- /// **DEPRECATED**, use [`Config::with_max_level()`] instead.
- #[deprecated(note = "use `.with_max_level()` instead")]
- pub fn with_min_level(self, level: Level) -> Self {
- self.with_max_level(level.to_level_filter())
- }
-
/// Changes the maximum log level.
///
/// Note, that `Trace` is the maximum level, because it provides the
@@ -279,15 +314,6 @@ impl Config {
self
}
- /// Change which log buffer is used
- ///
- /// By default, logs are sent to the `Main` log. Other logging buffers may only be accessible
- /// to certain processes.
- pub fn with_log_id(mut self, log_id: LogId) -> Self {
- self.log_id = Some(log_id);
- self
- }
-
/// Changes the Android logging system buffer to be used.
///
/// By default, logs are sent to the [`Main`] log. Other logging buffers may
@@ -295,7 +321,7 @@ impl Config {
///
/// [`Main`]: LogId::Main
pub fn with_log_buffer(mut self, buf_id: LogId) -> Self {
- self.log_id = Some(buf_id);
+ self.buf_id = Some(buf_id);
self
}
@@ -340,8 +366,10 @@ pub struct PlatformLogWriter<'a> {
priority: LogPriority,
#[cfg(not(target_os = "android"))]
priority: Level,
- #[cfg(target_os = "android")] log_id: log_ffi::log_id_t,
- #[cfg(not(target_os = "android"))] log_id: Option<LogId>,
+ #[cfg(target_os = "android")]
+ buf_id: Option<log_ffi::log_id_t>,
+ #[cfg(not(target_os = "android"))]
+ buf_id: Option<LogId>,
len: usize,
last_newline_index: usize,
tag: &'a CStr,
@@ -350,11 +378,15 @@ pub struct PlatformLogWriter<'a> {
impl<'a> PlatformLogWriter<'a> {
#[cfg(target_os = "android")]
- pub fn new_with_priority(log_id: Option<LogId>, priority: log_ffi::LogPriority, tag: &CStr) -> PlatformLogWriter {
+ pub fn new_with_priority(
+ buf_id: Option<LogId>,
+ priority: log_ffi::LogPriority,
+ tag: &CStr,
+ ) -> PlatformLogWriter<'_> {
#[allow(deprecated)] // created an issue #35 for this
PlatformLogWriter {
priority,
- log_id: LogId::to_native(log_id),
+ buf_id: LogId::to_native(buf_id),
len: 0,
last_newline_index: 0,
tag,
@@ -363,9 +395,9 @@ impl<'a> PlatformLogWriter<'a> {
}
#[cfg(target_os = "android")]
- pub fn new(log_id: Option<LogId>, level: Level, tag: &CStr) -> PlatformLogWriter {
- Self::new_with_priority(
- log_id,
+ pub fn new(buf_id: Option<LogId>, level: Level, tag: &CStr) -> PlatformLogWriter<'_> {
+ PlatformLogWriter::new_with_priority(
+ buf_id,
match level {
Level::Warn => LogPriority::WARN,
Level::Info => LogPriority::INFO,
@@ -378,11 +410,11 @@ impl<'a> PlatformLogWriter<'a> {
}
#[cfg(not(target_os = "android"))]
- pub fn new(log_id: Option<LogId>, level: Level, tag: &CStr) -> PlatformLogWriter {
+ pub fn new(buf_id: Option<LogId>, level: Level, tag: &CStr) -> PlatformLogWriter<'_> {
#[allow(deprecated)] // created an issue #35 for this
PlatformLogWriter {
priority: level,
- log_id,
+ buf_id,
len: 0,
last_newline_index: 0,
tag,
@@ -439,7 +471,7 @@ impl<'a> PlatformLogWriter<'a> {
});
let msg: &CStr = unsafe { CStr::from_ptr(self.buffer.as_ptr().cast()) };
- android_log(self.log_id, self.priority, self.tag, msg);
+ android_log(self.buf_id, self.priority, self.tag, msg);
unsafe { *self.buffer.get_unchecked_mut(len) = last_byte };
}
@@ -550,11 +582,11 @@ mod tests {
// Filter is checked in config_filter_match below.
let config = Config::default()
.with_max_level(LevelFilter::Trace)
- .with_log_id(LogId::System)
+ .with_log_buffer(LogId::System)
.with_tag("my_app");
assert_eq!(config.log_level, Some(LevelFilter::Trace));
- assert_eq!(config.log_id, Some(LogId::System));
+ assert_eq!(config.buf_id, Some(LogId::System));
assert_eq!(config.tag, Some(CString::new("my_app").unwrap()));
}
@@ -733,7 +765,11 @@ mod tests {
}
fn get_tag_writer() -> PlatformLogWriter<'static> {
- PlatformLogWriter::new(None, Level::Warn, CStr::from_bytes_with_nul(b"tag\0").unwrap())
+ PlatformLogWriter::new(
+ None,
+ Level::Warn,
+ CStr::from_bytes_with_nul(b"tag\0").unwrap(),
+ )
}
unsafe fn assume_init_slice<T>(slice: &[MaybeUninit<T>]) -> &[T] {