2024-09-27 00:59:59 +00:00
|
|
|
use alloc::ffi::CString;
|
2020-11-13 01:23:57 +00:00
|
|
|
use bevy_utils::tracing::{
|
|
|
|
field::Field,
|
|
|
|
span::{Attributes, Record},
|
|
|
|
Event, Id, Level, Subscriber,
|
|
|
|
};
|
2024-09-27 00:59:59 +00:00
|
|
|
use core::fmt::{Debug, Write};
|
2020-11-13 01:23:57 +00:00
|
|
|
use tracing_subscriber::{field::Visit, layer::Context, registry::LookupSpan, Layer};
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub(crate) struct AndroidLayer;
|
|
|
|
|
|
|
|
struct StringRecorder(String, bool);
|
|
|
|
impl StringRecorder {
|
|
|
|
fn new() -> Self {
|
|
|
|
StringRecorder(String::new(), false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Visit for StringRecorder {
|
|
|
|
fn record_debug(&mut self, field: &Field, value: &dyn Debug) {
|
|
|
|
if field.name() == "message" {
|
|
|
|
if !self.0.is_empty() {
|
|
|
|
self.0 = format!("{:?}\n{}", value, self.0)
|
|
|
|
} else {
|
|
|
|
self.0 = format!("{:?}", value)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if self.1 {
|
|
|
|
// following args
|
|
|
|
write!(self.0, " ").unwrap();
|
|
|
|
} else {
|
|
|
|
// first arg
|
|
|
|
self.1 = true;
|
|
|
|
}
|
|
|
|
write!(self.0, "{} = {:?};", field.name(), value).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 00:59:59 +00:00
|
|
|
impl Default for StringRecorder {
|
2020-11-13 01:23:57 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
StringRecorder::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: Subscriber + for<'a> LookupSpan<'a>> Layer<S> for AndroidLayer {
|
2021-11-12 03:08:27 +00:00
|
|
|
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) {
|
2020-11-13 01:23:57 +00:00
|
|
|
let mut new_debug_record = StringRecorder::new();
|
|
|
|
attrs.record(&mut new_debug_record);
|
|
|
|
|
|
|
|
if let Some(span_ref) = ctx.span(id) {
|
|
|
|
span_ref
|
|
|
|
.extensions_mut()
|
|
|
|
.insert::<StringRecorder>(new_debug_record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) {
|
|
|
|
if let Some(span_ref) = ctx.span(id) {
|
|
|
|
if let Some(debug_record) = span_ref.extensions_mut().get_mut::<StringRecorder>() {
|
|
|
|
values.record(debug_record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-27 03:30:08 +00:00
|
|
|
#[allow(unsafe_code)]
|
2020-11-13 01:23:57 +00:00
|
|
|
fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) {
|
2024-03-29 03:04:46 +00:00
|
|
|
fn sanitize(string: &str) -> CString {
|
2024-09-27 00:59:59 +00:00
|
|
|
let bytes: Vec<u8> = string
|
2024-03-29 03:04:46 +00:00
|
|
|
.as_bytes()
|
|
|
|
.into_iter()
|
|
|
|
.copied()
|
|
|
|
.filter(|byte| *byte != 0)
|
|
|
|
.collect();
|
|
|
|
CString::new(bytes).unwrap()
|
|
|
|
}
|
|
|
|
|
2020-11-13 01:23:57 +00:00
|
|
|
let mut recorder = StringRecorder::new();
|
|
|
|
event.record(&mut recorder);
|
|
|
|
let meta = event.metadata();
|
2024-03-29 03:04:46 +00:00
|
|
|
let priority = match *meta.level() {
|
2020-11-13 01:23:57 +00:00
|
|
|
Level::TRACE => android_log_sys::LogPriority::VERBOSE,
|
|
|
|
Level::DEBUG => android_log_sys::LogPriority::DEBUG,
|
|
|
|
Level::INFO => android_log_sys::LogPriority::INFO,
|
|
|
|
Level::WARN => android_log_sys::LogPriority::WARN,
|
|
|
|
Level::ERROR => android_log_sys::LogPriority::ERROR,
|
|
|
|
};
|
2024-03-29 03:04:46 +00:00
|
|
|
// SAFETY: Called only on Android platforms. priority is guaranteed to be in range of c_int.
|
|
|
|
// The provided tag and message are null terminated properly.
|
2020-11-13 01:23:57 +00:00
|
|
|
unsafe {
|
|
|
|
android_log_sys::__android_log_write(
|
|
|
|
priority as android_log_sys::c_int,
|
2024-03-29 03:04:46 +00:00
|
|
|
sanitize(meta.name()).as_ptr(),
|
|
|
|
sanitize(&recorder.0).as_ptr(),
|
2020-11-13 01:23:57 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|