2016-03-16 14:25:35 -04:00
|
|
|
// Copyright 2016 Matthew Collins
|
2015-09-28 18:37:14 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-09-29 15:09:36 -04:00
|
|
|
use std::any::Any;
|
2020-06-21 15:17:24 -04:00
|
|
|
use std::cell::{Ref, RefCell};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::io::{BufRead, BufReader, BufWriter, Write};
|
|
|
|
use std::marker::PhantomData;
|
2015-09-29 17:33:24 -04:00
|
|
|
use std::sync::{Arc, Mutex};
|
2019-05-29 11:21:56 -04:00
|
|
|
use std_or_web::fs;
|
2015-09-29 17:33:24 -04:00
|
|
|
|
2020-06-21 15:17:24 -04:00
|
|
|
use crate::format::{Color, Component, TextComponent};
|
2018-11-04 14:48:03 -05:00
|
|
|
use crate::render;
|
2020-06-21 15:17:24 -04:00
|
|
|
use crate::ui;
|
2015-09-28 18:37:14 -04:00
|
|
|
|
2019-05-22 19:12:43 -04:00
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
use web_sys;
|
|
|
|
#[cfg(target_arch = "wasm32")]
|
|
|
|
fn println_level(level: log::Level, s: String) {
|
|
|
|
let value = &wasm_bindgen::JsValue::from_str(&s);
|
|
|
|
use log::Level::*;
|
|
|
|
match level {
|
|
|
|
Trace => web_sys::console::debug_1(value),
|
|
|
|
Debug => web_sys::console::log_1(value),
|
|
|
|
Info => web_sys::console::info_1(value),
|
|
|
|
Warn => web_sys::console::warn_1(value),
|
|
|
|
Error => web_sys::console::error_1(value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(target_arch = "wasm32"))]
|
|
|
|
fn println_level(_level: log::Level, s: String) {
|
|
|
|
println!("{}", s);
|
|
|
|
}
|
|
|
|
|
2018-11-04 16:37:57 -05:00
|
|
|
const FILTERED_CRATES: &[&str] = &[
|
Replace hyper with reqwest (#7)
An old version of hyper was used before (0.8.0), in the process of updating to hyper 0.12.11, found this higher-level replacement/wrapper, reqwest 0.9.4 which is simpler to use than the latest hyper and serves the purpose of a simple HTTP client well
* Begin updating to hyper 0.12.11
https://github.com/iceiix/steven/issues/4#issuecomment-425759778
* Use type variables for hyper::Client
* Fix setting header syntax, Content-Type: application/json, 17->13
* Parse strings into URLs with url.parse::<hyper::Uri>().unwrap()
https://github.com/hyperium/hyper/blob/b20971cb4e5f158844aec5829eea1854e5b7d4b6/examples/client.rs#L25
* Use hyper::Request::post() then client.request() since client.post() removed
* wait() on the ResponseFuture to get the Result
* try! to unwrap the Result
* status() is now a method
* Concatenate body chunks unwrap into bytes, then parse JSON from byte slice, instead of from_reader which didn't compile
* Replace send() with wait() on ResponseFuture
* Parse HeaderValue to u64
* Slices implement std::io::Read trait
* Read into_bytes() instead of read_to_end()
* Disable boxed logger for now to workaround 'expected function, found macro'
* Remove unnecessary mutability, warnings
* Hack to parse twice to avoid double move
* Use hyper-rustls pure Rust implementation for TLS for HTTPS in hyper
* Start converting to reqwest: add Protocol::Error and reqwest::Error conversion
* Use reqwest, replacing hyper, in protocol
* Convert resources to use reqwest instead of hyper
* Convert skin download to reqwest, instead of hyper
* Remove hyper
* Revert unnecessary variable name change req/body to reduce diff
* Revert unnecessary whitespace change to reduce diff, align indentation on .
* Fix authenticating to server, wrong method and join URL
* Update Cargo.lock
2018-10-27 20:03:34 -04:00
|
|
|
//"reqwest", // TODO: needed?
|
2016-03-20 20:15:41 -04:00
|
|
|
"mime",
|
|
|
|
];
|
|
|
|
|
2015-09-29 15:09:36 -04:00
|
|
|
pub struct CVar<T: Sized + Any + 'static> {
|
2015-10-07 14:36:59 -04:00
|
|
|
pub name: &'static str,
|
|
|
|
pub ty: PhantomData<T>,
|
|
|
|
pub description: &'static str,
|
|
|
|
pub mutable: bool,
|
|
|
|
pub serializable: bool,
|
2019-11-02 18:37:39 -04:00
|
|
|
pub default: &'static dyn Fn() -> T,
|
2015-09-28 18:37:14 -04:00
|
|
|
}
|
|
|
|
|
2021-01-20 18:22:28 -05:00
|
|
|
pub const LOG_LEVEL_TERM: CVar<String> = CVar {
|
|
|
|
ty: PhantomData,
|
|
|
|
name: "log_level_term",
|
|
|
|
description: "log level of messages to log to the terminal",
|
|
|
|
mutable: false,
|
|
|
|
serializable: true,
|
|
|
|
default: &|| "info".to_owned(),
|
|
|
|
};
|
|
|
|
|
|
|
|
pub const LOG_LEVEL_FILE: CVar<String> = CVar {
|
|
|
|
ty: PhantomData,
|
|
|
|
name: "log_level_file",
|
|
|
|
description: "log level of messages to log to the log file",
|
|
|
|
mutable: false,
|
|
|
|
serializable: true,
|
|
|
|
default: &|| "trace".to_owned(),
|
|
|
|
};
|
|
|
|
|
|
|
|
pub fn register_vars(vars: &mut Vars) {
|
|
|
|
vars.register(LOG_LEVEL_TERM);
|
|
|
|
vars.register(LOG_LEVEL_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn log_level_from_str(s: &str, default: log::Level) -> log::Level {
|
|
|
|
// TODO: no opposite of FromStr in log crate?
|
|
|
|
use log::Level::*;
|
|
|
|
match s {
|
|
|
|
"trace" => Trace,
|
|
|
|
"debug" => Debug,
|
|
|
|
"info" => Info,
|
|
|
|
"warn" => Warn,
|
|
|
|
"error" => Error,
|
|
|
|
_ => default,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-09 04:56:55 -04:00
|
|
|
impl Var for CVar<i64> {
|
2019-11-02 18:37:39 -04:00
|
|
|
fn serialize(&self, val: &Box<dyn Any>) -> String {
|
2016-04-09 04:56:55 -04:00
|
|
|
val.downcast_ref::<i64>().unwrap().to_string()
|
|
|
|
}
|
|
|
|
|
2019-11-02 18:37:39 -04:00
|
|
|
fn deserialize(&self, input: &str) -> Box<dyn Any> {
|
2016-04-09 04:56:55 -04:00
|
|
|
Box::new(input.parse::<i64>().unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn description(&self) -> &'static str {
|
|
|
|
self.description
|
|
|
|
}
|
|
|
|
|
|
|
|
fn can_serialize(&self) -> bool {
|
|
|
|
self.serializable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Var for CVar<bool> {
|
2019-11-02 18:37:39 -04:00
|
|
|
fn serialize(&self, val: &Box<dyn Any>) -> String {
|
2016-04-09 04:56:55 -04:00
|
|
|
val.downcast_ref::<bool>().unwrap().to_string()
|
|
|
|
}
|
|
|
|
|
2019-11-02 18:37:39 -04:00
|
|
|
fn deserialize(&self, input: &str) -> Box<dyn Any> {
|
2016-04-09 04:56:55 -04:00
|
|
|
Box::new(input.parse::<bool>().unwrap())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn description(&self) -> &'static str {
|
|
|
|
self.description
|
|
|
|
}
|
|
|
|
|
|
|
|
fn can_serialize(&self) -> bool {
|
|
|
|
self.serializable
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-29 18:24:58 -04:00
|
|
|
impl Var for CVar<String> {
|
2019-11-02 18:37:39 -04:00
|
|
|
fn serialize(&self, val: &Box<dyn Any>) -> String {
|
2015-10-07 14:36:59 -04:00
|
|
|
format!("\"{}\"", val.downcast_ref::<String>().unwrap())
|
|
|
|
}
|
2015-09-28 18:37:14 -04:00
|
|
|
|
2019-11-02 18:37:39 -04:00
|
|
|
fn deserialize(&self, input: &str) -> Box<dyn Any> {
|
2015-10-07 14:36:59 -04:00
|
|
|
Box::new((&input[1..input.len() - 1]).to_owned())
|
|
|
|
}
|
2015-09-28 18:37:14 -04:00
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
fn description(&self) -> &'static str {
|
|
|
|
self.description
|
|
|
|
}
|
|
|
|
fn can_serialize(&self) -> bool {
|
|
|
|
self.serializable
|
|
|
|
}
|
2015-09-29 18:24:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait Var {
|
2019-11-02 18:37:39 -04:00
|
|
|
fn serialize(&self, val: &Box<dyn Any>) -> String;
|
|
|
|
fn deserialize(&self, input: &str) -> Box<dyn Any>;
|
2015-10-07 14:36:59 -04:00
|
|
|
fn description(&self) -> &'static str;
|
|
|
|
fn can_serialize(&self) -> bool;
|
2015-09-28 18:37:14 -04:00
|
|
|
}
|
|
|
|
|
2016-09-15 10:15:52 -04:00
|
|
|
#[derive(Default)]
|
2016-04-21 06:40:50 -04:00
|
|
|
pub struct Vars {
|
2015-10-07 14:36:59 -04:00
|
|
|
names: HashMap<String, &'static str>,
|
2019-11-02 18:37:39 -04:00
|
|
|
vars: HashMap<&'static str, Box<dyn Var>>,
|
|
|
|
var_values: HashMap<&'static str, RefCell<Box<dyn Any>>>,
|
2015-09-28 18:37:14 -04:00
|
|
|
}
|
|
|
|
|
2016-04-21 06:40:50 -04:00
|
|
|
impl Vars {
|
2020-06-21 15:17:24 -04:00
|
|
|
pub fn new() -> Vars {
|
|
|
|
Default::default()
|
|
|
|
}
|
2015-09-29 15:09:36 -04:00
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
pub fn register<T: Sized + Any>(&mut self, var: CVar<T>)
|
2020-06-21 15:17:24 -04:00
|
|
|
where
|
|
|
|
CVar<T>: Var,
|
2015-10-07 14:36:59 -04:00
|
|
|
{
|
|
|
|
if self.vars.contains_key(var.name) {
|
|
|
|
panic!("Key registered twice {}", var.name);
|
|
|
|
}
|
|
|
|
self.names.insert(var.name.to_owned(), var.name);
|
2020-06-21 15:17:24 -04:00
|
|
|
self.var_values
|
|
|
|
.insert(var.name, RefCell::new(Box::new((var.default)())));
|
2015-10-07 14:36:59 -04:00
|
|
|
self.vars.insert(var.name, Box::new(var));
|
|
|
|
}
|
2015-09-29 15:09:36 -04:00
|
|
|
|
2016-09-15 10:15:52 -04:00
|
|
|
pub fn get<T: Sized + Any>(&self, var: CVar<T>) -> Ref<T>
|
2020-06-21 15:17:24 -04:00
|
|
|
where
|
|
|
|
CVar<T>: Var,
|
2015-10-07 14:36:59 -04:00
|
|
|
{
|
2016-03-20 16:17:21 -04:00
|
|
|
// Should never fail
|
2016-04-21 06:40:50 -04:00
|
|
|
let var = self.var_values.get(var.name).unwrap().borrow();
|
|
|
|
Ref::map(var, |v| v.downcast_ref::<T>().unwrap())
|
2015-10-07 14:36:59 -04:00
|
|
|
}
|
2015-09-28 18:37:14 -04:00
|
|
|
|
2016-04-21 06:40:50 -04:00
|
|
|
pub fn set<T: Sized + Any>(&self, var: CVar<T>, val: T)
|
2020-06-21 15:17:24 -04:00
|
|
|
where
|
|
|
|
CVar<T>: Var,
|
2015-10-07 14:36:59 -04:00
|
|
|
{
|
2016-04-21 06:40:50 -04:00
|
|
|
*self.var_values.get(var.name).unwrap().borrow_mut() = Box::new(val);
|
2015-10-07 14:36:59 -04:00
|
|
|
self.save_config();
|
|
|
|
}
|
2015-09-29 17:33:24 -04:00
|
|
|
|
2016-04-21 06:40:50 -04:00
|
|
|
pub fn load_config(&mut self) {
|
|
|
|
if let Ok(file) = fs::File::open("conf.cfg") {
|
|
|
|
let reader = BufReader::new(file);
|
|
|
|
for line in reader.lines() {
|
|
|
|
let line = line.unwrap();
|
|
|
|
if line.starts_with('#') || line.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
2020-06-21 15:17:24 -04:00
|
|
|
let parts = line
|
|
|
|
.splitn(2, ' ')
|
|
|
|
.map(|v| v.to_owned())
|
|
|
|
.collect::<Vec<String>>();
|
2016-04-21 06:40:50 -04:00
|
|
|
let (name, arg) = (&parts[0], &parts[1]);
|
|
|
|
if let Some(var_name) = self.names.get(name) {
|
|
|
|
let var = self.vars.get(var_name).unwrap();
|
2016-09-15 10:15:52 -04:00
|
|
|
let val = var.deserialize(arg);
|
2016-04-21 06:40:50 -04:00
|
|
|
if var.can_serialize() {
|
|
|
|
self.var_values.insert(var_name, RefCell::new(val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save_config(&self) {
|
|
|
|
let mut file = BufWriter::new(fs::File::create("conf.cfg").unwrap());
|
|
|
|
for (name, var) in &self.vars {
|
|
|
|
if !var.can_serialize() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for line in var.description().lines() {
|
2020-06-29 21:06:35 -04:00
|
|
|
writeln!(file, "# {}", line).unwrap();
|
2016-04-21 06:40:50 -04:00
|
|
|
}
|
2020-06-21 15:17:24 -04:00
|
|
|
write!(
|
|
|
|
file,
|
|
|
|
"{} {}\n\n",
|
|
|
|
name,
|
|
|
|
var.serialize(&self.var_values.get(name).unwrap().borrow())
|
|
|
|
)
|
|
|
|
.unwrap();
|
2016-04-21 06:40:50 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Console {
|
|
|
|
history: Vec<Component>,
|
2016-04-24 07:22:04 -04:00
|
|
|
dirty: bool,
|
2021-01-20 18:22:28 -05:00
|
|
|
logfile: fs::File,
|
|
|
|
log_level_term: log::Level,
|
|
|
|
log_level_file: log::Level,
|
2016-04-21 06:40:50 -04:00
|
|
|
|
2016-04-24 07:22:04 -04:00
|
|
|
elements: Option<ConsoleElements>,
|
2016-04-21 06:40:50 -04:00
|
|
|
active: bool,
|
|
|
|
position: f64,
|
|
|
|
}
|
|
|
|
|
2016-04-24 07:22:04 -04:00
|
|
|
struct ConsoleElements {
|
|
|
|
background: ui::ImageRef,
|
|
|
|
lines: Vec<ui::FormattedRef>,
|
|
|
|
}
|
|
|
|
|
2021-01-20 18:22:28 -05:00
|
|
|
impl Default for Console {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-21 06:40:50 -04:00
|
|
|
impl Console {
|
|
|
|
pub fn new() -> Console {
|
|
|
|
Console {
|
|
|
|
history: vec![Component::Text(TextComponent::new("")); 200],
|
2016-04-24 07:22:04 -04:00
|
|
|
dirty: false,
|
2021-01-20 18:22:28 -05:00
|
|
|
logfile: fs::File::create("client.log").expect("failed to open log file"),
|
|
|
|
log_level_term: log::Level::Info,
|
|
|
|
log_level_file: log::Level::Trace,
|
2016-04-21 06:40:50 -04:00
|
|
|
|
2016-04-24 07:22:04 -04:00
|
|
|
elements: None,
|
2016-04-21 06:40:50 -04:00
|
|
|
active: false,
|
|
|
|
position: -220.0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-20 18:22:28 -05:00
|
|
|
pub fn configure(&mut self, vars: &Vars) {
|
|
|
|
self.log_level_term = log_level_from_str(&vars.get(LOG_LEVEL_TERM), log::Level::Info);
|
|
|
|
self.log_level_file = log_level_from_str(&vars.get(LOG_LEVEL_FILE), log::Level::Trace);
|
|
|
|
}
|
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
pub fn is_active(&self) -> bool {
|
|
|
|
self.active
|
|
|
|
}
|
2015-09-29 17:33:24 -04:00
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
pub fn toggle(&mut self) {
|
|
|
|
self.active = !self.active;
|
|
|
|
}
|
2015-09-29 17:33:24 -04:00
|
|
|
|
2021-01-24 20:41:16 -05:00
|
|
|
pub fn activate(&mut self) {
|
|
|
|
self.active = true;
|
|
|
|
}
|
|
|
|
|
2020-06-21 15:17:24 -04:00
|
|
|
pub fn tick(
|
|
|
|
&mut self,
|
|
|
|
ui_container: &mut ui::Container,
|
|
|
|
renderer: &render::Renderer,
|
|
|
|
delta: f64,
|
|
|
|
width: f64,
|
|
|
|
) {
|
2015-10-07 14:36:59 -04:00
|
|
|
if !self.active && self.position <= -220.0 {
|
2016-04-24 07:22:04 -04:00
|
|
|
self.elements = None;
|
2015-10-07 14:36:59 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if self.active {
|
|
|
|
if self.position < 0.0 {
|
|
|
|
self.position += delta * 4.0;
|
|
|
|
} else {
|
|
|
|
self.position = 0.0;
|
|
|
|
}
|
2016-03-26 10:24:26 -04:00
|
|
|
} else if self.position > -220.0 {
|
|
|
|
self.position -= delta * 4.0;
|
2015-10-07 14:36:59 -04:00
|
|
|
} else {
|
2016-03-26 10:24:26 -04:00
|
|
|
self.position = -220.0;
|
2015-10-07 14:36:59 -04:00
|
|
|
}
|
2016-04-24 07:22:04 -04:00
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
let w = match ui_container.mode {
|
|
|
|
ui::Mode::Scaled => width,
|
|
|
|
ui::Mode::Unscaled(scale) => 854.0 / scale,
|
|
|
|
};
|
2016-04-24 07:22:04 -04:00
|
|
|
if self.elements.is_none() {
|
|
|
|
let background = ui::ImageBuilder::new()
|
|
|
|
.texture("steven:solid")
|
|
|
|
.position(0.0, self.position)
|
|
|
|
.size(w, 220.0)
|
|
|
|
.colour((0, 0, 0, 180))
|
|
|
|
.draw_index(500)
|
|
|
|
.create(ui_container);
|
|
|
|
self.elements = Some(ConsoleElements {
|
2018-11-04 16:43:30 -05:00
|
|
|
background,
|
2016-04-24 07:22:04 -04:00
|
|
|
lines: vec![],
|
|
|
|
});
|
|
|
|
self.dirty = true;
|
2015-10-07 14:36:59 -04:00
|
|
|
}
|
2016-04-24 07:22:04 -04:00
|
|
|
let elements = self.elements.as_mut().unwrap();
|
|
|
|
let mut background = elements.background.borrow_mut();
|
|
|
|
background.y = self.position;
|
|
|
|
background.width = w;
|
|
|
|
|
|
|
|
if self.dirty {
|
|
|
|
self.dirty = false;
|
|
|
|
elements.lines.clear();
|
|
|
|
|
|
|
|
let mut offset = 0.0;
|
|
|
|
for line in self.history.iter().rev() {
|
|
|
|
if offset >= 210.0 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let (_, height) = ui::Formatted::compute_size(renderer, line, w - 10.0);
|
2020-06-21 15:17:24 -04:00
|
|
|
elements.lines.push(
|
|
|
|
ui::FormattedBuilder::new()
|
|
|
|
.text(line.clone())
|
|
|
|
.position(5.0, 5.0 + offset)
|
|
|
|
.max_width(w - 10.0)
|
|
|
|
.alignment(ui::VAttach::Bottom, ui::HAttach::Left)
|
|
|
|
.create(&mut *background),
|
|
|
|
);
|
2016-04-24 07:22:04 -04:00
|
|
|
offset += height;
|
|
|
|
}
|
2015-10-07 14:36:59 -04:00
|
|
|
}
|
|
|
|
}
|
2015-09-29 17:33:24 -04:00
|
|
|
|
2018-09-30 19:36:00 -04:00
|
|
|
fn log(&mut self, record: &log::Record) {
|
2016-03-20 20:15:41 -04:00
|
|
|
for filtered in FILTERED_CRATES {
|
2018-09-30 19:36:00 -04:00
|
|
|
if record.module_path().unwrap_or("").starts_with(filtered) {
|
2016-03-20 20:15:41 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-30 19:36:00 -04:00
|
|
|
let mut file = &record.file().unwrap_or("").replace("\\", "/")[..];
|
2015-10-07 14:36:59 -04:00
|
|
|
if let Some(pos) = file.rfind("src/") {
|
|
|
|
file = &file[pos + 4..];
|
|
|
|
}
|
2016-03-20 20:15:41 -04:00
|
|
|
|
2021-01-20 18:22:28 -05:00
|
|
|
let line = format!(
|
|
|
|
"[{}:{}][{}] {}",
|
|
|
|
file,
|
|
|
|
record.line().unwrap_or(0),
|
2020-06-21 15:17:24 -04:00
|
|
|
record.level(),
|
2021-01-20 18:22:28 -05:00
|
|
|
record.args()
|
2020-06-21 15:17:24 -04:00
|
|
|
);
|
2021-01-20 18:22:28 -05:00
|
|
|
|
|
|
|
if record.level() <= self.log_level_file {
|
|
|
|
self.logfile.write_all(line.as_bytes()).unwrap();
|
|
|
|
self.logfile.write_all(b"\n").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
if record.level() <= self.log_level_term {
|
|
|
|
println_level(record.level(), line);
|
|
|
|
}
|
|
|
|
|
2015-10-07 14:36:59 -04:00
|
|
|
self.history.remove(0);
|
|
|
|
let mut msg = TextComponent::new("");
|
|
|
|
msg.modifier.extra = Some(vec![
|
2016-03-20 16:17:21 -04:00
|
|
|
Component::Text(TextComponent::new("[")),
|
|
|
|
{
|
|
|
|
let mut msg = TextComponent::new(file);
|
|
|
|
msg.modifier.color = Some(Color::Green);
|
|
|
|
Component::Text(msg)
|
|
|
|
},
|
|
|
|
Component::Text(TextComponent::new(":")),
|
|
|
|
{
|
2018-09-30 19:36:00 -04:00
|
|
|
let mut msg = TextComponent::new(&format!("{}", record.line().unwrap_or(0)));
|
2016-03-20 16:17:21 -04:00
|
|
|
msg.modifier.color = Some(Color::Aqua);
|
|
|
|
Component::Text(msg)
|
|
|
|
},
|
|
|
|
Component::Text(TextComponent::new("]")),
|
|
|
|
Component::Text(TextComponent::new("[")),
|
|
|
|
{
|
|
|
|
let mut msg = TextComponent::new(&format!("{}", record.level()));
|
|
|
|
msg.modifier.color = Some(match record.level() {
|
2018-09-30 19:36:00 -04:00
|
|
|
log::Level::Debug => Color::Green,
|
|
|
|
log::Level::Error => Color::Red,
|
|
|
|
log::Level::Warn => Color::Yellow,
|
|
|
|
log::Level::Info => Color::Aqua,
|
|
|
|
log::Level::Trace => Color::Blue,
|
2016-03-20 16:17:21 -04:00
|
|
|
});
|
|
|
|
Component::Text(msg)
|
|
|
|
},
|
|
|
|
Component::Text(TextComponent::new("] ")),
|
2020-06-21 15:17:24 -04:00
|
|
|
Component::Text(TextComponent::new(&format!("{}", record.args()))),
|
2016-03-20 16:17:21 -04:00
|
|
|
]);
|
2015-10-07 14:36:59 -04:00
|
|
|
self.history.push(Component::Text(msg));
|
2016-04-24 07:22:04 -04:00
|
|
|
self.dirty = true;
|
2015-10-07 14:36:59 -04:00
|
|
|
}
|
2015-09-29 17:33:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ConsoleProxy {
|
2015-10-07 14:36:59 -04:00
|
|
|
console: Arc<Mutex<Console>>,
|
2015-09-29 17:33:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ConsoleProxy {
|
2015-10-07 14:36:59 -04:00
|
|
|
pub fn new(con: Arc<Mutex<Console>>) -> ConsoleProxy {
|
|
|
|
ConsoleProxy { console: con }
|
|
|
|
}
|
2015-09-29 17:33:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl log::Log for ConsoleProxy {
|
2018-09-30 19:36:00 -04:00
|
|
|
fn enabled(&self, metadata: &log::Metadata) -> bool {
|
|
|
|
metadata.level() <= log::Level::Trace
|
2015-10-07 14:36:59 -04:00
|
|
|
}
|
2015-09-29 17:33:24 -04:00
|
|
|
|
2018-09-30 19:36:00 -04:00
|
|
|
fn log(&self, record: &log::Record) {
|
2015-10-07 14:36:59 -04:00
|
|
|
if self.enabled(record.metadata()) {
|
|
|
|
self.console.lock().unwrap().log(record);
|
|
|
|
}
|
|
|
|
}
|
2018-09-30 19:36:00 -04:00
|
|
|
|
2020-06-21 15:17:24 -04:00
|
|
|
fn flush(&self) {}
|
2015-09-29 17:33:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for ConsoleProxy {}
|
2015-09-29 18:24:58 -04:00
|
|
|
unsafe impl Sync for ConsoleProxy {}
|