Skip to content

Commit

Permalink
android issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Feb 9, 2024
1 parent c57a1ca commit 142abb7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

pub mod native {

use crate::error::{Error, Result};
use crate::{
error::{Error, Result},
ArgVerbosity,
};
use crossbeam::channel::{unbounded, Receiver, Sender};
use jni::{
objects::{GlobalRef, JClass, JMethodID, JObject, JString, JValue},
signature::{Primitive, ReturnType},
sys::{jboolean, jint},
sys::jint,
JNIEnv, JavaVM,
};
use std::{
Expand Down Expand Up @@ -154,11 +157,11 @@ pub mod native {
vpn_service: JObject,
config_path: JString,
stat_path: JString,
verbose: jboolean,
verbosity: jint,
) -> jint {
let mut env = env;

let log_level = if verbose != 0 { "trace" } else { "info" };
let log_level = ArgVerbosity::try_from(verbosity).unwrap().to_string();
let root = module_path!().split("::").next().unwrap();
let filter_str = &format!("off,{root}={log_level}");
let filter = android_logger::FilterBuilder::new().parse(filter_str).build();
Expand Down
29 changes: 29 additions & 0 deletions src/cmdopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ pub enum ArgVerbosity {
Trace,
}

#[cfg(target_os = "android")]
impl TryFrom<jni::sys::jint> for ArgVerbosity {
type Error = std::io::Error;
fn try_from(value: jni::sys::jint) -> Result<Self, <Self as TryFrom<jni::sys::jint>>::Error> {
match value {
0 => Ok(ArgVerbosity::Off),
1 => Ok(ArgVerbosity::Error),
2 => Ok(ArgVerbosity::Warn),
3 => Ok(ArgVerbosity::Info),
4 => Ok(ArgVerbosity::Debug),
5 => Ok(ArgVerbosity::Trace),
_ => Err(std::io::Error::new(std::io::ErrorKind::InvalidInput, "Invalid verbosity level")),
}
}
}

impl From<ArgVerbosity> for log::LevelFilter {
fn from(verbosity: ArgVerbosity) -> Self {
match verbosity {
Expand All @@ -43,6 +59,19 @@ impl From<log::Level> for ArgVerbosity {
}
}

impl std::fmt::Display for ArgVerbosity {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ArgVerbosity::Off => write!(f, "off"),
ArgVerbosity::Error => write!(f, "error"),
ArgVerbosity::Warn => write!(f, "warn"),
ArgVerbosity::Info => write!(f, "info"),
ArgVerbosity::Debug => write!(f, "debug"),
ArgVerbosity::Trace => write!(f, "trace"),
}
}
}

/// Proxy tunnel over tls
#[derive(clap::Parser, Debug, Clone, PartialEq, Eq)]
#[command(author, version, about = "Proxy tunnel over tls.", long_about = None)]
Expand Down

0 comments on commit 142abb7

Please sign in to comment.