log: date -> sec.milli since init

This commit is contained in:
hinto-janaiyo 2022-11-20 20:21:47 -05:00
parent 42c89fcdd2
commit 3b37ac7be3
No known key found for this signature in database
GPG key ID: B1C5A64B80691E45
5 changed files with 50 additions and 159 deletions

View file

@ -58,7 +58,7 @@ pub fn get_gupax_data_path() -> Result<PathBuf, TomlError> {
match dirs::data_dir() {
Some(mut path) => {
path.push(DIRECTORY);
info!("OS | Data path ... OK ... {}", path.display());
info!("OS | Data path ... {}", path.display());
create_gupax_dir(&path)?;
Ok(path)
},
@ -217,7 +217,7 @@ impl State {
info!("State | Creating new default...");
let new = Self::new();
let string = match toml::ser::to_string(&new) {
Ok(o) => { info!("State | Serialization ... OK"); o },
Ok(o) => o,
Err(e) => { error!("State | Couldn't serialize default file: {}", e); return Err(TomlError::Serialize(e)) },
};
fs::write(&path, &string)?;
@ -265,14 +265,6 @@ impl State {
//---------------------------------------------------------------------------------------------------- [Node] Impl
impl Node {
pub fn new() -> Self {
Self {
ip: String::new(),
rpc: "18081".to_string(),
zmq: "18083".to_string(),
}
}
pub fn localhost() -> Self {
Self {
ip: "localhost".to_string(),

View file

@ -262,6 +262,24 @@ impl Default for Tab {
}
//---------------------------------------------------------------------------------------------------- [ErrorState] struct
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ErrorButtons {
YesNo,
StayQuit,
ResetState,
ResetNode,
Okay,
Quit,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ErrorFerris {
Happy,
Oops,
Error,
Panic,
}
pub struct ErrorState {
error: bool, // Is there an error?
msg: String, // What message to display?
@ -300,25 +318,6 @@ impl ErrorState {
}
}
//---------------------------------------------------------------------------------------------------- [ErrorButtons] enum
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ErrorButtons {
YesNo,
StayQuit,
ResetState,
ResetNode,
Okay,
Quit,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ErrorFerris {
Happy,
Oops,
Error,
Panic,
}
//---------------------------------------------------------------------------------------------------- [Images] struct
struct Images {
banner: RetainedImage,
@ -394,13 +393,9 @@ fn init_text_styles(ctx: &egui::Context, width: f32) {
// ctx.set_style(style);
//}
fn init_logger() {
// #[cfg(debug_assertions)]
let filter = LevelFilter::Info;
// #[cfg(not(debug_assertions))]
// let filter = LevelFilter::Warn;
fn init_logger(now: Instant) {
use env_logger::fmt::Color;
Builder::new().format(|buf, record| {
Builder::new().format(move |buf, record| {
let level;
let mut style = buf.style();
match record.level() {
@ -414,12 +409,12 @@ fn init_logger() {
buf,
"[{}] [{}] [{}:{}] {}",
style.set_bold(true).value(level),
buf.style().set_dimmed(true).value(chrono::Local::now().format("%F %T%.3f")),
buf.style().set_dimmed(true).value(format!("{:.7}", now.elapsed().as_secs_f32())),
buf.style().set_dimmed(true).value(record.file().unwrap_or("???")),
buf.style().set_dimmed(true).value(record.line().unwrap_or(0)),
record.args(),
)
}).filter_level(filter).write_style(WriteStyle::Always).parse_default_env().format_timestamp_millis().init();
}).filter_level(LevelFilter::Info).write_style(WriteStyle::Always).parse_default_env().format_timestamp_millis().init();
info!("init_logger() ... OK");
}
@ -471,7 +466,6 @@ fn init_auto(app: &App) {
}
fn reset_state(path: &PathBuf) -> Result<(), TomlError> {
info!("Resetting [state.toml]...");
match State::create_new(path) {
Ok(_) => { info!("Resetting [state.toml] ... OK"); Ok(()) },
Err(e) => { error!("Resetting [state.toml] ... FAIL ... {}", e); Err(e) },
@ -479,7 +473,6 @@ fn reset_state(path: &PathBuf) -> Result<(), TomlError> {
}
fn reset_nodes(path: &PathBuf) -> Result<(), TomlError> {
info!("Resetting [node.toml]...");
match Node::create_new(path) {
Ok(_) => { info!("Resetting [node.toml] ... OK"); Ok(()) },
Err(e) => { error!("Resetting [node.toml] ... FAIL ... {}", e); Err(e) },
@ -489,11 +482,15 @@ fn reset_nodes(path: &PathBuf) -> Result<(), TomlError> {
fn reset(path: &PathBuf, state: &PathBuf, node: &PathBuf) {
let mut code = 0;
// Attempt to remove directory first
info!("OS data path ... {}", path.display());
match std::fs::remove_dir_all(path) {
Ok(_) => info!("Removing OS data path ... OK"),
Err(e) => { error!("Removing OS data path ... FAIL ... {}", e); code = 1; },
}
// Recreate
match create_gupax_dir(path) {
Ok(_) => (),
Err(_) => code = 1,
}
match reset_state(state) {
Ok(_) => (),
Err(_) => code = 1,
@ -538,8 +535,8 @@ fn parse_args<S: Into<String>>(mut app: App, panic: S) -> App {
match arg.as_str() {
"--state" => { info!("Printing state..."); print_disk_file(&app.state_path); }
"--nodes" => { info!("Printing node list..."); print_disk_file(&app.node_path); }
"--reset-state" => if let Ok(()) = reset_state(&app.state_path) { exit(0) } else { exit(1) },
"--reset-nodes" => if let Ok(()) = reset_nodes(&app.node_path) { exit(0) } else { exit(1) },
"--reset-state" => if let Ok(()) = reset_state(&app.state_path) { println!("\nState reset ... OK"); exit(0); } else { println!("\nState reset ... FAIL"); exit(1) },
"--reset-nodes" => if let Ok(()) = reset_nodes(&app.node_path) { println!("\nNode reset ... OK"); exit(0) } else { println!("\nNode reset ... FAIL"); exit(1) },
"--reset-all" => reset(&app.os_data_path, &app.state_path, &app.node_path),
"--no-startup" => app.no_startup = true,
_ => { eprintln!("[Gupax error] Invalid option: [{}]\nFor help, use: [--help]", arg); exit(1); },
@ -590,60 +587,10 @@ fn print_disk_file(path: &PathBuf) {
}
}
//---------------------------------------------------------------------------------------------------- [App] frame for [Panic] situations
fn panic_main(error: String) {
error!("{}", error);
let options = Panic::options();
let name = format!("Gupax {}", GUPAX_VERSION);
eframe::run_native(&name, options, Box::new(|cc| Box::new(Panic::new(cc, error))),);
exit(1);
}
struct Panic { error_msg: String, }
impl Panic {
fn options() -> NativeOptions {
let mut options = eframe::NativeOptions::default();
let frame = Option::from(Vec2::new(1280.0, 720.0));
options.min_window_size = frame;
options.max_window_size = frame;
options.initial_window_size = frame;
options.follow_system_theme = false;
options.default_theme = eframe::Theme::Dark;
let icon = image::load_from_memory(BYTES_ICON).expect("Failed to read icon bytes").to_rgba8();
let (icon_width, icon_height) = icon.dimensions();
options.icon_data = Some(eframe::IconData {
rgba: icon.into_raw(),
width: icon_width,
height: icon_height,
});
info!("Panic::options() ... OK");
options
}
fn new(cc: &eframe::CreationContext<'_>, error_msg: String) -> Self {
let resolution = cc.integration_info.window_info.size;
init_text_styles(&cc.egui_ctx, resolution[0] as f32);
Self { error_msg }
}
}
impl eframe::App for Panic {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
let width = ui.available_width();
let height = ui.available_height();
init_text_styles(ctx, width);
ui.add_sized([width, height/8.0], Label::new("Gupax has encountered a fatal error:"));
ui.add_sized([width, height/8.0], Label::new(&self.error_msg));
ui.add_sized([width, height/3.0], Label::new("Please report to: https://github.com/hinto-janaiyo/gupax/issues"));
ui.add_sized([width, height/3.0], egui::Button::new("Quit")).clicked() && exit(1)
});
}
}
//---------------------------------------------------------------------------------------------------- Main [App] frame
fn main() {
let now = Instant::now();
init_logger();
init_logger(now);
let options = init_options();
match clean_dir() {
Ok(_) => info!("Temporary folder cleanup ... OK"),
@ -652,7 +599,7 @@ fn main() {
let mut app = App::new();
app.now = now;
init_auto(&app);
info!("Init ... DONE ... Took [{}] seconds", now.elapsed().as_secs_f32());
info!("Init ... DONE");
eframe::run_native(&app.name_version.clone(), options, Box::new(|cc| Box::new(App::cc(cc, app))),);
}

View file

@ -18,14 +18,14 @@
use crate::State;
use serde::{Serialize,Deserialize};
use std::time::{Instant,Duration};
use std::collections::HashMap;
use std::sync::{Arc,Mutex};
use egui::Color32;
use log::*;
//use hyper::{
// Client,Body,Request,
// header::{HeaderValue,LOCATION},
//};
use hyper::{
client::HttpConnector,
Client,Body,Request,
header::{HeaderValue,LOCATION},
};
//---------------------------------------------------------------------------------------------------- Node list
// Community Monerod nodes. All of these have ZMQ on 18083.
@ -191,7 +191,7 @@ impl Ping {
}
//---------------------------------------------------------------------------------------------------- Main Ping function
// Intermediate thread for spawning thread
// Intermediate function for spawning thread
pub fn spawn_thread(ping: &Arc<Mutex<Self>>, og: &Arc<Mutex<State>>) {
let ping = Arc::clone(&ping);
let og = Arc::clone(og);
@ -239,8 +239,8 @@ impl Ping {
// Create HTTP client
let info = format!("{}", "Creating HTTP Client");
ping.lock().unwrap().msg = info;
let client: hyper::client::Client<hyper::client::HttpConnector> = hyper::Client::builder()
.build(hyper::client::HttpConnector::new());
let client: Client<HttpConnector> = Client::builder()
.build(HttpConnector::new());
// Random User Agent
let rand_user_agent = crate::Pkg::get_user_agent();
@ -252,7 +252,7 @@ impl Ping {
let client = client.clone();
let ping = Arc::clone(&ping);
let node_vec = Arc::clone(&node_vec);
let request = hyper::Request::builder()
let request = Request::builder()
.method("POST")
.uri("http://".to_string() + ip + "/json_rpc")
.header("User-Agent", rand_user_agent)
@ -282,7 +282,7 @@ impl Ping {
Ok(())
}
async fn response(client: hyper::client::Client<hyper::client::HttpConnector>, request: hyper::Request<hyper::Body>, ip: &'static str, ping: Arc<Mutex<Self>>, percent: f32, node_vec: Arc<Mutex<Vec<NodeData>>>) {
async fn response(client: Client<HttpConnector>, request: Request<Body>, ip: &'static str, ping: Arc<Mutex<Self>>, percent: f32, node_vec: Arc<Mutex<Vec<NodeData>>>) {
let id = ip_to_enum(ip);
let now = Instant::now();
let ms;

View file

@ -26,7 +26,6 @@ use egui::{
TextStyle::*,
};
use std::sync::{Arc,Mutex};
use std::thread;
use regex::Regex;
use log::*;

View file

@ -119,56 +119,12 @@ const XMRIG_BINARY: &'static str = "xmrig";
// Some fake Curl/Wget user-agents because GitHub API requires one and a Tor browser
// user-agent might be fingerprintable without all the associated headers.
const FAKE_USER_AGENT: [&'static str; 50] = [
"Wget/1.16.3",
"Wget/1.17",
"Wget/1.17.1",
"Wget/1.18",
"Wget/1.18",
"Wget/1.19",
"Wget/1.19.1",
"Wget/1.19.2",
"Wget/1.19.3",
"Wget/1.19.4",
"Wget/1.19.5",
"Wget/1.20",
"Wget/1.20.1",
"Wget/1.20.2",
"Wget/1.20.3",
"Wget/1.21",
"Wget/1.21.1",
"Wget/1.21.2",
"Wget/1.21.3",
"curl/7.64.1",
"curl/7.65.0",
"curl/7.65.1",
"curl/7.65.2",
"curl/7.65.3",
"curl/7.66.0",
"curl/7.67.0",
"curl/7.68.0",
"curl/7.69.0",
"curl/7.69.1",
"curl/7.70.0",
"curl/7.70.1",
"curl/7.71.0",
"curl/7.71.1",
"curl/7.72.0",
"curl/7.73.0",
"curl/7.74.0",
"curl/7.75.0",
"curl/7.76.0",
"curl/7.76.1",
"curl/7.77.0",
"curl/7.78.0",
"curl/7.79.0",
"curl/7.79.1",
"curl/7.80.0",
"curl/7.81.0",
"curl/7.82.0",
"curl/7.83.0",
"curl/7.83.1",
"curl/7.84.0",
"curl/7.85.0",
"Wget/1.16.3","Wget/1.17","Wget/1.17.1","Wget/1.18","Wget/1.18","Wget/1.19","Wget/1.19.1","Wget/1.19.2","Wget/1.19.3","Wget/1.19.4",
"Wget/1.19.5","Wget/1.20","Wget/1.20.1","Wget/1.20.2","Wget/1.20.3","Wget/1.21","Wget/1.21.1","Wget/1.21.2","Wget/1.21.3",
"curl/7.64.1","curl/7.65.0","curl/7.65.1","curl/7.65.2","curl/7.65.3","curl/7.66.0","curl/7.67.0","curl/7.68.0","curl/7.69.0",
"curl/7.69.1","curl/7.70.0","curl/7.70.1","curl/7.71.0","curl/7.71.1","curl/7.72.0","curl/7.73.0","curl/7.74.0","curl/7.75.0",
"curl/7.76.0","curl/7.76.1","curl/7.77.0","curl/7.78.0","curl/7.79.0","curl/7.79.1","curl/7.80.0","curl/7.81.0","curl/7.82.0",
"curl/7.83.0","curl/7.83.1","curl/7.84.0","curl/7.85.0",
];
const MSG_NONE: &'static str = "No update in progress";
@ -351,8 +307,6 @@ impl Update {
std::fs::create_dir(&tmp_dir)?;
// Make Pkg vector
let prog = update.lock().unwrap().prog.clone();
let msg = update.lock().unwrap().msg.clone();
let mut vec = vec![
Pkg::new(Gupax),
Pkg::new(P2pool),
@ -369,12 +323,11 @@ impl Update {
} else {
*update.lock().unwrap().msg.lock().unwrap() = MSG_HTTPS.to_string()
}
let prog = *update.lock().unwrap().prog.lock().unwrap();
info!("Update | {}", update.lock().unwrap().msg.lock().unwrap());
let tor = update.lock().unwrap().tor;
let mut client = Self::get_client(tor)?;
*update.lock().unwrap().prog.lock().unwrap() += 5.0;
info!("Update | Init ... OK ... {}%", prog);
info!("Update | Init ... OK ... {}%", update.lock().unwrap().prog.lock().unwrap());
//---------------------------------------------------------------------------------------------------- Metadata
*update.lock().unwrap().msg.lock().unwrap() = MSG_METADATA.to_string();