From 20617c026369f9ff8160f81a9f07a46e5a0c123d Mon Sep 17 00:00:00 2001 From: Denis Maximov Date: Fri, 13 Nov 2020 17:44:24 +0200 Subject: [PATCH] feat(development): add --debug flag (#41) * wip: #39 create a -d --debug flag * fix: switch join with concat --- src/main.rs | 15 +++++++++++++-- src/pty_bus.rs | 35 ++++++++++++++++++++++++++++++----- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index ea3594c3..becdd457 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,10 @@ pub struct Opt { open_file: Option, #[structopt(long)] /// Maximum panes on screen, caution: opening more panes will close old ones - max_panes: Option + max_panes: Option, + + #[structopt(short, long)] + debug: bool } fn _debug_log_to_file (message: String) { @@ -53,6 +56,12 @@ fn _debug_log_to_file (message: String) { file.write_all("\n".as_bytes()).unwrap(); } +fn delete_log_files() -> std::io::Result<()> { + std::fs::remove_dir_all("/tmp/mosaic-logs").ok(); + std::fs::create_dir_all("/tmp/mosaic-logs").ok(); + Ok(()) +} + pub fn main() { let os_input = get_os_input(); let opts = Opt::from_args(); @@ -91,13 +100,15 @@ pub enum AppInstruction { pub fn start(mut os_input: Box, opts: Opt) { let mut active_threads = vec![]; + delete_log_files().unwrap(); + let full_screen_ws = os_input.get_terminal_size_using_fd(0); os_input.into_raw_mode(0); let (send_screen_instructions, receive_screen_instructions): (Sender, Receiver) = channel(); let (send_pty_instructions, receive_pty_instructions): (Sender, Receiver) = channel(); let (send_app_instructions, receive_app_instructions): (Sender, Receiver) = channel(); let mut screen = Screen::new(receive_screen_instructions, send_pty_instructions.clone(), send_app_instructions.clone(), &full_screen_ws, os_input.clone(), opts.max_panes); - let mut pty_bus = PtyBus::new(receive_pty_instructions, send_screen_instructions.clone(), os_input.clone()); + let mut pty_bus = PtyBus::new(receive_pty_instructions, send_screen_instructions.clone(), os_input.clone(), opts.debug); active_threads.push( thread::Builder::new() diff --git a/src/pty_bus.rs b/src/pty_bus.rs index 705f8e4c..148eb33e 100644 --- a/src/pty_bus.rs +++ b/src/pty_bus.rs @@ -34,6 +34,26 @@ fn _debug_log_to_file (message: String) { file.write_all("\n".as_bytes()).unwrap(); } +fn debug_to_file(message: u8, pid: RawFd) { + use std::fs::OpenOptions; + use std::io::prelude::*; + let mut path = PathBuf::new(); + path.push( + [ + String::from("/tmp/mosaic-logs/mosaic-"), + pid.to_string(), + String::from(".log"), + ] + .concat(), + ); + let mut file = OpenOptions::new() + .append(true) + .create(true) + .open(path) + .unwrap(); + file.write_all(&[message]).unwrap(); +} + impl Stream for ReadFromPid { type Item = Vec; fn poll_next(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll> { @@ -147,9 +167,10 @@ pub struct PtyBus { pub receive_pty_instructions: Receiver, pub id_to_child_pid: HashMap, os_input: Box, + debug_to_file: bool } -fn stream_terminal_bytes(pid: RawFd, send_screen_instructions: Sender, os_input: Box) { +fn stream_terminal_bytes(pid: RawFd, send_screen_instructions: Sender, os_input: Box, debug: bool) { task::spawn({ async move { let mut vte_parser = vte::Parser::new(); @@ -163,6 +184,9 @@ fn stream_terminal_bytes(pid: RawFd, send_screen_instructions: Sender, send_screen_instructions: Sender, os_input: Box) -> Self { + pub fn new (receive_pty_instructions: Receiver, send_screen_instructions: Sender, os_input: Box, debug_to_file: bool) -> Self { PtyBus { send_screen_instructions, receive_pty_instructions, os_input, id_to_child_pid: HashMap::new(), + debug_to_file } } pub fn spawn_terminal(&mut self, file_to_open: Option) { let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(file_to_open); - stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone()); + stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone(), self.debug_to_file); self.id_to_child_pid.insert(pid_primary, pid_secondary); self.send_screen_instructions.send(ScreenInstruction::NewPane(pid_primary)).unwrap(); } pub fn spawn_terminal_vertically(&mut self, file_to_open: Option) { let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(file_to_open); - stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone()); + stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone(), self.debug_to_file); self.id_to_child_pid.insert(pid_primary, pid_secondary); self.send_screen_instructions.send(ScreenInstruction::VerticalSplit(pid_primary)).unwrap(); } pub fn spawn_terminal_horizontally(&mut self, file_to_open: Option) { let (pid_primary, pid_secondary): (RawFd, RawFd) = self.os_input.spawn_terminal(file_to_open); - stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone()); + stream_terminal_bytes(pid_primary, self.send_screen_instructions.clone(), self.os_input.clone(), self.debug_to_file); self.id_to_child_pid.insert(pid_primary, pid_secondary); self.send_screen_instructions.send(ScreenInstruction::HorizontalSplit(pid_primary)).unwrap(); }