mirror of
https://github.com/timvisee/lazymc.git
synced 2025-05-18 20:30:23 -07:00
Add proper logging
This commit is contained in:
parent
fe9f5dc936
commit
1de68e7335
1
.env.example
Normal file
1
.env.example
Normal file
@ -0,0 +1 @@
|
||||
RUST_LOG=info
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
.env
|
||||
/target
|
||||
|
||||
# Test server
|
||||
|
102
Cargo.lock
generated
102
Cargo.lock
generated
@ -8,6 +8,26 @@ version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "0.7.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "0.1.7"
|
||||
@ -62,6 +82,25 @@ dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dotenv"
|
||||
version = "0.15.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f"
|
||||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"humantime",
|
||||
"log",
|
||||
"regex",
|
||||
"termcolor",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "flate2"
|
||||
version = "1.0.22"
|
||||
@ -162,6 +201,15 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
|
||||
dependencies = [
|
||||
"quick-error",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "0.4.8"
|
||||
@ -173,9 +221,12 @@ name = "lazymc"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"dotenv",
|
||||
"futures",
|
||||
"libc",
|
||||
"log",
|
||||
"minecraft-protocol",
|
||||
"pretty_env_logger",
|
||||
"rand 0.8.4",
|
||||
"tokio",
|
||||
]
|
||||
@ -316,6 +367,16 @@ version = "0.2.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed0cfbc8191465bed66e1718596ee0b0b35d5ee1f41c5df2189d0fe8bde535ba"
|
||||
|
||||
[[package]]
|
||||
name = "pretty_env_logger"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "926d36b9553851b8b0005f1275891b392ee4d2d833852c417ed025477350fb9d"
|
||||
dependencies = [
|
||||
"env_logger",
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.32"
|
||||
@ -325,6 +386,12 @@ dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quick-error"
|
||||
version = "1.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.10"
|
||||
@ -489,6 +556,23 @@ dependencies = [
|
||||
"rand_core 0.3.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.6.25"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.5"
|
||||
@ -546,6 +630,15 @@ dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termcolor"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.13.0"
|
||||
@ -614,6 +707,15 @@ version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
|
@ -25,3 +25,7 @@ libc = "0.2"
|
||||
minecraft-protocol = { git = "https://github.com/timvisee/minecraft-protocol", rev = "c578492" }
|
||||
rand = "0.8"
|
||||
tokio = { version = "1", default-features = false, features = ["rt", "rt-multi-thread", "io-util", "net", "macros", "time", "process", "signal"] }
|
||||
|
||||
dotenv = "0.15"
|
||||
log = "0.4"
|
||||
pretty_env_logger = "0.4"
|
||||
|
27
src/main.rs
27
src/main.rs
@ -1,3 +1,6 @@
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
pub(crate) mod config;
|
||||
pub(crate) mod monitor;
|
||||
pub(crate) mod protocol;
|
||||
@ -28,7 +31,11 @@ use server::ServerState;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), ()> {
|
||||
println!(
|
||||
// Initialize logging
|
||||
let _ = dotenv::dotenv();
|
||||
pretty_env_logger::init();
|
||||
|
||||
info!(
|
||||
"Proxying public {} to internal {}",
|
||||
ADDRESS_PUBLIC, ADDRESS_PROXY,
|
||||
);
|
||||
@ -62,7 +69,7 @@ async fn main() -> Result<(), ()> {
|
||||
// When server is not online, spawn a status server
|
||||
let transfer = status_server(client, inbound, server_state.clone()).map(|r| {
|
||||
if let Err(err) = r {
|
||||
println!("Failed to serve status: {:?}", err);
|
||||
error!("Failed to serve status: {:?}", err);
|
||||
}
|
||||
});
|
||||
|
||||
@ -71,7 +78,7 @@ async fn main() -> Result<(), ()> {
|
||||
// When server is online, proxy all
|
||||
let transfer = proxy(inbound, ADDRESS_PROXY.to_string()).map(|r| {
|
||||
if let Err(err) = r {
|
||||
println!("Failed to proxy: {:?}", err);
|
||||
error!("Failed to proxy: {:?}", err);
|
||||
}
|
||||
});
|
||||
|
||||
@ -110,8 +117,8 @@ pub async fn read_packet<'a>(
|
||||
let (consumed, len) = match types::read_var_int(&buf) {
|
||||
Ok(result) => result,
|
||||
Err(err) => {
|
||||
eprintln!("Failed to read packet length, should retry!");
|
||||
eprintln!("{:?}", (&buf).as_ref());
|
||||
error!("Failed to read packet length, should retry!");
|
||||
error!("{:?}", (&buf).as_ref());
|
||||
return Err(err);
|
||||
}
|
||||
};
|
||||
@ -161,7 +168,7 @@ async fn status_server(
|
||||
Ok(Some(packet)) => packet,
|
||||
Ok(None) => break,
|
||||
Err(_) => {
|
||||
eprintln!("Closing connection, error occurred");
|
||||
error!("Closing connection, error occurred");
|
||||
break;
|
||||
}
|
||||
};
|
||||
@ -243,10 +250,10 @@ async fn status_server(
|
||||
continue;
|
||||
}
|
||||
|
||||
// // Show unhandled packet warning
|
||||
// eprintln!("Received unhandled packet:");
|
||||
// eprintln!("- State: {:?}", client.state());
|
||||
// eprintln!("- Packet ID: {}", packet.id);
|
||||
// Show unhandled packet warning
|
||||
debug!("Received unhandled packet:");
|
||||
debug!("- State: {:?}", client.state());
|
||||
debug!("- Packet ID: {}", packet.id);
|
||||
}
|
||||
|
||||
// Gracefully close connection
|
||||
|
@ -35,7 +35,7 @@ pub async fn poll_server(addr: SocketAddr) -> bool {
|
||||
/// Monitor server.
|
||||
pub async fn monitor_server(addr: SocketAddr, state: Arc<ServerState>) {
|
||||
loop {
|
||||
// TODO: eprintln!("Polling {} ... ", addr);
|
||||
trace!("Polling {} ... ", addr);
|
||||
let online = poll_server(addr).await;
|
||||
|
||||
state.set_online(online);
|
||||
|
@ -42,7 +42,7 @@ impl ServerState {
|
||||
/// Kill any running server.
|
||||
pub fn kill_server(&self) -> bool {
|
||||
if let Some(pid) = *self.pid.lock().unwrap() {
|
||||
eprintln!("[lazymc] Sending kill signal to server");
|
||||
warn!("Sending kill signal to server");
|
||||
kill_gracefully(pid);
|
||||
return true;
|
||||
}
|
||||
@ -60,13 +60,13 @@ impl ServerState {
|
||||
pub async fn start(state: Arc<ServerState>) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut cmd = Command::new(SERVER_CMD);
|
||||
|
||||
eprintln!("[lazymc] Starting server...");
|
||||
info!("Starting server...");
|
||||
let mut child = cmd.spawn()?;
|
||||
|
||||
state.set_pid(Some(child.id().expect("unknown server PID")));
|
||||
|
||||
let status = child.wait().await?;
|
||||
eprintln!("[lazymc] Server stopped (status: {})\n", status);
|
||||
info!("Server stopped (status: {})\n", status);
|
||||
|
||||
// Reset online and starting state
|
||||
// TODO: also set this when returning early due to error
|
||||
|
Loading…
x
Reference in New Issue
Block a user