Skip to content

Commit

Permalink
trace logging
Browse files Browse the repository at this point in the history
  • Loading branch information
WT-MM committed Dec 6, 2024
1 parent 5945575 commit ea501ee
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 14 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ build/
dist/
*.so
out*/

# Dev
*.patch
robstridev2/
4 changes: 4 additions & 0 deletions daemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
83 changes: 73 additions & 10 deletions daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -63,11 +79,22 @@ async fn run_server(
Ok(())
}

struct DaemonState {
_guard: Option<tracing_appender::non_blocking::WorkerGuard>,
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())
Expand All @@ -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,
)
Expand All @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions platforms/kbot/src/process_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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),
Expand Down

0 comments on commit ea501ee

Please sign in to comment.