From 8fca2a9eb667146b80c579ae1ea3986ce8f4e68b Mon Sep 17 00:00:00 2001 From: NikVolf Date: Fri, 5 May 2017 15:51:08 +0300 Subject: [PATCH] prints to logger --- Cargo.toml | 3 +++ src/lib.rs | 7 ++++++- src/logger.rs | 26 ++++++++++++++++++++++++++ src/optimizer.rs | 18 +++++++++--------- src/symbols.rs | 2 +- 5 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 src/logger.rs diff --git a/Cargo.toml b/Cargo.toml index 860b957..1edba74 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,9 @@ authors = ["NikVolf "] [dependencies] parity-wasm = { git="https://github.com/nikvolf/parity-wasm" } +log = "0.3" +env_logger = "0.4" +lazy_static = "0.2" [lib] diff --git a/src/lib.rs b/src/lib.rs index 926cc32..da5e3dc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,13 @@ extern crate parity_wasm; +extern crate env_logger; +#[macro_use] extern crate log; +#[macro_use] extern crate lazy_static; mod optimizer; mod gas; mod symbols; +mod logger; pub use optimizer::optimize; -pub use gas::inject_gas_counter; \ No newline at end of file +pub use gas::inject_gas_counter; +pub use logger::init_log; \ No newline at end of file diff --git a/src/logger.rs b/src/logger.rs new file mode 100644 index 0000000..aa1d574 --- /dev/null +++ b/src/logger.rs @@ -0,0 +1,26 @@ +extern crate log; + +use std::env; +use log::LogLevelFilter; +use env_logger::LogBuilder; + +lazy_static! { + static ref LOG_DUMMY: bool = { + let mut builder = LogBuilder::new(); + builder.filter(None, LogLevelFilter::Info); + + if let Ok(log) = env::var("RUST_LOG") { + builder.parse(&log); + } + + if let Ok(_) = builder.init() { + println!("logger initialized"); + } + true + }; +} + +/// Intialize log with default settings +pub fn init_log() { + let _ = *LOG_DUMMY; +} diff --git a/src/optimizer.rs b/src/optimizer.rs index 090a348..2f6b3a8 100644 --- a/src/optimizer.rs +++ b/src/optimizer.rs @@ -41,7 +41,7 @@ pub fn optimize( expand_symbols(module, &mut stay); for symbol in stay.iter() { - println!("symbol to stay: {:?}", symbol); + trace!("symbol to stay: {:?}", symbol); } // Keep track of referreable symbols to rewire calls/globals @@ -62,7 +62,7 @@ pub fn optimize( } else { type_section(module).expect("Code section to exist").types_mut().remove(index); eliminated_types.push(old_index); - println!("Eliminated type({})", old_index); + trace!("Eliminated type({})", old_index); } old_index += 1; } @@ -85,7 +85,7 @@ pub fn optimize( } else { remove = true; eliminated_funcs.push(top_funcs); - println!("Eliminated import({}) func({}, {})", old_index, top_funcs, imports.entries()[index].field()); + trace!("Eliminated import({}) func({}, {})", old_index, top_funcs, imports.entries()[index].field()); } top_funcs += 1; }, @@ -95,7 +95,7 @@ pub fn optimize( } else { remove = true; eliminated_globals.push(top_globals); - println!("Eliminated import({}) global({}, {})", old_index, top_globals, imports.entries()[index].field()); + trace!("Eliminated import({}) global({}, {})", old_index, top_globals, imports.entries()[index].field()); } top_globals += 1; }, @@ -127,7 +127,7 @@ pub fn optimize( } else { globals.entries_mut().remove(index); eliminated_globals.push(top_globals + old_index); - println!("Eliminated global({})", top_globals + old_index); + trace!("Eliminated global({})", top_globals + old_index); } old_index += 1; } @@ -146,7 +146,7 @@ pub fn optimize( code_section(module).expect("Code section to exist").bodies_mut().remove(index); eliminated_funcs.push(top_funcs + old_index); - println!("Eliminated function({})", top_funcs + old_index); + trace!("Eliminated function({})", top_funcs + old_index); } old_index += 1; } @@ -163,7 +163,7 @@ pub fn optimize( if stay.contains(&Symbol::Export(old_index)) { index += 1; } else { - println!("Eliminated export({}, {})", old_index, exports.entries_mut()[index].field()); + trace!("Eliminated export({}, {})", old_index, exports.entries_mut()[index].field()); exports.entries_mut().remove(index); } old_index += 1; @@ -251,7 +251,7 @@ pub fn update_call_index(opcodes: &mut elements::Opcodes, eliminated_indices: &[ }, &mut Call(ref mut call_index) => { let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *call_index).count(); - println!("rewired call {} -> call {}", *call_index, *call_index - totalle as u32); + trace!("rewired call {} -> call {}", *call_index, *call_index - totalle as u32); *call_index -= totalle as u32; }, _ => { }, @@ -269,7 +269,7 @@ pub fn update_global_index(opcodes: &mut Vec, eliminated_indic }, &mut GetGlobal(ref mut index) | &mut SetGlobal(ref mut index) => { let totalle = eliminated_indices.iter().take_while(|i| (**i as u32) < *index).count(); - println!("rewired global {} -> global {}", *index, *index - totalle as u32); + trace!("rewired global {} -> global {}", *index, *index - totalle as u32); *index -= totalle as u32; }, _ => { }, diff --git a/src/symbols.rs b/src/symbols.rs index f76f9e8..098f767 100644 --- a/src/symbols.rs +++ b/src/symbols.rs @@ -75,7 +75,7 @@ pub fn expand_symbols(module: &elements::Module, set: &mut HashSet) { Some(s) => s, _ => { break; } }; - println!("Processing symbol {:?}", next); + trace!("Processing symbol {:?}", next); match next { Export(idx) => {