From 05c3d0832c76f599d58e7d8474b301569d7f1e7c Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 19 Feb 2023 09:54:08 -0600 Subject: [PATCH] hmm how do you debug a shell --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 2 ++ src/log.rs | 19 +++++++++++++++++++ src/main.rs | 12 +++++++----- 4 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/log.rs diff --git a/Cargo.lock b/Cargo.lock index 151058b..ad2612e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,21 @@ version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "224afbd727c3d6e4b90103ece64b8d1b67fbb1973b1046c2281eed3f3803f800" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "log" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" +dependencies = [ + "cfg-if", +] + [[package]] name = "proc-macro2" version = "1.0.51" @@ -68,6 +83,7 @@ name = "wash" version = "0.1.0" dependencies = [ "anyhow", + "log", "thiserror", "windows", ] diff --git a/Cargo.toml b/Cargo.toml index 37c32f4..0082b59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,8 @@ edition = "2021" anyhow = "1.0" thiserror = "1.0" +log = "0.4" + [dependencies.windows] version = "0.44.0" features = [ diff --git a/src/log.rs b/src/log.rs new file mode 100644 index 0000000..28f4563 --- /dev/null +++ b/src/log.rs @@ -0,0 +1,19 @@ +pub use log::{debug, error, info, set_logger, set_max_level, trace, warn, LevelFilter}; + +use log::{Metadata, Record}; + +pub struct Log; + +impl log::Log for Log { + fn enabled(&self, _metadata: &Metadata) -> bool { + true + } + + fn log(&self, record: &Record) { + if self.enabled(record.metadata()) { + println!("{} - {}", record.level(), record.args()); + } + } + + fn flush(&self) {} +} diff --git a/src/main.rs b/src/main.rs index d83452f..4e73258 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ mod error; +mod log; -use std::io::{self, Write}; use windows::Win32::{Foundation::HANDLE, System::Console}; use anyhow::{Context, Result}; -use crate::error::Error; +use crate::{error::Error, log::*}; fn print_input_mode(mode: Console::CONSOLE_MODE) { // Characters read by the ReadFile or ReadConsole function are written to the active screen @@ -197,8 +197,6 @@ fn setup_stdin() -> Result<()> { unsafe { let handle = stdin_handle()?; Error::check(Console::GetConsoleMode(handle, &mut mode))?; - println!("Stdin details:"); - print_input_mode(mode); // allow terminal input characters mode |= Console::ENABLE_VIRTUAL_TERMINAL_INPUT; @@ -216,6 +214,9 @@ fn setup_stdin() -> Result<()> { mode |= Console::ENABLE_MOUSE_INPUT; Error::check(Console::SetConsoleMode(handle, mode))?; + Error::check(Console::GetConsoleMode(handle, &mut mode))?; + println!("Stdin details:"); + print_input_mode(mode); } Ok(()) } @@ -235,6 +236,7 @@ fn setup_stdout() -> Result<()> { } fn main() -> Result<()> { + _ = set_logger(&Log {}).map(|()| set_max_level(LevelFilter::Info)); setup_stdin()?; setup_stdout()?; @@ -243,7 +245,7 @@ fn main() -> Result<()> { loop { let mut n: u32 = 0; unsafe { - Error::check(Console::ReadConsoleInputA(stdin, &mut buf, &mut n))?; + Error::check(Console::ReadConsoleInputW(stdin, &mut buf, &mut n))?; } let n = n as usize; println!("read: {:?}", n);