log4r/
lib.rs

1use std::fmt::{Display, Formatter, Result};
2
3pub enum LogTypes {
4    Success,
5    Info,
6    Warning,
7    Error,
8    Critical,
9    Custom(String),
10}
11
12impl Display for LogTypes {
13    fn fmt(&self, f: &mut Formatter) -> Result {
14        match self {
15            LogTypes::Success => write!(f, "\x1b[0;92m"),
16            LogTypes::Info => write!(f, "\x1b[0;36m"),
17            LogTypes::Warning => write!(f, "\x1b[0;93m"),
18            LogTypes::Error => write!(f, "\x1b[0;91m"),
19            LogTypes::Critical => write!(f, "\x1b[1;97m\x1b[1;101m"),
20            LogTypes::Custom(s) => write!(f, "{}", s),
21        }
22    }
23}
24
25/// Prints a green message in the terminal with a `[SUCCESS]` label
26///
27/// ```
28/// use log4r;
29/// use log4r::LogTypes;
30/// let s: String = String::from("This is a custom log");
31/// log4r::log(s, LogTypes::Custom("\x1b[1;97m\x1b[1;101m".to_string()));
32/// ```
33pub fn log(str: String, log_type: LogTypes) {
34    match log_type {
35        LogTypes::Success => println!("{}[SUCCESS] {}\x1b[0;0m", log_type, str),
36        LogTypes::Info => println!("{}[INFO] {}\x1b[0;0m", log_type, str),
37        LogTypes::Warning => println!("{}[WARNING] {}\x1b[0;0m", log_type, str),
38        LogTypes::Error => println!("{}[ERROR] {}\x1b[0;0m", log_type, str),
39        LogTypes::Critical => println!("{}[URGENT] {}\x1b[0;0m", log_type, str),
40        LogTypes::Custom(esc) =>  println!("{}[CUSTOM LOG]  {}\x1b[0;0m", esc, str),
41    }
42}
43
44/// Prints a green message in the terminal with a `[SUCCESS]` label
45///
46/// ```
47/// use log4r;
48/// let s: String = String::from("User has logged in");
49/// log4r::success(s);
50/// ```
51pub fn success(s: String) {
52    log(s, LogTypes::Success)
53}
54
55/// Prints a blue message in the terminal with a `[INFO]` label
56///
57/// ```
58/// use log4r;
59/// let s: String = String::from("User has logged in");
60/// log4r::info(s);
61/// ```
62pub fn info(s: String) {
63    log(s, LogTypes::Info)
64}
65
66/// Prints a yellow message in the terminal with a `[WARNING]` label
67///
68/// ```
69/// use log4r;
70/// let s: String = String::from("User has logged in");
71/// log4r::warning(s);
72/// ```
73pub fn warning(s: String) {
74    log(s, LogTypes::Warning)
75}
76
77/// Prints a red message in the terminal with a `[ERROR]` label
78///
79/// ```
80/// use log4r;
81/// let s: String = String::from("User has logged in");
82/// log4r::error(s);
83/// ```
84pub fn error(s: String) {
85    log(s, LogTypes::Error)
86}
87
88/// Prints a white and red message in the terminal with a `[CRITICAL]` label
89///
90/// ```
91/// use log4r;
92/// let s: String = String::from("User has logged in");
93/// log4r::critical(s);
94/// ```
95pub fn critical(s: String) {
96    log(s, LogTypes::Critical)
97}