Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
fix(parity-clib): enable logger
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 committed Dec 11, 2018
1 parent 3dc1e21 commit df07353
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
4 changes: 3 additions & 1 deletion parity-clib-examples/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 13 additions & 1 deletion parity-clib/parity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`.
///
Expand Down
43 changes: 36 additions & 7 deletions parity-clib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(|| {
Expand Down Expand Up @@ -87,19 +92,42 @@ 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 = {
let cb = CallbackStr(cfg.on_client_restart_cb, cfg.on_client_restart_cb_custom);
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,
};
Expand Down Expand Up @@ -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(&params, &mut out) {
let dummy_logger = Logger { mode: ptr::null(), mode_len: 0, file: ptr::null(), file_len: 0 };
match parity_start(&params, dummy_logger, &mut out) {
0 => out as usize as jlong,
_ => {
let _ = env.throw_new("java/lang/Exception", "failed to start Parity");
Expand Down
4 changes: 2 additions & 2 deletions parity/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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]
Expand Down

0 comments on commit df07353

Please sign in to comment.