diff --git a/Cargo.toml b/Cargo.toml index 7790ab5..8c21bda 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,8 +27,8 @@ crash_reporting=[] tokio_console=["console-subscriber"] [dependencies] -# buttplug = { path = "../buttplug/buttplug" } -buttplug = "7.1.11" +buttplug = { path = "../buttplug/buttplug" } +# buttplug = "7.1.11" argh = "0.1.12" log = "0.4.20" futures = "0.3.29" diff --git a/src/bin/main.rs b/src/bin/main.rs index 559b174..eff1ca8 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,15 +1,18 @@ use argh::FromArgs; use getset::{CopyGetters, Getters}; -use intiface_engine::ButtplugRepeater; use intiface_engine::{ - setup_console_logging, EngineOptions, EngineOptionsBuilder, IntifaceEngine, IntifaceEngineError, + EngineOptions, EngineOptionsBuilder, IntifaceEngine, IntifaceEngineError, IntifaceError, }; use std::fs; use tokio::{select, signal::ctrl_c}; -use tracing::debug; -use tracing::info; -use tracing::Level; +use tracing::{debug, info, Level}; +use tracing_subscriber::{ + filter::{EnvFilter, LevelFilter}, + layer::SubscriberExt, + util::SubscriberInitExt, +}; + const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -156,12 +159,12 @@ pub struct IntifaceCLIArguments { #[argh(option)] #[getset(get_copy = "pub")] repeater_port: Option, - + /// if set, use repeater mode instead of engine mode #[argh(option)] #[getset(get = "pub")] repeater_remote_address: Option, - + #[cfg(debug_assertions)] /// crash the main thread (that holds the runtime) #[argh(switch)] @@ -176,6 +179,29 @@ pub struct IntifaceCLIArguments { crash_task_thread: bool, } +pub fn setup_console_logging(log_level: Option) { + if log_level.is_some() { + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer()) + //.with(sentry_tracing::layer()) + .with(LevelFilter::from(log_level)) + .try_init() + .unwrap(); + } else { + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer()) + //.with(sentry_tracing::layer()) + .with( + EnvFilter::try_from_default_env() + .or_else(|_| EnvFilter::try_new("info")) + .unwrap(), + ) + .try_init() + .unwrap(); + }; + println!("Intiface Server, starting up with stdout output."); +} + impl TryFrom for EngineOptions { type Error = IntifaceError; fn try_from(args: IntifaceCLIArguments) -> Result { @@ -236,9 +262,6 @@ impl TryFrom for EngineOptions { .crash_task_thread(args.crash_task_thread()); } - if let Some(value) = args.log() { - builder.log_level(value); - } if let Some(value) = args.websocket_port() { builder.websocket_port(value); } @@ -260,7 +283,7 @@ impl TryFrom for EngineOptions { } } -#[tokio::main] +#[tokio::main(flavor = "current_thread")] //#[tokio::main] async fn main() -> Result<(), IntifaceEngineError> { let args: IntifaceCLIArguments = argh::from_env(); if args.server_version() { @@ -279,27 +302,21 @@ async fn main() -> Result<(), IntifaceEngineError> { return Ok(()); } - if args.repeater() { + if args.frontend_websocket_port().is_none() { setup_console_logging(args.log()); - let repeater = ButtplugRepeater::new(args.repeater_port().unwrap(), &args.repeater_remote_address().as_ref().unwrap()); - repeater.listen().await; - } else { - if args.frontend_websocket_port().is_none() { - setup_console_logging(args.log()); + } + + let options = EngineOptions::try_from(args).map_err(IntifaceEngineError::from)?; + let engine = IntifaceEngine::default(); + select! { + _ = engine.run(&options, None) => { + } - - let options = EngineOptions::try_from(args).map_err(IntifaceEngineError::from)?; - let engine = IntifaceEngine::default(); - select! { - _ = engine.run(&options, None, true) => { - - } - _ = ctrl_c() => { - info!("Control-c hit, exiting."); - engine.stop(); - } + _ = ctrl_c() => { + info!("Control-c hit, exiting."); + engine.stop(); } } - + Ok(()) } diff --git a/src/engine.rs b/src/engine.rs index f7b9e7f..4488d26 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -6,9 +6,8 @@ use crate::{ frontend_external_event_loop, frontend_server_event_loop, process_messages::EngineMessage, setup_frontend, Frontend, }, - logging::setup_frontend_logging, options::EngineOptions, - ButtplugRemoteServer, ButtplugServerConnectorError, IntifaceError, + ButtplugRemoteServer, ButtplugServerConnectorError, IntifaceError, ButtplugRepeater, }; use buttplug::{ core::{ @@ -21,7 +20,7 @@ use buttplug::{ server::ButtplugServerBuilder, }; use once_cell::sync::OnceCell; -use std::{str::FromStr, sync::Arc, time::Duration}; +use std::{sync::Arc, time::Duration}; use tokio::select; use tokio_util::sync::CancellationToken; @@ -133,7 +132,6 @@ impl IntifaceEngine { &self, options: &EngineOptions, external_frontend: Option>, - skip_logging_setup: bool, ) -> Result<(), IntifaceEngineError> { // At this point we will have received and validated options. @@ -152,6 +150,7 @@ impl IntifaceEngine { None }; */ + // Create the cancellation tokens for let frontend_cancellation_token = CancellationToken::new(); let frontend_cancellation_child_token = frontend_cancellation_token.child_token(); @@ -176,10 +175,12 @@ impl IntifaceEngine { frontend.connect().await.unwrap(); frontend.send(EngineMessage::EngineStarted {}).await; - if !skip_logging_setup { - if let Some(level) = options.log_level() { - setup_frontend_logging(tracing::Level::from_str(level).unwrap(), frontend.clone()); - } + + if options.repeater_mode() { + info!("Starting repeater"); + let repeater = ButtplugRepeater::new(options.repeater_local_port().unwrap(), &options.repeater_remote_address().as_ref().unwrap()); + repeater.listen().await; + return Ok(()); } // Set up crash logging for the duration of the server session. diff --git a/src/lib.rs b/src/lib.rs index 07decad..309a273 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,7 +5,6 @@ mod device_communication_managers; mod engine; mod error; mod frontend; -mod logging; mod options; mod remote_server; mod repeater; @@ -13,7 +12,6 @@ pub use backdoor_server::BackdoorServer; pub use engine::IntifaceEngine; pub use error::*; pub use frontend::{EngineMessage, Frontend, IntifaceMessage}; -pub use logging::{setup_console_logging, setup_frontend_logging, BroadcastWriter}; pub use options::{EngineOptions, EngineOptionsBuilder, EngineOptionsExternal}; pub use remote_server::{ButtplugRemoteServer, ButtplugServerConnectorError}; pub use repeater::ButtplugRepeater; \ No newline at end of file diff --git a/src/logging.rs b/src/logging.rs deleted file mode 100644 index 8dedec2..0000000 --- a/src/logging.rs +++ /dev/null @@ -1,166 +0,0 @@ -use crate::frontend::{EngineMessage, Frontend}; -#[cfg(feature = "tokio_console")] -use console_subscriber; -use lazy_static::lazy_static; -use once_cell::sync::OnceCell; -use std::sync::Arc; -use tokio::{select, sync::broadcast}; -use tracing::Level; -use tracing_subscriber::{ - filter::{EnvFilter, LevelFilter}, - layer::SubscriberExt, - util::SubscriberInitExt, -}; - -static FRONTEND_LOGGING_SET: OnceCell = OnceCell::new(); -lazy_static! { - static ref LOG_BROADCASTER: Arc>> = Arc::new(broadcast::channel(255).0); -} - -use tracing_subscriber::fmt::MakeWriter; - -pub struct BroadcastWriter { - log_sender: Arc>>, -} - -impl BroadcastWriter { - pub fn new(sender: Arc>>) -> Self { - Self { log_sender: sender } - } -} - -impl std::io::Write for BroadcastWriter { - fn write(&mut self, buf: &[u8]) -> Result { - let sender = self.log_sender.clone(); - let len = buf.len(); - let send_buf = buf.to_vec(); - let _ = sender.send(send_buf.to_vec()); - Ok(len) - } - - fn flush(&mut self) -> Result<(), std::io::Error> { - Ok(()) - } -} - -impl MakeWriter<'_> for BroadcastWriter { - type Writer = BroadcastWriter; - fn make_writer(&self) -> Self::Writer { - BroadcastWriter::new(self.log_sender.clone()) - } -} - -pub fn setup_frontend_logging(log_level: Level, frontend: Arc) { - // Add panic hook for emitting backtraces through the logging system. - log_panics::init(); - let mut receiver = LOG_BROADCASTER.subscribe(); - let log_sender = frontend.clone(); - let notifier = log_sender.disconnect_notifier(); - tokio::spawn(async move { - // We can log until our receiver disappears at this point. - loop { - select! { - log = receiver.recv() => { - match log { - Ok(log) => { - log_sender - .send(EngineMessage::EngineLog { - message: std::str::from_utf8(&log).unwrap().to_owned(), - }) - .await; - } - Err(_) => return - } - } - _ = notifier.notified() => return - } - } - }); - - if FRONTEND_LOGGING_SET.get().is_none() { - FRONTEND_LOGGING_SET.set(true).unwrap(); - #[cfg(feature = "tokio_console")] - { - /* - let console = console_subscriber::ConsoleLayer::builder() - //.server_addr(([0, 0, 0, 0], 5555)) - .spawn(); - tracing_subscriber::registry() - .with(LevelFilter::from(log_level)) - .with( - tracing_subscriber::fmt::layer() - .json() - //.with_max_level(log_level) - .with_ansi(false) - .with_writer(move || BroadcastWriter::new(LOG_BROADCASTER.clone())), - ) - .with(sentry_tracing::layer()) - .with(console) - .try_init() - .unwrap(); - */ - //console_subscriber::init(); - console_subscriber::ConsoleLayer::builder() - .server_addr(([0, 0, 0, 0], 5555)) - .init(); - } - #[cfg(not(feature = "tokio_console"))] - { - if std::env::var("RUST_LOG").is_ok() { - tracing_subscriber::registry() - .with( - EnvFilter::try_from_default_env() - .or_else(|_| EnvFilter::try_new("info")) - .unwrap(), - ) - .with( - tracing_subscriber::fmt::layer() - .json() - //.with_max_level(log_level) - .with_ansi(false) - .with_writer(move || BroadcastWriter::new(LOG_BROADCASTER.clone())), - ) - //.with(sentry_tracing::layer()) - .try_init() - .unwrap(); - } else { - tracing_subscriber::registry() - .with(LevelFilter::from(log_level)) - .with( - tracing_subscriber::fmt::layer() - .json() - //.with_max_level(log_level) - .with_ansi(false) - .with_writer(move || BroadcastWriter::new(LOG_BROADCASTER.clone())), - ) - //.with(sentry_tracing::layer()) - .try_init() - .unwrap(); - info!("Logging subscriber added to registry"); - } - } - } -} - -pub fn setup_console_logging(log_level: Option) { - if log_level.is_some() { - tracing_subscriber::registry() - .with(tracing_subscriber::fmt::layer()) - //.with(sentry_tracing::layer()) - .with(LevelFilter::from(log_level)) - .try_init() - .unwrap(); - } else { - tracing_subscriber::registry() - .with(tracing_subscriber::fmt::layer()) - //.with(sentry_tracing::layer()) - .with( - EnvFilter::try_from_default_env() - .or_else(|_| EnvFilter::try_new("info")) - .unwrap(), - ) - .try_init() - .unwrap(); - }; - println!("Intiface Server, starting up with stdout output."); -} diff --git a/src/options.rs b/src/options.rs index 6ad35d3..1450dcc 100644 --- a/src/options.rs +++ b/src/options.rs @@ -1,5 +1,4 @@ use getset::{CopyGetters, Getters}; -use tracing::Level; #[derive(CopyGetters, Getters, Default, Debug, Clone)] pub struct EngineOptions { @@ -25,8 +24,6 @@ pub struct EngineOptions { frontend_in_process_channel: bool, #[getset(get_copy = "pub")] max_ping_time: u32, - #[getset(get = "pub")] - log_level: Option, #[getset(get_copy = "pub")] allow_raw_messages: bool, #[getset(get_copy = "pub")] @@ -76,7 +73,6 @@ pub struct EngineOptionsExternal { pub frontend_websocket_port: Option, pub frontend_in_process_channel: bool, pub max_ping_time: u32, - pub log_level: Option, pub allow_raw_messages: bool, pub use_bluetooth_le: bool, pub use_serial_port: bool, @@ -110,7 +106,6 @@ impl From for EngineOptions { frontend_websocket_port: other.frontend_websocket_port, frontend_in_process_channel: other.frontend_in_process_channel, max_ping_time: other.max_ping_time, - log_level: other.log_level, allow_raw_messages: other.allow_raw_messages, use_bluetooth_le: other.use_bluetooth_le, use_serial_port: other.use_serial_port, @@ -261,11 +256,6 @@ impl EngineOptionsBuilder { self } - pub fn log_level(&mut self, level: Level) -> &mut Self { - self.options.log_level = Some(level.to_string()); - self - } - pub fn broadcast_server_mdns(&mut self, value: bool) -> &mut Self { self.options.broadcast_server_mdns = value; self