From 73f80c1eb7c4b82cd444fd0c93ce40f80efc3595 Mon Sep 17 00:00:00 2001 From: James ABIB Date: Sun, 3 Dec 2023 13:45:29 +0100 Subject: [PATCH] feat(logger): implement env_logger and server logger usage --- README.md | 28 ++++++++++++++++++++++++++++ client/Cargo.toml | 1 + server/Cargo.toml | 1 + server/src/main.rs | 7 +++++-- server/src/services/reader.rs | 6 ++++-- server/src/services/server_runner.rs | 14 +++++++++----- shared/Cargo.toml | 2 ++ shared/src/lib.rs | 1 + shared/src/logger/mod.rs | 5 +++++ 9 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 shared/src/logger/mod.rs diff --git a/README.md b/README.md index f0fc945..45cbebb 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,34 @@ To run the server with CLI, use the following command: cargo run -p server -- -h ``` +## Using debug log level + +To use the debug log level, use the following command: + +### Unix/Linux/MacOS + +```bash +RUST_LOG=debug cargo run -p client +RUST_LOG=debug cargo run -p server +``` + +### Windows + +#### CMD + +```bash +set RUST_LOG=debug +cargo run -p client +cargo run -p server +``` + +#### PowerShell + +```bash +$env:RUST_LOG="debug"; cargo run -p client +$env:RUST_LOG="debug"; cargo run -p server +``` + ## Documentation To generate documentation for all packages without including dependencies (recommended): diff --git a/client/Cargo.toml b/client/Cargo.toml index cfec729..858956a 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -8,6 +8,7 @@ edition = "2021" [dependencies] image = { version = "0.24.7", features = [] } +log = "0.4.20" complex = { path = "../complex" } shared = { path = "../shared" } server = { path = "../server" } diff --git a/server/Cargo.toml b/server/Cargo.toml index 09ac827..b1e6fe8 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -12,6 +12,7 @@ shared = { path = "../shared" } cli = { path = "../cli" } serde = { version = "1.0.193", features = ["derive"] } serde_json ="1.0.108" +log = "0.4.20" [lib] path = "src/lib.rs" diff --git a/server/src/main.rs b/server/src/main.rs index 0378eeb..0d4f734 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -2,14 +2,17 @@ use cli::{ operation::parse_to_address, parser::{CliArgs, CliServerArgs, Parser}, }; +use log::{info, error}; use server::services::server_runner::run_server; fn main() -> std::io::Result<()> { + shared::logger::init_logger(); + let cli_args: CliArgs = CliArgs::Server(CliServerArgs::parse()); let address = parse_to_address(cli_args); match run_server(address.as_str()) { - Ok(_) => println!("[SERVER] Server stopped."), - Err(e) => println!("[SERVER] Server stopped with error: {}", e), + Ok(_) => info!("Server stopped successfully!"), + Err(_e) => error!("Could not start the server"), } Ok(()) diff --git a/server/src/services/reader.rs b/server/src/services/reader.rs index cfde831..b1c01d4 100644 --- a/server/src/services/reader.rs +++ b/server/src/services/reader.rs @@ -4,6 +4,8 @@ use std::{ time::Duration, }; +use log::debug; + /// Reads a message from a TCP stream, parsing and returning the JSON component. /// /// This function first reads the message size and JSON size from the stream. @@ -63,14 +65,14 @@ pub fn read_message(stream: &mut TcpStream) -> io::Result { Err(e) => return Err(io::Error::new(io::ErrorKind::InvalidData, e)), }; - println!("Réponse JSON du serveur: {}", json_str); + debug!("Réponse JSON du serveur: {}", json_str); // donnée supplémentaire en binaire let binary_data_size = total_size - json_size; if binary_data_size > 0 { let mut binary_buffer = vec![0; binary_data_size]; stream.read_exact(&mut binary_buffer)?; - println!("Données binaires reçues: {:?}", binary_buffer); + debug!("Données binaires reçues: {:?}", binary_buffer); } Ok(json_str) diff --git a/server/src/services/server_runner.rs b/server/src/services/server_runner.rs index 8d837e4..eee1633 100644 --- a/server/src/services/server_runner.rs +++ b/server/src/services/server_runner.rs @@ -1,3 +1,5 @@ +use log::{info, error}; + use crate::messages::handler::handle_client; use std::net::TcpListener; @@ -28,12 +30,12 @@ use std::net::TcpListener; /// } /// ``` pub fn run_server(address: &str) -> std::io::Result<()> { - println!("[SERVER] - Starting server on {}", address); + info!("Starting server on {}", address); let listener = match TcpListener::bind(address) { Ok(listener) => listener, Err(e) => { - println!("[SERVER] - Failed to bind to address: {}", e); - return Ok(()); + error!("Failed to bind to address: {}", e); + return Err(e); } }; @@ -46,6 +48,8 @@ pub fn run_server(address: &str) -> std::io::Result<()> { #[cfg(test)] mod server_runner_tests { + use log::error; + use super::*; use std::net::TcpStream; use std::thread; @@ -61,8 +65,8 @@ mod server_runner_tests { thread::sleep(Duration::from_millis(100)); match TcpStream::connect(address) { - Ok(_) => println!("Successfully connected to server"), - Err(e) => panic!("Failed to connect to server: {}", e), + Ok(_) => info!("Successfully connected to server"), + Err(e) => error!("Failed to connect to server: {}", e), } } } diff --git a/shared/Cargo.toml b/shared/Cargo.toml index 3b42f19..645c50e 100644 --- a/shared/Cargo.toml +++ b/shared/Cargo.toml @@ -12,6 +12,8 @@ path = "src/lib.rs" rand = "0.8.5" serde = { version = "1.0.193", features = ["derive"] } serde_json ="1.0.108" +env_logger = "0.10.1" +log = "0.4.20" [dev-dependencies] tempfile = "3.8.1" diff --git a/shared/src/lib.rs b/shared/src/lib.rs index b4ab6a6..764b25a 100644 --- a/shared/src/lib.rs +++ b/shared/src/lib.rs @@ -1,2 +1,3 @@ pub mod types; pub mod utils; +pub mod logger; diff --git a/shared/src/logger/mod.rs b/shared/src/logger/mod.rs new file mode 100644 index 0000000..419169a --- /dev/null +++ b/shared/src/logger/mod.rs @@ -0,0 +1,5 @@ +pub fn init_logger() { + env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")) + .format_timestamp(Some(env_logger::TimestampPrecision::Millis)) + .init(); +} \ No newline at end of file