Skip to content

Commit 8b7fe0b

Browse files
authored
Merge pull request #87 from dextero/no-truncate-2
Avoid rewriting the preconfigured tag
2 parents d829810 + 47488c9 commit 8b7fe0b

File tree

1 file changed

+18
-21
lines changed

1 file changed

+18
-21
lines changed

src/lib.rs

+18-21
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ impl AndroidLogger {
140140

141141
static ANDROID_LOGGER: OnceLock<AndroidLogger> = OnceLock::new();
142142

143-
// Maximum length of a tag that does not require allocation.
143+
/// Maximum length of a tag that does not require allocation.
144+
///
145+
/// Tags configured explicitly in [Config] will not cause an extra allocation. When the tag is
146+
/// derived from the module path, paths longer than this limit will trigger an allocation for each
147+
/// log statement.
148+
///
149+
/// The terminating nullbyte does not count towards this limit.
144150
const LOGGING_TAG_MAX_LEN: usize = 127;
145151
const LOGGING_MSG_MAX_LEN: usize = 4000;
146152

@@ -163,31 +169,22 @@ impl Log for AndroidLogger {
163169
return;
164170
}
165171

166-
// tag longer than LOGGING_TAG_MAX_LEN causes allocation
172+
// Temporary storage for null-terminating record.module_path() if it's needed.
173+
// Tags too long to fit here cause allocation.
167174
let mut tag_bytes: [MaybeUninit<u8>; LOGGING_TAG_MAX_LEN + 1] = uninit_array();
175+
// In case we end up allocating, keep the CString alive.
176+
let _owned_tag;
168177

169178
let module_path = record.module_path().unwrap_or_default();
170179

171-
// If no tag was specified, use module name
172-
let custom_tag = &config.tag;
173-
let tag = custom_tag
174-
.as_ref()
175-
.map(|s| s.as_bytes())
176-
.unwrap_or_else(|| module_path.as_bytes());
177-
178-
// In case we end up allocating, keep the CString alive.
179-
let _owned_tag;
180-
let tag = if tag.len() < tag_bytes.len() {
181-
// truncate the tag here to fit into LOGGING_TAG_MAX_LEN
182-
fill_tag_bytes(&mut tag_bytes, tag)
180+
let tag = if let Some(tag) = &config.tag {
181+
tag
182+
} else if module_path.len() < tag_bytes.len() {
183+
fill_tag_bytes(&mut tag_bytes, module_path.as_bytes())
183184
} else {
184185
// Tag longer than available stack buffer; allocate.
185-
// We're using either
186-
// - CString::as_bytes on config.tag, or
187-
// - str::as_bytes on record.module_path()
188-
// Neither of those include the terminating nullbyte.
189-
_owned_tag = CString::new(tag)
190-
.expect("config.tag or record.module_path() should never contain nullbytes");
186+
_owned_tag = CString::new(module_path.as_bytes())
187+
.expect("record.module_path() shouldn't contain nullbytes");
191188
_owned_tag.as_ref()
192189
};
193190

@@ -197,7 +194,7 @@ impl Log for AndroidLogger {
197194

198195
// If a custom tag is used, add the module path to the message.
199196
// Use PlatformLogWriter to output chunks if they exceed max size.
200-
let _ = match (custom_tag, &config.custom_format) {
197+
let _ = match (&config.tag, &config.custom_format) {
201198
(_, Some(format)) => format(&mut writer, record),
202199
(Some(_), _) => fmt::write(
203200
&mut writer,

0 commit comments

Comments
 (0)