Update debug view

This commit is contained in:
Alexandre Bury 2019-02-22 13:55:07 -08:00
parent 8b3f5a8de7
commit 3a7e10cffe
8 changed files with 52 additions and 10 deletions

View File

@ -30,6 +30,7 @@ libc = "0.2.47"
term_size = { version = "0.3.1", optional = true }
crossbeam-channel = "0.3.6"
lazy_static = "1.2.0"
chrono = "0.4.6"
[dependencies.num]
default-features = false

View File

@ -10,7 +10,7 @@ fn main() {
let mut siv = cursive::Cursive::default();
siv.add_global_callback('q', cursive::Cursive::quit);
siv.add_global_callback('~', cursive::Cursive::toggle_debug_view);
siv.add_global_callback('l', |_| debug!("Wooo"));
siv.add_global_callback('l', |_| warn!("Wooo"));
error!("BAD!!!");
siv.run();

View File

@ -188,7 +188,6 @@ impl Cursive {
let stack = self.screen_mut();
if let Some(pos) = stack.find_layer_from_id(DEBUG_VIEW_ID) {
info!("Foundit");
stack.remove_layer(pos);
} else {
stack.add_layer(

View File

@ -81,6 +81,7 @@ extern crate maplit;
#[cfg(unix)]
extern crate signal_hook;
extern crate chrono;
extern crate libc;
extern crate num;
extern crate owning_ref;

View File

@ -10,9 +10,19 @@ struct CursiveLogger;
static LOGGER: CursiveLogger = CursiveLogger;
/// A log record.
pub struct Record {
/// Log level used for this record
pub level: log::Level,
/// Time this message was logged
pub time: chrono::DateTime<chrono::Utc>,
/// Message content
pub message: String,
}
lazy_static! {
/// Circular buffer for logs. Use it to implement `DebugView`.
pub static ref LOGS: Mutex<VecDeque<(log::Level, String)>> =
pub static ref LOGS: Mutex<VecDeque<Record>> =
Mutex::new(VecDeque::new());
}
@ -27,7 +37,11 @@ impl log::Log for CursiveLogger {
if logs.len() == logs.capacity() {
logs.pop_front();
}
logs.push_back((record.level(), format!("{}", record.args())));
logs.push_back(Record {
level: record.level(),
message: format!("{}", record.args()),
time: chrono::Utc::now(),
});
}
fn flush(&self) {}

View File

@ -121,7 +121,7 @@ impl Color {
/// * `"light green"` becomes `Color::Light(BaseColor::Green)`
/// * `"default"` becomes `Color::TerminalDefault`
/// * `"#123456"` becomes `Color::Rgb(0x12, 0x34, 0x56)`
pub(crate) fn parse(value: &str) -> Option<Self> {
pub fn parse(value: &str) -> Option<Self> {
Some(match value {
"black" => Color::Dark(BaseColor::Black),
"red" => Color::Dark(BaseColor::Red),

View File

@ -1,4 +1,4 @@
use super::{Color, ColorPair, Palette, PaletteColor};
use super::{BaseColor, Color, ColorPair, Palette, PaletteColor};
/// Possible color style for a cell.
///
@ -91,6 +91,12 @@ impl From<Color> for ColorStyle {
}
}
impl From<BaseColor> for ColorStyle {
fn from(color: BaseColor) -> Self {
Self::new(Color::Dark(color), PaletteColor::View)
}
}
impl From<PaletteColor> for ColorStyle {
fn from(color: PaletteColor) -> Self {
Self::new(color, PaletteColor::View)

View File

@ -1,4 +1,5 @@
use logger;
use theme;
use vec::Vec2;
use view::View;
use Printer;
@ -24,8 +25,26 @@ impl View for DebugView {
// Only print the last logs, so skip what doesn't fit
let skipped = logs.len().saturating_sub(printer.size.y);
for (i, &(level, ref text)) in logs.iter().skip(skipped).enumerate() {
printer.print((0, i), &format!("[{}] {}", level, text));
for (i, record) in logs.iter().skip(skipped).enumerate() {
// TODO: Apply style to message? (Ex: errors in bold?)
printer.print(
(0, i),
&format!(
"{} | [ ] {}",
record.time.with_timezone(&chrono::Local).format("%T%.3f"),
record.message
),
);
let color = match record.level {
log::Level::Error => theme::BaseColor::Red,
log::Level::Warn => theme::BaseColor::Yellow,
log::Level::Info => theme::BaseColor::Black,
log::Level::Debug => theme::BaseColor::Green,
log::Level::Trace => theme::BaseColor::Blue,
};
printer.with_color(color.into(), |printer| {
printer.print((16, i), &format!("{:5}", record.level))
});
}
}
@ -33,10 +52,12 @@ impl View for DebugView {
// TODO: read the logs, and compute the required size to print it.
let logs = logger::LOGS.lock().unwrap();
// The longest line sets the width
let level_width = 8; // Width of "[ERROR] "
let time_width = 16; // Width of "23:59:59.123 | "
// The longest line sets the width
let w = logs
.iter()
.map(|&(_, ref content)| content.width() + "[ERROR] ".width())
.map(|record| record.message.width() + level_width + time_width)
.max()
.unwrap_or(1);
let h = logs.len();