Fixed error handling

This commit is contained in:
Sebastian 2024-06-22 23:19:04 +02:00
parent 9bb7cc7a2c
commit ecb5399a42
3 changed files with 139 additions and 10 deletions

97
Cargo.lock generated
View file

@ -17,6 +17,12 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "anyhow"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25bdb32cbbdce2b519a9cd7df3a678443100e265d5e25ca763b7572a5104f5f3"
[[package]]
name = "autocfg"
version = "1.2.0"
@ -62,6 +68,27 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "colored"
version = "1.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f741c91823341bebf717d4c71bda820630ce065443b58bd1b7451af008355"
dependencies = [
"is-terminal",
"lazy_static",
"winapi",
]
[[package]]
name = "fern"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9f0c14694cbd524c8720dd69b0e3179344f04ebb5f90f2e4a440c6ea3b2f1ee"
dependencies = [
"colored",
"log",
]
[[package]]
name = "gimli"
version = "0.28.1"
@ -74,6 +101,29 @@ version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "is-terminal"
version = "0.4.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f23ff5ef2b80d608d61efee834934d862cd92461afc0560dedf493e4c033738b"
dependencies = [
"hermit-abi",
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.154"
@ -90,6 +140,12 @@ dependencies = [
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]]
name = "memchr"
version = "2.7.2"
@ -119,7 +175,7 @@ checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
dependencies = [
"libc",
"wasi",
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
@ -202,6 +258,10 @@ dependencies = [
name = "radomctld"
version = "0.1.0"
dependencies = [
"anyhow",
"fern",
"humantime",
"log",
"nom",
"tokio",
"tokio-macros 0.2.6",
@ -250,7 +310,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9"
dependencies = [
"libc",
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
@ -291,7 +351,7 @@ dependencies = [
"signal-hook-registry",
"socket2",
"tokio-macros 2.2.0",
"windows-sys",
"windows-sys 0.48.0",
]
[[package]]
@ -328,6 +388,28 @@ version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.48.0"
@ -337,6 +419,15 @@ dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.5",
]
[[package]]
name = "windows-targets"
version = "0.48.5"

View file

@ -4,6 +4,10 @@ version = "0.1.0"
edition = "2021"
[dependencies]
anyhow = "1.0.83"
fern = { version = "0.6.2", features = ["colored"] }
humantime = "2.1.0"
log = "0.4.21"
nom = "7.1.3"
tokio = {version = "1.37.0", features = ["full"]}
tokio-macros = { version = "0.2.0-alpha.6" }

View file

@ -1,5 +1,9 @@
mod rotctl;
use anyhow::Result;
use fern::colors::{Color, ColoredLevelConfig};
use log::{debug, error, info, warn};
use std::io;
use tokio::{
self,
io::{AsyncBufReadExt, AsyncWriteExt, BufStream},
@ -10,8 +14,6 @@ use tokio::{
use rotctl::Command;
use std::io;
async fn process_socket(
socket: TcpStream,
cmd_tx: mpsc::Sender<Command>,
@ -27,7 +29,7 @@ async fn process_socket(
return;
}
println!("> {}", line);
debug!("Received: {}", line.strip_suffix("\n").unwrap());
match rotctl::parse_command(&line) {
Ok(cmd) => match cmd {
@ -52,7 +54,8 @@ async fn process_socket(
}
},
Err(msg) => {
stream.write_all(msg.as_bytes()).await.unwrap();
error!("Unable to parse input:\n{}", msg);
stream.write_all("RPRT 6\n".as_bytes()).await.unwrap();
stream.flush().await.unwrap();
}
}
@ -76,7 +79,7 @@ async fn control_rotor(mut rx_cmd: mpsc::Receiver<Command>, pos_tx: watch::Sende
Some(command) = rx_cmd.recv() => {
match command {
Command::SetPos(az, el) => {
println!("Received set pos {} {}", az, el);
info!("Received set pos {} {}", az, el);
target_az = az;
target_el = el;
}
@ -103,8 +106,39 @@ async fn control_rotor(mut rx_cmd: mpsc::Receiver<Command>, pos_tx: watch::Sende
}
}
fn setup_logger() -> Result<()> {
let colors = ColoredLevelConfig::new()
.info(Color::Green)
.error(Color::Red)
.warn(Color::Yellow)
.debug(Color::Blue);
fern::Dispatch::new()
// Perform allocation-free log formatting
.format(move |out, message, record| {
out.finish(format_args!(
"[{} {} {}] {}",
humantime::format_rfc3339_millis(std::time::SystemTime::now()),
colors.color(record.level()),
record.target(),
message
))
})
// Add blanket level filter -
.level(log::LevelFilter::Debug)
// - and per-module overrides
.chain(std::io::stdout())
.chain(fern::log_file("output.log")?)
// Apply globally
.apply()?;
Ok(())
}
#[tokio::main]
async fn main() -> io::Result<()> {
async fn main() -> Result<()> {
setup_logger()?;
let (cmd_tx, cmd_rx) = mpsc::channel::<Command>(16);
let (pos_tx, pos_rx) = watch::channel::<(f32, f32)>((0.0, 0.0));