logi/
lib.rs

1//! # Logi library
2//! This library is used to log messages in a structured and beautiful way.
3//!
4//! ## About
5//! This lib uses the `println` macro to log messages, exposes some macros to log messages in different levels.
6//!
7//! The levels are:
8//! - **trace**: for debug messages.
9//! - **debug**: for info messages.
10//! - **success**: for success messages.
11//! - **warn**: for warning messages.
12//! - **error**: for error messages.
13//! - **custom**: for custom messages.
14//!
15//! ## Examples
16//! ```rust
17//! #[macro_use] // We only need to import some macros to use `Logi`.
18//! extern crate logi;
19//!
20//! // Let's start logging!
21//! fn main() {
22//!    // Info level.
23//!    trace!("Starting the CLI.");   // 02:12:22 | 🔧 TRCE | Starting the CLI.
24//!    debug!("Starting the CLI.");   // 02:12:22 | 🔮 DBUG | Starting the CLI.
25//!
26//!    // Confirmation level.
27//!    success!("Starting the CLI."); // 02:12:22 | 🎉 YEEE | Starting the CLI.
28//!
29//!    // Warning & Error levels.
30//!    warn!("Starting the CLI.");    // 02:12:22 | 💡 WARN | Starting the CLI.
31//!    error!("Starting the CLI.");   // 02:12:22 | 💥 F#CK | Starting the CLI.
32//!
33//!    // Custom level. (Thats a different macro, here we define the level of the message as the way we want.)
34//!    custom!("🧭 CSTM".to_string(), format!("Starting the {}.", "CLI")); // 20:39:24 | 🧭 CSTM | Starting the CLI.
35//! }
36//! ```
37
38mod interface;
39pub use interface::Level;
40
41mod functions;
42pub use functions::*;
43
44/// 💥 - Logs a message at the error level.
45#[macro_export]
46macro_rules! error {
47    // error!("a {} event", "log")
48    ($($arg:tt)+) => ($crate::log($crate::Flag::Level($crate::Level::Error), format!($($arg)+)))
49}
50
51/// 💡 - Logs a message at the warn level.
52#[macro_export]
53macro_rules! warn {
54    // warn!("a {} event", "log")
55    ($($arg:tt)+) => ($crate::log($crate::Flag::Level($crate::Level::Warn), format!($($arg)+)))
56}
57
58/// 🔮 - Logs a message at the debug level.
59#[macro_export]
60macro_rules! debug {
61    // debug!("a {} event", "log")
62    ($($arg:tt)+) => ($crate::log($crate::Flag::Level($crate::Level::Debug), format!($($arg)+)))
63}
64
65/// 📰 - Logs a message at the info level.
66#[macro_export]
67macro_rules! info {
68    // info!("a {} event", "log")
69    ($($arg:tt)+) => ($crate::log($crate::Flag::Level($crate::Level::Info), format!($($arg)+)))
70}
71
72/// 🎉 - Logs a message at the success level.
73#[macro_export]
74macro_rules! success {
75    // success!("a {} event", "log")
76    ($($arg:tt)+) => ($crate::log($crate::Flag::Level($crate::Level::Success), format!($($arg)+)))
77}
78
79/// 🔧 - Logs a message at the trace level.
80#[macro_export]
81macro_rules! trace {
82    // trace!("a {} event", "log")
83    ($($arg:tt)+) => ($crate::log($crate::Flag::Level($crate::Level::Trace), format!($($arg)+)))
84}
85
86/// 💭 - Logs a message at the help level.
87#[macro_export]
88macro_rules! help {
89    // help!("a {} event", "log")
90    ($($arg:tt)+) => ($crate::log($crate::Flag::Level($crate::Level::Help), format!($($arg)+)))
91}
92
93/// 🧠 - Logs a message at the custom level.
94#[macro_export]
95macro_rules! custom {
96    // custom!("a {} event", "log")
97    ($level: expr, $message: expr) => {
98        $crate::log($crate::Flag::String($level), $message)
99    };
100}
101
102/// 😵 - Logs a message at the Fatal level.
103#[macro_export]
104macro_rules! fatal {
105    // fatal!("a {} event", "log")
106    ($($arg:tt)+) => ($crate::log($crate::Flag::Level($crate::Level::Fatal), format!($($arg)+)))
107}
108
109////// ! Test Section ! //////
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114
115    #[test] // Macro output captures.
116    fn macros() {
117        trace!("Trace message."); // 02:12:22 | 🔧 TRCE | Trace message.
118        debug!("Debug message."); // 02:12:22 | 🔮 DBUG | Debug message.
119        info!("Info message."); // 02:12:22 | 📰 INFO | Info message.
120        success!("Success message."); // 02:12:22 | 🎉 YEEE | Success message.
121        warn!("Warn message."); // 02:12:22 | 💡 WARN | Warn message.
122        error!("Error message."); // 02:12:22 | 💥 F#CK | Error message.
123        custom!("🧭 CSTM".to_string(), format!("Custom message.")); // 20:39:24 | 🧭 CSTM | Custom message.
124    }
125}