refactor: reorganize source and tests

This commit is contained in:
objz committed 2026-07-27 11:28:57 +02:00
1 parent 15d76c18a0
commit c6205a337c
139 files changed
+5532 -5548

No files matched your search

+62
View File
@@ -0,0 +1,62 @@
// per-launch log files stored under minecraft/logs/launches/
// each launch gets its own timestamped file so you can go back and see what
// crashed last tuesday at 3am
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(crate::storage::MINECRAFT_DIR_NAME)
.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(),
}
}
#[cfg(test)]
#[path = "../tests/logs/files.rs"]
mod tests;
+72
View File
@@ -0,0 +1,72 @@
// in-memory ring buffer for stdout/stderr from running mc instances.
// capped at 2000 lines per instance so it doesn't eat all the RAM
// if someone leaves a server running for a week. you're welcome.
use std::collections::{HashMap, VecDeque};
use std::sync::LazyLock;
use std::sync::{Arc, Mutex};
use crate::instance::launch::parser::{LogLevel, LogStream, ParsedLogEvent};
const MAX_LINES: usize = 2000;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LiveLogLine {
pub level: LogLevel,
pub stream: LogStream,
pub text: String,
}
type LogsMap = Arc<Mutex<HashMap<String, VecDeque<LiveLogLine>>>>;
pub static LOGS: LazyLock<LogsMap> = LazyLock::new(|| Arc::new(Mutex::new(HashMap::new())));
pub fn push(name: &str, line: impl Into<String>) {
push_line(name, LogLevel::Info, LogStream::Stdout, line);
}
pub fn push_event(name: &str, event: ParsedLogEvent) {
for line in event.lines {
push_line(name, event.level, event.primary_stream, line);
}
}
pub fn push_line(name: &str, level: LogLevel, stream: LogStream, line: impl Into<String>) {
if let Ok(mut logs) = LOGS.lock() {
let buf = logs.entry(name.to_string()).or_insert_with(VecDeque::new);
buf.push_back(LiveLogLine {
level,
stream,
text: line.into(),
});
while buf.len() > MAX_LINES {
buf.pop_front();
}
}
}
pub fn get_entries(name: &str) -> Vec<LiveLogLine> {
LOGS.lock()
.ok()
.and_then(|logs| logs.get(name).map(|buf| buf.iter().cloned().collect()))
.unwrap_or_default()
}
pub fn get_all(name: &str) -> Vec<String> {
LOGS.lock()
.ok()
.and_then(|logs| {
logs.get(name)
.map(|buf| buf.iter().map(|line| line.text.clone()).collect())
})
.unwrap_or_default()
}
pub fn clear(name: &str) {
if let Ok(mut logs) = LOGS.lock() {
logs.remove(name);
}
}
#[cfg(test)]
#[path = "../tests/logs/live.rs"]
mod tests;
+2
View File
@@ -0,0 +1,2 @@
pub mod files;
pub mod live;