@@ -140,7 +140,13 @@ impl AndroidLogger {
140
140
141
141
static ANDROID_LOGGER : OnceLock < AndroidLogger > = OnceLock :: new ( ) ;
142
142
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.
144
150
const LOGGING_TAG_MAX_LEN : usize = 127 ;
145
151
const LOGGING_MSG_MAX_LEN : usize = 4000 ;
146
152
@@ -163,31 +169,22 @@ impl Log for AndroidLogger {
163
169
return ;
164
170
}
165
171
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.
167
174
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;
168
177
169
178
let module_path = record. module_path ( ) . unwrap_or_default ( ) ;
170
179
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 ( ) )
183
184
} else {
184
185
// 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" ) ;
191
188
_owned_tag. as_ref ( )
192
189
} ;
193
190
@@ -197,7 +194,7 @@ impl Log for AndroidLogger {
197
194
198
195
// If a custom tag is used, add the module path to the message.
199
196
// 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 ) {
201
198
( _, Some ( format) ) => format ( & mut writer, record) ,
202
199
( Some ( _) , _) => fmt:: write (
203
200
& mut writer,
0 commit comments