feat(tui): redesign logs tab with split pane viewer
This commit is contained in:
1 parent
24a8afe29b
commit
83dd7efc11
7 files changed
+543
-13
No files matched your search
@@ -282,31 +282,52 @@ pub async fn launch(
|
||||
|
||||
crate::running::set_state(&name, crate::running::RunState::Running);
|
||||
|
||||
let log_file_path =
|
||||
crate::instance::log_files::create_log_file(instances_dir, &name);
|
||||
|
||||
let name_for_task = name.clone();
|
||||
let instances_dir_owned = instances_dir.to_path_buf();
|
||||
let meta_dir_owned = meta_dir.to_path_buf();
|
||||
|
||||
tokio::spawn(async move {
|
||||
use std::io::Write;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tokio::io::AsyncBufReadExt;
|
||||
|
||||
let log_writer: Arc<Mutex<Option<std::fs::File>>> = Arc::new(Mutex::new(
|
||||
log_file_path.and_then(|p| std::fs::File::create(p).ok()),
|
||||
));
|
||||
|
||||
if let Some(stdout) = child.stdout.take() {
|
||||
let n = name_for_task.clone();
|
||||
let w = log_writer.clone();
|
||||
let mut lines = tokio::io::BufReader::new(stdout).lines();
|
||||
tokio::spawn(async move {
|
||||
while let Ok(Some(line)) = lines.next_line().await {
|
||||
tracing::info!(target: "mc_instance", "[{}] {}", n, line);
|
||||
crate::instance_logs::push(&n, &line);
|
||||
if let Ok(mut f) = w.lock() {
|
||||
if let Some(f) = f.as_mut() {
|
||||
let _ = writeln!(f, "{}", line);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if let Some(stderr) = child.stderr.take() {
|
||||
let n = name_for_task.clone();
|
||||
let w = log_writer.clone();
|
||||
let mut lines = tokio::io::BufReader::new(stderr).lines();
|
||||
tokio::spawn(async move {
|
||||
while let Ok(Some(line)) = lines.next_line().await {
|
||||
tracing::warn!(target: "mc_instance", "[{}] {}", n, line);
|
||||
crate::instance_logs::push(&n, &line);
|
||||
if let Ok(mut f) = w.lock() {
|
||||
if let Some(f) = f.as_mut() {
|
||||
let _ = writeln!(f, "[STDERR] {}", line);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LogFileEntry {
|
||||
pub name: String,
|
||||
pub path: PathBuf,
|
||||
}
|
||||
|
||||
pub fn log_dir(instances_dir: &Path, instance_name: &str) -> PathBuf {
|
||||
instances_dir
|
||||
.join(instance_name)
|
||||
.join(".minecraft")
|
||||
.join("logs")
|
||||
.join("launches")
|
||||
}
|
||||
|
||||
pub fn create_log_file(instances_dir: &Path, instance_name: &str) -> Option<PathBuf> {
|
||||
let dir = log_dir(instances_dir, instance_name);
|
||||
std::fs::create_dir_all(&dir).ok()?;
|
||||
let now = chrono::Local::now().format("%Y-%m-%d_%H-%M-%S");
|
||||
Some(dir.join(format!("{now}.log")))
|
||||
}
|
||||
|
||||
pub fn scan_log_files(instances_dir: &Path, instance_name: &str) -> Vec<LogFileEntry> {
|
||||
let dir = log_dir(instances_dir, instance_name);
|
||||
|
||||
let read_dir = match std::fs::read_dir(&dir) {
|
||||
Ok(rd) => rd,
|
||||
Err(_) => return Vec::new(),
|
||||
};
|
||||
|
||||
let mut entries: Vec<LogFileEntry> = read_dir
|
||||
.flatten()
|
||||
.filter_map(|entry| {
|
||||
let path = entry.path();
|
||||
let name = path.file_name()?.to_str()?.to_string();
|
||||
if name.ends_with(".log") {
|
||||
Some(LogFileEntry { name, path })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
entries.sort_by(|a, b| b.name.cmp(&a.name));
|
||||
entries
|
||||
}
|
||||
|
||||
pub fn read_log_file(path: &Path) -> Vec<String> {
|
||||
match std::fs::read_to_string(path) {
|
||||
Ok(content) => content.lines().map(|l| l.to_string()).collect(),
|
||||
Err(_) => Vec::new(),
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
pub mod launch;
|
||||
pub mod log_files;
|
||||
pub mod manager;
|
||||
pub mod loader;
|
||||
pub mod models;
|
||||
|
||||
Reference in new issue
Block a user