ios work
80
veilid-tools/src/tests/android/mod.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
use super::*;
|
||||
|
||||
use jni::errors::Result as JniResult;
|
||||
use jni::{objects::GlobalRef, objects::JObject, objects::JString, JNIEnv, JavaVM};
|
||||
use lazy_static::*;
|
||||
use std::backtrace::Backtrace;
|
||||
use std::panic;
|
||||
use tracing::*;
|
||||
use tracing_subscriber::prelude::*;
|
||||
use tracing_subscriber::*;
|
||||
|
||||
pub struct AndroidGlobals {
|
||||
pub vm: JavaVM,
|
||||
pub ctx: GlobalRef,
|
||||
}
|
||||
|
||||
impl Drop for AndroidGlobals {
|
||||
fn drop(&mut self) {
|
||||
// Ensure we're attached before dropping GlobalRef
|
||||
self.vm.attach_current_thread_as_daemon().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref ANDROID_GLOBALS: Arc<Mutex<Option<AndroidGlobals>>> = Arc::new(Mutex::new(None));
|
||||
}
|
||||
|
||||
pub fn veilid_tools_setup_android_no_log<'a>(env: JNIEnv<'a>, ctx: JObject<'a>) {
|
||||
*ANDROID_GLOBALS.lock() = Some(AndroidGlobals {
|
||||
vm: env.get_java_vm().unwrap(),
|
||||
ctx: env.new_global_ref(ctx).unwrap(),
|
||||
});
|
||||
}
|
||||
|
||||
pub fn veilid_tools_setup<'a>(env: JNIEnv<'a>, ctx: JObject<'a>, log_tag: &'a str) {
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "tracing")] {
|
||||
// Set up subscriber and layers
|
||||
|
||||
let subscriber = Registry::default();
|
||||
let mut layers = Vec::new();
|
||||
let layer = tracing_android::layer(log_tag)
|
||||
.expect("failed to set up android logging")
|
||||
.with_filter(LevelFilter::TRACE);
|
||||
layers.push(layer.boxed());
|
||||
|
||||
let subscriber = subscriber.with(layers);
|
||||
subscriber
|
||||
.try_init()
|
||||
.expect("failed to init android tracing");
|
||||
}
|
||||
}
|
||||
|
||||
// Set up panic hook for backtraces
|
||||
panic::set_hook(Box::new(|panic_info| {
|
||||
let bt = Backtrace::capture();
|
||||
error!("Backtrace:\n{:?}", bt);
|
||||
}));
|
||||
|
||||
veilid_core_setup_android_no_log(env, ctx);
|
||||
}
|
||||
|
||||
pub fn get_android_globals() -> (JavaVM, GlobalRef) {
|
||||
let globals_locked = ANDROID_GLOBALS.lock();
|
||||
let globals = globals_locked.as_ref().unwrap();
|
||||
let env = globals.vm.attach_current_thread_as_daemon().unwrap();
|
||||
let vm = env.get_java_vm().unwrap();
|
||||
let ctx = globals.ctx.clone();
|
||||
(vm, ctx)
|
||||
}
|
||||
|
||||
pub fn with_null_local_frame<'b, T, F>(env: JNIEnv<'b>, s: i32, f: F) -> JniResult<T>
|
||||
where
|
||||
F: FnOnce() -> JniResult<T>,
|
||||
{
|
||||
env.push_local_frame(s)?;
|
||||
let out = f();
|
||||
env.pop_local_frame(JObject::null())?;
|
||||
out
|
||||
}
|
||||
|
Before Width: | Height: | Size: 3.5 KiB After Width: | Height: | Size: 3.5 KiB |
|
Before Width: | Height: | Size: 5.2 KiB After Width: | Height: | Size: 5.2 KiB |
|
Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 3.3 KiB After Width: | Height: | Size: 3.3 KiB |
|
Before Width: | Height: | Size: 4.8 KiB After Width: | Height: | Size: 4.8 KiB |
|
Before Width: | Height: | Size: 7.3 KiB After Width: | Height: | Size: 7.3 KiB |
|
Before Width: | Height: | Size: 7.7 KiB After Width: | Height: | Size: 7.7 KiB |
|
Before Width: | Height: | Size: 12 KiB After Width: | Height: | Size: 12 KiB |
|
Before Width: | Height: | Size: 10 KiB After Width: | Height: | Size: 10 KiB |
|
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 16 KiB |