Skip to content

Commit

Permalink
feat(logger): implement env_logger and server logger usage
Browse files Browse the repository at this point in the history
  • Loading branch information
jabibamman committed Dec 3, 2023
1 parent 63176e2 commit 73f80c1
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 9 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
7 changes: 5 additions & 2 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
6 changes: 4 additions & 2 deletions server/src/services/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -63,14 +65,14 @@ pub fn read_message(stream: &mut TcpStream) -> io::Result<String> {
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)
Expand Down
14 changes: 9 additions & 5 deletions server/src/services/server_runner.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use log::{info, error};

use crate::messages::handler::handle_client;
use std::net::TcpListener;

Expand Down Expand Up @@ -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);
}
};

Expand All @@ -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;
Expand All @@ -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),
}
}
}
2 changes: 2 additions & 0 deletions shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod types;
pub mod utils;
pub mod logger;
5 changes: 5 additions & 0 deletions shared/src/logger/mod.rs
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 73f80c1

Please sign in to comment.