Skip to content
This repository was archived by the owner on Dec 15, 2021. It is now read-only.

Commit dad78e1

Browse files
author
Alex Helfet
committed
Added --follow / -F flag to follow the input file, like tail -f.
1 parent 2f46b4a commit dad78e1

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

src/bin/itmdump.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ fn run() -> Result<()> {
7979
.short("f")
8080
.help("Path to file (or named pipe) to read from")
8181
.takes_value(true))
82+
.arg(Arg::with_name("follow")
83+
.long("follow")
84+
.short("F")
85+
.help("Keep the file open after reading through it and \
86+
append new output as it is written. Like `tail -f'."))
8287
.arg(Arg::with_name("port")
8388
.long("stimulus")
8489
.short("s")
@@ -96,6 +101,8 @@ fn run() -> Result<()> {
96101
.parse::<u8>()
97102
.expect("Arg validator should ensure this parses");
98103

104+
let follow = matches.is_present("follow");
105+
99106
let mut stream = open_read(&matches)?;
100107

101108
let stdout = io::stdout();
@@ -135,13 +142,23 @@ fn run() -> Result<()> {
135142
}
136143
}
137144
})() {
138-
match e.kind() {
139-
io::ErrorKind::UnexpectedEof => {
145+
// Error caught in main loop as `e`.
146+
if e.kind() == io::ErrorKind::UnexpectedEof {
147+
if follow {
148+
// TODO: There's a bug here where we can lose data.
149+
// UnexpectedEof is returned when read_exact() encounters EOF
150+
// before it fills its buffer, but in that case it may have
151+
// already read _some_ data, which we discard here.
152+
//
153+
// We could buffer input until we can read a full packet,
154+
// or turn parsing into a state machine.
140155
thread::sleep(Duration::from_millis(100));
156+
} else {
157+
// !follow and EOF. Exit.
158+
return Ok(())
141159
}
142-
_ => {
143-
error!("{}", e);
144-
}
160+
} else {
161+
error!("{}", e);
145162
}
146163
}
147164
}

0 commit comments

Comments
 (0)