async_rawlogger/
lib.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
use arc_swap::ArcSwap;
use concat_strs_derive::concat_strs;
pub use log::{
	debug,
	error,
	info,
	log,
	log_enabled,
	logger,
	trace,
	warn,
	Level,
	LevelFilter,
	Record,
};
use std::borrow::Cow;
use std::fmt::Display;
use std::hash::{
	BuildHasher,
	Hash,
	Hasher,
};
use std::io::{
	stderr,
	Error as IoError,
	Write,
};
use std::sync::atomic::{
	AtomicBool,
	AtomicUsize,
	Ordering,
};
use std::sync::Arc;
use std::time::{
	Duration,
	Instant,
};
use time::format_description::OwnedFormatItem;
use time::OffsetDateTime;

use crossbeam_channel::{
	bounded,
	unbounded,
	Receiver,
	RecvTimeoutError,
	Sender,
	TrySendError,
};
use hashbrown::{
	DefaultHashBuilder,
	HashMap,
};
use log::{
	kv::Key,
	set_boxed_logger,
	set_max_level,
	Log,
	Metadata,
	SetLoggerError,
};

use tm::{
	duration,
	now,
	to_utc,
	Time,
};

mod tm {
	use super::*;

	pub type Time = std::time::SystemTime;
	#[inline]
	pub fn now() -> Time {
		std::time::SystemTime::now()
	}
	#[inline]
	pub fn to_utc(time: Time) -> OffsetDateTime {
		time.into()
	}

	#[inline]
	pub fn duration(
		from: Time,
		to: Time,
	) -> Duration {
		to.duration_since(from).unwrap_or_default()
	}
}

struct LogMsg {
	time: Time,
	msg: Box<dyn Sync + Send + Display>,
	level: Level,
	target: String,
	limit: u32,
	limit_key: u64,
}
impl LogMsg {
	fn write(
		self,
		filters: &Vec<Directive>,
		appenders: &mut HashMap<&'static str, Box<dyn Write + Send>>,
		root: &mut Box<dyn Write + Send>,
		root_level: LevelFilter,
		missed_log: &mut HashMap<u64, i64, nohash_hasher::BuildNoHashHasher<u64>>,
		last_log: &mut HashMap<u64, Time, nohash_hasher::BuildNoHashHasher<u64>>,
		time_format: &time::format_description::OwnedFormatItem,
	) {
		let writer = if let Some(filter) = filters
			.iter()
			.find(|x| self.target.starts_with(x.path))
		{
			if filter
				.level
				.map(|l| l < self.level)
				.unwrap_or(false)
			{
				return;
			}
			filter
				.appender
				.and_then(|n| appenders.get_mut(n))
				.unwrap_or(root)
		} else {
			if root_level < self.level {
				return;
			}
			root
		};

		let msg = self.msg.to_string();
		if msg.is_empty() {
			return;
		}

		let now = now();

		if self.limit > 0 {
			let missed_entry = missed_log
				.entry(self.limit_key)
				.or_insert_with(|| 0);
			if let Some(last) = last_log.get(&self.limit_key) {
				if duration(*last, now) < Duration::from_millis(self.limit as u64) {
					*missed_entry += 1;
					return;
				}
			}
			last_log.insert(self.limit_key, now);
			let delay = duration(self.time, now);
			let utc_datetime = to_utc(self.time);

			let s = concat_strs!(
				&utc_datetime
					.format(&time_format)
					.unwrap_or_else(|_| utc_datetime
						.format(&time::format_description::well_known::Rfc3339)
						.unwrap()),
				' ',
				&delay.as_millis().to_string(),
				"ms ",
				&missed_entry.to_string(),
				' ',
				&msg,
				"\n"
			);

			if let Err(e) = writer.write_all(s.as_bytes()) {
				eprintln!("logger write message failed: {e}");
			}
			*missed_entry = 0;
		} else {
			let delay = duration(self.time, now);
			let utc_datetime = to_utc(self.time);

			let s = concat_strs!(
				&utc_datetime
					.format(&time_format)
					.unwrap_or_else(|_| utc_datetime
						.format(&time::format_description::well_known::Rfc3339)
						.unwrap()),
				' ',
				&delay.as_millis().to_string(),
				"ms ",
				&msg,
				"\n"
			);
			if let Err(e) = writer.write_all(s.as_bytes()) {
				eprintln!("logger write message failed: {e}");
			};
		}
	}
}

