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

Commit 6e0f12f

Browse files
bors[bot]japaric
andcommitted
Merge #19
19: move from error-chain to failure r=japaric a=japaric Co-authored-by: Jorge Aparicio <[email protected]>
2 parents 97b443c + 516862b commit 6e0f12f

File tree

8 files changed

+180
-116
lines changed

8 files changed

+180
-116
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.rs.bk
2+
.#*
23
target

CHANGELOG.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
## [v0.3.0] - 2018-07-04
11+
12+
### Changed
13+
14+
- Moved error handling from error-chain failure.
15+
16+
### Fixed
17+
18+
- Include the crate version in the output of `itmdump -V`
19+
- sporadic EOF errors in follow mode
20+
1021
## [v0.2.1] - 2018-02-25
1122

1223
### Changed
@@ -49,7 +60,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4960
- `itmdump` tool that parses instrumentation packets from the stimulus port 0
5061
and dumps the payload to `stdout`.
5162

52-
[Unreleased]: https://github.com/japaric/itm/compare/v0.2.1...HEAD
63+
[Unreleased]: https://github.com/japaric/itm/compare/v0.3.0...HEAD
64+
[v0.3.0]: https://github.com/japaric/itm/compare/v0.2.1...v0.3.0
5365
[v0.2.1]: https://github.com/japaric/itm/compare/v0.2.0...v0.2.1
5466
[v0.2.0]: https://github.com/japaric/itm/compare/v0.1.1...v0.2.0
5567
[v0.1.1]: https://github.com/japaric/itm/compare/v0.1.0...v0.1.1

Cargo.lock

Lines changed: 60 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ keywords = ["parse", "tool", "ARM", "ITM"]
77
license = "MIT OR Apache-2.0"
88
name = "itm"
99
repository = "https://github.com/japaric/itm"
10-
version = "0.2.1"
10+
version = "0.3.0"
1111

1212
[dependencies]
1313
chrono = "^0.4"
1414
clap = "^2.14.0"
1515
env_logger = "^0.4.3"
16-
error-chain = "^0.11.0"
16+
failure = "0.1.1"
17+
failure_derive = "0.1.1"
1718
log = "^0.3.8"
1819

1920
[dev-dependencies]

src/bin/itmdump.rs

Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
extern crate chrono;
44
extern crate clap;
55
extern crate env_logger;
6+
extern crate failure;
67
extern crate itm;
78
#[macro_use]
89
extern crate log;
910

1011
use clap::{App, Arg, ArgMatches};
11-
use itm::error::{Error, ErrorKind, Result, ResultExt};
12-
use itm::{packet, Decoder};
12+
use itm::{packet, Decoder, Error};
1313
use log::{LogLevelFilter, LogRecord};
1414
use std::fs::File;
1515
use std::io::Write;
@@ -33,31 +33,14 @@ fn main() {
3333
.init()
3434
.unwrap();
3535

36-
fn show_backtrace() -> bool {
37-
env::var("RUST_BACKTRACE").as_ref().map(|s| &s[..]) == Ok("1")
38-
}
39-
4036
if let Err(e) = run() {
41-
let stderr = io::stderr();
42-
let mut stderr = stderr.lock();
43-
44-
writeln!(stderr, "{}", e).unwrap();
45-
46-
for e in e.iter().skip(1) {
47-
writeln!(stderr, "caused by: {}", e).unwrap();
48-
}
49-
50-
if show_backtrace() {
51-
if let Some(backtrace) = e.backtrace() {
52-
writeln!(stderr, "{:?}", backtrace).unwrap();
53-
}
54-
}
37+
eprintln!("{}", e);
5538

5639
process::exit(1)
5740
}
5841
}
5942

60-
fn run() -> Result<()> {
43+
fn run() -> Result<(), failure::Error> {
6144
let matches = App::new("itmdump")
6245
.version(concat!(
6346
env!("CARGO_PKG_VERSION"),
@@ -117,41 +100,39 @@ fn run() -> Result<()> {
117100
}
118101
_ => (),
119102
},
120-
Err(e @ Error(ErrorKind::UnknownHeader(_), _)) => {
121-
// We don't know this header type; warn and continue.
122-
debug!("{}", e);
123-
}
124-
Err(Error(ErrorKind::EofBeforePacket, _)) => {
125-
if follow {
126-
// NOTE 10 ms let us achieve 60 FPS in the worst case scenario
127-
thread::sleep(Duration::from_millis(10));
103+
Err(fe) => {
104+
if let Some(ie) = fe.downcast_ref::<Error>().cloned() {
105+
match ie {
106+
Error::UnknownHeader { byte } => {
107+
// We don't know this header type; warn and continue.
108+
debug!("unknown header byte: {:x}", byte);
109+
}
110+
Error::EofBeforePacket => {
111+
if follow {
112+
// NOTE 10 ms let us achieve 60 FPS in the worst case scenario
113+
thread::sleep(Duration::from_millis(10));
114+
} else {
115+
// !follow and EOF. Exit.
116+
return Ok(());
117+
}
118+
}
119+
_ => return Err(fe.into()),
120+
}
128121
} else {
129-
// !follow and EOF. Exit.
130-
return Ok(());
122+
return Err(fe);
131123
}
132124
}
133-
134-
// FIXME: If in follow mode, we may try to read a packet
135-
// but reach the end of the file in the middle. Currently
136-
// we'd just return an error and hence exit. When we
137-
// receive an error, we've already read and discarded some
138-
// data, so we can't just go around this loop again.
139-
//
140-
// We could make a file following wrapper around `read`
141-
// that blocks in a loop retrying the read and sleeping if
142-
// there's no data.
143-
Err(e) => return Err(e),
144125
}
145126
} // end of read loop
146127

147128
// Unreachable.
148129
}
149130

150-
fn open_read(matches: &ArgMatches) -> Result<Box<io::Read + 'static>> {
131+
fn open_read(matches: &ArgMatches) -> Result<Box<io::Read + 'static>, io::Error> {
151132
let path = matches.value_of("file");
152133
Ok(match path {
153134
Some(path) => {
154-
let f = File::open(path).chain_err(|| format!("Couldn't open source file '{}'", path))?;
135+
let f = File::open(path)?;
155136
Box::new(f) as Box<io::Read + 'static>
156137
}
157138
None => Box::new(io::stdin()) as Box<io::Read + 'static>,

0 commit comments

Comments
 (0)