Skip to content

Commit a250e33

Browse files
committed
Generalize hex tracing
1 parent c4b93bb commit a250e33

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

src/u2ftypes.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,7 @@
55
use std::{cmp, fmt, io, str};
66

77
use crate::consts::*;
8-
use crate::util::io_err;
9-
10-
pub fn to_hex(data: &[u8], joiner: &str) -> String {
11-
let parts: Vec<String> = data.iter().map(|byte| format!("{:02x}", byte)).collect();
12-
parts.join(joiner)
13-
}
14-
15-
pub fn trace_hex(data: &[u8]) {
16-
if log_enabled!(log::Level::Trace) {
17-
trace!("USB send: {}", to_hex(data, ""));
18-
}
19-
}
8+
use crate::util::{io_err, trace_hex, to_hex};
209

2110
// Trait for representing U2F HID Devices. Requires getters/setters for the
2211
// channel ID, created during device initialization.
@@ -97,7 +86,7 @@ impl U2FHIDInit {
9786

9887
let count = cmp::min(data.len(), dev.out_init_data_size());
9988
frame[8..8 + count].copy_from_slice(&data[..count]);
100-
trace_hex(&frame);
89+
trace_hex("USB send", &frame);
10190

10291
if dev.write(&frame)? != frame.len() {
10392
return Err(io_err("device write failed"));
@@ -150,7 +139,7 @@ impl U2FHIDCont {
150139

151140
let count = cmp::min(data.len(), dev.out_cont_data_size());
152141
frame[6..6 + count].copy_from_slice(&data[..count]);
153-
trace_hex(&frame);
142+
trace_hex("USB send", &frame);
154143

155144
if dev.write(&frame)? != frame.len() {
156145
return Err(io_err("device write failed"));

src/util.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ extern crate libc;
66

77
use std::io;
88

9+
use log;
10+
911
macro_rules! try_or {
1012
($val:expr, $or:expr) => {
1113
match $val {
@@ -65,3 +67,14 @@ pub fn from_unix_result<T: Signed>(rv: T) -> io::Result<T> {
6567
pub fn io_err(msg: &str) -> io::Error {
6668
io::Error::new(io::ErrorKind::Other, msg)
6769
}
70+
71+
pub fn to_hex(data: &[u8], joiner: &str) -> String {
72+
let parts: Vec<String> = data.iter().map(|byte| format!("{:02x}", byte)).collect();
73+
parts.join(joiner)
74+
}
75+
76+
pub fn trace_hex(label: &str, data: &[u8]) {
77+
if log_enabled!(log::Level::Trace) {
78+
trace!("{}: {}", label, to_hex(data, ""));
79+
}
80+
}

0 commit comments

Comments
 (0)