Skip to content

Commit

Permalink
Add backtraces to more log
Browse files Browse the repository at this point in the history
Improve messages when backtraces are disabled
  • Loading branch information
zmerp committed Aug 16, 2023
1 parent f650c5d commit 7b6e66f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 39 deletions.
2 changes: 1 addition & 1 deletion alvr/audio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ pub fn receive_samples_loop(
while running.value() {
match receiver.recv_buffer(Duration::from_millis(500), &mut receiver_buffer) {
Ok(true) => (),
Ok(false) | Err(ConnectionError::TryAgain) => continue,
Ok(false) | Err(ConnectionError::TryAgain(_)) => continue,
Err(ConnectionError::Other(e)) => return Err(e),
};

Expand Down
18 changes: 9 additions & 9 deletions alvr/client_core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ pub fn connection_lifecycle_loop(
connection_pipeline(recommended_view_resolution, supported_refresh_rates.clone())
{
let message = format!("Connection error:\n{e}\nCheck the PC for more details");
error!("Connection error: {message}");
set_hud_message(&message);
error!("Connection error: {e}");
}
} else {
debug!("Skip try connection because the device is sleeping");
Expand Down Expand Up @@ -134,7 +134,7 @@ fn connection_pipeline(
}

if let Err(e) = announcer_socket.broadcast() {
warn!("Broadcast error: {e}");
warn!("Broadcast error: {e:?}");

set_hud_message(NETWORK_UNREACHABLE_MESSAGE);

Expand Down Expand Up @@ -260,7 +260,7 @@ fn connection_pipeline(
.to_con()?;

if let Err(e) = control_sender.send(&ClientControlPacket::StreamReady) {
info!("Server disconnected. Cause: {e}");
info!("Server disconnected. Cause: {e:?}");
set_hud_message(SERVER_DISCONNECTED_MESSAGE);
return Ok(());
}
Expand Down Expand Up @@ -313,7 +313,7 @@ fn connection_pipeline(
while IS_STREAMING.value() {
match video_receiver.recv_buffer(STREAMING_RECV_TIMEOUT, &mut receiver_buffer) {
Ok(true) => (),
Ok(false) | Err(ConnectionError::TryAgain) => continue,
Ok(false) | Err(ConnectionError::TryAgain(_)) => continue,
Err(ConnectionError::Other(_)) => return,
}

Expand Down Expand Up @@ -397,7 +397,7 @@ fn connection_pipeline(
while IS_STREAMING.value() {
let haptics = match haptics_receiver.recv_header_only(STREAMING_RECV_TIMEOUT) {
Ok(packet) => packet,
Err(ConnectionError::TryAgain) => continue,
Err(ConnectionError::TryAgain(_)) => continue,
Err(ConnectionError::Other(_)) => return,
};

Expand All @@ -424,7 +424,7 @@ fn connection_pipeline(
&mut *CONTROL_SENDER.lock(),
) {
if let Err(e) = sender.send(&packet) {
info!("Server disconnected. Cause: {e}");
info!("Server disconnected. Cause: {e:?}");
set_hud_message(SERVER_DISCONNECTED_MESSAGE);

break;
Expand Down Expand Up @@ -479,7 +479,7 @@ fn connection_pipeline(
return;
}
Ok(_) => (),
Err(ConnectionError::TryAgain) => (),
Err(ConnectionError::TryAgain(_)) => continue,
Err(e) => {
info!("{SERVER_DISCONNECTED_MESSAGE} Cause: {e}");
set_hud_message(SERVER_DISCONNECTED_MESSAGE);
Expand All @@ -498,8 +498,8 @@ fn connection_pipeline(
let res = stream_socket.recv(runtime, STREAMING_RECV_TIMEOUT);
match res {
Ok(()) => (),
Err(ConnectionError::TryAgain) => continue,
Err(ConnectionError::Other(e)) => {
Err(ConnectionError::TryAgain(_)) => continue,
Err(e) => {
info!("Client disconnected. Cause: {e}");
set_hud_message(SERVER_DISCONNECTED_MESSAGE);
if let Some(notifier) = &*DISCONNECT_SERVER_NOTIFIER.lock() {
Expand Down
24 changes: 10 additions & 14 deletions alvr/common/src/connection_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@ use std::{
};

pub enum ConnectionError {
TryAgain,
TryAgain(anyhow::Error),
Other(anyhow::Error),
}

impl Display for ConnectionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ConnectionError::TryAgain => write!(f, "Timeout"),
ConnectionError::Other(e) => write!(f, "{e}\n{}", e.backtrace()),
}
let (ConnectionError::TryAgain(e) | ConnectionError::Other(e)) = self;
write!(f, "{e:?}")
}
}

pub type ConResult<T = ()> = Result<T, ConnectionError>;

pub fn try_again<T>() -> ConResult<T> {
Err(ConnectionError::TryAgain)
Err(ConnectionError::TryAgain(anyhow!("Try again")))
}

#[macro_export]
Expand All @@ -40,7 +38,7 @@ pub trait ToCon<T> {

impl<T> ToCon<T> for Option<T> {
fn to_con(self) -> ConResult<T> {
self.ok_or_else(|| ConnectionError::Other(anyhow::anyhow!("Unexpected None")))
self.ok_or_else(|| ConnectionError::Other(anyhow!("Unexpected None")))
}
}

Expand Down Expand Up @@ -68,7 +66,7 @@ impl<T> HandleTryAgain<T> for io::Result<T> {
fn handle_try_again(self) -> ConResult<T> {
self.map_err(|e| {
if e.kind() == io::ErrorKind::TimedOut || e.kind() == io::ErrorKind::WouldBlock {
ConnectionError::TryAgain
ConnectionError::TryAgain(e.into())
} else {
ConnectionError::Other(e.into())
}
Expand All @@ -79,19 +77,17 @@ impl<T> HandleTryAgain<T> for io::Result<T> {
impl<T> HandleTryAgain<T> for std::result::Result<T, RecvTimeoutError> {
fn handle_try_again(self) -> ConResult<T> {
self.map_err(|e| match e {
RecvTimeoutError::Timeout => ConnectionError::TryAgain,
RecvTimeoutError::Disconnected => {
ConnectionError::Other(anyhow!("Channel disconnected"))
}
RecvTimeoutError::Timeout => ConnectionError::TryAgain(e.into()),
RecvTimeoutError::Disconnected => ConnectionError::Other(e.into()),
})
}
}

impl<T> HandleTryAgain<T> for std::result::Result<T, TryRecvError> {
fn handle_try_again(self) -> ConResult<T> {
self.map_err(|e| match e {
TryRecvError::Empty => ConnectionError::TryAgain,
TryRecvError::Disconnected => ConnectionError::Other(anyhow!("Channel disconnected")),
TryRecvError::Empty => ConnectionError::TryAgain(e.into()),
TryRecvError::Disconnected => ConnectionError::Other(e.into()),
})
}
}
4 changes: 0 additions & 4 deletions alvr/common/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,3 @@ impl<T, E: Error + Send + Sync + 'static> ToAny<T> for Result<T, E> {
}
}
}

pub fn with_backtrace(error: anyhow::Error) -> String {
format!("{error}\n{}", error.backtrace())
}
22 changes: 11 additions & 11 deletions alvr/server/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ pub fn handshake_loop() {
let mut welcome_socket = match WelcomeSocket::new(RETRY_CONNECT_MIN_INTERVAL) {
Ok(socket) => socket,
Err(e) => {
error!("Failed to create discovery socket: {e}");
error!("Failed to create discovery socket: {e:?}");
return;
}
};
Expand Down Expand Up @@ -257,7 +257,7 @@ pub fn handshake_loop() {
Ok(pair) => pair,
Err(e) => {
if let ConnectionError::Other(e) = e {
warn!("UDP handshake listening error: {e}");
warn!("UDP handshake listening error: {e:?}");
}

continue;
Expand Down Expand Up @@ -589,7 +589,7 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
) {
Ok(data) => data,
Err(e) => {
warn!("New audio device failed: {e}");
warn!("New audio device failed: {e:?}");
thread::sleep(RETRY_CONNECT_MIN_INTERVAL);
continue;
}
Expand Down Expand Up @@ -617,7 +617,7 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
2,
config.mute_when_streaming,
) {
error!("Audio record error: {e}");
error!("Audio record error: {e:?}");
}

#[cfg(windows)]
Expand Down Expand Up @@ -697,7 +697,7 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
while IS_STREAMING.value() {
let tracking = match tracking_receiver.recv_header_only(STREAMING_RECV_TIMEOUT) {
Ok(tracking) => tracking,
Err(ConnectionError::TryAgain) => continue,
Err(ConnectionError::TryAgain(_)) => continue,
Err(ConnectionError::Other(_)) => return,
};

Expand Down Expand Up @@ -806,7 +806,7 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
while IS_STREAMING.value() {
let client_stats = match statics_receiver.recv_header_only(STREAMING_RECV_TIMEOUT) {
Ok(stats) => stats,
Err(ConnectionError::TryAgain) => continue,
Err(ConnectionError::TryAgain(_)) => continue,
Err(ConnectionError::Other(_)) => return,
};

Expand All @@ -833,7 +833,7 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
move || {
while IS_STREAMING.value() {
if let Err(e) = control_sender.lock().send(&ServerControlPacket::KeepAlive) {
info!("Client disconnected. Cause: {e}");
info!("Client disconnected. Cause: {e:?}");

SERVER_DATA_MANAGER.write().update_client_list(
client_hostname,
Expand All @@ -860,8 +860,8 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
while IS_STREAMING.value() {
let packet = match control_receiver.recv() {
Ok(packet) => packet,
Err(ConnectionError::TryAgain) => continue,
Err(ConnectionError::Other(e)) => {
Err(ConnectionError::TryAgain(_)) => continue,
Err(e) => {
info!("Client disconnected. Cause: {e}");

SERVER_DATA_MANAGER.write().update_client_list(
Expand Down Expand Up @@ -994,8 +994,8 @@ fn try_connect(mut client_ips: HashMap<IpAddr, String>) -> ConResult {
let res = stream_socket.recv(runtime, STREAMING_RECV_TIMEOUT);
match res {
Ok(()) => (),
Err(ConnectionError::TryAgain) => continue,
Err(ConnectionError::Other(e)) => {
Err(ConnectionError::TryAgain(_)) => continue,
Err(e) => {
info!("Client disconnected. Cause: {e}");

SERVER_DATA_MANAGER.write().update_client_list(
Expand Down

0 comments on commit 7b6e66f

Please sign in to comment.