@@ -179,7 +179,7 @@ impl AndroidLogger {
179
179
180
180
static ANDROID_LOGGER : OnceLock < AndroidLogger > = OnceLock :: new ( ) ;
181
181
182
- const LOGGING_TAG_MAX_LEN : usize = 23 ;
182
+ const LOGGING_TAG_MAX_LEN : usize = 128 ;
183
183
const LOGGING_MSG_MAX_LEN : usize = 4000 ;
184
184
185
185
impl Default for AndroidLogger {
@@ -223,10 +223,20 @@ impl Log for AndroidLogger {
223
223
. map ( |s| s. as_bytes ( ) )
224
224
. unwrap_or_else ( || module_path. as_bytes ( ) ) ;
225
225
226
- // truncate the tag here to fit into LOGGING_TAG_MAX_LEN
227
- self . fill_tag_bytes ( & mut tag_bytes, tag) ;
228
- // use stack array as C string
229
- let tag: & CStr = unsafe { CStr :: from_ptr ( mem:: transmute ( tag_bytes. as_ptr ( ) ) ) } ;
226
+ // In case we end up allocating, keep the CString alive.
227
+ let mut _owned_tag = None ;
228
+ let tag: & CStr = if tag. len ( ) < tag_bytes. len ( ) {
229
+ // use stack array as C string
230
+ self . fill_tag_bytes ( & mut tag_bytes, tag) ;
231
+ // SAFETY: fill_tag_bytes always puts a nullbyte in tag_bytes.
232
+ unsafe { CStr :: from_ptr ( mem:: transmute ( tag_bytes. as_ptr ( ) ) ) }
233
+ } else {
234
+ // Tag longer than available stack buffer; allocate.
235
+ // SAFETY: if tag contains nullbytes, the Android logger will just ignore any
236
+ // characters that follow it.
237
+ _owned_tag = Some ( unsafe { CString :: from_vec_unchecked ( tag. to_vec ( ) ) } ) ;
238
+ _owned_tag. as_ref ( ) . unwrap ( )
239
+ } ;
230
240
231
241
// message must not exceed LOGGING_MSG_MAX_LEN
232
242
// therefore split log message into multiple log calls
0 commit comments