Feat: Baut eine eigene Log-Funktion.
This commit is contained in:
@@ -2,5 +2,7 @@
|
|||||||
name = "SmartMount"
|
name = "SmartMount"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
authors = ['DragonSlayer_14']
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
time = { version="0.3.41", features = ["formatting", "macros", "local-offset"] }
|
||||||
|
|||||||
73
src/log.rs
Normal file
73
src/log.rs
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
use std::any::Any;
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
use std::io::{IsTerminal, stdout};
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
use time::{OffsetDateTime, macros::format_description};
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
pub enum LogLevel {
|
||||||
|
Error = 1,
|
||||||
|
Warn = 2,
|
||||||
|
Info = 3,
|
||||||
|
Debug = 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for LogLevel {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
LogLevel::Error => write!(f, "ERROR"),
|
||||||
|
LogLevel::Warn => write!(f, "WARN"),
|
||||||
|
LogLevel::Info => write!(f, "INFO"),
|
||||||
|
LogLevel::Debug => write!(f, "DEBUG"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static IS_TERMINAL: OnceLock<bool> = OnceLock::new();
|
||||||
|
static LOG_LEVEL: LogLevel = LogLevel::Debug; // TODO: LogLevel aus Config laden
|
||||||
|
|
||||||
|
pub fn log(tag: &str, message: &str, log_level: LogLevel) {
|
||||||
|
if log_level.type_id() >= LOG_LEVEL.type_id() {
|
||||||
|
let message: String = format_message(tag, message, &log_level);
|
||||||
|
|
||||||
|
if is_terminal() {
|
||||||
|
if log_level == LogLevel::Error {
|
||||||
|
eprintln!("{}", message)
|
||||||
|
} else {
|
||||||
|
println!("{}", message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_terminal() -> bool {
|
||||||
|
*IS_TERMINAL.get_or_init(|| stdout().is_terminal())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn format_message(tag: &str, message: &str, log_level: &LogLevel) -> String {
|
||||||
|
let mut prefix: String = String::new();
|
||||||
|
|
||||||
|
let now_local: OffsetDateTime =
|
||||||
|
OffsetDateTime::now_local().unwrap_or(OffsetDateTime::now_utc());
|
||||||
|
let fmt =
|
||||||
|
format_description!("[day].[month].[year] [hour]:[minute]:[second].[subsecond digits:3]");
|
||||||
|
|
||||||
|
if tag != "" {
|
||||||
|
prefix = format!("[{}]", tag);
|
||||||
|
}
|
||||||
|
|
||||||
|
let message = format!(
|
||||||
|
"[{}][{}][{}]: {}",
|
||||||
|
now_local.format(fmt).unwrap(),
|
||||||
|
log_level.to_string(),
|
||||||
|
prefix,
|
||||||
|
message
|
||||||
|
);
|
||||||
|
|
||||||
|
match log_level {
|
||||||
|
&LogLevel::Error => format!("{}{}", "🛑", message),
|
||||||
|
&LogLevel::Warn => format!("{}{}", "⚠️", message),
|
||||||
|
&LogLevel::Info => format!("{}{}", "ℹ️", message),
|
||||||
|
&LogLevel::Debug => format!("{}{}", "🚧", message),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,10 @@
|
|||||||
|
use crate::log::LogLevel;
|
||||||
|
|
||||||
|
mod log;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
log::log("main", "Hello, world!", LogLevel::Error);
|
||||||
|
log::log("main", "Hello, world!", LogLevel::Warn);
|
||||||
|
log::log("main", "Hello, world!", LogLevel::Info);
|
||||||
|
log::log("main", "Hello, world!", LogLevel::Debug);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user