more debugging

This commit is contained in:
Richard
2026-01-28 22:00:10 +00:00
parent 1e35286eec
commit 7360f7ee70

View File

@@ -13,7 +13,7 @@ use tokio_tungstenite::{
Message,
},
};
use tracing::{error, info, warn};
use tracing::{debug, error, info, warn, Level};
#[derive(Parser)]
#[command(name = "websocket-debug")]
@@ -26,17 +26,23 @@ struct Args {
/// Bearer token for Authorization header
#[arg(long)]
bearer_token: Option<String>,
/// Enable debug logging (shows request/response headers)
#[arg(long)]
debug: bool,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();
let log_level = if args.debug { Level::DEBUG } else { Level::INFO };
tracing_subscriber::fmt()
.with_target(false)
.with_thread_ids(false)
.with_max_level(log_level)
.init();
let args = Args::parse();
// Create session directory
let timestamp = Local::now().format("%Y%m%d-%H%M%S");
let session_dir = PathBuf::from(format!("session-{}", timestamp));
@@ -49,6 +55,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let letter = (b'A' + idx as u8) as char;
let bearer_token = args.bearer_token.clone();
let url = url.clone();
let debug_enabled = args.debug;
connect_futures.push(async move {
let mut request = url
@@ -64,12 +71,42 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
info!("[{}] Connecting to {}", letter, url);
let (ws_stream, response) = connect_async_with_config(request, None, false)
.await
.map_err(|e| format!("[{}] Failed to connect to {}: {}", letter, url, e))?;
if debug_enabled {
debug!("[{}] Request: {} {}", letter, request.method(), request.uri());
for (name, value) in request.headers() {
debug!("[{}] {}: {}", letter, name, value.to_str().unwrap_or("<binary>"));
}
}
let (ws_stream, response) = match connect_async_with_config(request, None, false).await {
Ok(result) => result,
Err(e) => {
if debug_enabled {
if let tokio_tungstenite::tungstenite::Error::Http(ref response) = e {
debug!("[{}] Error response: {} {}", letter, response.status().as_u16(), response.status().canonical_reason().unwrap_or(""));
for (name, value) in response.headers() {
debug!("[{}] {}: {}", letter, name, value.to_str().unwrap_or("<binary>"));
}
if let Some(body) = response.body() {
if let Ok(body_str) = std::str::from_utf8(body) {
debug!("[{}] Response body: {}", letter, body_str);
}
}
}
}
return Err(format!("[{}] Failed to connect to {}: {}", letter, url, e));
}
};
info!("[{}] Connected successfully (status: {})", letter, response.status());
if debug_enabled {
debug!("[{}] Response: {} {}", letter, response.status().as_u16(), response.status().canonical_reason().unwrap_or(""));
for (name, value) in response.headers() {
debug!("[{}] {}: {}", letter, name, value.to_str().unwrap_or("<binary>"));
}
}
Ok::<_, String>((letter, url, ws_stream))
});
}