Skip to content

Commit

Permalink
chore: Remove logging for library instances
Browse files Browse the repository at this point in the history
Logging should be handled by applications, not by the library.
  • Loading branch information
qdot committed Nov 23, 2023
1 parent e3b006d commit ad1a572
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 217 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
75 changes: 46 additions & 29 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -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");

Expand Down Expand Up @@ -156,12 +159,12 @@ pub struct IntifaceCLIArguments {
#[argh(option)]
#[getset(get_copy = "pub")]
repeater_port: Option<u16>,

/// if set, use repeater mode instead of engine mode
#[argh(option)]
#[getset(get = "pub")]
repeater_remote_address: Option<String>,

#[cfg(debug_assertions)]
/// crash the main thread (that holds the runtime)
#[argh(switch)]
Expand All @@ -176,6 +179,29 @@ pub struct IntifaceCLIArguments {
crash_task_thread: bool,
}

pub fn setup_console_logging(log_level: Option<Level>) {
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<IntifaceCLIArguments> for EngineOptions {
type Error = IntifaceError;
fn try_from(args: IntifaceCLIArguments) -> Result<Self, IntifaceError> {
Expand Down Expand Up @@ -236,9 +262,6 @@ impl TryFrom<IntifaceCLIArguments> 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);
}
Expand All @@ -260,7 +283,7 @@ impl TryFrom<IntifaceCLIArguments> 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() {
Expand All @@ -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(())
}
17 changes: 9 additions & 8 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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;

Expand Down Expand Up @@ -133,7 +132,6 @@ impl IntifaceEngine {
&self,
options: &EngineOptions,
external_frontend: Option<Arc<dyn Frontend>>,
skip_logging_setup: bool,
) -> Result<(), IntifaceEngineError> {
// At this point we will have received and validated options.

Expand All @@ -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();
Expand All @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ mod device_communication_managers;
mod engine;
mod error;
mod frontend;
mod logging;
mod options;
mod remote_server;
mod repeater;
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;
166 changes: 0 additions & 166 deletions src/logging.rs

This file was deleted.

Loading

0 comments on commit ad1a572

Please sign in to comment.