diff --git a/Cargo.lock b/Cargo.lock index 3cc06740..bda5e58f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,22 @@ dependencies = [ "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "android_log-sys" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "android_logger" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "android_log-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "appdirs" version = "0.2.0" @@ -51,6 +67,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "aw_server" version = "0.1.0" dependencies = [ + "android_logger 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)", "appdirs 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam_requests 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -262,6 +279,18 @@ dependencies = [ "syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "env_logger" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "atty 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", + "humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "error-chain" version = "0.10.0" @@ -352,6 +381,14 @@ name = "httparse" version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "humantime" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "hyper" version = "0.10.16" @@ -1246,6 +1283,14 @@ dependencies = [ "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "termcolor" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "wincolor 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "thread_local" version = "0.3.6" @@ -1464,6 +1509,15 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "wincolor" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winconsole" version = "0.10.0" @@ -1496,6 +1550,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "58fb5e95d83b38284460a5fda7d6470aa0b8844d283a0b614b8535e880800d2d" +"checksum android_log-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b8052e2d8aabbb8d556d6abbcce2a22b9590996c5f849b9c7ce4544a2e3b984e" +"checksum android_logger 0.8.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca95a50614f191f185629deeb44ab647291e50291b9d5f19f46c932b05a2d7f5" "checksum appdirs 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d512b3e61196d27562dcc71446a58ba8a93d3bed2a03a87f96101b9a17f1d378" "checksum approx 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08abcc3b4e9339e33a3d0a5ed15d84a687350c05689d825e0f6655eef9e76a94" "checksum ascii 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3ae7d751998c189c1d4468cf0a39bb2eae052a9c58d50ebb3b9591ee3813ad50" @@ -1526,6 +1582,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum devise 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "74e04ba2d03c5fa0d954c061fc8c9c288badadffc272ebb87679a89846de3ed3" "checksum devise_codegen 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "066ceb7928ca93a9bedc6d0e612a8a0424048b0ab1f75971b203d01420c055d7" "checksum devise_core 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cf41c59b22b5e3ec0ea55c7847e5f358d340f3a8d6d53a5cf4f1564967f96487" +"checksum env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3" "checksum error-chain 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d9435d864e017c3c6afeac1654189b06cdb491cf2ff73dbf0d73b0f292f42ff8" "checksum fallible-iterator 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" "checksum fallible-streaming-iterator 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" @@ -1539,6 +1596,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum getrandom 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "6171a6cc63fbabbe27c2b5ee268e8b7fe5dc1eb0dd2dfad537c1dfed6f69117e" "checksum groupable 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "32619942b8be646939eaf3db0602b39f5229b74575b67efc897811ded1db4e57" "checksum httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" +"checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114" "checksum hyper 0.10.16 (registry+https://github.com/rust-lang/crates.io-index)" = "0a0652d9a2609a968c14be1a9ea00bf4b1d64e2e1f53a1b51b6fff3a6e829273" "checksum idna 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d" @@ -1637,6 +1695,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" "checksum syn 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "158521e6f544e7e3dcfc370ac180794aa38cb34a1b1e07609376d4adcf429b93" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" +"checksum termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "96d6098003bde162e4277c70665bd87c326f5a0c3f3fbfb285787fa482d54e6e" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum tiny_http 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1661fa0a44c95d01604bd05c66732a446c657efb62b5164a7a083a3b552b4951" @@ -1668,6 +1727,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +"checksum wincolor 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "96f5016b18804d24db43cebf3c77269e7569b8954a8464501c216cc5e070eaa9" "checksum winconsole 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ef84b96d10db72dd980056666d7f1e7663ce93d82fa33b63e71c966f4cf5032" "checksum ws2_32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" "checksum yansi 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d60c3b48c9cdec42fb06b3b84b5b087405e1fa1c644a1af3930e4dfafe93de48" diff --git a/Cargo.toml b/Cargo.toml index 258d3733..4b2e76e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,3 +33,4 @@ toml = "0.5" [target.'cfg(target_os="android")'.dependencies] jni = { version = "0.5", default-features = false } libc = "0.2" +android_logger = "0.8" diff --git a/compile-android.sh b/compile-android.sh index 801f8255..706bf5d0 100755 --- a/compile-android.sh +++ b/compile-android.sh @@ -12,6 +12,7 @@ if [ $RELEASE ]; then echo "Building in release mode... (slow)"; else echo "Building in debug mode... (fast)" + RELEASE=false; fi ORIG_PATH="$PATH" diff --git a/src/android/logcat.rs b/src/android/logcat.rs deleted file mode 100644 index 30265ed1..00000000 --- a/src/android/logcat.rs +++ /dev/null @@ -1,96 +0,0 @@ -// From: https://github.com/servo/servo/pull/21812/files -// With modifications from latest: https://github.com/servo/servo/blob/5de6d87c97050db35cfb0a575e14b4d9b6207ac5/ports/libsimpleservo/jniapi/src/lib.rs#L480 - -extern crate libc; -use self::libc::{pipe, dup2, read}; -use std::os::raw::{c_char, c_int}; -use std::thread; - -extern "C" { - pub fn __android_log_write(prio: c_int, tag: *const c_char, text: *const c_char) -> c_int; -} - -pub fn redirect_stdout_to_logcat() { - // The first step is to redirect stdout and stderr to the logs. - // We redirect stdout and stderr to a custom descriptor. - let mut pfd: [c_int; 2] = [0, 0]; - unsafe { - pipe(pfd.as_mut_ptr()); - dup2(pfd[1], 1); - dup2(pfd[1], 2); - } - - let descriptor = pfd[0]; - - // Then we spawn a thread whose only job is to read from the other side of the - // pipe and redirect to the logs. - let _detached = thread::spawn(move || { - const BUF_LENGTH: usize = 512; - let mut buf = vec![b'\0' as c_char; BUF_LENGTH]; - - // Always keep at least one null terminator - const BUF_AVAILABLE: usize = BUF_LENGTH - 1; - let buf = &mut buf[..BUF_AVAILABLE]; - - let mut cursor = 0_usize; - - let tag = b"aw-server-rust\0".as_ptr() as _; - - loop { - let result = { - let read_into = &mut buf[cursor..]; - unsafe { - read( - descriptor, - read_into.as_mut_ptr() as *mut _, - read_into.len(), - ) - } - }; - - let end = if result == 0 { - return; - } else if result < 0 { - unsafe { - __android_log_write( - 3, - tag, - b"error in log thread; closing\0".as_ptr() as *const _, - ); - } - return; - } else { - result as usize + cursor - }; - - // Only modify the portion of the buffer that contains real data. - let buf = &mut buf[0..end]; - - if let Some(last_newline_pos) = buf.iter().rposition(|&c| c == b'\n' as c_char) { - buf[last_newline_pos] = b'\0' as c_char; - unsafe { - __android_log_write(3, tag, buf.as_ptr()); - } - if last_newline_pos < buf.len() - 1 { - let pos_after_newline = last_newline_pos + 1; - let len_not_logged_yet = buf[pos_after_newline..].len(); - for j in 0..len_not_logged_yet as usize { - buf[j] = buf[pos_after_newline + j]; - } - cursor = len_not_logged_yet; - } else { - cursor = 0; - } - } else if end == BUF_AVAILABLE { - // No newline found but the buffer is full, flush it anyway. - // `buf.as_ptr()` is null-terminated by BUF_LENGTH being 1 less than BUF_AVAILABLE. - unsafe { - __android_log_write(3, tag, buf.as_ptr()); - } - cursor = 0; - } else { - cursor = end; - } - } - }); -} diff --git a/src/android/mod.rs b/src/android/mod.rs index 3427c2a7..9ac652e6 100644 --- a/src/android/mod.rs +++ b/src/android/mod.rs @@ -1,14 +1,17 @@ // Based On the following guide from Mozilla: // https://mozilla.github.io/firefox-browser-architecture/experiments/2017-09-21-rust-on-android.html +extern crate android_logger; + use std::os::raw::{c_char}; use std::ffi::{CString, CStr}; use std::sync::Mutex; - -mod logcat; use crate::dirs; -use crate::android::logcat::{redirect_stdout_to_logcat}; + +use log::Level; +use android_logger::Config; + #[no_mangle] pub extern fn rust_greeting(to: *const c_char) -> *mut c_char { @@ -79,10 +82,10 @@ pub mod android { use crate::endpoints; - println!("Building server state..."); + info!("Building server state..."); let asset_path = jstring_to_string(&env, java_asset_path); - println!("Using asset dir: {}", asset_path); + info!("Using asset dir: {}", asset_path); let server_state = endpoints::ServerState { datastore: Mutex::new(openDatastore()), @@ -101,11 +104,19 @@ pub mod android { #[no_mangle] pub unsafe extern fn Java_net_activitywatch_android_RustInterface_initialize(env: JNIEnv, _: JClass) { if !INITIALIZED { - redirect_stdout_to_logcat(); - println!("Initializing aw-server-rust..."); - println!("Redirecting aw-server-rust stdout/stderr to logcat"); + android_logger::init_once( + Config::default() + .with_min_level(Level::Trace) // limit log level + .with_tag("aw-server-rust") // logs will show under mytag tag + //.with_filter( // configure messages for specific crate + // FilterBuilder::new() + // .parse("debug,hello::crate=error") + // .build()) + ); + info!("Initializing aw-server-rust..."); + debug!("Redirected aw-server-rust stdout/stderr to logcat"); } else { - println!("Already initialized"); + info!("Already initialized"); } INITIALIZED = true; @@ -116,7 +127,7 @@ pub mod android { #[no_mangle] pub unsafe extern fn Java_net_activitywatch_android_RustInterface_setDataDir(env: JNIEnv, _: JClass, java_dir: JString) { - println!("Setting android data dir"); + debug!("Setting android data dir"); dirs::set_android_data_dir(&jstring_to_string(&env, java_dir)); } diff --git a/src/query/functions.rs b/src/query/functions.rs index e2512403..1b1a763b 100644 --- a/src/query/functions.rs +++ b/src/query/functions.rs @@ -34,7 +34,7 @@ mod qfunctions { pub fn print(args: Vec, _env: &HashMap<&str, DataType>, _ds: &Datastore) -> Result { for arg in args { - println!("{:?}", arg); + info!("{:?}", arg); } return Ok(DataType::None()); } diff --git a/src/transform/mod.rs b/src/transform/mod.rs index 93a6dd84..32e8cd95 100644 --- a/src/transform/mod.rs +++ b/src/transform/mod.rs @@ -92,7 +92,7 @@ pub fn flood(events: Vec, pulsetime: chrono::Duration) -> Vec { if gap < pulsetime { if e1.data == e2.data { if chrono::Duration::seconds(0) > gap && !warned_negative_gap_safe { - println!("Gap was of negative duration ({}s), but could be safely merged. This error will only show once per batch.", gap); + warn!("Gap was of negative duration ({}s), but could be safely merged. This error will only show once per batch.", gap); warned_negative_gap_safe = true; } // Extend e1 to the middle between e1 and e2 @@ -102,7 +102,7 @@ pub fn flood(events: Vec, pulsetime: chrono::Duration) -> Vec { } else { if chrono::Duration::seconds(0) > gap { if !warned_negative_gap_unsafe { - println!("Gap was of negative duration ({}s) and could NOT be safely merged. This error will only show once per batch.", gap); + warn!("Gap was of negative duration ({}s) and could NOT be safely merged. This error will only show once per batch.", gap); warned_negative_gap_unsafe = true; } continue;