mirror of
https://github.com/timvisee/lazymc.git
synced 2025-05-19 04:40:22 -07:00
Tweak logging
This commit is contained in:
parent
a8375534ed
commit
a1b64a2b24
18
src/main.rs
18
src/main.rs
@ -19,21 +19,31 @@ pub(crate) mod status;
|
||||
pub(crate) mod types;
|
||||
pub(crate) mod util;
|
||||
|
||||
use std::env;
|
||||
|
||||
use clap::App;
|
||||
|
||||
/// Main entrypoint.
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), ()> {
|
||||
// Initialize logging
|
||||
// TODO: set default levels!
|
||||
let _ = dotenv::dotenv();
|
||||
pretty_env_logger::init();
|
||||
// Initialize logger
|
||||
init_log();
|
||||
|
||||
// Build clap app, invoke intended action
|
||||
let app = cli::app();
|
||||
invoke_action(app).await
|
||||
}
|
||||
|
||||
/// Initialize logger.
|
||||
fn init_log() {
|
||||
// Set default log level, load from .env
|
||||
env::set_var("RUST_LOG", "info");
|
||||
let _ = dotenv::dotenv();
|
||||
|
||||
// Initialize logger
|
||||
pretty_env_logger::init();
|
||||
}
|
||||
|
||||
/// Invoke an action.
|
||||
async fn invoke_action<'a>(app: App<'a>) -> Result<(), ()> {
|
||||
let matches = app.get_matches();
|
||||
|
@ -30,13 +30,13 @@ pub async fn monitor_server(config: Arc<Config>, state: Arc<ServerState>) {
|
||||
|
||||
loop {
|
||||
// Poll server state and update internal status
|
||||
trace!("Fetching status for {} ... ", addr);
|
||||
trace!(target: "lazymc::monitor", "Fetching status for {} ... ", addr);
|
||||
let status = poll_server(addr).await;
|
||||
state.update_status(status);
|
||||
|
||||
// Sleep server when it's bedtime
|
||||
if state.should_sleep(&config) {
|
||||
info!("Server has been idle, sleeping...");
|
||||
info!(target: "lazymc::montior", "Server has been idle, sleeping...");
|
||||
state.kill_server();
|
||||
}
|
||||
|
||||
|
@ -169,7 +169,7 @@ pub async fn read_packet<'a>(
|
||||
let (consumed, len) = match types::read_var_int(&buf) {
|
||||
Ok(result) => result,
|
||||
Err(err) => {
|
||||
error!("Malformed packet, could not read packet length");
|
||||
error!(target: "lazymc", "Malformed packet, could not read packet length");
|
||||
return Err(err);
|
||||
}
|
||||
};
|
||||
|
@ -59,7 +59,7 @@ impl ServerState {
|
||||
/// Kill any running server.
|
||||
pub fn kill_server(&self) -> bool {
|
||||
if let Some(pid) = *self.pid.lock().unwrap() {
|
||||
debug!("Sending kill signal to server");
|
||||
debug!(target: "lazymc", "Sending kill signal to server");
|
||||
kill_gracefully(pid);
|
||||
|
||||
// TODO: should we set this?
|
||||
@ -100,7 +100,7 @@ impl ServerState {
|
||||
// If server just came online, update last active time
|
||||
if !was_online && online {
|
||||
// TODO: move this somewhere else
|
||||
info!("Server is now online");
|
||||
info!(target: "lazymc::monitor", "Server is now online");
|
||||
self.update_last_active_time();
|
||||
}
|
||||
|
||||
@ -164,13 +164,13 @@ pub async fn start(
|
||||
cmd.current_dir(&config.server.directory);
|
||||
cmd.kill_on_drop(true);
|
||||
|
||||
info!("Starting server...");
|
||||
info!(target: "lazymc", "Starting server...");
|
||||
let mut child = cmd.spawn()?;
|
||||
|
||||
state.set_pid(Some(child.id().expect("unknown server PID")));
|
||||
|
||||
let status = child.wait().await?;
|
||||
info!("Server stopped (status: {})\n", status);
|
||||
info!(target: "lazymc", "Server stopped (status: {})\n", status);
|
||||
|
||||
// Reset online and starting state
|
||||
// TODO: also set this when returning early due to error
|
||||
@ -186,9 +186,9 @@ pub async fn start(
|
||||
fn kill_gracefully(pid: u32) {
|
||||
#[cfg(unix)]
|
||||
unsafe {
|
||||
debug!("Sending SIGTERM signal to {} to kill server", pid);
|
||||
debug!(target: "lazymc", "Sending SIGTERM signal to {} to kill server", pid);
|
||||
let result = libc::kill(pid as i32, libc::SIGTERM);
|
||||
trace!("SIGTERM result: {}", result);
|
||||
trace!(target: "lazymc", "SIGTERM result: {}", result);
|
||||
|
||||
// TODO: send sigterm to childs as well?
|
||||
// TODO: handle error if != 0
|
||||
|
@ -28,7 +28,8 @@ pub async fn service(config: Arc<Config>) -> Result<(), ()> {
|
||||
})?;
|
||||
|
||||
info!(
|
||||
"Proxying egress {} to ingress {}",
|
||||
target: "lazymc",
|
||||
"Proxying public {} to server {}",
|
||||
config.public.address, config.server.address,
|
||||
);
|
||||
|
||||
@ -48,7 +49,7 @@ pub async fn service(config: Arc<Config>) -> Result<(), ()> {
|
||||
let transfer = status::serve(client, inbound, config.clone(), server_state.clone())
|
||||
.map(|r| {
|
||||
if let Err(err) = r {
|
||||
warn!("Failed to serve status: {:?}", err);
|
||||
warn!(target: "lazymc", "Failed to serve status: {:?}", err);
|
||||
}
|
||||
});
|
||||
|
||||
@ -57,7 +58,7 @@ pub async fn service(config: Arc<Config>) -> Result<(), ()> {
|
||||
// When server is online, proxy all
|
||||
let transfer = proxy::proxy(inbound, config.server.address).map(|r| {
|
||||
if let Err(err) = r {
|
||||
warn!("Failed to proxy: {}", err);
|
||||
warn!(target: "lazymc", "Failed to proxy: {}", err);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -38,7 +38,7 @@ pub async fn serve(
|
||||
Ok(Some(packet)) => packet,
|
||||
Ok(None) => break,
|
||||
Err(_) => {
|
||||
error!("Closing connection, error occurred");
|
||||
error!(target: "lazymc", "Closing connection, error occurred");
|
||||
break;
|
||||
}
|
||||
};
|
||||
@ -129,9 +129,9 @@ pub async fn serve(
|
||||
}
|
||||
|
||||
// Show unhandled packet warning
|
||||
debug!("Received unhandled packet:");
|
||||
debug!("- State: {:?}", client.state());
|
||||
debug!("- Packet ID: {}", packet.id);
|
||||
debug!(target: "lazymc", "Received unhandled packet:");
|
||||
debug!(target: "lazymc", "- State: {:?}", client.state());
|
||||
debug!(target: "lazymc", "- Packet ID: {}", packet.id);
|
||||
}
|
||||
|
||||
// Gracefully close connection
|
||||
|
Loading…
x
Reference in New Issue
Block a user