feat: add async runtime with tokio and tracing
This commit is contained in:
1 parent
cb3453f0d0
commit
a8f409d2ff
5 files changed
+1603
-39
No files matched your search
Generated
+1559
-8
File diff suppressed because it is too large.
Load diff
@@ -5,6 +5,8 @@ authors = ["72-S"]
|
|||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
async-trait = "0.1"
|
||||||
|
chrono = { version = "0.4.39", features = ["serde"] }
|
||||||
clap = "4.5.27"
|
clap = "4.5.27"
|
||||||
color-eyre = "0.6.3"
|
color-eyre = "0.6.3"
|
||||||
config = "0.15.6"
|
config = "0.15.6"
|
||||||
@@ -12,7 +14,12 @@ crossterm = "0.28.1"
|
|||||||
dirs-next = "2.0.0"
|
dirs-next = "2.0.0"
|
||||||
once_cell = "1.20.3"
|
once_cell = "1.20.3"
|
||||||
ratatui = "0.29.0"
|
ratatui = "0.29.0"
|
||||||
|
reqwest = { version = "0.12", features = ["json", "stream"] }
|
||||||
serde = { version = "1.0.217", features = ["derive"] }
|
serde = { version = "1.0.217", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
|
thiserror = "2"
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-appender = "0.2"
|
tracing-appender = "0.2"
|
||||||
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
|
||||||
|
zip = "2"
|
||||||
+8
-3
@@ -1,9 +1,14 @@
|
|||||||
mod cli;
|
mod cli;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
|
pub mod instance;
|
||||||
|
pub mod net;
|
||||||
pub mod tui;
|
pub mod tui;
|
||||||
|
|
||||||
fn main() {
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
let _guard = tui::logging::init();
|
let _guard = tui::logging::init();
|
||||||
color_eyre::install().expect("Failed to install color-eyre");
|
if let Err(e) = color_eyre::install() {
|
||||||
cli::init()
|
eprintln!("Failed to install color-eyre: {}", e);
|
||||||
|
}
|
||||||
|
cli::init().await
|
||||||
}
|
}
|
||||||
+24
-26
@@ -1,8 +1,6 @@
|
|||||||
use std::collections::VecDeque;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use tracing::field::{Field, Visit};
|
use tracing::field::{Field, Visit};
|
||||||
use tracing::{Level, Subscriber};
|
use tracing::{Level, Subscriber};
|
||||||
use tracing_appender::non_blocking::WorkerGuard;
|
use tracing_appender::non_blocking::WorkerGuard;
|
||||||
@@ -11,21 +9,20 @@ use tracing_subscriber::prelude::*;
|
|||||||
use tracing_subscriber::{EnvFilter, Layer};
|
use tracing_subscriber::{EnvFilter, Layer};
|
||||||
|
|
||||||
use crate::config::get_config_path;
|
use crate::config::get_config_path;
|
||||||
|
use crate::tui::error_buffer::{self, ErrorEvent};
|
||||||
const MAX_STATUS_EVENTS: usize = 50;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct StatusEvent {
|
|
||||||
pub level: Level,
|
|
||||||
pub message: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub static STATUS_EVENTS: Lazy<Arc<Mutex<VecDeque<StatusEvent>>>> =
|
|
||||||
Lazy::new(|| Arc::new(Mutex::new(VecDeque::new())));
|
|
||||||
|
|
||||||
pub fn init() -> WorkerGuard {
|
pub fn init() -> WorkerGuard {
|
||||||
let log_dir = get_config_path();
|
let log_dir = get_config_path();
|
||||||
std::fs::create_dir_all(&log_dir).expect("Failed to create log directory");
|
match std::fs::create_dir_all(&log_dir) {
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!(
|
||||||
|
"Warning: failed to create log directory {}: {}",
|
||||||
|
log_dir.display(),
|
||||||
|
e
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let file_appender = tracing_appender::rolling::daily(&log_dir, "mcl.log");
|
let file_appender = tracing_appender::rolling::daily(&log_dir, "mcl.log");
|
||||||
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
|
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
|
||||||
@@ -41,19 +38,17 @@ pub fn init() -> WorkerGuard {
|
|||||||
.with_ansi(false)
|
.with_ansi(false)
|
||||||
.with_filter(env_filter),
|
.with_filter(env_filter),
|
||||||
)
|
)
|
||||||
.with(StatusLayer::new(STATUS_EVENTS.clone()))
|
.with(StatusLayer::new(error_buffer::ERROR_EVENTS.clone()))
|
||||||
.init();
|
.init();
|
||||||
|
|
||||||
guard
|
guard
|
||||||
}
|
}
|
||||||
|
|
||||||
struct StatusLayer {
|
struct StatusLayer;
|
||||||
events: Arc<Mutex<VecDeque<StatusEvent>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl StatusLayer {
|
impl StatusLayer {
|
||||||
fn new(events: Arc<Mutex<VecDeque<StatusEvent>>>) -> Self {
|
fn new(_events: Arc<Mutex<std::collections::VecDeque<ErrorEvent>>>) -> Self {
|
||||||
Self { events }
|
Self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,21 +56,24 @@ impl<S: Subscriber> Layer<S> for StatusLayer {
|
|||||||
fn on_event(&self, event: &tracing::Event<'_>, _ctx: Context<'_, S>) {
|
fn on_event(&self, event: &tracing::Event<'_>, _ctx: Context<'_, S>) {
|
||||||
let level = *event.metadata().level();
|
let level = *event.metadata().level();
|
||||||
|
|
||||||
if level > Level::WARN {
|
if level > Level::INFO {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut visitor = MessageVisitor::default();
|
let mut visitor = MessageVisitor::default();
|
||||||
event.record(&mut visitor);
|
event.record(&mut visitor);
|
||||||
|
|
||||||
if let Ok(mut events) = self.events.lock() {
|
crate::tui::log_buffer::push_log(crate::tui::log_buffer::LogEntry {
|
||||||
events.push_back(StatusEvent {
|
level,
|
||||||
|
message: visitor.message.clone(),
|
||||||
|
});
|
||||||
|
|
||||||
|
if level <= Level::WARN {
|
||||||
|
error_buffer::push_error(ErrorEvent {
|
||||||
level,
|
level,
|
||||||
message: visitor.message,
|
message: visitor.message,
|
||||||
|
pushed_at: std::time::Instant::now(),
|
||||||
});
|
});
|
||||||
while events.len() > MAX_STATUS_EVENTS {
|
|
||||||
events.pop_front();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-2
@@ -1,5 +1,8 @@
|
|||||||
pub mod layout;
|
pub mod layout;
|
||||||
|
pub mod log_buffer;
|
||||||
pub mod logging;
|
pub mod logging;
|
||||||
|
pub mod error_buffer;
|
||||||
|
pub mod progress;
|
||||||
pub mod theme;
|
pub mod theme;
|
||||||
pub mod widgets;
|
pub mod widgets;
|
||||||
|
|
||||||
@@ -11,9 +14,9 @@ use crossterm::{
|
|||||||
};
|
};
|
||||||
use ratatui::{prelude::CrosstermBackend, Terminal};
|
use ratatui::{prelude::CrosstermBackend, Terminal};
|
||||||
|
|
||||||
pub fn show() -> color_eyre::Result<()> {
|
pub async fn show() -> color_eyre::Result<()> {
|
||||||
let mut terminal = init_ratatui()?;
|
let mut terminal = init_ratatui()?;
|
||||||
let result = layout::App::default().run(&mut terminal);
|
let result = layout::App::default().run(&mut terminal).await;
|
||||||
if let Err(err) = restore_ratatui() {
|
if let Err(err) = restore_ratatui() {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"failed to restore terminal. Run 'reset' or restart your terminal to recover: {}",
|
"failed to restore terminal. Run 'reset' or restart your terminal to recover: {}",
|
||||||
|
|||||||
Reference in new issue
Block a user