enum LoggerInput {
	LogMsg(LogMsg),
	Flush,
}

#[derive(Debug)]
enum LoggerOutput {
	Flushed,
	FlushError(std::io::Error),
}

pub trait FtLogFormat: Send + Sync {
	/// turn an reference to record into a box object, which can be sent to log thread
	/// and then formatted into string.
	fn msg(
		&self,
		record: &Record,
	) -> Box<dyn Send + Sync + Display>;
}

/// Default ftlog formatter
///
/// The default ftlog format is like:
/// ```text
/// INFO main [examples/ftlog.rs:27] Hello, world!
/// ```
///
/// Since ftlog cannot customize timestamp, the corresponding part is omitted.
/// The actual log output is like:
/// ```text
/// 2022-11-22 17:02:12.574+08 0ms INFO main [examples/ftlog.rs:27] Hello, world!
/// ```
pub struct FtLogFormatter;
impl FtLogFormat for FtLogFormatter {
	/// Return a box object that contains required data (e.g. thread name, line of code, etc.) for later formatting into string
	#[inline]
	fn msg(
		&self,
		record: &Record,
	) -> Box<dyn Send + Sync + Display> {
		Box::new(Message {
			level: record.level(),
			file: record
				.file_static()
				.map(|s| Cow::Borrowed(s))
				.or_else(|| record.file().map(|s| Cow::Owned(s.to_owned())))
				.unwrap_or(Cow::Borrowed("")),
			line: record.line(),
			args: record
				.args()
				.as_str()
				.map(|s| Cow::Borrowed(s))
				.unwrap_or_else(|| Cow::Owned(record.args().to_string())),
		})
	}
}

struct Message {
	level: Level,
	file: Cow<'static, str>,
	line: Option<u32>,
	args: Cow<'static, str>,
}

impl Display for Message {
	fn fmt(
		&self,
		f: &mut std::fmt::Formatter<'_>,
	) -> std::fmt::Result {
		f.write_str(&format!(
			"{} [{}:{}] {}",
			self.level,
			self.file,
			self.line.unwrap_or(0),
			self.args
		))
	}
}

struct DiscardState {
	last: ArcSwap<Instant>,
	count: AtomicUsize,
}

/// A guard that flushes logs associated to a Logger on a drop
///
/// With this guard, you can ensure all logs are written to destination
/// when the application exits.
pub struct LoggerGuard {
	queue: Sender<LoggerInput>,
	notification: Receiver<LoggerOutput>,
}
impl Drop for LoggerGuard {
	fn drop(&mut self) {
		self.queue
			.send(LoggerInput::Flush)
			.expect("logger queue closed when flushing, this is a bug");
		self.notification
			.recv()
			.expect("logger notification closed, this is a bug");
	}
}
/// ftlog global logger
pub struct Logger {
	format: Box<dyn FtLogFormat>,
	level: LevelFilter,
	queue: Sender<LoggerInput>,
	notification: Receiver<LoggerOutput>,
	block: bool,
	discard_state: Option<DiscardState>,
	stopped: AtomicBool,
}

impl Logger {
	pub fn init(self) -> Result<LoggerGuard, SetLoggerError> {
		let guard = LoggerGuard {
			queue: self.queue.clone(),
			notification: self.notification.clone(),
		};

		set_max_level(self.level);
		let boxed = Box::new(self);
		set_boxed_logger(boxed).map(|_| guard)
	}
}

impl Log for Logger {
	#[inline]
	fn enabled(
		&self,
		metadata: &Metadata,
	) -> bool {
		self.level >= metadata.level()
	}

