From 5beec8d9b9febb84fbca60dc3ea281238c0131f7 Mon Sep 17 00:00:00 2001 From: AviiNL Date: Sat, 23 Dec 2023 09:54:55 +0100 Subject: [PATCH] Logging --- Cargo.lock | 3 +++ Cargo.toml | 3 +++ crates/dcs-grpc/build.rs | 7 ------- crates/guardian_core/Cargo.toml | 1 + crates/guardian_core/src/dcs/mission.rs | 10 +++++----- crates/guardian_core/src/dcs/text.rs | 3 ++- crates/guardian_core/src/srs.rs | 17 +++++++++-------- crates/guardian_core/src/tts.rs | 9 +++++---- src/commands/bogeydope.rs | 5 +++-- src/commands/cleartripwire.rs | 10 ++++++---- src/commands/radiocheck.rs | 9 ++++++--- src/commands/tripwire.rs | 25 ++++++++++++------------- src/main.rs | 14 ++++++++++++++ 13 files changed, 69 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c790265..2fd4fbe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2288,6 +2288,8 @@ dependencies = [ "serde", "tokio", "toml", + "tracing", + "tracing-subscriber", ] [[package]] @@ -2327,6 +2329,7 @@ dependencies = [ "tokio-stream", "tokio-util", "tonic", + "tracing", "uuid", "windows 0.52.0", ] diff --git a/Cargo.toml b/Cargo.toml index 5a1a625..b1e2f41 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,6 +42,7 @@ serde_json = "1.0" tokio = { version = "1.34", features = ["macros", "rt-multi-thread", "signal"] } clap = { version = "4.4", features = ["derive"] } shlex = "1.2" +tracing = "0.1.40" [dependencies] bevy.workspace = true @@ -50,4 +51,6 @@ tokio.workspace = true serde.workspace = true guardian_commands.workspace = true guardian_core.workspace = true +tracing.workspace = true toml = "0.8" +tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } diff --git a/crates/dcs-grpc/build.rs b/crates/dcs-grpc/build.rs index 23636e0..a852cb8 100644 --- a/crates/dcs-grpc/build.rs +++ b/crates/dcs-grpc/build.rs @@ -2,20 +2,13 @@ fn main() -> Result<(), Box> { std::env::set_var("PROTOC", protoc_bundled::PROTOC); std::env::set_var("PROTOC_INCLUDE", protoc_bundled::PROTOC_INCLUDE); - println!("cargo:rerun-if-changed=migrations"); println!("cargo:rerun-if-changed=protos/dcs"); build_dcs_grpc_db()?; - // build_commands()?; - Ok(()) } -// fn build_commands() -> Result<(), Box> { -// Ok(()) -// } - fn build_dcs_grpc_db() -> Result<(), Box> { tonic_build::configure() .type_attribute(".", "#[derive(::serde::Serialize, ::serde::Deserialize)]") diff --git a/crates/guardian_core/Cargo.toml b/crates/guardian_core/Cargo.toml index 2afe759..2f9f234 100644 --- a/crates/guardian_core/Cargo.toml +++ b/crates/guardian_core/Cargo.toml @@ -16,6 +16,7 @@ serde.workspace = true serde_json.workspace = true shlex.workspace = true tokio.workspace = true +tracing.workspace = true async-compat = "0.2.1" serde_repr = "0.1" crossbeam-channel = "0.5.9" diff --git a/crates/guardian_core/src/dcs/mission.rs b/crates/guardian_core/src/dcs/mission.rs index 6004b64..21d5f83 100644 --- a/crates/guardian_core/src/dcs/mission.rs +++ b/crates/guardian_core/src/dcs/mission.rs @@ -20,6 +20,7 @@ use dcs_grpc::dcs::{ StreamUnitsRequest, }, }; +use tracing::{debug, info, warn}; #[derive(Clone)] enum Response { @@ -52,11 +53,11 @@ fn connect_to_grpc(mut commands: Commands, tokio: Res, url: Res, url: Res { - // eprintln!("Empty?"); break; } Err(e) => { - eprintln!("Error from gRPC: {:?}", e); + debug!("Error from gRPC: {:?}", e); // verbose or debug log break; } } } - eprintln!("Disconnected from DCS: {}", url); + warn!("Disconnected from DCS: {}", url); // warn tx.send(Response::Disconnected).ok(); } }); diff --git a/crates/guardian_core/src/dcs/text.rs b/crates/guardian_core/src/dcs/text.rs index 99d47ec..8cdb104 100644 --- a/crates/guardian_core/src/dcs/text.rs +++ b/crates/guardian_core/src/dcs/text.rs @@ -19,6 +19,7 @@ use dcs_grpc::dcs::{ common::v0::Coalition, net::v0::{net_service_client::NetServiceClient, SendChatRequest}, }; +use tracing::info; use crate::{components::GrpcBaseUrl, TokioResource}; @@ -57,7 +58,7 @@ fn send_text_message( // target_player_id: player_id, }; - println!("> {}", message); + info!("> {}", message); client.send_chat(request).await.ok(); }); diff --git a/crates/guardian_core/src/srs.rs b/crates/guardian_core/src/srs.rs index b4ec932..75568b8 100644 --- a/crates/guardian_core/src/srs.rs +++ b/crates/guardian_core/src/srs.rs @@ -8,6 +8,7 @@ mod voice_command; use guardian_commands::{call::parse_call, ConsoleCommandEntered, ConsoleConfiguration}; use serde::{Deserialize, Serialize}; use simsearch::SimSearch; +use tracing::{error, info, warn}; pub use voice_command::VoiceCommand; use crossbeam_channel::Receiver; @@ -166,7 +167,7 @@ fn listen_srs( radio.handle = Some(tokio.0.spawn(async move { loop { let Ok(tcp) = TcpStream::connect(addr).await else { - eprintln!("Connection failed: {}", addr); + warn!("Connection failed: {}", addr); sleep(Duration::from_secs(5)).await; continue; }; @@ -188,7 +189,7 @@ fn listen_srs( client: client.clone(), version: SRS_VERSION.to_string(), })).await { - eprintln!("Srs error: {:?}", e); + error!("Srs error: {:?}", e); sleep(Duration::from_secs(5)).await; continue; }; @@ -246,7 +247,7 @@ fn listen_srs( Ok(data) => { match &data { Message::VersionMismatch(VersionMismatchMessage { version, .. }) => { - eprintln!("Version mismatch {} != {}", SRS_VERSION, version); + error!("Version mismatch {} != {}", SRS_VERSION, version); }, Message::Sync(SyncMessage { clients, .. }) => { for client in clients.iter() { @@ -324,7 +325,7 @@ fn listen_srs( } }; } - eprintln!("Disconnected from SRS: {}", addr); + warn!("Disconnected from SRS: {}", addr); } #[allow(unreachable_code)] Ok(()) @@ -495,7 +496,10 @@ fn handle_voice_command( continue; }; - println!("< {}\n\t{}", rawr.clone(), cmd.clone()); + let raw = rawr.trim().to_string(); + + info!("Received: {}", raw); + info!("Interpreted as: {}", cmd); let Ok((_, call)) = parse_call(cmd) else { // no command, casual conversations? @@ -505,9 +509,6 @@ fn handle_voice_command( let cmd = call.command; if config.commands.get(&cmd).is_some() { - let raw = rawr.clone(); - let raw = raw.trim().to_string(); - command_entered.send(ConsoleCommandEntered { command_name: cmd.clone(), raw, diff --git a/crates/guardian_core/src/tts.rs b/crates/guardian_core/src/tts.rs index 1dae2a5..c8c22e9 100644 --- a/crates/guardian_core/src/tts.rs +++ b/crates/guardian_core/src/tts.rs @@ -3,6 +3,7 @@ use std::borrow::Cow; use tokio::sync::Mutex; +use tracing::{debug, error, warn}; use windows::core::HSTRING; use windows::Media::SpeechSynthesis::SpeechSynthesizer; use windows::Storage::Streams::DataReader; @@ -58,21 +59,21 @@ pub async fn synthesize(text: &str, voice: &Voice) -> Result>, WinEr let lang = v.Language()?.to_string(); if lang.starts_with("en-") { let name = v.DisplayName()?.to_string(); - println!("Using WIN voice: {}", name); + debug!("Using WIN voice: {}", name); voice_info = Some(v); break; } } if voice_info.is_none() { - println!("Could not find any english Windows TTS voice"); + error!("Could not find any english Windows TTS voice"); } } if voice_info.is_none() { let all_voices = SpeechSynthesizer::AllVoices()?; let len = all_voices.Size()? as usize; - println!( + warn!( "Available WIN voices are (you don't have to include the `Microsoft` prefix in \ the name):" ); @@ -84,7 +85,7 @@ pub async fn synthesize(text: &str, voice: &Voice) -> Result>, WinEr } let name = v.DisplayName()?.to_string(); - println!("- {} ({})", name, lang); + warn!("- {} ({})", name, lang); } } diff --git a/src/commands/bogeydope.rs b/src/commands/bogeydope.rs index f45a173..26bb4fb 100644 --- a/src/commands/bogeydope.rs +++ b/src/commands/bogeydope.rs @@ -5,6 +5,7 @@ use bevy::ecs::{ use clap::Parser; use guardian_commands::{ConsoleCommand, NamedCommand}; use guardian_core::{components::*, dcs::text::TextMessage, srs::voice::VoiceMessage}; +use tracing::debug; use crate::braa::Braa; @@ -37,12 +38,12 @@ pub fn bogey_dope( }; let Ok((p_callsign, p_pos)) = player.get(*pilot) else { - eprintln!("no player, died?"); + debug!("no player, died?"); return; }; let Ok(a_callsign) = awacs.get(*operator) else { - eprintln!("no awacs, they died?"); + debug!("no awacs, they died?"); return; }; diff --git a/src/commands/cleartripwire.rs b/src/commands/cleartripwire.rs index 741044b..c02af51 100644 --- a/src/commands/cleartripwire.rs +++ b/src/commands/cleartripwire.rs @@ -2,6 +2,7 @@ use bevy::ecs::system::{Commands, Query, Resource}; use clap::Parser; use guardian_commands::{ConsoleCommand, NamedCommand}; use guardian_core::{components::*, dcs::text::TextMessage, srs::voice::VoiceMessage}; +use tracing::debug; use crate::tripwire::Tripwire; @@ -25,20 +26,21 @@ pub fn clear_tripwire( return; }; let Some(pilot) = &cmd.pilot else { - return; // no pilot + debug!("no pilot"); + return; }; let Some(operator) = &cmd.operator else { - eprintln!("no operator"); + debug!("no operator"); return; }; let Ok(p_callsign) = callsign.get(*pilot) else { - eprintln!("no player, died?"); + debug!("no player, died?"); return; }; let Ok(a_callsign) = callsign.get(*operator) else { - eprintln!("no awacs, they died?"); + debug!("no awacs, they died?"); return; }; diff --git a/src/commands/radiocheck.rs b/src/commands/radiocheck.rs index ac35c2a..bc6ca66 100644 --- a/src/commands/radiocheck.rs +++ b/src/commands/radiocheck.rs @@ -2,6 +2,7 @@ use bevy::ecs::system::{Commands, Query, Resource}; use clap::Parser; use guardian_commands::{ConsoleCommand, NamedCommand}; use guardian_core::{components::*, dcs::text::TextMessage, srs::voice::VoiceMessage}; +use tracing::debug; #[derive(Parser, Debug)] pub struct RadioCheck; @@ -23,19 +24,21 @@ pub fn radio_check( return; }; let Some(pilot) = &cmd.pilot else { - return; // no pilot + debug!("no pilot"); + return; }; let Some(operator) = &cmd.operator else { + debug!("no operator"); return; }; let Ok(p_callsign) = callsign.get(*pilot) else { - eprintln!("no player, died?"); + debug!("no player, died?"); return; }; let Ok(a_callsign) = callsign.get(*operator) else { - eprintln!("no awacs, they died?"); + debug!("no awacs, they died?"); return; }; diff --git a/src/commands/tripwire.rs b/src/commands/tripwire.rs index 989c421..3a067a0 100644 --- a/src/commands/tripwire.rs +++ b/src/commands/tripwire.rs @@ -2,6 +2,7 @@ use bevy::ecs::system::{Commands, Query, Resource}; use clap::Parser; use guardian_commands::{ConsoleCommand, NamedCommand}; use guardian_core::{components::*, dcs::text::TextMessage, srs::voice::VoiceMessage}; +use tracing::debug; use crate::tripwire::Tripwire; @@ -25,25 +26,25 @@ pub fn set_tripwire( return; }; let Some(pilot) = &cmd.pilot else { - return; // no pilot - }; - let Some(operator) = &cmd.operator else { - eprintln!("no operator"); + debug!("no pilot"); return; }; - let Some(readback) = &cmd.raw else { - eprintln!("no readback"); - // no raw found, wut + let Some(operator) = &cmd.operator else { + debug!("no operator"); return; }; let Ok(p_callsign) = callsign.get(*pilot) else { - eprintln!("no player, died?"); + debug!("no player, died?"); return; }; let Ok(a_callsign) = callsign.get(*operator) else { - eprintln!("no awacs, they died?"); + debug!("no awacs, they died?"); + return; + }; + let Some(readback) = &cmd.raw else { + debug!("no readback"); return; }; @@ -51,10 +52,8 @@ pub fn set_tripwire( // check the last part, if it's a number, interpret as miles let Some(last) = parts.nth_back(0) else { - eprintln!("no last part?"); - // there is no last.. dafuq? - // there is always a last, otherwise we wouldnt be here - return; // readback error + debug!("no last part"); + return; }; let distance = match last.parse::() { diff --git a/src/main.rs b/src/main.rs index 2f77391..43cf6dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -23,6 +23,18 @@ use tripwire::TripwirePlugin; #[tokio::main] async fn main() -> Result<(), Box> { + let subscriber = tracing_subscriber::FmtSubscriber::builder() + .with_env_filter( + "trace,bevy=error,tokio_util=error,h2=error,hyper=error,tower=error,tonic=error", + ) + // .with_span_events(tracing_subscriber::fmt::format::FmtSpan::CLOSE) + .with_file(false) + .with_line_number(false) + .with_target(true) + .finish(); + + tracing::subscriber::set_global_default(subscriber)?; + let config = Config::load("config.toml".into())?; App::new() @@ -50,6 +62,8 @@ fn give_awacs_radio( continue; // not configured for this unit }; + bevy::log::info!("{} now has a radio", callsign); + commands.entity(ent).insert(Radio::new( channel.frequency, channel.modulation,