Merge branch 'timestamps' into 'main'

Timestamps

See merge request veilid/veilid!201
This commit is contained in:
Christien Rioux 2023-10-07 19:09:10 +00:00
commit abf0e18c77
2 changed files with 61 additions and 11 deletions

View File

@ -62,5 +62,7 @@ flume = { version = "^0", features = ["async"] }
data-encoding = { version = "^2" } data-encoding = { version = "^2" }
indent = { version = "0.1.1" } indent = { version = "0.1.1" }
chrono = "0.4.26"
[dev-dependencies] [dev-dependencies]
serial_test = "^2" serial_test = "^2"

View File

@ -2,6 +2,7 @@ use crate::command_processor::*;
use crate::peers_table_view::*; use crate::peers_table_view::*;
use crate::settings::Settings; use crate::settings::Settings;
use crate::tools::*; use crate::tools::*;
use async_tungstenite::tungstenite::http::header::STRICT_TRANSPORT_SECURITY;
use crossbeam_channel::Sender; use crossbeam_channel::Sender;
use cursive::align::*; use cursive::align::*;
use cursive::event::*; use cursive::event::*;
@ -16,7 +17,11 @@ use cursive_flexi_logger_view::{CursiveLogWriter, FlexiLoggerView};
// use cursive_multiplex::*; // use cursive_multiplex::*;
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
use std::io::Write; use std::io::Write;
use std::time::{SystemTime, UNIX_EPOCH};
use thiserror::Error; use thiserror::Error;
use std::sync::atomic::{AtomicU64, Ordering};
use chrono::{Datelike, Timelike};
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
/// ///
struct Dirty<T> { struct Dirty<T> {
@ -47,6 +52,10 @@ impl<T> Dirty<T> {
pub type UICallback = Box<dyn Fn(&mut Cursive) + Send>; pub type UICallback = Box<dyn Fn(&mut Cursive) + Send>;
static START_TIME: AtomicU64 = AtomicU64::new(0);
struct UIState { struct UIState {
attachment_state: Dirty<String>, attachment_state: Dirty<String>,
public_internet_ready: Dirty<bool>, public_internet_ready: Dirty<bool>,
@ -101,7 +110,6 @@ pub enum DumbError {
impl UI { impl UI {
///////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////
// Private functions // Private functions
fn command_processor(s: &mut Cursive) -> CommandProcessor { fn command_processor(s: &mut Cursive) -> CommandProcessor {
let inner = Self::inner(s); let inner = Self::inner(s);
inner.cmdproc.as_ref().unwrap().clone() inner.cmdproc.as_ref().unwrap().clone()
@ -349,7 +357,7 @@ impl UI {
cursive_flexi_logger_view::parse_lines_to_log( cursive_flexi_logger_view::parse_lines_to_log(
ColorStyle::primary().into(), ColorStyle::primary().into(),
format!("> {}", text), format!("> {} {}", UI::cli_ts(Self::get_start_time()) , text),
); );
match Self::run_command(s, text) { match Self::run_command(s, text) {
Ok(_) => {} Ok(_) => {}
@ -357,7 +365,7 @@ impl UI {
let color = *Self::inner_mut(s).log_colors.get(&Level::Error).unwrap(); let color = *Self::inner_mut(s).log_colors.get(&Level::Error).unwrap();
cursive_flexi_logger_view::parse_lines_to_log( cursive_flexi_logger_view::parse_lines_to_log(
color.into(), color.into(),
format!(" Error: {}", e), format!(" {} Error: {}", UI::cli_ts(Self::get_start_time()), e)
); );
} }
} }
@ -463,13 +471,13 @@ impl UI {
let color = *Self::inner_mut(s).log_colors.get(&Level::Info).unwrap(); let color = *Self::inner_mut(s).log_colors.get(&Level::Info).unwrap();
cursive_flexi_logger_view::parse_lines_to_log( cursive_flexi_logger_view::parse_lines_to_log(
color.into(), color.into(),
format!(">> Copied: {}", text.as_ref()), format!(">> {} Copied: {}", UI::cli_ts(Self::get_start_time()), text.as_ref()),
); );
} else { } else {
let color = *Self::inner_mut(s).log_colors.get(&Level::Warn).unwrap(); let color = *Self::inner_mut(s).log_colors.get(&Level::Warn).unwrap();
cursive_flexi_logger_view::parse_lines_to_log( cursive_flexi_logger_view::parse_lines_to_log(
color.into(), color.into(),
">> Could not copy to clipboard".to_string(), format!(">> {} Could not copy to clipboard", UI::cli_ts(Self::get_start_time()))
); );
} }
} else { } else {
@ -485,14 +493,16 @@ impl UI {
.is_ok() .is_ok()
&& std::io::stdout().flush().is_ok() && std::io::stdout().flush().is_ok()
{ {
if std::io::stdout().flush().is_ok() {
let color = *Self::inner_mut(s).log_colors.get(&Level::Info).unwrap(); let color = *Self::inner_mut(s).log_colors.get(&Level::Info).unwrap();
cursive_flexi_logger_view::parse_lines_to_log( cursive_flexi_logger_view::parse_lines_to_log(
color.into(), color.into(),
format!(">> Copied: {}", text.as_ref()), format!(">> {} Copied: {}", UI::cli_ts(Self::get_start_time()), text.as_ref()),
); );
} }
} }
} }
}
fn on_submit_peers_table_view(s: &mut Cursive, _row: usize, index: usize) { fn on_submit_peers_table_view(s: &mut Cursive, _row: usize, index: usize) {
let peers_table_view = UI::peers(s); let peers_table_view = UI::peers(s);
@ -778,12 +788,49 @@ impl UI {
} }
} }
//////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////
// Public functions // Public functions
pub fn cli_ts(ts: u64) -> String {
//let ts = get_timestamp();
let now = chrono::DateTime::<chrono::Utc>::from(SystemTime::now());
let date = chrono::DateTime::<chrono::Utc>::from(UNIX_EPOCH + Duration::from_micros(ts));
let show_year = now.year() != date.year();
let show_month = show_year || now.month() != date.month();
let show_date = show_month || now.day() != date.day();
if show_year || show_month || show_date {
UI::set_start_time();
}
format!("{}{}",
if show_year || show_month || show_date {
format!("Day changed: {:04}/{:02}/{:02} \n",now.year(), now.month(), now.day())
} else {
"".to_owned()
},
format!("{:02}:{:02}:{:02}",
now.hour(),
now.minute(),
now.second()
))
}
pub fn set_start_time() {
START_TIME.store(get_timestamp(), Ordering::Relaxed)
}
pub fn get_start_time() -> u64 {
START_TIME.load(Ordering::Relaxed)
}
pub fn new(node_log_scrollback: usize, settings: &Settings) -> (Self, UISender) { pub fn new(node_log_scrollback: usize, settings: &Settings) -> (Self, UISender) {
cursive_flexi_logger_view::resize(node_log_scrollback); cursive_flexi_logger_view::resize(node_log_scrollback);
UI::set_start_time();
// Instantiate the cursive runnable // Instantiate the cursive runnable
let runnable = CursiveRunnable::new( let runnable = CursiveRunnable::new(
|| -> Result<Box<dyn cursive::backend::Backend>, Box<DumbError>> { || -> Result<Box<dyn cursive::backend::Backend>, Box<DumbError>> {
@ -938,6 +985,7 @@ impl UI {
// pub fn run(&mut self) { // pub fn run(&mut self) {
// self.siv.run(); // self.siv.run();
// } // }
} }
type CallbackSink = Box<dyn FnOnce(&mut Cursive) + 'static + Send>; type CallbackSink = Box<dyn FnOnce(&mut Cursive) + 'static + Send>;
@ -1040,7 +1088,7 @@ impl UISender {
{ {
let inner = self.inner.lock(); let inner = self.inner.lock();
let color = *inner.log_colors.get(&log_color).unwrap(); let color = *inner.log_colors.get(&log_color).unwrap();
cursive_flexi_logger_view::parse_lines_to_log(color.into(), event); cursive_flexi_logger_view::parse_lines_to_log(color.into(), format!("{}: {}", UI::cli_ts(UI::get_start_time()), event));
} }
let _ = self.cb_sink.send(Box::new(UI::update_cb)); let _ = self.cb_sink.send(Box::new(UI::update_cb));
} }