From 8983b943cfec0bcda9d0a5dc94b3ef7549b95220 Mon Sep 17 00:00:00 2001 From: wyhaya Date: Thu, 19 Sep 2019 13:12:14 +0800 Subject: [PATCH] use log library --- Cargo.lock | 39 ++++++++++++++++++++++++++------------- Cargo.toml | 8 +++++--- README.md | 26 +++++++++++++------------- src/logger.rs | 28 ++++++++++++++++++++++++++++ src/main.rs | 44 ++++++++++++-------------------------------- 5 files changed, 84 insertions(+), 61 deletions(-) create mode 100644 src/logger.rs diff --git a/Cargo.lock b/Cargo.lock index 53fbce3..45bd2e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,7 +36,7 @@ dependencies = [ "crossbeam-channel 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", "futures-preview 0.3.0-alpha.18 (registry+https://github.com/rust-lang/crates.io-index)", "futures-timer 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", @@ -139,7 +139,7 @@ version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -294,7 +294,7 @@ dependencies = [ [[package]] name = "lazy_static" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -449,18 +449,18 @@ dependencies = [ [[package]] name = "regex" -version = "1.2.1" +version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "regex-syntax 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)", "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "regex-syntax" -version = "0.6.11" +version = "0.6.12" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -509,7 +509,17 @@ name = "thread_local" version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "time" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", + "redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -519,13 +529,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "updns" -version = "0.0.3" +version = "0.0.4" dependencies = [ "ace 0.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "async-std 0.99.4 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.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.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -602,7 +614,7 @@ dependencies = [ "checksum futures-util-preview 0.3.0-alpha.18 (registry+https://github.com/rust-lang/crates.io-index)" = "7df53daff1e98cc024bf2720f3ceb0414d96fbb0a94f3cad3a5c3bf3be1d261c" "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" -"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" +"checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" "checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba" "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" "checksum memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88579771288728879b57485cc7d6b07d648c9f0141eb955f8ab7f9d45394468e" @@ -621,14 +633,15 @@ dependencies = [ "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" "checksum redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)" = "2439c63f3f6139d1b57529d16bc3b8bb855230c8efcc5d3a896c8bea7c3b1e84" "checksum redox_users 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ecedbca3bf205f8d8f5c2b44d83cd0690e39ee84b951ed649e9f1841132b66d" -"checksum regex 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "88c3d9193984285d544df4a30c23a4e62ead42edf70a4452ceb76dac1ce05c26" -"checksum regex-syntax 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "b143cceb2ca5e56d5671988ef8b15615733e7ee16cd348e064333b251b89343f" +"checksum regex 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dc220bd33bdce8f093101afe22a037b8eb0e5af33592e6a9caafff0d4cb81cbd" +"checksum regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "11a7e20d1cce64ef2fed88b66d347f88bd9babb82845b2b858f3edbf59a4f716" "checksum rust-argon2 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "4ca4eaef519b494d1f2848fc602d18816fed808a981aedf4f1f00ceb7c9d32cf" "checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" "checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f" "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 unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" diff --git a/Cargo.toml b/Cargo.toml index 981a945..39de61f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "updns" -version = "0.0.3" +version = "0.0.4" edition = "2018" authors = ["wyhaya "] @@ -21,5 +21,7 @@ keywords = [ ace = "0.0.2" async-std = "0.99.4" dirs = "2.0.2" -lazy_static = "1.3.0" -regex = "1.2.1" \ No newline at end of file +lazy_static = "1.4.0" +log = {version = "0.4.8", features = ["release_max_level_info"]} +regex = "1.3.1" +time = "0.1.42" \ No newline at end of file diff --git a/README.md b/README.md index 16a1bce..00125d6 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,19 @@ You may use `sudo` to run this command because you will use the `53` port, make Now change your local DNS server to `127.0.0.1` 🚀 -### Command +## Running in docker + +Build docker image +```bash +docker build -t updns . +``` + +Start up +```bash +docker run -d --name updns -p 53:53/udp -v /root/updns/:/root/.updns/ --restart always updns +``` + +## Command ``` Usage: @@ -51,18 +63,6 @@ Option: -c Specify a config file ``` -## Running in docker - -Build docker image -```bash -docker build -t updns . -``` - -Start up -```bash -docker run -d --name updns -p 53:53/udp -v /root/updns/:/root/.updns/ --restart always updns -``` - ## Config You can use `updns config` command and then call `vim` quick edit, or use `updns path` find the updns's installation directory and edit the `config` file diff --git a/src/logger.rs b/src/logger.rs new file mode 100644 index 0000000..48e34e0 --- /dev/null +++ b/src/logger.rs @@ -0,0 +1,28 @@ +use log::{Level, LevelFilter, Metadata, Record, SetLoggerError}; + +static LOGGER: Logger = Logger; + +pub fn init() -> Result<(), SetLoggerError> { + log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Trace)) +} + +struct Logger; + +impl log::Log for Logger { + fn enabled(&self, meta: &Metadata) -> bool { + meta.level() != Level::Trace + } + + fn log(&self, record: &Record) { + if self.enabled(record.metadata()) { + println!( + "{} {}: {}", + time::now().strftime("[%Y-%m-%d][%H:%M:%S]").unwrap(), + record.level(), + record.args() + ); + } + } + + fn flush(&self) {} +} diff --git a/src/main.rs b/src/main.rs index 16494d1..a5ceaab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,9 @@ -#![feature(const_vec_new)] - #[macro_use] extern crate lazy_static; mod config; mod lib; +mod logger; mod watch; use ace::App; @@ -14,6 +13,7 @@ use async_std::task; use config::{Config, Hosts, Invalid, InvalidType}; use dirs; use lib::*; +use log::{error, info, warn}; use regex::Regex; use std::env; use std::net::{IpAddr, SocketAddr}; @@ -30,30 +30,9 @@ const WATCH_INTERVAL: u64 = 3000; static mut PROXY: Vec = Vec::new(); static mut HOSTS: Option = None; -macro_rules! log { - ($($arg:tt)*) => { - println!($($arg)*); - }; -} - -macro_rules! warn { - ($($arg:tt)*) => { - print!("\x1B[{}m{}\x1B[0m", "1;33", "warning: "); - println!($($arg)*); - }; -} - -macro_rules! error { - ($($arg:tt)*) => { - eprint!("\x1B[{}m{}\x1B[0m", "1;31", "error: "); - eprintln!($($arg)*); - }; -} - macro_rules! exit { ($($arg:tt)*) => { { - eprint!("\x1B[{}m{}\x1B[0m", "1;31", "error: "); eprintln!($($arg)*); std::process::exit(1) } @@ -61,6 +40,7 @@ macro_rules! exit { } fn main() { + let _ = logger::init(); let app = App::new(env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")) .cmd("add", "Add a DNS record") .cmd("rm", "Remove a DNS record") @@ -105,7 +85,7 @@ fn main() { let mut config = match Config::new(&config_path) { Ok(c) => c, - Err(err) => exit!("Failed to read config file: {:?}\n{:?}", &config_path, err), + Err(err) => exit!("Failed to read config file {:?}\n{:?}", &config_path, err), }; if let Err(err) = config.add(&values[0], &values[1]) { exit!("Add record failed\n{:?}", err); @@ -117,7 +97,7 @@ fn main() { exit!("'rm' value: [DOMAIN | IP]"); } match ask("Confirm delete? Y/N\n") { - Ok(_) => log!("todo"), + Ok(_) => println!("todo"), Err(err) => exit!("{:?}", err), } } @@ -131,7 +111,7 @@ fn main() { } } for (domain, ip) in hosts.iter() { - log!("{:domain$} {}", domain.as_str(), ip, domain = n); + println!("{:domain$} {}", domain.as_str(), ip, domain = n); } } "config" => { @@ -152,7 +132,7 @@ fn main() { Ok(p) => p.display().to_string(), Err(err) => exit!("Failed to get directory\n{:?}", err), }; - log!( + println!( "Binary: {}\nConfig: {}", binary, config_path.to_string_lossy() @@ -225,7 +205,7 @@ fn update_config(mut proxy: Vec, hosts: Hosts) { fn config_parse(file: &PathBuf) -> (Config, Vec, Vec, Hosts) { let mut config = match Config::new(file) { Ok(c) => c, - Err(err) => exit!("Failed to read config file: {:?}\n{:?}", file, err), + Err(err) => exit!("Failed to read config file {:?}\n{:?}", file, err), }; let (binds, proxy, hosts, errors) = match config.parse() { @@ -247,7 +227,7 @@ fn output_invalid(errors: Vec) { InvalidType::Other => "Invalid line", }; warn!("{}", msg); - log!("Line {}: {}", invalid.line, invalid.source); + info!("Line {}: {}", invalid.line, invalid.source); } } } @@ -256,7 +236,7 @@ async fn watch_config(p: PathBuf) { let mut watch = Watch::new(p, WATCH_INTERVAL); watch .for_each(|c| { - log!("Reload the configuration file: {:?}", &c); + info!("Reload the configuration file: {:?}", &c); if let Ok(mut config) = Config::new(c) { if let Ok((_, proxy, hosts, errors)) = config.parse() { update_config(proxy, hosts); @@ -270,7 +250,7 @@ async fn watch_config(p: PathBuf) { async fn run_server(addr: SocketAddr) { let socket = match UdpSocket::bind(&addr).await { Ok(socket) => { - log!("Start listening to '{}'", addr); + info!("Start listening to '{}'", addr); socket } Err(err) => exit!("Binding '{}' failed\n{:?}", addr, err), @@ -364,7 +344,7 @@ async fn handle(mut req: BytePacketBuffer, len: usize) -> io::Result> { None => return proxy(&req.buf[..len]).await, }; - log!("Query: {} Type: {:?}", query.name, query.qtype); + info!("{} {:?}", query.name, query.qtype); // Whether to proxy let answer = match get_answer(&query.name, query.qtype) {