From ea501eefa8367cf64ff6404808390376afdbae6b Mon Sep 17 00:00:00 2001 From: WT-MM Date: Fri, 6 Dec 2024 02:10:43 +0000 Subject: [PATCH] trace logging --- .gitignore | 4 ++ daemon/Cargo.toml | 4 ++ daemon/src/main.rs | 83 +++++++++++++++++++++++---- platforms/kbot/src/process_manager.rs | 8 +-- 4 files changed, 85 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 7ddd7f5..37e353c 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,7 @@ build/ dist/ *.so out*/ + +# Dev +*.patch +robstridev2/ diff --git a/daemon/Cargo.toml b/daemon/Cargo.toml index eaad24f..3070aa2 100644 --- a/daemon/Cargo.toml +++ b/daemon/Cargo.toml @@ -20,6 +20,10 @@ tower = "0.5" gstreamer = "0.23" gstreamer-base = "0.23" glib = "0.17" +tracing-appender = "0.2" +clap = { version = "4.0", features = ["derive"] } +chrono = "0.4" +directories = "5.0" kos-sim = { version = "0.1.0", path = "../platforms/sim", optional = true } kos-stub = { version = "0.1.0", path = "../platforms/stub", optional = true } diff --git a/daemon/src/main.rs b/daemon/src/main.rs index 7ead2ef..6d04efe 100644 --- a/daemon/src/main.rs +++ b/daemon/src/main.rs @@ -14,6 +14,14 @@ use tokio::sync::Mutex; use tonic::transport::Server; use tracing::{debug, error, info}; use tracing_subscriber::filter::EnvFilter; +use tracing_appender::rolling::RollingFileAppender; +use tracing_appender::non_blocking::NonBlocking; +use tracing_subscriber::Layer; +use tracing_subscriber::prelude::*; +use chrono::Local; +use clap::Parser; +use directories::BaseDirs; +use std::path::PathBuf; #[cfg(not(any(feature = "kos-sim", feature = "kos-zeroth-01", feature = "kos-kbot")))] use kos_stub::StubPlatform as PlatformImpl; @@ -27,6 +35,14 @@ use kos_zeroth_01::Zeroth01Platform as PlatformImpl; #[cfg(feature = "kos-kbot")] use kos_kbot::KbotPlatform as PlatformImpl; +#[derive(Parser, Debug)] +#[command(author, version, about, long_about = None)] +struct Args { + /// Enable file logging + #[arg(long, default_value_t = false)] + log: bool, +} + fn add_service_to_router( router: tonic::transport::server::Router, service: ServiceEnum, @@ -63,11 +79,22 @@ async fn run_server( Ok(()) } +struct DaemonState { + _guard: Option, + platform: PlatformImpl, +} + #[tokio::main] async fn main() -> Result<()> { - // logging - tracing_subscriber::fmt() - .with_env_filter( + let args = Args::parse(); + + // tracing + let subscriber = tracing_subscriber::registry(); + + // Always add stdout layer + let stdout_layer = tracing_subscriber::fmt::layer() + .with_writer(std::io::stdout) + .with_filter( EnvFilter::from_default_env() .add_directive("h2=error".parse().unwrap()) .add_directive("grpc=error".parse().unwrap()) @@ -76,14 +103,50 @@ async fn main() -> Result<()> { .add_directive("polling=error".parse().unwrap()) .add_directive("async_io=error".parse().unwrap()) .add_directive("krec=error".parse().unwrap()), - ) - .init(); - - let mut platform = PlatformImpl::new(); + ); + + let subscriber = subscriber.with(stdout_layer); + + let guard = if args.log { + let log_dir = if let Some(base_dirs) = BaseDirs::new() { + base_dirs.data_local_dir() + .join("kos") + .join("logs") + } else { + PathBuf::from("~/.local/share/kos/logs") + }; + + std::fs::create_dir_all(&log_dir)?; + + let timestamp = Local::now().format("%Y%m%d_%H%M%S"); + let log_name = format!("kos-daemon_{}.log", timestamp); + + let file_appender = RollingFileAppender::new( + tracing_appender::rolling::Rotation::NEVER, + log_dir, + &log_name, + ); + let (non_blocking, guard) = tracing_appender::non_blocking(file_appender); + + let file_layer = tracing_subscriber::fmt::layer() + .with_writer(non_blocking) + .with_filter(tracing_subscriber::filter::LevelFilter::TRACE); + + subscriber.with(file_layer).init(); + Some(guard) + } else { + subscriber.init(); + None + }; + + let mut state = DaemonState { + _guard: guard, + platform: PlatformImpl::new(), + }; // telemetry Telemetry::initialize( - format!("{}-{}", platform.name(), platform.serial()).as_str(), + format!("{}-{}", state.platform.name(), state.platform.serial()).as_str(), "localhost", 1883, ) @@ -92,9 +155,9 @@ async fn main() -> Result<()> { let operations_store = Arc::new(Mutex::new(HashMap::new())); let operations_service = Arc::new(OperationsServiceImpl::new(operations_store)); - platform.initialize(operations_service.clone())?; + state.platform.initialize(operations_service.clone())?; - if let Err(e) = run_server(&platform, operations_service).await { + if let Err(e) = run_server(&state.platform, operations_service).await { error!("Server error: {:?}", e); std::process::exit(1); } diff --git a/platforms/kbot/src/process_manager.rs b/platforms/kbot/src/process_manager.rs index 15751ef..85e0077 100644 --- a/platforms/kbot/src/process_manager.rs +++ b/platforms/kbot/src/process_manager.rs @@ -245,10 +245,10 @@ impl ProcessManager for KBotProcessManager { *kclip_uuid = Some(new_uuid.clone()); drop(kclip_uuid); - let (pipeline, _sink) = Self::create_pipeline(&video_path)?; + // let (pipeline, _sink) = Self::create_pipeline(&video_path)?; - // Start the pipeline - pipeline.set_state(gst::State::Playing)?; + // // Start the pipeline + // pipeline.set_state(gst::State::Playing)?; // Start telemetry logger let logger = TelemetryLogger::new( @@ -265,7 +265,7 @@ impl ProcessManager for KBotProcessManager { drop(telemetry_logger); let mut pipeline_guard = self.pipeline.lock().await; - *pipeline_guard = Some(pipeline); + *pipeline_guard = None; Ok(KClipStartResponse { clip_uuid: Some(new_uuid),