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
use arc_swap::ArcSwap;
use crossbeam_channel::{
	bounded,
	unbounded,
	Receiver,
	RecvTimeoutError,
	Sender,
	TrySendError,
};
pub use log::{
	debug,
	error,
	info,
	log,
	log_enabled,
	logger,
	trace,
	warn,
	Level,
	LevelFilter,
	Record,
};
use log::{
	set_boxed_logger,
	set_max_level,
	Log,
	Metadata,
	SetLoggerError,
};
use logmsg::LogMsg;
use std::{
	borrow::Cow,
	fmt::Display,
	io::{
		stderr,
		Error as IoError,
		Write,
	},
	sync::{
		atomic::{
			AtomicBool,
			AtomicUsize,
			Ordering,
		},
		Arc,
	},
	time::{
		Duration,
		Instant,
	},
};
mod logmsg;

enum LoggerInput {
	LogMsg(LogMsg),
	Flush,
}

#[derive(Debug)]
enum LoggerOutput {
	Flushed,
}

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>;
}

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 msg = self.format.msg(record);
		let msg = LoggerInput::LogMsg(LogMsg {
			msg,
			target: record.target().to_owned(),
			level: record.level(),
		});
		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) {
		let _ = self
			.queue
			.send(LoggerInput::Flush)
			.map_err(|e| eprintln!("logger queue closed when flushing, this is a bug: {e}"));
	}
}

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

pub struct Builder {
	format: Box<dyn FtLogFormat>,
	level: Option<LevelFilter>,
	root_level: Option<LevelFilter>,
	root: Box<dyn Write + Send>,
	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>,
}

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>,
			filters: Vec::new(),
			bounded_channel_option: Some(BoundedChannelOption {
				size: 100_000,
				block: false,
				print: true,
			}),
		}
	}

	/// 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
	}

	#[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,
				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 mut filters = self.filters;
		filters.sort_by(|a, b| a.path.len().cmp(&b.path.len()));
		filters.reverse();
		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 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_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 root, root_level,
							);
						}
						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 root, root_level,
									);
								} else {
									break 'queue;
								}
							}
							notification_sender
								.send(LoggerOutput::Flushed)
								.expect("logger notification failed");
						}
						Err(RecvTimeoutError::Timeout) => {
							if last_flush.elapsed() > Duration::from_millis(1000) {
								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()
	}
}