Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
WT-MM committed Dec 6, 2024
1 parent 0d33ba2 commit 034ed01
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
53 changes: 33 additions & 20 deletions daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@
// This will run the gRPC server and, if applicable, a runtime loop
// (e.g., actuator polling, loaded model inference).

use chrono::Local;
use clap::Parser;
use directories::BaseDirs;
use eyre::Result;
use flate2::write::GzEncoder;
use flate2::Compression;
use kos_core::google_proto::longrunning::operations_server::OperationsServer;
use kos_core::services::OperationsServiceImpl;
use kos_core::telemetry::Telemetry;
use kos_core::Platform;
use kos_core::ServiceEnum;
use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::PathBuf;
use std::sync::Arc;
use tokio::signal;
use tokio::sync::Mutex;
use tonic::transport::Server;
use tracing::{debug, error, info};
use tracing_subscriber::filter::EnvFilter;
use tracing_subscriber::Layer;
use tracing_subscriber::prelude::*;
use chrono::Local;
use clap::Parser;
use directories::BaseDirs;
use std::path::PathBuf;
use std::io::{self, Write, BufWriter};
use flate2::write::GzEncoder;
use flate2::Compression;
use std::fs::File;
use tokio::signal;
use tracing_subscriber::Layer;

#[cfg(not(any(feature = "kos-sim", feature = "kos-zeroth-01", feature = "kos-kbot")))]
use kos_stub::StubPlatform as PlatformImpl;
Expand Down Expand Up @@ -92,6 +92,7 @@ struct CompressedWriter {
path: PathBuf,
}

// TODO: The encoder doesn't close properly, so this needs to be fixed later.
impl CompressedWriter {
fn new(path: impl AsRef<std::path::Path>) -> io::Result<Self> {
let file = File::create(path.as_ref())?;
Expand Down Expand Up @@ -139,13 +140,23 @@ impl Write for CompressedWriter {
Ok(size)
}
Err(e) => {
error!("Failed to write to compressed log {}: {}", self.path.display(), e);
error!(
"Failed to write to compressed log {}: {}",
self.path.display(),
e
);
Err(e)
}
}
} else {
error!("Attempted to write to finalized log {}", self.path.display());
Err(io::Error::new(io::ErrorKind::Other, "Writer has been finalized"))
error!(
"Attempted to write to finalized log {}",
self.path.display()
);
Err(io::Error::new(
io::ErrorKind::Other,
"Writer has been finalized",
))
}
}

Expand All @@ -157,7 +168,11 @@ impl Write for CompressedWriter {
impl Drop for CompressedWriter {
fn drop(&mut self) {
if let Err(e) = self.finalize() {
error!("Failed to finalize compressed log {}: {}", self.path.display(), e);
error!(
"Failed to finalize compressed log {}: {}",
self.path.display(),
e
);
}
}
}
Expand All @@ -168,7 +183,7 @@ async fn main() -> Result<()> {

// tracing
let subscriber = tracing_subscriber::registry();

// Always add stdout layer
let stdout_layer = tracing_subscriber::fmt::layer()
.with_writer(std::io::stdout)
Expand All @@ -187,9 +202,7 @@ async fn main() -> Result<()> {

let guard = if args.log {
let log_dir = if let Some(base_dirs) = BaseDirs::new() {
base_dirs.data_local_dir()
.join("kos")
.join("logs")
base_dirs.data_local_dir().join("kos").join("logs")
} else {
PathBuf::from("~/.local/share/kos/logs")
};
Expand All @@ -202,7 +215,7 @@ async fn main() -> Result<()> {
let log_path = log_dir.join(&final_name);

info!("Writing compressed logs to: {}", log_path.display());

let compressed_writer = CompressedWriter::new(&log_path)?;
let (non_blocking, guard) = tracing_appender::non_blocking(compressed_writer);

Expand All @@ -228,7 +241,7 @@ async fn main() -> Result<()> {

// Setup signal handler
let (shutdown_tx, mut shutdown_rx) = tokio::sync::oneshot::channel();

tokio::spawn(async move {
if let Ok(()) = signal::ctrl_c().await {
let _ = shutdown_tx.send(());
Expand Down
2 changes: 1 addition & 1 deletion platforms/kbot/src/actuator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl KBotActuator {
let discovered_ids = supervisor
.scan_bus(0xFD, port, desired_actuator_types)
.await?;

for (idx, (motor_id, _)) in desired_actuator_types.iter().enumerate() {
if discovered_ids.contains(motor_id) {
found_motors[idx] = true;
Expand Down
6 changes: 2 additions & 4 deletions platforms/kbot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Platform for KbotPlatform {
Box::pin(async move {
if cfg!(target_os = "linux") {
tracing::debug!("Initializing KBot services for Linux");

let process_manager =
KBotProcessManager::new(self.name().to_string(), self.serial())
.wrap_err("Failed to initialize GStreamer process manager")?;
Expand All @@ -74,9 +74,7 @@ impl Platform for KbotPlatform {
// "/dev/ttyCH341USB1",
// "/dev/ttyCH341USB2",
// "/dev/ttyCH341USB3",
"can0",
"can1",
"can2",
"can0", "can1", "can2",
],
Duration::from_secs(1),
// Duration::from_nanos(3_333_333),
Expand Down

0 comments on commit 034ed01

Please sign in to comment.