diff --git a/parity-clib-examples/cpp/main.cpp b/parity-clib-examples/cpp/main.cpp index c5e83d06492..5aa4a876608 100644 --- a/parity-clib-examples/cpp/main.cpp +++ b/parity-clib-examples/cpp/main.cpp @@ -34,7 +34,9 @@ int main() { } void* parity; - if (parity_start(&cfg, &parity) != 0) { + char mode[] = "rpc=trace"; + Logger logger = { .mode = mode, .mode_len = strlen(mode), .file = NULL, .file_len = 0 }; + if (parity_start(&cfg, logger, &parity) != 0) { return 1; } diff --git a/parity-clib/parity.h b/parity-clib/parity.h index 9be077b4d30..02d3c8b1ac7 100644 --- a/parity-clib/parity.h +++ b/parity-clib/parity.h @@ -37,6 +37,18 @@ struct ParityParams { void *on_client_restart_cb_custom; }; +/// Logger options for `parity_start` +struct Logger { + /// Logger mode (Rust log format, see module docs for `ethcore-logger` + const char* mode; + /// Length of the `Logger mode` + size_t mode_len; + /// File name to write the log to + const char* file; + /// Length of the file name + size_t file_len; +}; + #ifdef __cplusplus extern "C" { #endif @@ -78,7 +90,7 @@ void parity_config_destroy(void* cfg); /// On success, the produced object will be written to the `void*` pointed by `out`. /// /// Returns 0 on success, and non-zero on error. -int parity_start(const ParityParams* params, void** out); +int parity_start(const ParityParams* params, Logger logger, void** out); /// Destroys the parity client created with `parity_start`. /// diff --git a/parity-clib/src/lib.rs b/parity-clib/src/lib.rs index 7791f97bd5e..b5c2cb9493a 100644 --- a/parity-clib/src/lib.rs +++ b/parity-clib/src/lib.rs @@ -22,11 +22,8 @@ extern crate jni; extern crate parity_ethereum; extern crate panic_hook; +use std::{str, slice, panic, ptr}; use std::os::raw::{c_char, c_void, c_int}; -use std::panic; -use std::ptr; -use std::slice; -use std::str; #[cfg(feature = "jni")] use std::mem; @@ -40,6 +37,14 @@ pub struct ParityParams { pub on_client_restart_cb_custom: *mut c_void, } +#[repr(C)] +pub struct Logger { + pub mode: *const char, + pub mode_len: usize, + pub file: *const char, + pub file_len: usize, +} + #[no_mangle] pub unsafe extern fn parity_config_from_cli(args: *const *const c_char, args_lens: *const usize, len: usize, output: *mut *mut c_void) -> c_int { panic::catch_unwind(|| { @@ -87,11 +92,34 @@ pub unsafe extern fn parity_config_destroy(cfg: *mut c_void) { } #[no_mangle] -pub unsafe extern fn parity_start(cfg: *const ParityParams, output: *mut *mut c_void) -> c_int { +pub unsafe extern fn parity_start(cfg: *const ParityParams, logger: Logger, output: *mut *mut c_void) -> c_int { panic::catch_unwind(|| { *output = ptr::null_mut(); let cfg: &ParityParams = &*cfg; + let mode = { + if logger.mode_len == 0 { + None + } else { + let mode = slice::from_raw_parts(logger.mode as *const u8, logger.mode_len); + String::from_utf8(mode.to_owned()).ok() + } + }; + + let file = { + if logger.file_len == 0 { + None + } else { + let mode = slice::from_raw_parts(logger.mode as *const u8, logger.mode_len); + String::from_utf8(mode.to_owned()).ok() + } + }; + + let mut log_cfg = parity_ethereum::LoggerConfig::default(); + log_cfg.mode = mode; + log_cfg.file = file; + + let logger = parity_ethereum::setup_log(&log_cfg).expect("Logger initialized only once; qed"); let config = Box::from_raw(cfg.configuration as *mut parity_ethereum::Configuration); let on_client_restart_cb = { @@ -99,7 +127,7 @@ pub unsafe extern fn parity_start(cfg: *const ParityParams, output: *mut *mut c_ move |new_chain: String| { cb.call(&new_chain); } }; - let action = match parity_ethereum::start(*config, on_client_restart_cb, || {}) { + let action = match parity_ethereum::start(*config, logger, on_client_restart_cb, || {}) { Ok(action) => action, Err(_) => return 1, }; @@ -215,7 +243,8 @@ pub unsafe extern "system" fn Java_io_parity_ethereum_Parity_build(env: JNIEnv, }; let mut out = ptr::null_mut(); - match parity_start(¶ms, &mut out) { + let dummy_logger = Logger { mode: ptr::null(), mode_len: 0, file: ptr::null(), file_len: 0 }; + match parity_start(¶ms, dummy_logger, &mut out) { 0 => out as usize as jlong, _ => { let _ = env.throw_new("java/lang/Exception", "failed to start Parity"); diff --git a/parity/lib.rs b/parity/lib.rs index 9407a07f98f..df315f00e74 100644 --- a/parity/lib.rs +++ b/parity/lib.rs @@ -111,6 +111,7 @@ mod db; use std::io::BufReader; use std::fs::File; +use std::sync::Arc; use hash::keccak_buffer; use cli::Args; use configuration::{Cmd, Execute}; @@ -120,8 +121,7 @@ use std::alloc::System; pub use self::configuration::Configuration; pub use self::run::RunningClient; -use ethcore_logger::RotatingLogger; -use std::sync::Arc; +pub use ethcore_logger::{Config as LoggerConfig, setup_log, RotatingLogger}; #[cfg(feature = "memory_profiling")] #[global_allocator]