Skip to content

Commit

Permalink
Enable logging in the application user directory
Browse files Browse the repository at this point in the history
wasmerio/wasmer#1379 is caused when calling a Java method from a wasm file via imports. That prevents the wasm file from calling even android_logger. So I switched to use simplelog to write log on a file
  • Loading branch information
YAMAMOTO Yuji committed Apr 17, 2020
1 parent ec4b8d1 commit 6387f88
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {
private static native void JNIExecuteWasm(MainActivity self, byte[] module_bytes) throws Exception;
private static native void JNIExecuteWasm(MainActivity self, String logPath, byte[] module_bytes) throws Exception;

@Keep
public void Test() {
Expand Down Expand Up @@ -44,7 +45,8 @@ protected void onCreate(Bundle savedInstanceState) {

// Run the file
System.out.println("Calling JNIExecuteWasm!");
JNIExecuteWasm(this, module_bytes);
String logPath = getFilesDir().toString() + "/wasmer_android.log";
JNIExecuteWasm(this, logPath, module_bytes);
System.out.println("Finished calling JNIExecuteWasm!");
} catch (Exception e) {
e.printStackTrace();
Expand Down
Binary file modified android/app/src/main/jniLibs/arm64-v8a/libwasmer_android.so
Binary file not shown.
Binary file modified android/app/src/main/jniLibs/x86_64/libwasmer_android.so
Binary file not shown.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.1'
classpath 'com.android.tools.build:gradle:3.6.2'


// NOTE: Do not place your application dependencies here; they belong
Expand Down
153 changes: 153 additions & 0 deletions android_runtime/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions android_runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ crate-type = ["dylib"]
jni = { version = "0.16.0", default-features = false }
lazy_static = "1.4.0"
android_logger = "0.8"
simplelog = "0.7.5"
wasmer-runtime = { default-features = false, path = "../wasmer/lib/runtime/", features = ["singlepass", "default-backend-singlepass"] }
wasmer-singlepass-backend = { optional = true, path = "../wasmer/lib/singlepass-backend/" }
log = "0.4"
38 changes: 34 additions & 4 deletions android_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ extern crate jni;
#[macro_use]
extern crate log;
extern crate android_logger;
extern crate simplelog;

use android_logger::Config;
use jni::{
errors::ErrorKind,
objects::{GlobalRef, JClass, JObject},
objects::{GlobalRef, JClass, JObject, JString},
sys::jbyteArray,
JNIEnv, JavaVM,
};
use log::Level;
use simplelog::*;
use std::ffi::CStr;
use std::fs::File;
use std::sync::Mutex;
use wasmer_runtime::{compile, func, imports, Ctx, ImportObject, Instance, Module};
use wasmer_runtime::{compile, func, imports, ImportObject, Instance, Module};

lazy_static! {
static ref ENV: Mutex<Option<JavaVM>> = { Mutex::new(None) };
Expand All @@ -26,9 +29,13 @@ pub unsafe extern "C" fn Java_com_wasmer_android_MainActivity_JNIExecuteWasm(
env: JNIEnv,
_: JClass,
callback: JObject,
jlog_path: JString,
module_bytes: jbyteArray,
) {
android_logger::init_once(Config::default().with_min_level(Level::Trace));
if let Err(err) = init_simplelog(&env, jlog_path) {
init_android_logger();
warn!("{}", err);
}

std::panic::set_hook(Box::new(|panic_info| {
error!("ERR: {}", panic_info.to_string());
Expand All @@ -54,6 +61,28 @@ pub unsafe extern "C" fn Java_com_wasmer_android_MainActivity_JNIExecuteWasm(
java_test();
}

fn init_android_logger() {
android_logger::init_once(android_logger::Config::default().with_min_level(Level::Trace));
}

fn init_simplelog(env: &JNIEnv, jlog_path: JString) -> Result<(), String> {
let log_path = unsafe {
CStr::from_ptr(
env.get_string(jlog_path)
.map_err(|err| err.to_string())?
.as_ptr(),
)
}
.to_str()
.map_err(|err| err.to_string())?;
WriteLogger::init(
LevelFilter::Trace,
Config::default(),
File::create(log_path).map_err(|err| err.to_string())?,
)
.map_err(|err| err.to_string())
}

pub fn load_module(module_bytes: &[u8]) -> Instance {
// Compile the module.
let module = compile(&module_bytes).unwrap();
Expand All @@ -74,6 +103,7 @@ fn create_import_object(_module: &Module) -> ImportObject {
}

fn java_test() {
info!("BEGIN java_test");
// Get env.
let ovm = &*ENV.lock().unwrap();
let vm = ovm.as_ref().unwrap();
Expand Down

0 comments on commit 6387f88

Please sign in to comment.