Feat: Macht Code auch für Windows verwendbar.
This commit is contained in:
17
src/log.rs
17
src/log.rs
@@ -1,14 +1,12 @@
|
||||
use std::any::Any;
|
||||
use std::env;
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::fs::{create_dir_all, File, OpenOptions};
|
||||
use std::io::{stdout, IsTerminal, Write};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
use std::env;
|
||||
|
||||
use time::{macros::format_description, OffsetDateTime};
|
||||
|
||||
#[derive(PartialEq)]
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
|
||||
pub enum LogLevel {
|
||||
Error = 1,
|
||||
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();
|
||||
|
||||
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);
|
||||
|
||||
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> {
|
||||
let program = program_name();
|
||||
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 date_str = OffsetDateTime::now_local()
|
||||
.unwrap_or(OffsetDateTime::now_utc())
|
||||
.format(today_fmt)
|
||||
.unwrap_or_else(|_| "0000-00-00".to_string());
|
||||
let file_name = format!("log-{}.log", date_str);
|
||||
|
||||
let mut dir = PathBuf::from(base_dir);
|
||||
create_dir_all(&dir)?;
|
||||
let file_name = format!("log-{}.log", date_str);
|
||||
dir.push(file_name);
|
||||
|
||||
OpenOptions::new()
|
||||
|
||||
Reference in New Issue
Block a user