Skip to content

Commit

Permalink
server/logging_pipe: Replace log::error!
Browse files Browse the repository at this point in the history
with better error logging by means of `non_fatal`. This preserves the
original error and allows adding context information on top. Also makes
error formatting more uniform across the application.
  • Loading branch information
har7an committed Oct 30, 2022
1 parent 7329de7 commit 48d3ebc
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions zellij-server/src/logging_pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{

use log::{debug, error};
use wasmer_wasi::{WasiFile, WasiFsError};
use zellij_utils::serde;
use zellij_utils::{errors::prelude::*, serde};

use chrono::prelude::*;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -75,27 +75,30 @@ impl Write for LoggingPipe {
fn flush(&mut self) -> std::io::Result<()> {
self.buffer.make_contiguous();

if let Ok(converted_buffer) = std::str::from_utf8(self.buffer.as_slices().0) {
if converted_buffer.contains('\n') {
let mut consumed_bytes = 0;
let mut split_converted_buffer = converted_buffer.split('\n').peekable();

while let Some(msg) = split_converted_buffer.next() {
if split_converted_buffer.peek().is_none() {
// Log last chunk iff the last char is endline. Otherwise do not do it.
if converted_buffer.ends_with('\n') && !msg.is_empty() {
match std::str::from_utf8(self.buffer.as_slices().0) {
Ok(converted_buffer) => {
if converted_buffer.contains('\n') {
let mut consumed_bytes = 0;
let mut split_converted_buffer = converted_buffer.split('\n').peekable();

while let Some(msg) = split_converted_buffer.next() {
if split_converted_buffer.peek().is_none() {
// Log last chunk iff the last char is endline. Otherwise do not do it.
if converted_buffer.ends_with('\n') && !msg.is_empty() {
self.log_message(msg);
consumed_bytes += msg.len() + 1;
}
} else {
self.log_message(msg);
consumed_bytes += msg.len() + 1;
}
} else {
self.log_message(msg);
consumed_bytes += msg.len() + 1;
}
drop(self.buffer.drain(..consumed_bytes));
}
drop(self.buffer.drain(..consumed_bytes));
}
} else {
error!("Buffer conversion didn't work. This is unexpected");
},
Err(e) => Err::<(), _>(e)
.context("failed to flush logging pipe buffer")
.non_fatal(),
}

Ok(())
Expand Down

0 comments on commit 48d3ebc

Please sign in to comment.