Skip to content

Commit

Permalink
✨ feat: Enhance logging in rust lib
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Aug 1, 2024
1 parent fe1ddf3 commit b2830f8
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 13 deletions.
3 changes: 2 additions & 1 deletion docs/release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## 1.0.0

* Revised internal error handling to relay exception in `transform()`
* Revised internal error handling by relaying exception in `parse()`, `transform()`, `transpile()`
* Enhanced logging in rust lib

## 0.11.0

Expand Down
11 changes: 11 additions & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,14 @@ export CXX=aarch64-linux-android24-clang++
export RUSTFLAGS="-L ${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/14.0.6/lib/linux/aarch64 -L ${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/24 -C target-feature=+crt-static"
cargo build --release --target aarch64-linux-android && deno run --allow-all ../scripts/ts/copy_swc4j_lib.ts -o android -a arm64
```

## Logging

The debug log can be turned on as follows.

```sh
# Linux / MacOS
export RUST_LOG=debug
# Windows
set RUST_LOG=debug
```
1 change: 1 addition & 0 deletions rust/src/ast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23484,6 +23484,7 @@ static mut JAVA_CLASS_WITH_STMT: Option<JavaSwc4jAstWithStmt> = None;
static mut JAVA_CLASS_YIELD_EXPR: Option<JavaSwc4jAstYieldExpr> = None;

pub fn init<'local>(env: &mut JNIEnv<'local>) {
log::debug!("init()");
unsafe {
JAVA_CLASS_ = Some(JavaISwc4jAst::new(env));
JAVA_CLASS_ASSIGN_TARGET = Some(JavaISwc4jAstAssignTarget::new(env));
Expand Down
1 change: 1 addition & 0 deletions rust/src/comment_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ static mut JAVA_COMMENT: Option<JavaSwc4jComment> = None;
static mut JAVA_COMMENTS: Option<JavaSwc4jComments> = None;

pub fn init<'local>(env: &mut JNIEnv<'local>) {
log::debug!("init()");
unsafe {
JAVA_COMMENT = Some(JavaSwc4jComment::new(env));
JAVA_COMMENTS = Some(JavaSwc4jComments::new(env));
Expand Down
7 changes: 7 additions & 0 deletions rust/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ fn parse_by_mode(
parse_mode: enums::ParseMode,
plugin_host: &mut Option<plugin_utils::PluginHost>,
) -> Result<ParsedSource> {
log::debug!("parse_by_mode({:?})", parse_mode);
let result = if let Some(plugin_host) = plugin_host {
let code: &str = &parse_params.text.to_owned();
let mut error: Option<Error> = None;
Expand Down Expand Up @@ -80,6 +81,8 @@ fn parse_by_mode(
}

pub fn parse<'local>(code: String, options: options::ParseOptions) -> Result<outputs::ParseOutput> {
log::debug!("parse()");
log::debug!("{:?}", options);
let specifier = options.get_specifier()?;
let parse_params = ParseParams {
specifier,
Expand All @@ -95,6 +98,8 @@ pub fn parse<'local>(code: String, options: options::ParseOptions) -> Result<out
}

pub fn transform<'local>(code: String, options: options::TransformOptions) -> Result<outputs::TransformOutput> {
log::debug!("transform()");
log::debug!("{:?}", options);
let specifier = options.get_specifier()?;
let parse_params = ParseParams {
specifier: specifier.clone(),
Expand Down Expand Up @@ -170,6 +175,8 @@ pub fn transform<'local>(code: String, options: options::TransformOptions) -> Re
}

pub fn transpile<'local>(code: String, options: options::TranspileOptions) -> Result<outputs::TranspileOutput> {
log::debug!("transpile()");
log::debug!("{:?}", options);
let specifier = options.get_specifier()?;
let parse_params = ParseParams {
specifier,
Expand Down
1 change: 1 addition & 0 deletions rust/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ declare_identifiable_enum!(
);

pub fn init<'local>(env: &mut JNIEnv<'local>) {
log::debug!("init()");
unsafe {
JAVA_CLASS_ACCESSIBILITY = Some(JavaAccessibility::new(env));
JAVA_CLASS_ASSIGN_OP = Some(JavaAssignOp::new(env));
Expand Down
25 changes: 13 additions & 12 deletions rust/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,18 @@ impl JavaCoreException {
static mut JAVA_CORE_EXCEPTION: Option<JavaCoreException> = None;

pub fn init<'local>(env: &mut JNIEnv<'local>) {
log::debug!("init()");
unsafe {
JAVA_CORE_EXCEPTION = Some(JavaCoreException::new(env));
}
}

pub fn throw_parse_error<'local, 'a>(env: &mut JNIEnv<'local>, message: &'a str) -> jobject {
let java_core_exception = unsafe { JAVA_CORE_EXCEPTION.as_ref().unwrap() };
let has_exception = env.exception_check();
let cause = if has_exception.is_ok() && has_exception.unwrap() {
let cause = env.exception_occurred().expect("Couldn't get exception occurred");
env.exception_clear().expect("Could'n clear exception occurred");
let cause = if env.exception_check().unwrap_or(false) {
log::error!("Exception occurred in parse()");
let cause = env.exception_occurred().expect("Couldn't get exception occurred in parse()");
env.exception_clear().expect("Could'n clear exception occurred in parse()");
Some(cause)
} else {
None
Expand All @@ -168,10 +169,10 @@ pub fn throw_parse_error<'local, 'a>(env: &mut JNIEnv<'local>, message: &'a str)

pub fn throw_transform_error<'local, 'a>(env: &mut JNIEnv<'local>, message: &'a str) -> jobject {
let java_core_exception = unsafe { JAVA_CORE_EXCEPTION.as_ref().unwrap() };
let has_exception = env.exception_check();
let cause = if has_exception.is_ok() && has_exception.unwrap() {
let cause = env.exception_occurred().expect("Couldn't get exception occurred");
env.exception_clear().expect("Could'n clear exception occurred");
let cause = if env.exception_check().unwrap_or(false) {
log::error!("Exception occurred in transform()");
let cause = env.exception_occurred().expect("Couldn't get exception occurred in transform()");
env.exception_clear().expect("Could'n clear exception occurred in transform()");
Some(cause)
} else {
None
Expand All @@ -182,10 +183,10 @@ pub fn throw_transform_error<'local, 'a>(env: &mut JNIEnv<'local>, message: &'a

pub fn throw_transpile_error<'local, 'a>(env: &mut JNIEnv<'local>, message: &'a str) -> jobject {
let java_core_exception = unsafe { JAVA_CORE_EXCEPTION.as_ref().unwrap() };
let has_exception = env.exception_check();
let cause = if has_exception.is_ok() && has_exception.unwrap() {
let cause = env.exception_occurred().expect("Couldn't get exception occurred");
env.exception_clear().expect("Could'n clear exception occurred");
let cause = if env.exception_check().unwrap_or(false) {
log::error!("Exception occurred in transpile()");
let cause = env.exception_occurred().expect("Couldn't get exception occurred in transpile()");
env.exception_clear().expect("Could'n clear exception occurred in transpile()");
Some(cause)
} else {
None
Expand Down
1 change: 1 addition & 0 deletions rust/src/jni_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ static mut JAVA_OPTIONAL: Option<JavaOptional> = None;
static mut JAVA_URL: Option<JavaURL> = None;

pub fn init<'local>(env: &mut JNIEnv<'local>) {
log::debug!("init()");
unsafe {
JAVA_ARRAY_LIST = Some(JavaArrayList::new(env));
JAVA_HASH_MAP = Some(JavaHashMap::new(env));
Expand Down
4 changes: 4 additions & 0 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub extern "system" fn Java_com_caoccao_javet_swc4j_Swc4jNative_coreGetVersion<'
env: JNIEnv<'local>,
_: JClass<'local>,
) -> jstring {
log::debug!("Java_com_caoccao_javet_swc4j_Swc4jNative_coreGetVersion()");
string_to_jstring!(env, core::get_version()).as_raw()
}

Expand All @@ -70,6 +71,7 @@ pub extern "system" fn Java_com_caoccao_javet_swc4j_Swc4jNative_coreParse<'local
code: jstring,
options: jobject,
) -> jobject {
log::debug!("Java_com_caoccao_javet_swc4j_Swc4jNative_coreParse()");
match core_parse(&mut env, code, options) {
Ok(output) => output,
Err(err) => error::throw_parse_error(&mut env, err.to_string().as_str()),
Expand All @@ -83,6 +85,7 @@ pub extern "system" fn Java_com_caoccao_javet_swc4j_Swc4jNative_coreTransform<'l
code: jstring,
options: jobject,
) -> jobject {
log::debug!("Java_com_caoccao_javet_swc4j_Swc4jNative_coreTransform()");
match core_transform(&mut env, code, options) {
Ok(output) => output,
Err(err) => error::throw_transform_error(&mut env, err.to_string().as_str()),
Expand All @@ -96,6 +99,7 @@ pub extern "system" fn Java_com_caoccao_javet_swc4j_Swc4jNative_coreTranspile<'l
code: jstring,
options: jobject,
) -> jobject {
log::debug!("Java_com_caoccao_javet_swc4j_Swc4jNative_coreTranspile()");
match core_transpile(&mut env, code, options) {
Ok(output) => output,
Err(err) => error::throw_transpile_error(&mut env, err.to_string().as_str()),
Expand Down
1 change: 1 addition & 0 deletions rust/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,7 @@ static mut JAVA_TRANSFORM_OPTIONS: Option<JavaSwc4jTransformOptions> = None;
static mut JAVA_TRANSPILE_OPTIONS: Option<JavaSwc4jTranspileOptions> = None;

pub fn init<'local>(env: &mut JNIEnv<'local>) {
log::debug!("init()");
unsafe {
JAVA_PARSE_OPTIONS = Some(JavaSwc4jParseOptions::new(env));
JAVA_TRANSFORM_OPTIONS = Some(JavaSwc4jTransformOptions::new(env));
Expand Down
1 change: 1 addition & 0 deletions rust/src/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ static mut JAVA_TRANSFORM_OUTPUT: Option<JavaSwc4jTransformOutput> = None;
static mut JAVA_TRANSPILE_OUTPUT: Option<JavaSwc4jTranspileOutput> = None;

pub fn init<'local>(env: &mut JNIEnv<'local>) {
log::debug!("init()");
unsafe {
JAVA_PARSE_OUTPUT = Some(JavaSwc4jParseOutput::new(env));
JAVA_TRANSFORM_OUTPUT = Some(JavaSwc4jTransformOutput::new(env));
Expand Down
4 changes: 4 additions & 0 deletions rust/src/plugin_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl<'local> PluginHost<'local> {
}

pub fn process_module(&mut self, s: &str, module: Module) -> Result<Module> {
log::debug!("process_module()");
let java_class = unsafe { JAVA_CLASS_I_PLUGIN_HOST.as_ref().unwrap() };
let mut map = ByteToIndexMap::new();
module.register_with_map(&mut map);
Expand All @@ -62,6 +63,7 @@ impl<'local> PluginHost<'local> {
}

pub fn process_program(&mut self, s: &str, program: Program) -> Result<Program> {
log::debug!("process_program()");
let java_class = unsafe { JAVA_CLASS_I_PLUGIN_HOST.as_ref().unwrap() };
let mut map = ByteToIndexMap::new();
program.register_with_map(&mut map);
Expand All @@ -86,6 +88,7 @@ impl<'local> PluginHost<'local> {
}

pub fn process_script(&mut self, s: &str, script: Script) -> Result<Script> {
log::debug!("process_script()");
let java_class = unsafe { JAVA_CLASS_I_PLUGIN_HOST.as_ref().unwrap() };
let mut map = ByteToIndexMap::new();
script.register_with_map(&mut map);
Expand Down Expand Up @@ -173,6 +176,7 @@ impl JavaISwc4jPluginHost {
static mut JAVA_CLASS_I_PLUGIN_HOST: Option<JavaISwc4jPluginHost> = None;

pub fn init<'local>(env: &mut JNIEnv<'local>) {
log::debug!("init()");
unsafe {
JAVA_CLASS_I_PLUGIN_HOST = Some(JavaISwc4jPluginHost::new(env));
}
Expand Down
1 change: 1 addition & 0 deletions rust/src/span_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ impl JavaSwc4jSpan {
static mut JAVA_CLASS_SPAN: Option<JavaSwc4jSpan> = None;

pub fn init<'local>(env: &mut JNIEnv<'local>) {
log::debug!("init()");
unsafe {
JAVA_CLASS_SPAN = Some(JavaSwc4jSpan::new(env));
}
Expand Down
1 change: 1 addition & 0 deletions rust/src/token_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ impl JavaSwc4jTokenFactory {
static mut JAVA_TOKEN_FACTORY: Option<JavaSwc4jTokenFactory> = None;

pub fn init<'local>(env: &mut JNIEnv<'local>) {
log::debug!("init()");
unsafe {
JAVA_TOKEN_FACTORY = Some(JavaSwc4jTokenFactory::new(env));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public void testJNI() throws IOException {
assertTrue(structCounter.get() > 0);
lines.addAll(declarationLines);
lines.add("\npub fn init<'local>(env: &mut JNIEnv<'local>) {");
lines.add(" log::debug!(\"init()\");");
lines.add(" unsafe {");
lines.addAll(initLines);
lines.add(" }");
Expand Down

0 comments on commit b2830f8

Please sign in to comment.