Feat: Macht Code auch für Windows verwendbar.

This commit is contained in:
2025-08-18 13:11:30 +02:00
parent a02228d300
commit c2e7c4a9df

View File

@@ -1,14 +1,12 @@
use std::any::Any; use std::env;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::fs::{create_dir_all, File, OpenOptions}; use std::fs::{create_dir_all, File, OpenOptions};
use std::io::{stdout, IsTerminal, Write}; use std::io::{stdout, IsTerminal, Write};
use std::path::PathBuf;
use std::sync::{Mutex, OnceLock}; use std::sync::{Mutex, OnceLock};
use std::env;
use time::{macros::format_description, OffsetDateTime}; use time::{macros::format_description, OffsetDateTime};
#[derive(PartialEq)] #[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
pub enum LogLevel { pub enum LogLevel {
Error = 1, Error = 1,
Warn = 2, Warn = 2,
@@ -33,7 +31,7 @@ static LOG_LEVEL: LogLevel = LogLevel::Debug; // TODO: LogLevel aus Config laden
static LOG_FILE: OnceLock<Mutex<Option<File>>> = OnceLock::new(); static LOG_FILE: OnceLock<Mutex<Option<File>>> = OnceLock::new();
pub fn log(tag: &str, message: &str, log_level: LogLevel) { pub fn log(tag: &str, message: &str, log_level: LogLevel) {
if log_level.type_id() >= LOG_LEVEL.type_id() { if log_level <= LOG_LEVEL {
let message: String = format_message(tag, message, &log_level); let message: String = format_message(tag, message, &log_level);
if is_terminal() { if is_terminal() {
@@ -96,17 +94,18 @@ fn get_or_init_log_file() -> &'static Mutex<Option<File>> {
fn open_log_file() -> std::io::Result<File> { fn open_log_file() -> std::io::Result<File> {
let program = program_name(); let program = program_name();
let rand = "13692bbf-a93b-43e9-9cc6-f05f94a8cfb6"; let rand = "13692bbf-a93b-43e9-9cc6-f05f94a8cfb6";
let base_dir = format!("/tmp/{}-{}", program, rand);
let mut dir = env::temp_dir();
dir.push(format!("{}-{}", program, rand));
create_dir_all(&dir)?;
let today_fmt = format_description!("[year]-[month]-[day]"); let today_fmt = format_description!("[year]-[month]-[day]");
let date_str = OffsetDateTime::now_local() let date_str = OffsetDateTime::now_local()
.unwrap_or(OffsetDateTime::now_utc()) .unwrap_or(OffsetDateTime::now_utc())
.format(today_fmt) .format(today_fmt)
.unwrap_or_else(|_| "0000-00-00".to_string()); .unwrap_or_else(|_| "0000-00-00".to_string());
let file_name = format!("log-{}.log", date_str);
let mut dir = PathBuf::from(base_dir); let file_name = format!("log-{}.log", date_str);
create_dir_all(&dir)?;
dir.push(file_name); dir.push(file_name);
OpenOptions::new() OpenOptions::new()