Skip to content

Commit

Permalink
Async prints with timer, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mersinvald committed Mar 23, 2017
1 parent c128e4f commit eaa258f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
11 changes: 11 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
macro_rules! cell {
($expr:expr) => (::std::cell::RefCell::new($expr))
}

macro_rules! uncell {
($expr:expr) => (*$expr.borrow())
}

macro_rules! uncell_mut {
($expr:expr) => (*$expr.borrow_mut())
}
42 changes: 19 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#[macro_use] extern crate lazy_static;
#[macro_use] extern crate serde_derive;
#[macro_use] extern crate derive_new;
extern crate serde;
extern crate toml;
extern crate serde_yaml;

#[macro_use] extern crate log;
#[macro_use] extern crate clap;
Expand All @@ -11,6 +12,8 @@ extern crate futures;
extern crate trust_dns;
extern crate tokio_core;

#[macro_use]
mod macros;
mod resolve;
mod config;
use resolve::*;
Expand All @@ -23,6 +26,7 @@ use std::io::{self, Read, Write};
use std::fs::File;
use std::sync::mpsc;
use std::thread;
use std::time::{Instant, Duration};

use std::rc::Rc;
use std::env;
Expand All @@ -33,7 +37,6 @@ use clap::{Arg, App};
use log::{LogRecord, LogLevelFilter};
use env_logger::LogBuilder;


fn process_args() -> (Vec<String>, Vec<String>, Vec<QueryType>) {
let app = App::new("Batch Resolve")
.about("Fast asynchronous DNS batch resolver")
Expand Down Expand Up @@ -194,15 +197,20 @@ fn main() {
let (status_tx, status_rx) = mpsc::channel::<Status>();

thread::spawn(move || {
// Print every 100ms
let mut instant = Instant::now();
for status in status_rx.iter() {
// draw_status(status.done, overall_count as u64, 20);
print!("{}/{} done, {}/{} succesed, {}/{} failed, {} errored\r",
status.done, overall_count,
status.success, overall_count,
status.fail, overall_count,
status.errored
);
stdout().flush().unwrap();
if instant.elapsed() > Duration::from_millis(100) {
instant = Instant::now();
// draw_status(status.done, overall_count as u64, 20);
print!("{}/{} done, {}/{} succesed, {}/{} failed, {} errored\r",
status.done, overall_count,
status.success, overall_count,
status.fail, overall_count,
status.errored
);
stdout().flush().unwrap();
}
}
});

Expand Down Expand Up @@ -252,7 +260,7 @@ fn write_file<'a, I: IntoIterator<Item=String>, P: AsRef<Path>>(data: I, path: P

fn setup_logger(level: LogLevelFilter) {
let format = |record: &LogRecord| {
format!("{}: {}", record.level(), record.args())
format!("{}: {}\t\t\t", record.level(), record.args())
};


Expand All @@ -266,15 +274,3 @@ fn setup_logger(level: LogLevelFilter) {

builder.init().unwrap();
}

fn draw_status(done: u64, all: u64, len: usize) {
let filled = (done as f64 / all as f64 * len as f64).ceil() as usize;
print!("[");
for _ in 0..filled {
print!("=");
}
for _ in filled..len {
print!(" ");
}
print!("]");
}
7 changes: 1 addition & 6 deletions src/resolve/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ use std::error::Error;

#[derive(Debug)]
pub enum ResolverError {
FuturesSendError,
ConnectionTimeout,
NameServerNotResolved,
DnsClientError(::trust_dns::error::ClientError),
Io(::std::io::Error)
}

unsafe impl Send for ResolverError {}
Expand All @@ -18,15 +16,12 @@ impl fmt::Display for ResolverError {
}
}


impl Error for ResolverError {
fn description(&self) -> &str {
match *self {
ResolverError::FuturesSendError => "Receiving end was closed",
ResolverError::ConnectionTimeout => "Connection timeout",
ResolverError::NameServerNotResolved => "Failed to resolve nameserver",
ResolverError::DnsClientError(ref err) => err.description(),
ResolverError::Io(ref err) => err.description(),
}
}
}
}

0 comments on commit eaa258f

Please sign in to comment.