Skip to content

Commit

Permalink
feat(wallet): add tab for error log to the wallet (#3250)
Browse files Browse the repository at this point in the history
Description
---
Add a new tab in the console wallet. In the new tab is a stdout log from the wallet. The stdout is redirected to a new logfile called "stdout.log"
I've added also some coloring, it's much easier to read the log file with some simple colors.

Motivation and Context
---
Easier to debug the wallet.

How Has This Been Tested?
---
Manually.
  • Loading branch information
stringhandler committed Aug 26, 2021
2 parents 0048c3b + 84622f7 commit 098f25d
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions applications/tari_console_wallet/Cargo.toml
Expand Up @@ -26,6 +26,7 @@ unicode-width = "0.1"
unicode-segmentation = "1.6.0"
log = { version = "0.4.8", features = ["std"] }
qrcode = { version = "0.12" }
regex = "1.5.4"
rpassword = "5.0"
rustyline = "6.0"
strum = "^0.19"
Expand Down
4 changes: 3 additions & 1 deletion applications/tari_console_wallet/src/ui/app.rs
Expand Up @@ -25,6 +25,7 @@ use crate::{
ui::{
components::{
base_node::BaseNode,
log_tab::LogTab,
menu::Menu,
network_tab::NetworkTab,
receive_tab::ReceiveTab,
Expand Down Expand Up @@ -85,7 +86,8 @@ impl<B: Backend> App<B> {
.add("Transactions".into(), Box::new(TransactionsTab::new()))
.add("Send".into(), Box::new(SendTab::new()))
.add("Receive".into(), Box::new(ReceiveTab::new()))
.add("Network".into(), Box::new(NetworkTab::new(base_node_selected)));
.add("Network".into(), Box::new(NetworkTab::new(base_node_selected)))
.add("Log".into(), Box::new(LogTab::new()));

let base_node_status = BaseNode::new();
let menu = Menu::new();
Expand Down
107 changes: 107 additions & 0 deletions applications/tari_console_wallet/src/ui/components/log_tab.rs
@@ -0,0 +1,107 @@
use crate::ui::{components::Component, state::AppState};
use regex::Regex;
use std::fs;
use tui::{
backend::Backend,
layout::{Constraint, Layout, Rect},
style::{Color, Modifier, Style},
text::{Span, Spans},
widgets::{Block, Borders, Paragraph, Wrap},
Frame,
};

pub struct LogTab {
scroll: u16,
re: Regex,
}

impl LogTab {
pub fn new() -> Self {
Self { scroll: 1,
re : Regex::new(
r"(?P<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d*) \[(?P<target>[^\]]*)\] (?P<level>INFO|WARN|DEBUG|ERROR|TRACE)\s*(?P<message> .*)",
)
.unwrap()
}
}

// Format the log line nicely. If it cannot be parsed then return raw line
fn format_line(&self, line: String) -> Spans {
match self.re.captures(line.as_str()) {
Some(caps) => Spans::from(vec![
Span::styled(caps["timestamp"].to_string(), Style::default().fg(Color::LightGreen)),
Span::raw(" ["),
Span::styled(caps["target"].to_string(), Style::default().fg(Color::LightMagenta)),
Span::raw("] "),
Span::styled(
caps["level"].to_string(),
Style::default().fg(match &caps["level"] {
"ERROR" => Color::LightRed,
"WARN" => Color::LightYellow,
_ => Color::LightMagenta,
}),
),
Span::raw(caps["message"].to_string()),
]),
// In case the line is not well formatted, just print as it is
None => Spans::from(vec![Span::raw(line)]),
}
}

fn draw_logs<B>(&mut self, f: &mut Frame<B>, area: Rect, _app_state: &AppState)
where B: Backend {
// First render the border and calculate the inner area
let block = Block::default().borders(Borders::ALL).title(Span::styled(
"StdOut log",
Style::default().fg(Color::White).add_modifier(Modifier::BOLD),
));
f.render_widget(block, area);
let log_area = Layout::default()
.constraints([Constraint::Min(42)].as_ref())
.margin(1)
.split(area);
// Read the log file
let content = match fs::read_to_string("log/wallet/stdout.log") {
Ok(content) => content,
Err(err) => format!("Error reading log : {}", err),
};
// Convert the content into Spans
let mut text: Vec<Spans> = content
.split('\n')
.map(|line| self.format_line(line.to_string()))
.collect();
// We want newest at the top
text.reverse();
// Render the Paragraph
let paragraph = Paragraph::new(text.clone())
.wrap(Wrap { trim: true })
.scroll((self.scroll, 0));
f.render_widget(paragraph, log_area[0]);
}
}

impl<B: Backend> Component<B> for LogTab {
fn draw(&mut self, f: &mut Frame<B>, area: Rect, app_state: &AppState) {
let areas = Layout::default()
.constraints([Constraint::Min(42)].as_ref())
.split(area);

self.draw_logs(f, areas[0], app_state);
}

fn on_key(&mut self, _app_state: &mut AppState, _c: char) {}

fn on_up(&mut self, _app_state: &mut AppState) {
if self.scroll > 1 {
self.scroll -= 1;
}
}

fn on_down(&mut self, _app_state: &mut AppState) {
self.scroll += 1;
}

fn on_esc(&mut self, _: &mut AppState) {}

fn on_backspace(&mut self, _app_state: &mut AppState) {}
}
1 change: 1 addition & 0 deletions applications/tari_console_wallet/src/ui/components/mod.rs
Expand Up @@ -23,6 +23,7 @@
pub mod balance;
pub mod base_node;
mod component;
pub mod log_tab;
pub(crate) mod menu;
pub mod network_tab;
pub mod receive_tab;
Expand Down
16 changes: 14 additions & 2 deletions common/logging/log4rs_sample_wallet.yml
Expand Up @@ -11,6 +11,17 @@
# timestamp [target] LEVEL message
refresh_rate: 30 seconds
appenders:
# An appender named "stdout" that writes to file.
stdout:
kind: file
path: "log/wallet/stdout.log"
append: false
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} [{t}] {h({l}):5} {m}{n}"
filters:
- kind: threshold
level: warn

# An appender named "network" that writes to a file with a custom pattern encoder
network:
kind: rolling_file
Expand Down Expand Up @@ -45,7 +56,7 @@ appenders:
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S.%f)} [{t}] [Thread:{I}] {l:5} {m}{n}"

# An appender named "base_layer" that writes to a file with a custom pattern encoder
# An appender named "base_layer" that writes to a file with a custom pattern encoder
other:
kind: rolling_file
path: "log/wallet/other.log"
Expand All @@ -67,6 +78,7 @@ root:
level: info
appenders:
- base_layer
- stdout

loggers:
# base_layer
Expand Down Expand Up @@ -106,7 +118,7 @@ loggers:
level: info
appenders:
- network
additive: false
# Route log events sent to the "mio" logger to the "other" appender
mio:
level: error
appenders:
Expand Down

0 comments on commit 098f25d

Please sign in to comment.