	fn log(
		&self,
		record: &Record,
	) {
		let limit = record
			.key_values()
			.get(Key::from_str("limit"))
			.and_then(|x| x.to_u64())
			.unwrap_or(0) as u32;

		let msg = self.format.msg(record);
		let limit_key = if limit == 0 {
			0
		} else {
			let mut b = DefaultHashBuilder::default().build_hasher();
			if let Some(p) = record.module_path() {
				p.as_bytes().hash(&mut b);
			} else {
				record
					.file()
					.unwrap_or("")
					.as_bytes()
					.hash(&mut b);
			}
			record.line().unwrap_or(0).hash(&mut b);
			b.finish()
		};
		let msg = LoggerInput::LogMsg(LogMsg {
			time: now(),
			msg,
			target: record.target().to_owned(),
			level: record.level(),
			limit,
			limit_key,
		});
		if self.block {
			if let Err(_) = self.queue.send(msg) {
				let stop = self.stopped.load(Ordering::SeqCst);
				if !stop {
					eprintln!("logger queue closed when logging, this is a bug");
					self.stopped.store(true, Ordering::SeqCst)
				}
			}
		} else {
			match self.queue.try_send(msg) {
				Err(TrySendError::Full(_)) => {
					if let Some(s) = &self.discard_state {
						let count = s.count.fetch_add(1, Ordering::SeqCst);
						if s.last.load().elapsed().as_secs() >= 5 {
							eprintln!("Excessive log messages. Log omitted: {count}");
							s.last.store(Arc::new(Instant::now()));
						}
					}
				}
				Err(TrySendError::Disconnected(_)) => {
					let stop = self.stopped.load(Ordering::SeqCst);
					if !stop {
						eprintln!("logger queue closed when logging, this is a bug");
						self.stopped.store(true, Ordering::SeqCst)
					}
				}
				_ => (),
			}
		}
	}

	fn flush(&self) {
		self.queue
			.send(LoggerInput::Flush)
			.expect("logger queue closed when flushing, this is a bug");
		if let LoggerOutput::FlushError(err) = self
			.notification
			.recv()
			.expect("logger notification closed, this is a bug")
		{
			eprintln!("Fail to flush: {err}");
		}
	}
}

struct BoundedChannelOption {
	size: usize,
	block: bool,
	print: bool,
}

/// Ftlog builder
///
/// ```
/// # use ftlog::appender::{FileAppender, Duration, Period};
/// # use log::LevelFilter;
/// let logger = ftlog::builder()
///     // use our own format
///     .format(ftlog::FtLogFormatter)
///     // global max log level
///     .max_log_level(LevelFilter::Info)
///     // define root appender, pass anything that is Write and Send
///     // omit `Builder::root` to write to stderr
///     .root(FileAppender::rotate_with_expire(
///         "./current.log",
///         Period::Day,
///         Duration::days(7),
///     ))
///     // ---------- configure additional filter ----------
///     // write to "ftlog-appender" appender, with different level filter
///     .filter("ftlog::appender", "ftlog-appender", LevelFilter::Error)
///     // write to root appender, but with different level filter
///     .filter("ftlog", None, LevelFilter::Trace)
///     // write to "ftlog" appender, with default level filter
///     .filter("ftlog::appender::file", "ftlog", None)
///     // ----------  configure additional appender ----------
///     // new appender
///     .appender("ftlog-appender", FileAppender::new("ftlog-appender.log"))
///     // new appender, rotate to new file every Day
///     .appender("ftlog", FileAppender::rotate("ftlog.log", Period::Day))
///     .build()
///     .expect("logger build failed");
/// ```
///
/// # Local timezone
/// For performance reason, `ftlog` only retrieves timezone info once and use this
/// local timezone offset forever. Thus timestamp in log does not aware of timezone
/// change by OS.
pub struct Builder {
	format: Box<dyn FtLogFormat>,
	time_format: Option<OwnedFormatItem>,
	level: Option<LevelFilter>,
	root_level: Option<LevelFilter>,
	root: Box<dyn Write + Send>,
	appenders: HashMap<&'static str, Box<dyn Write + Send + 'static>>,
	filters: Vec<Directive>,
	bounded_channel_option: Option<BoundedChannelOption>,
}

/// Handy function to get ftlog builder
#[inline]
pub fn builder() -> Builder {
	Builder::new()
}

struct Directive {
	path: &'static str,
	level: Option<LevelFilter>,
	appender: Option<&'static str>,
}

