Skip to main content

rfortune/
log.rs

1use std::fmt;
2
3/// Tipologie di messaggio per la console
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum LogKind {
6    Info,
7    Ok,
8    Ko,
9    Warning,
10}
11
12/// Logger per messaggi a console (no file)
13pub struct ConsoleLog;
14
15impl ConsoleLog {
16    /// Stampa un messaggio con colore e simbolo in base al tipo
17    pub fn log(kind: LogKind, msg: impl fmt::Display) {
18        match kind {
19            LogKind::Info => {
20                // ℹ = U+2139
21                println!("\x1b[36m\u{2139}  {}\x1b[0m", msg);
22            }
23            LogKind::Ok => {
24                // ✅ = U+2705
25                println!("\x1b[32m\u{2705} {}\x1b[0m", msg);
26            }
27            LogKind::Ko => {
28                // ❌ = U+274C
29                eprintln!("\x1b[31m\u{274C} {}\x1b[0m", msg);
30            }
31            LogKind::Warning => {
32                // ⚠️ = U+26A0 U+FE0F
33                println!("\x1b[33m\u{26A0}\u{FE0F} {}\x1b[0m", msg);
34            }
35        }
36    }
37
38    pub fn info(msg: impl fmt::Display) {
39        Self::log(LogKind::Info, msg);
40    }
41
42    pub fn ok(msg: impl fmt::Display) {
43        Self::log(LogKind::Ok, msg);
44    }
45
46    pub fn ko(msg: impl fmt::Display) {
47        Self::log(LogKind::Ko, msg);
48    }
49
50    pub fn warn(msg: impl fmt::Display) {
51        Self::log(LogKind::Warning, msg);
52    }
53}