From ae260cea85d6f95122d8642fc322e31db08d33e7 Mon Sep 17 00:00:00 2001 From: Cappy Ishihara Date: Sun, 2 Jun 2024 04:11:18 +0700 Subject: [PATCH] Change logging backend to tracing --- Cargo.lock | 34 ---------------------------------- Cargo.toml | 8 ++++---- src/auth.rs | 17 +++++++++++++---- src/main.rs | 6 ++++-- src/profile.rs | 10 +++++----- src/ws/handler.rs | 2 +- 6 files changed, 27 insertions(+), 50 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e03e83e..90b71fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -243,17 +243,6 @@ dependencies = [ "windows-targets 0.52.5", ] -[[package]] -name = "colored" -version = "1.9.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a5f741c91823341bebf717d4c71bda820630ce065443b58bd1b7451af008355" -dependencies = [ - "is-terminal", - "lazy_static", - "winapi", -] - [[package]] name = "core-foundation" version = "0.9.4" @@ -349,16 +338,6 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" -[[package]] -name = "fern" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9f0c14694cbd524c8720dd69b0e3179344f04ebb5f90f2e4a440c6ea3b2f1ee" -dependencies = [ - "colored", - "log", -] - [[package]] name = "fnv" version = "1.0.7" @@ -652,17 +631,6 @@ version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" -[[package]] -name = "is-terminal" -version = "0.4.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b" -dependencies = [ - "hermit-abi", - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "itoa" version = "1.0.11" @@ -1174,9 +1142,7 @@ dependencies = [ "base64 0.22.1", "chrono", "dashmap", - "fern", "hex", - "log", "rand", "reqwest", "ring", diff --git a/Cargo.toml b/Cargo.toml index 3716d88..b0e4df3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,9 @@ publish = false [dependencies] # Logging -log = "0.4.21" -fern = { version = "0.6.2", features = ["colored"] } +tracing-subscriber = { version = "0.3.18", features = ["env-filter", "chrono"] } +tracing = "0.1.40" +# fern = { version = "0.6.2", features = ["colored"] } # Errors handelers anyhow = "1.0.83" @@ -35,8 +36,7 @@ rand = "0.8.5" axum = { version = "0.7.5", features = ["ws", "macros", "http2"] } tower-http = { version = "0.5.2", features = ["trace"] } tokio = { version = "1.37.0", features = ["full"] } -tracing-subscriber = { version = "0.3.18", features = ["env-filter", "chrono"] } -tracing = "0.1.40" + # TODO: Sort it! # TODO: Replace Vec and &[u8] by Bytes diff --git a/src/auth.rs b/src/auth.rs index c06085b..e9ed076 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -8,9 +8,9 @@ use axum::{ routing::get, Router, }; -use log::{debug, info, trace}; use ring::digest::{self, digest}; use serde::Deserialize; +use tracing::{debug, info, trace}; use uuid::Uuid; use crate::AppState; @@ -108,7 +108,7 @@ where .headers .get("token") .and_then(|value| value.to_str().ok()); - trace!("[Extractor Token] Data: {token:?}"); + trace!(token = ?token); match token { Some(token) => Ok(Self(Some(token.to_string()))), None => Ok(Self(None)), @@ -134,6 +134,15 @@ impl ToString for AuthSystem { } } +/// Get UUID from JSON response +// Written to be reusable so we don't have to specify the same complex code twice +#[inline] +fn get_id_json(json: &serde_json::Value) -> anyhow::Result { + trace!("json: {json:#?}"); // For debugging, we'll get to this later! + let uuid = Uuid::parse_str(json.get("id").unwrap().as_str().unwrap())?; + Ok(uuid) +} + pub async fn has_joined( server_id: &str, username: &str, @@ -147,7 +156,7 @@ pub async fn has_joined( match res.status().as_u16() { 200 => { let json = serde_json::from_str::(&res.text().await?)?; - let uuid = Uuid::parse_str(json["id"].as_str().unwrap())?; + let uuid = get_id_json(&json)?; Ok(Some((uuid, AuthSystem::ElyBy))) }, 401 => Ok(None), @@ -161,7 +170,7 @@ pub async fn has_joined( match res.status().as_u16() { 200 => { let json = serde_json::from_str::(&res.text().await?)?; - let uuid = Uuid::parse_str(json["id"].as_str().unwrap())?; + let uuid = get_id_json(&json)?; Ok(Some((uuid, AuthSystem::Mojang))) }, 204 => Ok(None), diff --git a/src/main.rs b/src/main.rs index 9971330..35df3a1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,10 +5,10 @@ use axum::{ Router, }; use dashmap::DashMap; -use log::info; use std::sync::Arc; use tokio::sync::{broadcast, Mutex}; use tower_http::trace::TraceLayer; +use tracing::info; use uuid::Uuid; // WebSocket worker @@ -97,7 +97,9 @@ pub struct AppState { #[tokio::main] async fn main() -> Result<()> { tracing_subscriber::fmt::fmt() - .with_env_filter("trace,axum=info,tower_http=info,tokio=info,tungstenite=info,tokio_tungstenite=info") + .with_env_filter( + "trace,axum=info,tower_http=info,tokio=info,tungstenite=info,tokio_tungstenite=info", + ) .pretty() .init(); diff --git a/src/profile.rs b/src/profile.rs index 1677555..8f5dc68 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -5,7 +5,7 @@ use axum::{ extract::{Path, State}, Json, }; -use log::{debug, warn}; +use tracing::{debug, warn}; use serde_json::{json, Value}; use tokio::{ fs, @@ -25,7 +25,7 @@ pub async fn user_info( Path(uuid): Path, State(state): State, // FIXME: Variable doesn't using! ) -> Json { - log::info!("Receiving profile information for {}", uuid); + tracing::info!("Receiving profile information for {}", uuid); let formatted_uuid = format_uuid(&uuid); @@ -88,7 +88,7 @@ pub async fn user_info( #[debug_handler] pub async fn download_avatar(Path(uuid): Path) -> Result> { let uuid = format_uuid(&uuid); - log::info!("Requesting an avatar: {}", uuid); + tracing::info!("Requesting an avatar: {}", uuid); let mut file = if let Ok(file) = fs::File::open(format!("avatars/{}.moon", uuid)).await { file } else { @@ -118,7 +118,7 @@ pub async fn upload_avatar( }; if let Some(user_info) = state.authenticated.get(&token) { - log::info!( + tracing::info!( "{} ({}) trying to upload an avatar", user_info.uuid, user_info.username @@ -152,7 +152,7 @@ pub async fn delete_avatar(Token(token): Token, State(state): State) - None => http_error_ret!(UNAUTHORIZED, "Authentication error!"), }; if let Some(user_info) = state.authenticated.get(&token) { - log::info!( + tracing::info!( "{} ({}) is trying to delete the avatar", user_info.uuid, user_info.username diff --git a/src/ws/handler.rs b/src/ws/handler.rs index 0028619..28a5eb3 100644 --- a/src/ws/handler.rs +++ b/src/ws/handler.rs @@ -8,7 +8,7 @@ use axum::{ response::Response, }; use dashmap::DashMap; -use log::{debug, error, info, trace, warn}; +use tracing::{debug, error, info, trace, warn}; use tokio::sync::{ broadcast::{self, Receiver}, mpsc, Notify,