use crate::{Log, is_heartbeat};
#[derive(Debug, Clone, Copy)]
pub struct ScreenLogOptions {
pub show_events: bool,
pub show_heartbeats: bool,
pub show_incoming: bool,
pub show_outgoing: bool,
pub include_milliseconds: bool,
}
impl Default for ScreenLogOptions {
fn default() -> Self {
Self {
show_events: true,
show_heartbeats: true,
show_incoming: true,
show_outgoing: true,
include_milliseconds: false,
}
}
}
pub struct ScreenLog {
options: ScreenLogOptions,
}
impl Default for ScreenLog {
fn default() -> Self {
Self::new()
}
}
impl ScreenLog {
pub fn new() -> Self {
Self::with_options(ScreenLogOptions::default())
}
pub fn with_options(options: ScreenLogOptions) -> Self {
Self { options }
}
fn timestamp_prefix(&self) -> String {
crate::file::format_timestamp_prefix(self.options.include_milliseconds)
}
}
impl Log for ScreenLog {
fn on_incoming(&self, message: &str) {
if !self.options.show_incoming {
return;
}
if is_heartbeat(message) && !self.options.show_heartbeats {
return;
}
println!("{}<incoming> {message}", self.timestamp_prefix());
}
fn on_outgoing(&self, message: &str) {
if !self.options.show_outgoing {
return;
}
if is_heartbeat(message) && !self.options.show_heartbeats {
return;
}
println!("{}<outgoing> {message}", self.timestamp_prefix());
}
fn on_event(&self, text: &str) {
if !self.options.show_events {
return;
}
eprintln!("{}<event> {text}", self.timestamp_prefix());
}
}