Skip to content

Commit

Permalink
feat(container-log-page): Hide spinner for zero length logs
Browse files Browse the repository at this point in the history
  • Loading branch information
marhkb committed Dec 27, 2022
1 parent e58e068 commit 1c518e9
Showing 1 changed file with 80 additions and 40 deletions.
120 changes: 80 additions & 40 deletions src/view/container/log_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ mod imp {
Some("status"),
clone!(@weak obj => move |container, _| {
if container.status() == model::ContainerStatus::Running {
obj.follow_log_again();
obj.follow_log();
}
}),
);
}

obj.follow_log();
obj.init_log();
}

fn dispose(&self) {
Expand Down Expand Up @@ -327,46 +327,69 @@ impl LogPage {
imp.prev_adj.replace(adj.value());
}

fn follow_log(&self) {
fn init_log(&self) {
if let Some(container) = self.container().as_ref().and_then(model::Container::api) {
let mut perform = MarkupPerform::default();

utils::run_stream(
utils::run_stream_with_finish_handler(
container,
move |container| {
container
.logs(&basic_opts_builder(true, true).tail("512").build())
.logs(&basic_opts_builder(false, true).tail("512").build())
.boxed()
},
clone!(@weak self as obj => @default-return glib::Continue(false), move |result| {
obj.imp().stack.set_visible_child_name("loaded");
obj.append_line(result, &mut perform)
}),
clone!(@weak self as obj => move || {
obj.imp().stack.set_visible_child_name("loaded");
obj.follow_log();
}),
);
}
}

fn follow_log_again(&self) {
fn follow_log(&self) {
if let Some(container) = self.container().as_ref().and_then(model::Container::api) {
let mut perform = MarkupPerform::default();

let log_timestamps = self.imp().log_timestamps.borrow();
let opts = if !log_timestamps.is_empty() {
let since =
glib::DateTime::from_iso8601(&log_timestamps[log_timestamps.len() - 1], None)
.unwrap()
.to_unix()
+ 1;
basic_opts_builder(true, true).since(since.to_string())
} else {
basic_opts_builder(true, true)
let timestamps = self.imp().log_timestamps.borrow();
let mut iter = timestamps.iter().rev();

let opts = basic_opts_builder(true, true);
let (opts, mut skip) = match iter.next() {
Some(last) => (
opts.since(
glib::DateTime::from_iso8601(last, None)
.unwrap()
.to_unix()
.to_string(),
),
1 + iter.take_while(|t| *t == last).count(),
),
None => (opts, 0),
};

let mut perform = MarkupPerform::default();

utils::run_stream(
container,
move |container| container.logs(&opts.build()).boxed(),
clone!(@weak self as obj => @default-return glib::Continue(false), move |result| {
obj.append_line(result, &mut perform)
clone!(@weak self as obj => @default-return glib::Continue(false), move |result: podman::Result<podman::conn::TtyChunk>| {
if skip == 0 {
obj.append_line(result, &mut perform)
} else {
glib::Continue(
if let Some(line) = obj.foo(result) {
if perform.decode(&line) {
perform.move_out_buffer();
skip -= 1;
}
true
} else {
true
},
)
}
}),
);
}
Expand Down Expand Up @@ -394,34 +417,51 @@ impl LogPage {
})
}

fn foo(&self, result: podman::Result<podman::conn::TtyChunk>) -> Option<Vec<u8>> {
match result {
Ok(line) => Some(Vec::from(line)),
Err(e) => {
log::warn!("Stopping container log stream due to error: {e}");
utils::show_error_toast(
self.upcast_ref(),
&gettext("Error while following log"),
&e.to_string(),
);
None
}
}
}

fn insert(&self, line: Vec<u8>, perform: &mut MarkupPerform, at_end: bool) {
let imp = self.imp();
if perform.decode(&line) {
let line_buffer = perform.move_out_buffer();

if let Some((timestamp, log_message)) = line_buffer.split_once(' ') {
imp.fetch_until.get_or_init(|| timestamp.to_owned());
let (timestamp, log_message) = line_buffer.split_once(' ').unwrap();

let source_buffer = &*imp.source_buffer;
source_buffer.insert_markup(
&mut if at_end {
imp.source_buffer.end_iter()
} else {
imp.source_buffer.start_iter()
},
&if source_buffer.start_iter() == source_buffer.end_iter() {
Cow::Borrowed(log_message)
} else {
Cow::Owned(format!("\n{}", log_message))
},
);
imp.fetch_until.get_or_init(|| timestamp.to_owned());

let mut timestamps = imp.log_timestamps.borrow_mut();
if at_end {
timestamps.push_back(timestamp.to_owned());
let source_buffer = &*imp.source_buffer;
source_buffer.insert_markup(
&mut if at_end {
imp.source_buffer.end_iter()
} else {
timestamps.push_front(timestamp.to_owned());
}
imp.source_buffer.start_iter()
},
&if source_buffer.start_iter() == source_buffer.end_iter() {
Cow::Borrowed(log_message)
} else if at_end {
Cow::Owned(format!("\n{}", log_message))
} else {
Cow::Owned(format!("{}\n", log_message))
},
);

let mut timestamps = imp.log_timestamps.borrow_mut();
if at_end {
timestamps.push_back(timestamp.to_owned());
} else {
timestamps.push_front(timestamp.to_owned());
}
}
}
Expand Down

0 comments on commit 1c518e9

Please sign in to comment.