Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug output pane to cu-consolemon #219

Merged
merged 6 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions components/monitors/cu_consolemon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ categories.workspace = true
homepage.workspace = true
repository.workspace = true

[features]
default = ["debug_pane"]
AS1100K marked this conversation as resolved.
Show resolved Hide resolved
# Enables 'Debug Output' Pane
debug_pane = ["dep:log", "dep:chrono", "dep:dashmap"]

[dependencies]
cu29 = { workspace = true }
cu29-log-runtime = { workspace = true }
compact_str = { workspace = true }
ratatui = "0.29"
pfetch-logo-parser = "0.1"
Expand All @@ -23,3 +29,6 @@ tui-nodes = "0.8"
tui-widgets = "0.4" # 0.4.0 brings ratatui 0.29
color-eyre = "0.6"
gag = "1.0.0"
log = { version = "0.4", optional = true }
chrono = { version = "0.4", optional = true }
dashmap = { version = "6.1", optional = true }
7 changes: 5 additions & 2 deletions components/monitors/cu_consolemon/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ And in you copperconfig.ron:

```

The monitor has 3 screens:
The monitor has 4 screens:

- **SysInfo**: A quick system information screen (CPU, Memory, Distrib ...)
- **DAG**: A Directed Acyclic Graph of the tasks with their real time error status and short string info.
- **Latencies**: A list of the tasks with their real time latencies & assorted statistics (Jitter, Min, Max, Avg).
- **Debug Output** [`debug_pane`](#debug_pane-feature): A pane that displays debug logs in real-time.

## `debug_pane` feature


Enabled by default. Disable with `default-features = false`. Displays real-time logs
from [log](https://crates.io/crates/log), [cu29-log](https://crates.io/crates/cu29-log) and `stderr`.
135 changes: 135 additions & 0 deletions components/monitors/cu_consolemon/src/debug_pane.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use crate::UI;
use ratatui::layout::Rect;
use ratatui::widgets::{Block, Borders, Paragraph};
use ratatui::Frame;
use std::sync::atomic::Ordering;
use {
compact_str::CompactStringExt,
log::{Level, LevelFilter, Log, Metadata, Record},
std::collections::VecDeque,
std::io::Read,
std::sync::atomic::AtomicU16,
std::sync::mpsc::{Receiver, SyncSender},
};

pub struct DebugLog {
debug_log: VecDeque<String>,
pub(crate) max_rows: AtomicU16,
rx: Receiver<String>,
}

impl DebugLog {
pub fn new(max_lines: u16) -> (Self, SyncSender<String>) {
let (tx, rx) = std::sync::mpsc::sync_channel(1000);
(
Self {
debug_log: VecDeque::new(),
max_rows: AtomicU16::new(max_lines),
rx,
},
tx,
)
}

pub fn push_logs(&mut self, logs: String) {
if logs.is_empty() {
return;
}

self.debug_log.push_back(logs);
let max_row = self.max_rows.load(Ordering::SeqCst) as usize;
while self.debug_log.len() > max_row {
self.debug_log.pop_front();
}
}

pub fn update_logs(&mut self) {
let max_row = self.max_rows.load(Ordering::SeqCst) as usize;

for log in self.rx.try_iter() {
if log.is_empty() {
continue;
}

self.debug_log.push_back(log);
if self.debug_log.len() > max_row {
self.debug_log.pop_front();
}
}
}

pub fn get_logs(&mut self) -> String {
self.debug_log.join_compact("").to_string()
}
}

#[derive(Clone)]
pub struct LogSubscriber {
tx: SyncSender<String>,
}

impl LogSubscriber {
pub fn new(tx: SyncSender<String>) -> Self {
let log_subscriber = Self { tx };
log::set_boxed_logger(Box::new(log_subscriber.clone())).unwrap();
log::set_max_level(LevelFilter::Info);
log_subscriber
}

pub fn push_logs(&self, log: String) {
if let Err(err) = self.tx.send(log) {
eprintln!("Error Sending Logs to MPSC Channel: {}", err.0)
}
}
}

impl Log for LogSubscriber {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Debug
}

fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
let message = format!("[{}] - {}\n", record.level(), record.args());

self.push_logs(message);
}
}

fn flush(&self) {}
}

pub trait UIExt {
fn update_debug_output(&mut self);

fn draw_debug_output(&mut self, f: &mut Frame, area: Rect);
}

impl UIExt for UI {
fn update_debug_output(&mut self) {
let mut error_buffer = String::new();
self.error_redirect
.read_to_string(&mut error_buffer)
.unwrap();
self.debug_output.push_logs(error_buffer);
self.debug_output.update_logs();
}

fn draw_debug_output(&mut self, f: &mut Frame, area: Rect) {
let mut error_buffer = String::new();
self.error_redirect
.read_to_string(&mut error_buffer)
.unwrap();
self.debug_output.push_logs(error_buffer);

let debug_output = self.debug_output.get_logs();

let p = Paragraph::new(debug_output).block(
Block::default()
.title(" Debug Output ")
.title_bottom(format!("{} log entries", self.debug_output.debug_log.len()))
.borders(Borders::ALL),
);
f.render_widget(p, area);
}
}
103 changes: 95 additions & 8 deletions components/monitors/cu_consolemon/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "debug_pane")]
mod debug_pane;
pub mod sysinfo;

use ansi_to_tui::IntoText;
Expand All @@ -8,7 +10,8 @@ use cu29::config::{CuConfig, Node};
use cu29::cutask::CuMsgMetadata;
use cu29::monitoring::{CuDurationStatistics, CuMonitor, CuTaskState, Decision};
use cu29::{CuError, CuResult};
use gag::Gag;
#[cfg(feature = "debug_pane")]
use debug_pane::UIExt;
use ratatui::backend::CrosstermBackend;
use ratatui::buffer::Buffer;
use ratatui::crossterm::event::{DisableMouseCapture, EnableMouseCapture, Event, KeyCode};
Expand All @@ -33,11 +36,18 @@ use std::{io, thread};
use tui_nodes::{Connection, NodeGraph, NodeLayout};
use tui_widgets::scrollview::{ScrollView, ScrollViewState};

#[cfg(feature = "debug_pane")]
const MENU_CONTENT: &str = " [1] SysInfo [2] DAG [3] Latencies [4] Debug Output [q] Quit ";
#[cfg(not(feature = "debug_pane"))]
const MENU_CONTENT: &str = " [1] SysInfo [2] DAG [3] Latencies [q] Quit ";

#[derive(PartialEq)]
enum Screen {
Neofetch,
Dag,
Latency,
#[cfg(feature = "debug_pane")]
DebugOutput,
}

struct TaskStats {
Expand Down Expand Up @@ -282,9 +292,38 @@ struct UI {
sysinfo: String,
task_stats: Arc<Mutex<TaskStats>>,
nodes_scrollable_widget_state: NodesScrollableWidgetState,
#[cfg(feature = "debug_pane")]
error_redirect: gag::BufferRedirect,
#[cfg(feature = "debug_pane")]
debug_output: debug_pane::DebugLog,
}

impl UI {
#[cfg(feature = "debug_pane")]
fn new(
config: CuConfig,
task_ids: &'static [&'static str],
task_stats: Arc<Mutex<TaskStats>>,
task_statuses: Arc<Mutex<Vec<TaskStatus>>>,
error_redirect: gag::BufferRedirect,
debug_output: debug_pane::DebugLog,
) -> UI {
init_error_hooks();
let nodes_scrollable_widget_state =
NodesScrollableWidgetState::new(&config, task_statuses.clone());

Self {
task_ids,
active_screen: Screen::Neofetch,
sysinfo: sysinfo::pfetch_info(),
task_stats,
nodes_scrollable_widget_state,
error_redirect,
debug_output,
}
}

#[cfg(not(feature = "debug_pane"))]
fn new(
config: CuConfig,
task_ids: &'static [&'static str],
Expand All @@ -294,6 +333,7 @@ impl UI {
init_error_hooks();
let nodes_scrollable_widget_state =
NodesScrollableWidgetState::new(&config, task_statuses.clone());

Self {
task_ids,
active_screen: Screen::Neofetch,
Expand Down Expand Up @@ -443,7 +483,7 @@ impl UI {
)
.split(f.area());

let menu = Paragraph::new(" [1] SysInfo [2] DAG [3] Latencies [q] Quit")
let menu = Paragraph::new(MENU_CONTENT)
.style(
Style::default()
.fg(Color::Yellow)
Expand All @@ -469,11 +509,16 @@ impl UI {
self.draw_nodes(f, layout[1]);
}
Screen::Latency => self.draw_latency_table(f, layout[1]),
#[cfg(feature = "debug_pane")]
Screen::DebugOutput => self.draw_debug_output(f, layout[1]),
};
}

fn run_app<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> io::Result<()> {
loop {
#[cfg(feature = "debug_pane")]
self.update_debug_output();

terminal.draw(|f| {
self.draw(f);
})?;
Expand All @@ -484,6 +529,8 @@ impl UI {
KeyCode::Char('1') => self.active_screen = Screen::Neofetch,
KeyCode::Char('2') => self.active_screen = Screen::Dag,
KeyCode::Char('3') => self.active_screen = Screen::Latency,
#[cfg(feature = "debug_pane")]
KeyCode::Char('4') => self.active_screen = Screen::DebugOutput,
KeyCode::Char('r') => {
if self.active_screen == Screen::Latency {
self.task_stats.lock().unwrap().reset()
Expand Down Expand Up @@ -531,6 +578,11 @@ impl UI {
_ => {}
}
}

#[cfg(feature = "debug_pane")]
if let Event::Resize(_columns, rows) = event::read()? {
self.debug_output.max_rows.store(rows, Ordering::SeqCst)
}
}
AS1100K marked this conversation as resolved.
Show resolved Hide resolved
}
Ok(())
Expand Down Expand Up @@ -570,16 +622,51 @@ impl CuMonitor for CuConsoleMon {

setup_terminal();

// nukes stderr into orbit as anything in the stack can corrupt the terminal
let print_gag = Gag::stderr().unwrap();

let mut terminal =
Terminal::new(backend).expect("Failed to initialize terminal backend");
let mut ui = UI::new(config_dup, taskids, task_stats_ui, error_states);
ui.run_app(&mut terminal).expect("Failed to run app");

#[cfg(feature = "debug_pane")]
{
let max_lines = terminal.size().unwrap().height - 5;
let (debug_log, tx) = debug_pane::DebugLog::new(max_lines);

// redirect stderr, so it doesn't pop in the terminal
let error_redirect = gag::BufferRedirect::stderr().unwrap();

let mut ui = UI::new(
config_dup,
taskids,
task_stats_ui,
error_states,
error_redirect,
debug_log,
);

#[allow(unused_variables)]
let log_subscriber = debug_pane::LogSubscriber::new(tx);

// Override the cu29-log-runtime Log Subscriber
#[cfg(debug_assertions)]
{
*cu29_log_runtime::EXTRA_TEXT_LOGGER.write().unwrap() =
Some(Box::new(log_subscriber) as Box<dyn log::Log>);
}

ui.run_app(&mut terminal).expect("Failed to run app");
}

#[cfg(not(feature = "debug_pane"))]
AS1100K marked this conversation as resolved.
Show resolved Hide resolved
{
let stderr_gag = gag::Gag::stderr().unwrap();

let mut ui = UI::new(config_dup, taskids, task_stats_ui, error_states);
ui.run_app(&mut terminal).expect("Failed to run app");

drop(stderr_gag);
}

quitting.store(true, Ordering::SeqCst);
// restoring the terminal
drop(print_gag);
restore_terminal();
});

Expand Down
Loading
Loading