Browse Source
- Switched current ANSI frontend to trait, - Added barebones frontend, without fancy thingsmain
7 changed files with 158 additions and 97 deletions
@ -0,0 +1,74 @@ |
|||
use std::{io::stdout, sync::mpsc, time::Duration}; |
|||
|
|||
use fern::Dispatch; |
|||
use tokio::{ |
|||
io::{AsyncBufReadExt, BufReader}, |
|||
select, spawn, |
|||
task::spawn_blocking, |
|||
}; |
|||
|
|||
use crate::logger::simple_formatter; |
|||
|
|||
use super::{Close, LoggingFrontend, Stdin}; |
|||
|
|||
pub(super) struct BarebonesFrontend; |
|||
|
|||
impl LoggingFrontend for BarebonesFrontend { |
|||
fn setup_with_handler<F: Fn(String) -> String + Send + 'static>( |
|||
_: &crate::server_properties::ServerProperties, |
|||
handler: F, |
|||
logger_filter: fern::Dispatch, |
|||
) -> anyhow::Result<(Close, Stdin<String>)> { |
|||
let l = simple_formatter(Dispatch::new()).chain(stdout()); |
|||
logger_filter.chain(l).apply()?; |
|||
|
|||
log::info!("Saving output to output.log"); |
|||
|
|||
let (stdin_send, stdin) = mpsc::channel(); |
|||
let (close, close_recv) = mpsc::channel(); |
|||
let (close_send_async, mut close_recv_async) = tokio::sync::mpsc::unbounded_channel(); |
|||
|
|||
spawn_blocking(move || { |
|||
while let Err(mpsc::TryRecvError::Empty) = close_recv.try_recv() {} |
|||
close_send_async |
|||
.send(()) |
|||
.expect("Error resending close signal"); |
|||
}); |
|||
|
|||
let handle = spawn(async move { |
|||
select! {_ = close_recv_async.recv() => cleanup(), |
|||
_ = async move { |
|||
let mut lines = BufReader::new(tokio::io::stdin()).lines(); |
|||
while let Some(line) = lines.next_line().await.expect("Error reading line") { |
|||
log::info!(target: "command", " >> {}", line); |
|||
stdin_send.send(handler(line)).expect("Error sending input line"); |
|||
} |
|||
} => (), |
|||
_ = tokio::signal::ctrl_c() => {println!(); cleanup(); std::process::exit(0)}, |
|||
} |
|||
}); |
|||
|
|||
std::panic::set_hook(Box::new(|info| { |
|||
let thread = std::thread::current(); |
|||
let name = thread.name().unwrap_or("<unnamed>"); |
|||
log::error!("thread '{}' {}", name, info); |
|||
log::info!("Exiting in 5 seconds"); |
|||
std::thread::sleep(Duration::from_secs(5)); |
|||
std::process::exit(0) |
|||
})); |
|||
|
|||
Ok(( |
|||
Close { |
|||
close, |
|||
handle, |
|||
close_handler: Box::new(|| {}), |
|||
}, |
|||
stdin, |
|||
)) |
|||
} |
|||
} |
|||
|
|||
fn cleanup() { |
|||
log::info!(target: "cleanup", "Exiting server"); |
|||
println!("Exiting server, output has been saved to output.log"); |
|||
} |
|||
Loading…
Reference in new issue