Skip to content

Derive/implement Debug for Config and AndroidLogger #81

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

Merged
merged 1 commit into from
Mar 4, 2025
Merged
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
30 changes: 20 additions & 10 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 @@ -183,15 +184,6 @@ static ANDROID_LOGGER: OnceLock<AndroidLogger> = OnceLock::new();
const LOGGING_TAG_MAX_LEN: usize = 127;
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 {

// In case we end up allocating, keep the CString alive.
let _owned_tag;
let tag: &CStr = if tag.len() < tag_bytes.len() {
let tag = if tag.len() < tag_bytes.len() {
// truncate the tag here to fit into LOGGING_TAG_MAX_LEN
fill_tag_bytes(&mut tag_bytes, tag)
} else {
Expand Down Expand Up @@ -306,6 +298,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