Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive/implement Debug for Config and AndroidLogger #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ fn android_log(
fn android_log(_buf_id: Option<LogId>, _priority: Level, _tag: &CStr, _msg: &CStr) {}

/// Underlying android logger backend
#[derive(Debug, Default)]
pub struct AndroidLogger {
config: OnceLock<Config>,
}
Expand All @@ -182,15 +183,6 @@ static ANDROID_LOGGER: OnceLock<AndroidLogger> = OnceLock::new();
const LOGGING_TAG_MAX_LEN: usize = 23;
const LOGGING_MSG_MAX_LEN: usize = 4000;

impl Default for AndroidLogger {
/// Create a new logger with default config
fn default() -> AndroidLogger {
AndroidLogger {
config: OnceLock::from(Config::default()),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what Default already does on OnceLock. Since that's the only member, we can simply have #[derive(Default)] struct AndroidLogger ....

}
}
}

impl Log for AndroidLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
let config = self.config();
Expand Down Expand Up @@ -226,7 +218,7 @@ impl Log for AndroidLogger {
// truncate the tag here to fit into LOGGING_TAG_MAX_LEN
self.fill_tag_bytes(&mut tag_bytes, tag);
// use stack array as C string
let tag: &CStr = unsafe { CStr::from_ptr(mem::transmute(tag_bytes.as_ptr())) };
let tag = unsafe { CStr::from_ptr(mem::transmute(tag_bytes.as_ptr())) };
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved

// message must not exceed LOGGING_MSG_MAX_LEN
// therefore split log message into multiple log calls
Expand Down Expand Up @@ -279,6 +271,24 @@ pub struct Config {
custom_format: Option<FormatFn>,
}

impl fmt::Debug for Config {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Config")
.field("log_level", &self.log_level)
.field("buf_id", &self.buf_id)
.field("filter", &self.filter)
.field("tag", &self.tag)
.field(
"custom_format",
match &self.custom_format {
Some(_) => &"Some(_)",
None => &"None",
},
)
.finish()
}
}

impl Config {
/// Changes the maximum log level.
///
Expand Down Expand Up @@ -449,7 +459,7 @@ impl<'a> PlatformLogWriter<'a> {
self.buffer.get_unchecked_mut(len)
});

let msg: &CStr = unsafe { CStr::from_ptr(self.buffer.as_ptr().cast()) };
let msg = unsafe { CStr::from_ptr(self.buffer.as_ptr().cast()) };
android_log(self.buf_id, self.priority, self.tag, msg);

unsafe { *self.buffer.get_unchecked_mut(len) = last_byte };
Expand Down
Loading