added logging instead of print statements

This commit is contained in:
Nickiel12 2024-03-26 19:08:07 -07:00
parent bab184f140
commit 8572d89103
5 changed files with 132 additions and 31 deletions

96
Cargo.lock generated
View file

@ -328,6 +328,15 @@ dependencies = [
"typenum",
]
[[package]]
name = "deranged"
version = "0.3.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4"
dependencies = [
"powerfmt",
]
[[package]]
name = "digest"
version = "0.8.1"
@ -885,7 +894,7 @@ dependencies = [
"log 0.3.9",
"mime",
"num_cpus",
"time",
"time 0.1.45",
"traitobject",
"typeable",
"unicase",
@ -966,7 +975,9 @@ dependencies = [
"config",
"gilrs",
"gtk4",
"log 0.4.21",
"serde",
"simplelog",
"toml",
"websocket",
]
@ -1198,6 +1209,12 @@ dependencies = [
"minimal-lexical",
]
[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num_cpus"
version = "1.16.0"
@ -1208,6 +1225,15 @@ dependencies = [
"libc",
]
[[package]]
name = "num_threads"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9"
dependencies = [
"libc",
]
[[package]]
name = "once_cell"
version = "1.19.0"
@ -1405,6 +1431,12 @@ version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
[[package]]
name = "powerfmt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "proc-macro-crate"
version = "3.1.0"
@ -1764,6 +1796,17 @@ dependencies = [
"digest 0.10.7",
]
[[package]]
name = "simplelog"
version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16257adbfaef1ee58b1363bdc0664c9b8e1e30aed86049635fb5f147d065a9c0"
dependencies = [
"log 0.4.21",
"termcolor",
"time 0.3.34",
]
[[package]]
name = "slab"
version = "0.4.9"
@ -1841,6 +1884,15 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "termcolor"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [
"winapi-util",
]
[[package]]
name = "thiserror"
version = "1.0.58"
@ -1872,6 +1924,39 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "time"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749"
dependencies = [
"deranged",
"itoa",
"libc",
"num-conv",
"num_threads",
"powerfmt",
"serde",
"time-core",
"time-macros",
]
[[package]]
name = "time-core"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3"
[[package]]
name = "time-macros"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ba3a3ef41e6672a2f0f001392bb5dcd3ff0a9992d618ca761a11c3121547774"
dependencies = [
"num-conv",
"time-core",
]
[[package]]
name = "tiny-keccak"
version = "2.0.2"
@ -2279,6 +2364,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View file

@ -10,6 +10,8 @@ async-channel = "2.2.0"
config = "0.14.0"
gilrs = "0.10.6"
gtk = { version = "0.8.1", package = "gtk4", features = ["v4_12"] }
log = "0.4.21"
serde = { version = "1.0.197", features = ["derive"] }
simplelog = "0.12.2"
toml = "0.8.12"
websocket = "0.27.0"

View file

@ -4,18 +4,18 @@ use crate::JoystickThreadUpdate;
use async_channel::{Receiver, Sender};
use gilrs::{ev::filter::FilterFn, Axis, Button, Event, EventType, Filter, Gilrs, GilrsBuilder};
use std::net::TcpStream;
use std::panic::{self, AssertUnwindSafe};
use std::{
sync::{atomic::AtomicBool, Arc},
time::{Duration, Instant},
panic::{self, AssertUnwindSafe},
net::TcpStream,
};
use websocket::client::{sync::Client, ClientBuilder};
use websocket::Message;
use log::{info, error};
static MAX_RETRY_ATTEMPTS: u32 = 10;
struct SocketState {
pub ip: String,
pub port: i32,
@ -29,7 +29,7 @@ impl SocketState {
fn close_websocket(&mut self) {
if let Some(ref mut x) = self.socket {
println!("closing websocket");
info!("closing websocket");
x.send_message(&Message::close()).unwrap();
x.shutdown().unwrap();
self.socket = None;
@ -39,22 +39,21 @@ impl SocketState {
fn reconnect_websocket(&mut self) -> bool {
if self.ip.is_empty() {
self.socket = None;
return false
return false;
}
if let Ok(mut val) =
ClientBuilder::new(format!("ws://{}:{}", &self.ip, self.port).as_str())
if let Ok(mut val) = ClientBuilder::new(format!("ws://{}:{}", &self.ip, self.port).as_str())
{
if let Ok(val2) = val.connect_insecure() {
self.socket = Some(val2);
true
} else {
println!("couldn't connect websocket! : Step 1");
error!("couldn't connect websocket! : Step 1");
self.socket = None;
false
}
} else {
println!("couldn't connect websocket! : Step 2");
error!("couldn't connect websocket! : Step 2");
self.socket = None;
false
}
@ -96,7 +95,6 @@ impl FilterFn for UnknownSlayer {
}
}
pub fn joystick_websocket_loop(
tx: Sender<JoystickThreadUpdate>,
do_run: Arc<AtomicBool>,
@ -108,7 +106,7 @@ pub fn joystick_websocket_loop(
socket: SocketState {
ip: String::new(),
port: 0,
socket: None
socket: None,
},
try_reconnect: false,
retry_attempts: 0,
@ -119,7 +117,6 @@ pub fn joystick_websocket_loop(
last_update_time: Instant::now(),
};
loop {
match rx.try_recv() {
Ok(msg) => {
@ -131,7 +128,7 @@ pub fn joystick_websocket_loop(
port: state.socket.port as u32,
});
println!("ws://{}:{}", state.socket.ip, state.socket.port);
info!("Connecting to: ws://{}:{}", state.socket.ip, state.socket.port);
if msg.start_websocket {
if !state.socket.is_connected() {
@ -146,18 +143,18 @@ pub fn joystick_websocket_loop(
}
if state.try_reconnect {
if state.retry_attempts > MAX_RETRY_ATTEMPTS { state.try_reconnect = false; }
if state.retry_attempts > MAX_RETRY_ATTEMPTS {
state.try_reconnect = false;
}
if state.socket.is_connected() {
state.try_reconnect = false;
} else if state.socket.reconnect_websocket() {
state.try_reconnect = false;
state.retry_attempts = 0;
}
else {
} else {
state.retry_attempts += 1;
}
}
// catch unwind because some buttons on the joystick will panic the gilrs object
@ -184,7 +181,7 @@ pub fn joystick_websocket_loop(
})) {
Ok(_) => {}
Err(_) => {
println!("panic-causing event captured in gilrs event handler")
info!("panic-causing event captured in gilrs event handler")
}
}
@ -225,7 +222,7 @@ pub fn joystick_websocket_loop(
}
if !do_run.load(std::sync::atomic::Ordering::SeqCst) {
println!("Exiting thread");
info!("Exiting joystick thread");
break;
}
std::thread::sleep(Duration::from_millis(25));

View file

@ -1,6 +1,8 @@
use gtk::gdk::Display;
use gtk::{glib, Application};
use gtk::{prelude::*, CssProvider};
use log::info;
use simplelog::SimpleLogger;
use std::env;
mod config;
@ -17,6 +19,13 @@ pub struct JoystickThreadUpdate {
fn main() -> glib::ExitCode {
env::set_var("gtk_csd", "0");
match SimpleLogger::init(simplelog::LevelFilter::Debug, simplelog::Config::default()) {
Ok(_) => {}
Err(e) => {
println!("Failed to init the simplelogger!: {}", e);
}
}
// Create a new application
let app = Application::builder().application_id(APP_ID).build();
@ -27,7 +36,7 @@ fn main() -> glib::ExitCode {
// Run the application
let exit_code = app.run();
println!("Closing down");
info!("Closing down");
exit_code
}

View file

@ -1,8 +1,8 @@
use gtk::{glib, prelude::*, Box, Entry, Label, ListBox};
use gtk::{Application, ApplicationWindow, Button};
use serde::{Deserialize, Serialize};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::sync::{Arc, atomic::AtomicBool};
use log::error;
use crate::config::load_config;
use crate::{joystick_loop, JoystickThreadUpdate};
@ -97,7 +97,7 @@ pub fn build_ui(app: &Application) {
}) {
Ok(_) => { }
Err(async_channel::TrySendError::Closed(_)) => {panic!("Joystick thread was closed. Unrecoverable")}
Err(e) => {println!("There was an error: {e}")}
Err(e) => {error!("There was an error: {e}")}
}
}
}
@ -105,7 +105,6 @@ pub fn build_ui(app: &Application) {
glib::spawn_future_local(
glib::clone!(@weak axis_label, @weak button, @weak conn_status_label, @weak ip_entry, @weak port_entry, @strong rx => async move {
println!("Hello from spawn future local");
while let Ok(msg) = rx.recv().await {
axis_label.set_text(
format!("X: {:>4} Y: {:>4}", msg.x_axis.unwrap_or("0".to_string()), msg.y_axis.unwrap_or("0".to_string())).as_str()