From 253e9d90ce36e8258fc336160a5fcab8f05d6e2a Mon Sep 17 00:00:00 2001 From: TQ Hirsch Date: Sun, 19 Nov 2023 21:32:32 +0100 Subject: [PATCH] Enabled log output --- Cargo.lock | 7 +++++++ Cargo.toml | 3 ++- src/handler.rs | 4 ++++ src/main.rs | 17 +++++++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 1851b69..376bec1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -635,6 +635,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "0.14.27" @@ -1289,6 +1295,7 @@ dependencies = [ "async-tftp", "fern", "futures", + "humantime", "libc", "listenfd", "log", diff --git a/Cargo.toml b/Cargo.toml index 1fb8503..8fa014a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,5 @@ mlua = { version = "0.9.1", features = ["luau-jit", "vendored", "async", "send"] reqwest = { version = "0.11.22", features = ["stream"] } listenfd = "1.0.1" libc = "0.2.150" -users = "0.11.0" \ No newline at end of file +users = "0.11.0" +humantime = "2.1.0" \ No newline at end of file diff --git a/src/handler.rs b/src/handler.rs index 1b34897..df966e3 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -6,6 +6,7 @@ use async_tftp::async_trait; use async_tftp::packet::Error; use async_tftp::server::handlers::{DirHandler, DirHandlerMode}; use futures::{AsyncRead, AsyncWrite, TryStreamExt}; +use log::info; use reqwest::{Body, StatusCode}; use tokio_util::compat::TokioAsyncWriteCompatExt; use crate::engine::{Client, Engine, Resource}; @@ -44,6 +45,8 @@ impl async_tftp::server::Handler for Handler { // .to_str().ok_or(Error::FileNotFound)?.to_owned() let resource: Resource = self.engine.resolve(path.to_owned(), &mut lua_client, None).await?; + info!("GET {path:?} from {client:?} -> {resource}"); + match resource { Resource::Http(url) => { // TODO: Add headers describing client @@ -83,6 +86,7 @@ impl async_tftp::server::Handler for Handler { for_write: false, }; let resource: Resource = self.engine.resolve(path.to_owned(), &mut lua_client, size).await?; + info!("PUT {path:?} from {client:?} -> {resource}"); match resource { Resource::Http(url) => { diff --git a/src/main.rs b/src/main.rs index 93c5749..c084445 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use std::ffi::c_void; use std::net; use std::net::{Ipv4Addr, Ipv6Addr}; use std::path::{Path, PathBuf}; +use std::time::SystemTime; use anyhow::anyhow; use structopt::StructOpt; use tokio::signal::unix::SignalKind; @@ -36,6 +37,22 @@ struct Options { async fn main() -> anyhow::Result<()> { let opts = Options::from_args(); + // Configure logging + fern::Dispatch::new() + .format(|out, message, record| { + out.finish(format_args!( + "[{} {} {}] {}", + humantime::format_rfc3339_seconds(SystemTime::now()), + record.level(), + record.target(), + message + )) + }) + .level(log::LevelFilter::Debug) + .chain(std::io::stdout()) + .chain(fern::log_file("output.log")?) + .apply()?; + let group = opts.group.map(|name| { if let Ok(gid) = libc::gid_t::from_str_radix(name.as_str(), 10) { Ok(gid)