summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Gallant <jamslam@gmail.com>2024-05-13 09:34:15 -0400
committerAndrew Gallant <jamslam@gmail.com>2024-05-13 09:43:04 -0400
commitf1d23c06e30606b2428a4e32da8f0b5069e81280 (patch)
tree33f49130c48a198dcdd72a336d14a2f4939ea55a
parent22b677900f7ee74dcc0ec6791805120976d86168 (diff)
cli: add more logging for stdin heuristic detection
Stdin heuristic detection is complicated and opaque enough that it's worth having easy access to the complete story that leads ripgrep to decide whether to search stdin or not. Ref #2806
-rw-r--r--crates/cli/src/lib.rs61
1 files changed, 55 insertions, 6 deletions
diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs
index 643d796c..b27592d9 100644
--- a/crates/cli/src/lib.rs
+++ b/crates/cli/src/lib.rs
@@ -178,22 +178,71 @@ pub fn is_readable_stdin() -> bool {
};
let stdin = std::io::stdin();
- let Ok(fd) = stdin.as_fd().try_clone_to_owned() else { return false };
+ let fd = match stdin.as_fd().try_clone_to_owned() {
+ Ok(fd) => fd,
+ Err(err) => {
+ log::debug!(
+ "for heuristic stdin detection on Unix, \
+ could not clone stdin file descriptor \
+ (thus assuming stdin is not readable): {err}",
+ );
+ return false;
+ }
+ };
let file = File::from(fd);
- let Ok(md) = file.metadata() else { return false };
+ let md = match file.metadata() {
+ Ok(md) => md,
+ Err(err) => {
+ log::debug!(
+ "for heuristic stdin detection on Unix, \
+ could not get file metadata for stdin \
+ (thus assuming stdin is not readable): {err}",
+ );
+ return false;
+ }
+ };
let ft = md.file_type();
- ft.is_file() || ft.is_fifo() || ft.is_socket()
+ let is_file = ft.is_file();
+ let is_fifo = ft.is_fifo();
+ let is_socket = ft.is_socket();
+ let is_readable = is_file || is_fifo || is_socket;
+ log::debug!(
+ "for heuristic stdin detection on Unix, \
+ found that \
+ is_file={is_file}, is_fifo={is_fifo} and is_socket={is_socket}, \
+ and thus concluded that is_stdin_readable={is_readable}",
+ );
+ is_readable
}
#[cfg(windows)]
fn imp() -> bool {
- winapi_util::file::typ(winapi_util::HandleRef::stdin())
- .map(|t| t.is_disk() || t.is_pipe())
- .unwrap_or(false)
+ let stdin = winapi_util::HandleRef::stdin();
+ let typ = match winapi_util::file::typ(stdin) {
+ Ok(typ) => typ,
+ Err(err) => {
+ log::debug!(
+ "for heuristic stdin detection on Windows, \
+ could not get file type of stdin \
+ (thus assuming stdin is not readable): {err}",
+ );
+ return false;
+ }
+ };
+ let is_disk = typ.is_disk();
+ let is_pipe = typ.is_pipe();
+ let is_readable = is_disk || is_pipe;
+ log::debug!(
+ "for heuristic stdin detection on Windows, \
+ found that is_disk={is_disk} and is_pipe={is_pipe}, \
+ and thus concluded that is_stdin_readable={is_readable}",
+ );
+ is_readable
}
#[cfg(not(any(unix, windows)))]
fn imp() -> bool {
+ log::debug!("on non-{{Unix,Windows}}, assuming stdin is not readable");
false
}