commit b33417198c11400c8d776fd25e508907ac1b6155
parent d13c98d614233f0b31d023db457abb640b7604fb
Author: Tronje Krabbe <hi@tron.je>
Date: Mon, 9 Nov 2020 16:52:46 +0100
Add some simple logging via the env_logger crate
Diffstat:
4 files changed, 99 insertions(+), 2 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -6,6 +6,8 @@ version = "1.2.2"
dependencies = [
"async-std",
"async-tls",
+ "env_logger",
+ "log",
"mime_guess",
"once_cell",
"rustls",
@@ -13,6 +15,15 @@ dependencies = [
]
[[package]]
+name = "aho-corasick"
+version = "0.7.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
name = "async-channel"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -128,6 +139,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a"
[[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 = "autocfg"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -198,6 +220,19 @@ dependencies = [
]
[[package]]
+name = "env_logger"
+version = "0.8.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "54532e3223c5af90a6a757c90b5c5521564b07e5e7a958681bcd2afad421cdcd"
+dependencies = [
+ "atty",
+ "humantime",
+ "log",
+ "regex",
+ "termcolor",
+]
+
+[[package]]
name = "event-listener"
version = "2.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -268,6 +303,12 @@ dependencies = [
]
[[package]]
+name = "humantime"
+version = "2.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3c1ad908cc71012b7bea4d0c53ba96a8cba9962f048fa68d143376143d863b7a"
+
+[[package]]
name = "idna"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -427,6 +468,24 @@ dependencies = [
]
[[package]]
+name = "regex"
+version = "1.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "38cf2c13ed4745de91a5eb834e11c00bcc3709e773173b2ce4c56c9fbde04b9c"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+ "thread_local",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3b181ba2dcf07aaccad5448e8ead58db5b742cf85dfe035e2227f137a539a189"
+
+[[package]]
name = "ring"
version = "0.16.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -488,6 +547,24 @@ dependencies = [
]
[[package]]
+name = "termcolor"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
+dependencies = [
+ "winapi-util",
+]
+
+[[package]]
+name = "thread_local"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
+dependencies = [
+ "lazy_static",
+]
+
+[[package]]
name = "tinyvec"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -673,6 +750,15 @@ 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"
diff --git a/Cargo.toml b/Cargo.toml
@@ -17,6 +17,8 @@ mime_guess = "2.0"
once_cell = "1.4"
rustls = "0.18.0"
url = "2.1"
+log = "0.4"
+env_logger = "0.8"
[profile.release]
lto = true
diff --git a/README.md b/README.md
@@ -35,6 +35,12 @@ agate 0.0.0.0:1965 path/to/content/ cert.pem key.rsa
When a client requests the URL `gemini://example.com/foo/bar`, Agate will respond with the file at `path/to/content/foo/bar`. If there is a directory at that path, Agate will look for a file named `index.gmi` inside that directory.
+Optionally, set a log level via the `AGATE_LOG` environment variable. Logging is powered by the [env_logger crate](https://crates.io/crates/env_logger):
+
+```
+AGATE_LOG=info 0.0.0.0:1965 path/to.content/ cert.pem key.rsa
+```
+
[Gemini]: https://gemini.circumlunar.space/
[Rust]: https://www.rust-lang.org/
[home]: gemini://gem.limpet.net/agate/
diff --git a/src/main.rs b/src/main.rs
@@ -6,13 +6,15 @@ use std::{error::Error, ffi::OsStr, fs::File, io::BufReader, marker::Unpin, sync
use url::Url;
fn main() -> Result {
+ env_logger::Builder::from_env("AGATE_LOG").init();
task::block_on(async {
let listener = TcpListener::bind(&ARGS.sock_addr).await?;
let mut incoming = listener.incoming();
+ log::info!("Listening on {}...", ARGS.sock_addr);
while let Some(Ok(stream)) = incoming.next().await {
task::spawn(async {
if let Err(e) = handle_request(stream).await {
- eprintln!("Error: {:?}", e);
+ log::error!("{:?}", e);
}
});
}
@@ -110,7 +112,7 @@ async fn parse_request<R: Read + Unpin>(stream: &mut R) -> Result<Url> {
if url.scheme() != "gemini" {
Err("unsupported URL scheme")?
}
- eprintln!("Got request for {:?}", url);
+ log::info!("Got request for {:?}", url);
Ok(url)
}
@@ -146,6 +148,7 @@ async fn send_response<W: Write + Unpin>(url: Url, stream: &mut W) -> Result {
}
async fn respond<W: Write + Unpin>(stream: &mut W, status: &str, meta: &[&str]) -> Result {
+ log::info!("Responding with status {} and meta {:?}", status, meta);
stream.write_all(status.as_bytes()).await?;
stream.write_all(b" ").await?;
for m in meta {