Skip to content

Commit 91deee5

Browse files
committed
Add simple logging using env_logger
1 parent 17cb434 commit 91deee5

File tree

5 files changed

+222
-10
lines changed

5 files changed

+222
-10
lines changed

Cargo.lock

Lines changed: 194 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ serde = { version = "1.0.219", features = ["derive"] }
1414
serde_json = "1.0.140"
1515
serde_yaml = "0.9.34"
1616
base64 = "0.22.1"
17+
env_logger = "0.11.8"
18+
log = "0.4.27"

src/config.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use base64::{engine::general_purpose as base64_engine, Engine as _};
2+
use log::{error, info};
23
use serde::{Deserialize, Serialize};
34
use std::{fs, io, path::Path};
45

@@ -7,6 +8,7 @@ use crate::models::{Players, StatusResponse, Version};
78
#[derive(Clone, Serialize, Deserialize)]
89
#[serde(default)]
910
pub struct Config {
11+
pub debug: bool,
1012
pub host: String,
1113
pub port: u16,
1214
pub status: StatusResponse,
@@ -16,6 +18,7 @@ pub struct Config {
1618
impl Default for Config {
1719
fn default() -> Self {
1820
Config {
21+
debug: false,
1922
host: "127.0.0.1".to_string(),
2023
port: 25565,
2124
status: StatusResponse {
@@ -37,7 +40,7 @@ impl Default for Config {
3740
impl Config {
3841
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, io::Error> {
3942
if !path.as_ref().exists() {
40-
println!("Config file not found, using default configuration.");
43+
info!("Config file not found, using default configuration.");
4144
return Ok(Config::default());
4245
}
4346

@@ -50,7 +53,7 @@ impl Config {
5053
match Self::load_favicon_as_base64(favicon) {
5154
Ok(favicon_base64) => config.status.favicon = Some(favicon_base64),
5255
Err(e) => {
53-
eprintln!("Error loading favicon: {}", e);
56+
error!("Error loading favicon: {}", e);
5457
config.status.favicon = None;
5558
}
5659
}

src/main.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod protocol;
44
mod server;
55

66
use config::Config;
7+
use log::{debug, info};
78
use std::{env, io};
89
use tokio::net::TcpListener;
910

@@ -17,23 +18,27 @@ async fn main() -> io::Result<()> {
1718
}
1819
};
1920

20-
let listener = TcpListener::bind("0.0.0.0:25565").await?;
21-
println!("Server listening on port 25565");
21+
env_logger::init_from_env(
22+
env_logger::Env::default().default_filter_or(if !config.debug { "info" } else { "debug" }),
23+
);
24+
25+
let listener = TcpListener::bind(format!("{}:{}", config.host, config.port)).await?;
26+
info!("Server listening on port {}", config.port);
2227

2328
loop {
2429
match listener.accept().await {
2530
Ok((stream, addr)) => {
26-
println!("New connection from: {}", addr);
31+
debug!("New connection from: {}", addr);
2732

2833
let config = config.clone();
2934
tokio::spawn(async move {
3035
if let Err(e) = server::handle_client(stream, config).await {
31-
eprintln!("Error handling client {}: {}", addr, e);
36+
debug!("Error handling client {}: {}", addr, e);
3237
}
3338
});
3439
}
3540
Err(e) => {
36-
eprintln!("Error accepting connection: {}", e);
41+
debug!("Error accepting connection: {}", e);
3742
}
3843
}
3944
}

0 commit comments

Comments
 (0)