impl Builder {
	#[inline]
	/// Create a ftlog builder with default settings:
	/// - global log level: INFO
	/// - root log level: INFO
	/// - default formatter: `FtLogFormatter`
	/// - output to stderr
	/// - bounded channel between worker thread and log thread, with a size limit of 100_000
	/// - discard excessive log messages
	/// - log with timestamp of local timezone
	pub fn new() -> Builder {
		Builder {
			format: Box::new(FtLogFormatter),
			level: None,
			root_level: None,
			root: Box::new(stderr()) as Box<dyn Write + Send>,
			appenders: HashMap::new(),
			filters: Vec::new(),
			bounded_channel_option: Some(BoundedChannelOption {
				size: 100_000,
				block: false,
				print: true,
			}),
			time_format: None,
		}
	}

	/// Set custom formatter
	#[inline]
	pub fn format<F: FtLogFormat + 'static>(
		mut self,
		format: F,
	) -> Builder {
		self.format = Box::new(format);
		self
	}

	/// bound channel between worker thread and log thread
	///
	/// When `block_when_full` is true, it will block current thread where
	/// log macro (e.g. `log::info`) is called until log thread is able to handle new message.
	/// Otherwises, excessive log messages will be discarded.
	///
	/// By default, excessive log messages is discarded silently. To show how many log
	/// messages have been dropped, see `Builder::print_omitted_count()`.
	#[inline]
	pub fn bounded(
		mut self,
		size: usize,
		block_when_full: bool,
	) -> Builder {
		self.bounded_channel_option = Some(BoundedChannelOption {
			size,
			block: block_when_full,
			print: false,
		});
		self
	}

	/// whether to print the number of omitted logs if channel to log
	/// thread is bounded, and set to discard excessive log messages
	#[inline]
	pub fn print_omitted_count(
		mut self,
		print: bool,
	) -> Builder {
		self.bounded_channel_option.as_mut().map(|o| {
			o.print = print;
		});
		self
	}

	/// set channel size to unbound
	///
	/// **ATTENTION**: too much log message will lead to huge memory consumption,
	/// as log messages are queued to be handled by log thread.
	/// When log message exceed the current channel size, it will double the size by default,
	/// Since channel expansion asks for memory allocation, log calls can be slow down.
	#[inline]
	pub fn unbounded(mut self) -> Builder {
		self.bounded_channel_option = None;
		self
	}

	/// Add an additional appender with a name
	///
	/// Combine with `Builder::filter()`, ftlog can output log in different module
	/// path to different output target.
	#[inline]
	pub fn appender(
		mut self,
		name: &'static str,
		appender: impl Write + Send + 'static,
	) -> Builder {
		self.appenders.insert(name, Box::new(appender));
		self
	}

	/// Add a filter to redirect log to different output
	/// target (e.g. stderr, stdout, different files).
	///
	/// **ATTENTION**: level more verbose than `Builder::max_log_level` will be ignored.
	/// Say we configure `max_log_level` to INFO, and even if filter's level is set to DEBUG,
	/// ftlog will still log up to INFO.
	#[inline]
	pub fn filter<A: Into<Option<&'static str>>, L: Into<Option<LevelFilter>>>(
		mut self,
		module_path: &'static str,
		appender: A,
		level: L,
	) -> Builder {
		let appender = appender.into();
		let level = level.into();
		if appender.is_some() || level.is_some() {
			self.filters.push(Directive {
				path: module_path,
				appender: appender,
				level: level,
			});
		}
		self
	}

	#[inline]
	/// Set max log level
	///
	/// Logs with level more verbose than this will not be sent to log thread.
	pub fn max_log_level(
		mut self,
		level: LevelFilter,
	) -> Builder {
		self.level = Some(level);
		self
	}

	#[inline]
	/// Set max log level
	///
	/// Logs with level more verbose than this will not be sent to log thread.
	pub fn root_log_level(
		mut self,
		level: LevelFilter,
	) -> Builder {
		self.root_level = Some(level);
		self
	}

	/// Finish building ftlog logger
	///
	/// The call spawns a log thread to formatting log message into string,
	/// and write to output target.
	pub fn build(self) -> Result<Logger, IoError> {
		let time_format = self.time_format.unwrap_or_else(|| {
			time::format_description::parse_owned::<1>(
				"[year]-[month]-[day] [hour]:[minute]:[second].[subsecond digits:6]",
			)
			.unwrap()
		});
		let mut filters = self.filters;
		// sort filters' paths to ensure match for longest path
		filters.sort_by(|a, b| a.path.len().cmp(&b.path.len()));
		filters.reverse();
		// check appender name in filters are all valid
		for appender_name in filters.iter().filter_map(|x| x.appender) {
			if !self.appenders.contains_key(appender_name) {
				panic!("Appender {appender_name} not configured");
			}
		}
		let global_level = self.level.unwrap_or(LevelFilter::Info);
		let root_level = self.root_level.unwrap_or(global_level);
		if global_level < root_level {
			warn!("Logs with level more verbose than {global_level} will be ignored");
		}

		let (sync_sender, receiver) = match &self.bounded_channel_option {
			None => unbounded(),
			Some(option) => bounded(option.size),
		};
		let (notification_sender, notification_receiver) = bounded(1);
		std::thread::Builder::new()
			.name("logger".to_string())
			.spawn(move || {
				let mut appenders = self.appenders;
				let filters = filters;

				for filter in &filters {
					if let Some(level) = filter.level {
						if global_level < level {
							warn!(
								"Logs with level more verbose than {} will be ignored in `{}` ",
								global_level, filter.path
							);
						}
					}
				}

				let mut root = self.root;
				let mut last_log = HashMap::default();
				let mut missed_log = HashMap::default();
				let mut last_flush = Instant::now();
				let timeout = Duration::from_millis(200);
				loop {
					match receiver.recv_timeout(timeout) {
						Ok(LoggerInput::LogMsg(log_msg)) => {
							log_msg.write(
								&filters,
								&mut appenders,
								&mut root,
								root_level,
								&mut missed_log,
								&mut last_log,
								&time_format,
							);
						}
						Ok(LoggerInput::Flush) => {
							let max = receiver.len();
							'queue: for _ in 1..=max {
								if let Ok(LoggerInput::LogMsg(msg)) = receiver.try_recv() {
									msg.write(
										&filters,
										&mut appenders,
										&mut root,
										root_level,
										&mut missed_log,
										&mut last_log,
										&time_format,
									);
								} else {
									break 'queue;
								}
							}
							let flush_result = appenders
								.values_mut()
								.chain([&mut root])
								.find_map(|w| w.flush().err());
							if let Some(error) = flush_result {
								notification_sender
									.send(LoggerOutput::FlushError(
										error,
									))
									.expect("logger notification failed");
							} else {
								notification_sender
									.send(LoggerOutput::Flushed)
									.expect("logger notification failed");
							}
						}
						Err(RecvTimeoutError::Timeout) => {
							if last_flush.elapsed() > Duration::from_millis(1000) {
								let flush_errors = appenders
									.values_mut()
									.chain([&mut root])
									.filter_map(|w| w.flush().err());
								for err in flush_errors {
									log::warn!("Ftlog flush error: {}", err);
								}
								last_flush = Instant::now();
							}
						}
						Err(e) => {
							eprintln!("sender closed without sending a Quit first, this is a bug, {e}");
						}
					}
				}
			})?;
		let block = self
			.bounded_channel_option
			.as_ref()
			.map(|x| x.block)
			.unwrap_or(false);
		let print = self
			.bounded_channel_option
			.as_ref()
			.map(|x| x.print)
			.unwrap_or(false);
		Ok(Logger {
			format: self.format,
			level: global_level,
			queue: sync_sender,
			notification: notification_receiver,
			block,
			discard_state: if block || !print {
				None
			} else {
				Some(DiscardState {
					last: ArcSwap::new(Arc::new(Instant::now())),
					count: AtomicUsize::new(0),
				})
			},
			stopped: AtomicBool::new(false),
		})
	}

	/// try building and setting as global logger
	pub fn try_init(self) -> Result<LoggerGuard, Box<dyn std::error::Error>> {
		let logger = self.build()?;
		Ok(logger.init()?)
	}
}

impl Default for Builder {
	#[inline]
	fn default() -> Self {
		Builder::new()
	}
}