-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlib.rs
More file actions
176 lines (151 loc) · 4.06 KB
/
Copy pathlib.rs
File metadata and controls
176 lines (151 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#![allow(clippy::needless_return)]
//! A simple logging library for lambda-rs crates.
use std::fmt::Debug;
/// A trait for handling log messages.
pub mod handler;
/// The log level for the logger.
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
pub enum LogLevel {
TRACE,
DEBUG,
INFO,
WARN,
ERROR,
FATAL,
}
/// Logger implementation.
pub struct Logger {
name: String,
level: LogLevel,
handlers: Vec<Box<dyn handler::Handler>>,
}
impl Logger {
/// Creates a new logger with the given log level and name.
pub fn new(level: LogLevel, name: &str) -> Self {
Self {
name: name.to_string(),
level,
handlers: Vec::new(),
}
}
/// Returns the global logger.
pub fn global() -> &'static mut Self {
// TODO(vmarcella): Fix the instantiation for the global logger.
unsafe {
if LOGGER.is_none() {
LOGGER = Some(Logger {
level: LogLevel::TRACE,
name: "lambda-rs".to_string(),
handlers: vec![Box::new(handler::ConsoleHandler::new("lambda-rs"))],
});
}
};
return unsafe { &mut LOGGER }
.as_mut()
.expect("Logger not initialized");
}
/// Adds a handler to the logger. Handlers are called in the order they
/// are added.
pub fn add_handler(&mut self, handler: Box<dyn handler::Handler>) {
self.handlers.push(handler);
}
fn compare_levels(&self, level: LogLevel) -> bool {
level as u8 >= self.level as u8
}
/// Logs a trace message to all handlers.
pub fn trace(&mut self, message: String) {
if !self.compare_levels(LogLevel::TRACE) {
return;
}
for handler in self.handlers.iter_mut() {
handler.trace(message.clone());
}
}
/// Logs a debug message to all handlers.
pub fn debug(&mut self, message: String) {
if !self.compare_levels(LogLevel::DEBUG) {
return;
}
for handler in self.handlers.iter_mut() {
handler.debug(message.clone());
}
}
/// Logs an info message to all handlers.
pub fn info(&mut self, message: String) {
if !self.compare_levels(LogLevel::INFO) {
return;
}
for handler in self.handlers.iter_mut() {
handler.info(message.clone());
}
}
/// Logs a warning to all handlers.
pub fn warn(&mut self, message: String) {
if !self.compare_levels(LogLevel::WARN) {
return;
}
for handler in self.handlers.iter_mut() {
handler.warn(message.clone());
}
}
/// Logs an error to all handlers.
pub fn error(&mut self, message: String) {
if !self.compare_levels(LogLevel::ERROR) {
return;
}
for handler in self.handlers.iter_mut() {
handler.error(message.clone());
}
}
/// Logs a fatal error to all handlers and exits the program.
pub fn fatal(&mut self, message: String) {
if !self.compare_levels(LogLevel::FATAL) {
return;
}
for handler in self.handlers.iter_mut() {
handler.fatal(message.clone());
}
std::process::exit(1);
}
}
pub(crate) static mut LOGGER: Option<Logger> = None;
/// Trace logging macro using the global logger instance.
#[macro_export]
macro_rules! trace {
($($arg:tt)*) => {
logging::Logger::global().trace(format!("{}", format_args!($($arg)*)));
};
}
/// Trace logging macro using the global logger instance.
#[macro_export]
macro_rules! debug {
($($arg:tt)*) => {
logging::Logger::global().debug(format!("{}", format_args!($($arg)*)));
};
}
/// Trace logging macro using the global logger instance.
#[macro_export]
macro_rules! info {
($($arg:tt)*) => {
logging::Logger::global().info(format!("{}", format_args!($($arg)*)));
};
}
// Define logging macros that use the global logger instance
#[macro_export]
macro_rules! warn {
($($arg:tt)*) => {
logging::Logger::global().warn(format!("{}", format_args!($($arg)*)));
};
}
#[macro_export]
macro_rules! error {
($($arg:tt)*) => {
logging::Logger::global().error(format!("{}", format_args!($($arg)*)));
};
}
#[macro_export]
macro_rules! fatal {
($($arg:tt)*) => {
logging::Logger::global().fatal(format!("{}", format_args!($($arg)*)));
};
}