diff --git a/Cargo.lock b/Cargo.lock index 32b8d52..8875593 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,15 +2,109 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "env_logger" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" +dependencies = [ + "atty", + "log", + "termcolor", +] + +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "httparse" version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acd94fdbe1d4ff688b67b04eee2e17bd50995534a61539e45adfefb45e5e5503" +[[package]] +name = "libc" +version = "0.2.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b2f96d100e1cf1929e7719b7edb3b90ab5298072638fccd77be9ce942ecdfce" + +[[package]] +name = "log" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" +dependencies = [ + "cfg-if", +] + [[package]] name = "myip" version = "0.2.0" dependencies = [ + "env_logger", "httparse", + "log", +] + +[[package]] +name = "termcolor" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", ] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 678d6b4..e2e6487 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,8 @@ edition = "2018" [dependencies] httparse = "1.5.1" +log = "0.4" +env_logger = { version = "0.9", default-features = false, features = [ + "atty", + "termcolor", +] } diff --git a/myip.service b/myip.service index 552141a..f9984d9 100644 --- a/myip.service +++ b/myip.service @@ -3,7 +3,11 @@ Description=Replies with peer's IP on port 7878 After=network.target [Service] +# Change it to OFF|ERROR|WARN|INFO|DEBUG|TRACE +Environment="RUST_LOG=INFO" + ExecStart=/usr/local/bin/myip +Environment="RUST_LOG_STYLE=SYSTEMD" ProtectProc=invisible DynamicUser=true diff --git a/src/bin/main.rs b/src/bin/main.rs index 7c6f168..07b6140 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,11 +1,34 @@ use httparse; +use log::{error, info, trace}; use std::io::prelude::*; use std::net::{TcpListener, TcpStream}; use myip::ThreadPool; fn main() { - println!("Replying with most probable peer's ip on port 7878"); + let log_env = env_logger::Env::new().filter_or("MY_LOG_LEVEL", "trace"); + match std::env::var("RUST_LOG_STYLE") { + Ok(s) if s == "SYSTEMD" => env_logger::builder() + .format(|buf, record| { + writeln!( + buf, + "<{}>{}: {}", + match record.level() { + log::Level::Error => 3, + log::Level::Warn => 4, + log::Level::Info => 6, + log::Level::Debug => 7, + log::Level::Trace => 7, + }, + record.target(), + record.args() + ) + }) + .init(), + _ => env_logger::init_from_env(log_env), + }; + + info!("Replying with most probable peer's ip on port 7878"); let listener = TcpListener::bind("127.0.0.1:7878").unwrap(); let pool = ThreadPool::new(4); @@ -36,7 +59,7 @@ fn handle_connection(mut stream: TcpStream) { .to_string(), ); - // println!("Request: {}", String::from_utf8_lossy(&buffer[..])); + trace!("Request: {}", String::from_utf8_lossy(&buffer[..])); let (status_line, contents) = if buffer.starts_with(get) { let mut headers = [httparse::EMPTY_HEADER; 30]; @@ -49,7 +72,7 @@ fn handle_connection(mut stream: TcpStream) { ("HTTP/1.1 200 OK", peer_ip) } Err(e) => { - eprintln!("{:?}", e); + error!("{:?}", e); err404 } } diff --git a/src/lib.rs b/src/lib.rs index 6e9cb5a..597b535 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,8 @@ use std::{ thread, }; +use log::{debug, info, trace}; + pub struct ThreadPool { workers: Vec, sender: mpsc::Sender, @@ -43,16 +45,16 @@ impl ThreadPool { impl Drop for ThreadPool { fn drop(&mut self) { - println!("Sending terminate message to all workers."); + info!("Sending terminate message to all workers."); for _ in &self.workers { // Panic is an acceptable, if we cannot shut down cleanly self.sender.send(Message::Terminate).unwrap(); } - println!("Shutting down all workers."); + info!("Shutting down all workers."); for worker in &mut self.workers { - println!("Shutting down worker {}", worker.id); + debug!("Shutting down worker {}", worker.id); if let Some(thread) = worker.thread.take() { // Panic is an acceptable, if we cannot shut down cleanly @@ -85,12 +87,12 @@ impl Worker { match message { Message::NewJob(job) => { - println!("Worker {} got a job; executing.", id); + trace!("Worker {} got a job; executing.", id); job(); } Message::Terminate => { - println!("Worker {} was told to terminate.", id); + debug!("Worker {} was told to terminate.", id); break; } }