Skip to content

Commit f7ba6ab

Browse files
committed
Option to save to file.
Added: * Option to save output to file as well. Changed: * Fixing build errors by apt-get update first
1 parent 0a011c0 commit f7ba6ab

File tree

5 files changed

+96
-4
lines changed

5 files changed

+96
-4
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- uses: actions/checkout@v2
2929
- name: Install libudev-dev
3030
if: ${{ matrix.os == 'ubuntu-latest' }}
31-
run: sudo apt-get install -y libudev-dev
31+
run: sudo apt-get update && sudo apt-get install -y libudev-dev
3232
- uses: taiki-e/upload-rust-binary-action@v1
3333
with:
3434
# (required)

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2+
## [0.1.2]
3+
4+
Option to save to file.
5+
6+
Added:
7+
* Option to save output to file as well.
8+
19
## [0.1.1]
210

311
Opening serial port.

Cargo.lock

Lines changed: 57 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[package]
22
name = "zephyr-tools-monitor"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
77

88
[dependencies]
9+
chrono = "0.4.19"
910
pico-args = "0.4.2"
1011
serde_json = "1.0.73"
1112
serialport = "4.0.1"

src/main.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use core::time;
22
use std::{
3+
fs::File,
34
io::{self, BufRead, BufReader, Write},
45
thread::sleep,
56
time::Duration,
67
};
78

9+
use chrono::Local;
810
use serde_json;
911
use serialport::{self, available_ports};
1012

@@ -19,6 +21,7 @@ OPTIONS:
1921
--follow, Follow serial port if it disconnects
2022
--port, Port to connect to
2123
--baud, Baud to use (default 115200)
24+
--save, -s Automatically save output to file
2225
ARGS:
2326
<INPUT>
2427
";
@@ -29,6 +32,7 @@ struct AppArgs {
2932
port: Option<String>,
3033
baud: u32,
3134
follow: bool,
35+
save: bool,
3236
}
3337

3438
fn parse_args() -> Result<AppArgs, pico_args::Error> {
@@ -45,6 +49,7 @@ fn parse_args() -> Result<AppArgs, pico_args::Error> {
4549
port: pargs.opt_value_from_str("--port")?,
4650
baud: pargs.value_from_str("--baud").or(Ok(115_200))?,
4751
follow: pargs.contains(["-f", "--follow"]),
52+
save: pargs.contains(["-s", "--save"]),
4853
};
4954

5055
// It's up to the caller what to do with the remaining arguments.
@@ -93,6 +98,19 @@ fn main() {
9398

9499
let port_name = args.port.unwrap();
95100

101+
// Open file if active
102+
let mut file = match args.save {
103+
true => {
104+
let time = Local::now();
105+
106+
match File::create(format!("log-{}.txt", time.to_rfc3339())) {
107+
Ok(f) => Some(f),
108+
Err(_) => None,
109+
}
110+
}
111+
false => None,
112+
};
113+
96114
loop {
97115
// Open with settings
98116
let port = serialport::new(&port_name, args.baud)
@@ -109,7 +127,16 @@ fn main() {
109127

110128
for line in lines {
111129
match line {
112-
Ok(l) => println!("{}", l),
130+
Ok(l) => {
131+
println!("{}", l);
132+
133+
// Save to file as well..
134+
if let Some(ref mut f) = file {
135+
let _ = f.write(l.as_bytes());
136+
let _ = f.write(b"\n");
137+
let _ = f.flush();
138+
}
139+
}
113140
Err(e) => {
114141
if e.to_string().contains("Operation timed out") {
115142
continue;

0 commit comments

Comments
 (0)