-
Notifications
You must be signed in to change notification settings - Fork 187
/
logging.rs
452 lines (408 loc) · 12.1 KB
/
logging.rs
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
use libc;
use std::str::FromStr;
use std::cmp::{self,Ord};
use std::sync::Mutex;
use std::fmt::{Arguments,format};
use std::io::{stdout,Stdout,Write};
use std::ascii::AsciiExt;
use std::net::{SocketAddr,UdpSocket};
use std::net::TcpStream;
use mio_uds::UnixDatagram;
lazy_static! {
pub static ref LOGGER: Mutex<Logger> = Mutex::new(Logger::new());
pub static ref PID: i32 = unsafe { libc::getpid() };
pub static ref TAG: String = LOGGER.lock().unwrap().tag.clone();
}
pub struct Logger {
pub directives: Vec<LogDirective>,
pub backend: LoggerBackend,
pub tag: String,
}
impl Logger {
pub fn new() -> Logger {
Logger {
directives: vec!(LogDirective {
name: None,
level: LogLevelFilter::Error,
}),
backend: LoggerBackend::Stdout(stdout()),
tag: "WAAAAAAAH".to_string()
}
}
pub fn init(tag: String, spec: &str, backend: LoggerBackend) {
let directives = parse_logging_spec(spec);
if let Ok(ref mut logger) = LOGGER.lock() {
logger.set_directives(directives);
logger.backend = backend;
logger.tag = tag;
}
//trying to init the logger tag
let ref t = *TAG;
}
pub fn log<'a>(&mut self, meta: &LogMetadata, args: Arguments) {
if self.enabled(meta) {
match self.backend {
LoggerBackend::Stdout(ref mut stdout) => {
stdout.write_fmt(args);
},
//FIXME: should have a buffer to write to instead of allocating a string
LoggerBackend::Unix(ref mut socket) => {
socket.send(format(args).as_bytes());
},
//FIXME: should have a buffer to write to instead of allocating a string
LoggerBackend::Udp(ref mut socket, ref address) => {
socket.send_to(format(args).as_bytes(), address);
}
LoggerBackend::Tcp(ref mut socket) => {
socket.write_fmt(args);
},
}
}
}
pub fn set_directives(&mut self, directives: Vec<LogDirective>) {
self.directives = directives;
}
fn enabled(&self, meta: &LogMetadata) -> bool {
// Search for the longest match, the vector is assumed to be pre-sorted.
for directive in self.directives.iter().rev() {
match directive.name {
Some(ref name) if !meta.target.starts_with(&**name) => {},
Some(..) | None => {
return meta.level <= directive.level
}
}
}
false
}
}
pub enum LoggerBackend {
Stdout(Stdout),
Unix(UnixDatagram),
Udp(UdpSocket, SocketAddr),
Tcp(TcpStream)
}
#[repr(usize)]
#[derive(Copy, Eq, Debug)]
pub enum LogLevel {
/// The "error" level.
///
/// Designates very serious errors.
Error = 1, // This way these line up with the discriminants for LogLevelFilter below
/// The "warn" level.
///
/// Designates hazardous situations.
Warn,
/// The "info" level.
///
/// Designates useful information.
Info,
/// The "debug" level.
///
/// Designates lower priority information.
Debug,
/// The "trace" level.
///
/// Designates very low priority, often extremely verbose, information.
Trace,
}
static LOG_LEVEL_NAMES: [&'static str; 6] = ["OFF", "ERROR", "WARN", "INFO",
"DEBUG", "TRACE"];
impl Clone for LogLevel {
#[inline]
fn clone(&self) -> LogLevel {
*self
}
}
impl PartialEq for LogLevel {
#[inline]
fn eq(&self, other: &LogLevel) -> bool {
*self as usize == *other as usize
}
}
impl PartialEq<LogLevelFilter> for LogLevel {
#[inline]
fn eq(&self, other: &LogLevelFilter) -> bool {
*self as usize == *other as usize
}
}
impl PartialOrd for LogLevel {
#[inline]
fn partial_cmp(&self, other: &LogLevel) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl PartialOrd<LogLevelFilter> for LogLevel {
#[inline]
fn partial_cmp(&self, other: &LogLevelFilter) -> Option<cmp::Ordering> {
Some((*self as usize).cmp(&(*other as usize)))
}
}
impl Ord for LogLevel {
#[inline]
fn cmp(&self, other: &LogLevel) -> cmp::Ordering {
(*self as usize).cmp(&(*other as usize))
}
}
impl LogLevel {
fn from_usize(u: usize) -> Option<LogLevel> {
match u {
1 => Some(LogLevel::Error),
2 => Some(LogLevel::Warn),
3 => Some(LogLevel::Info),
4 => Some(LogLevel::Debug),
5 => Some(LogLevel::Trace),
_ => None
}
}
/// Returns the most verbose logging level.
#[inline]
pub fn max() -> LogLevel {
LogLevel::Trace
}
/// Converts the `LogLevel` to the equivalent `LogLevelFilter`.
#[inline]
pub fn to_log_level_filter(&self) -> LogLevelFilter {
LogLevelFilter::from_usize(*self as usize).unwrap()
}
}
#[repr(usize)]
#[derive(Copy, Eq, Debug)]
pub enum LogLevelFilter {
Off,
Error,
Warn,
Info,
Debug,
Trace,
}
impl Clone for LogLevelFilter {
#[inline]
fn clone(&self) -> LogLevelFilter {
*self
}
}
impl PartialEq for LogLevelFilter {
#[inline]
fn eq(&self, other: &LogLevelFilter) -> bool {
*self as usize == *other as usize
}
}
impl PartialEq<LogLevel> for LogLevelFilter {
#[inline]
fn eq(&self, other: &LogLevel) -> bool {
other.eq(self)
}
}
impl PartialOrd for LogLevelFilter {
#[inline]
fn partial_cmp(&self, other: &LogLevelFilter) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl PartialOrd<LogLevel> for LogLevelFilter {
#[inline]
fn partial_cmp(&self, other: &LogLevel) -> Option<cmp::Ordering> {
other.partial_cmp(self).map(|x| x.reverse())
}
}
impl Ord for LogLevelFilter {
#[inline]
fn cmp(&self, other: &LogLevelFilter) -> cmp::Ordering {
(*self as usize).cmp(&(*other as usize))
}
}
impl FromStr for LogLevelFilter {
type Err = ();
fn from_str(level: &str) -> Result<LogLevelFilter, ()> {
ok_or(LOG_LEVEL_NAMES.iter()
.position(|&name| name.eq_ignore_ascii_case(level))
.map(|p| LogLevelFilter::from_usize(p).unwrap()), ())
}
}
impl LogLevelFilter {
fn from_usize(u: usize) -> Option<LogLevelFilter> {
match u {
0 => Some(LogLevelFilter::Off),
1 => Some(LogLevelFilter::Error),
2 => Some(LogLevelFilter::Warn),
3 => Some(LogLevelFilter::Info),
4 => Some(LogLevelFilter::Debug),
5 => Some(LogLevelFilter::Trace),
_ => None
}
}
/// Returns the most verbose logging level filter.
#[inline]
pub fn max() -> LogLevelFilter {
LogLevelFilter::Trace
}
/// Converts `self` to the equivalent `LogLevel`.
///
/// Returns `None` if `self` is `LogLevelFilter::Off`.
#[inline]
pub fn to_log_level(&self) -> Option<LogLevel> {
LogLevel::from_usize(*self as usize)
}
}
/// Metadata about a log message.
pub struct LogMetadata {
pub level: LogLevel,
pub target: &'static str,
}
pub struct LogDirective {
name: Option<String>,
level: LogLevelFilter,
}
fn ok_or<T, E>(t: Option<T>, e: E) -> Result<T, E> {
match t {
Some(t) => Ok(t),
None => Err(e),
}
}
pub fn parse_logging_spec(spec: &str) -> Vec<LogDirective> {
let mut dirs = Vec::new();
let mut parts = spec.split('/');
let mods = parts.next();
let _ = parts.next();
if parts.next().is_some() {
println!("warning: invalid logging spec '{}', \
ignoring it (too many '/'s)", spec);
return dirs;
}
mods.map(|m| { for s in m.split(',') {
if s.len() == 0 { continue }
let mut parts = s.split('=');
let (log_level, name) = match (parts.next(), parts.next().map(|s| s.trim()), parts.next()) {
(Some(part0), None, None) => {
// if the single argument is a log-level string or number,
// treat that as a global fallback
match part0.parse() {
Ok(num) => (num, None),
Err(_) => (LogLevelFilter::max(), Some(part0)),
}
}
(Some(part0), Some(""), None) => (LogLevelFilter::max(), Some(part0)),
(Some(part0), Some(part1), None) => {
match part1.parse() {
Ok(num) => (num, Some(part0)),
_ => {
println!("warning: invalid logging spec '{}', \
ignoring it", part1);
continue
}
}
},
_ => {
println!("warning: invalid logging spec '{}', \
ignoring it", s);
continue
}
};
dirs.push(LogDirective {
name: name.map(|s| s.to_string()),
level: log_level,
});
}});
return dirs;
}
#[macro_export]
macro_rules! log {
(target: $target:expr, $lvl:expr, $format:expr, $level_tag:expr, $($arg:tt)+) => ({
use $crate::logging::LOGGER;
static _META: $crate::logging::LogMetadata = $crate::logging::LogMetadata {
level: $lvl,
target: module_path!(),
};
{
//FIXME: we will lose logs in multithreading like this
if let Ok(mut logger) = LOGGER.try_lock() {
logger.log(
&_META,
format_args!(
concat!("{}\t{}\t{}\t{}\t{}\t", $format, '\n'),
::time::now_utc().rfc3339(), ::time::precise_time_ns(), *$crate::logging::PID,
$level_tag, *$crate::logging::TAG, $($arg)+));
}
}
});
(target: $target:expr, $lvl:expr, $format:expr, $level_tag:expr) => ({
use $crate::logging::LOGGER;
static _META: $crate::logging::LogMetadata = $crate::logging::LogMetadata {
level: $lvl,
target: module_path!(),
};
{
//FIXME: we will lose logs in multithreading like this
if let Ok(mut logger) = LOGGER.try_lock() {
logger.log(
&_META,
format_args!(
concat!("{}\t{}\t{}\t{}\t{}\t", $format, '\n'),
::time::now_utc().rfc3339(), ::time::precise_time_ns(), *$crate::logging::PID,
$level_tag, *$crate::logging::TAG));
}
}
});
($lvl:expr, $($arg:tt)+) => (log!(target: module_path!(), $lvl, $($arg)+));
}
#[macro_export]
macro_rules! error {
($format:expr, $($arg:tt)*) => {
log!($crate::logging::LogLevel::Error, $format, "ERROR", $($arg)*);
};
($format:expr) => {
log!($crate::logging::LogLevel::Error, $format, "ERROR");
};
}
#[macro_export]
macro_rules! warn {
($format:expr, $($arg:tt)*) => {
use time;
use logging::PID;
log!($crate::logging::LogLevel::Warn, $format, "WARN", $($arg)*);
};
($format:expr) => {
log!($crate::logging::LogLevel::Warn, $format, "WARN");
}
}
#[macro_export]
macro_rules! info {
($format:expr, $($arg:tt)*) => {
log!($crate::logging::LogLevel::Info, $format, "INFO", $($arg)*);
};
($format:expr) => {
log!($crate::logging::LogLevel::Info, $format, "INFO");
}
}
#[macro_export]
macro_rules! debug {
($format:expr, $($arg:tt)*) => {
#[cfg(debug_assertions)]
log!($crate::logging::LogLevel::Debug, concat!("{}\t", $format),
"DEBUG", module_path!(), $($arg)*);
};
($format:expr) => {
#[cfg(debug_assertions)]
log!($crate::logging::LogLevel::Debug, concat!("{}\t", $format),
"DEBUG", module_path!());
}
}
#[macro_export]
macro_rules! trace {
($format:expr, $($arg:tt)*) => (
#[cfg(debug_assertions)]
log!($crate::logging::LogLevel::Trace, concat!("{}\t", $format),
"TRACE", module_path!(), $($arg)*);
);
($format:expr) => (
#[cfg(debug_assertions)]
log!($crate::logging::LogLevel::Trace, concat!("{}\t", $format),
"TRACE", module_path!());
)
}
#[macro_export]
macro_rules! setup_test_logger {
() => (
$crate::logging::Logger::init(module_path!().to_string(), "error", $crate::logging::LoggerBackend::Stdout(::std::io::stdout()));
);
}