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
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::Error as IoError,
	sync::atomic::{AtomicBool, Ordering},
	time::{Duration, Instant},
};
mod logmsg;

enum LoggerInput {
	LogMsg(LogMsg),
	Flush,
}

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(Cow::Borrowed)
				.or_else(|| record.file().map(|s| Cow::Owned(s.to_owned())))
				.unwrap_or(Cow::Borrowed("")),
			line: record.line(),
			args: record
				.args()
				.as_str()
				.map(Cow::Borrowed)
				.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
		))
	}
}

/// 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");
	}
}
/// global logger
pub struct Logger {
	format: Box<dyn FtLogFormat>,
	level: LevelFilter,
	queue: Sender<LoggerInput>,
	notification: Receiver<LoggerOutput>,
	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 {
			time: std::time::SystemTime::now(),
			msg,
		});
		match self.queue.try_send(msg) {
			Err(TrySendError::Full(_)) => {}
			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,
}

pub struct Builder {
	format: Box<dyn FtLogFormat>,
	level: Option<LevelFilter>,
	root_level: Option<LevelFilter>,
	bounded_channel_option: Option<BoundedChannelOption>,
}

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

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,
			bounded_channel_option: Some(BoundedChannelOption { size: 100_000 }),
		}
	}

	/// Set custom formatter
	#[inline]
	pub fn format<F: FtLogFormat + 'static>(
		mut self,
		format: F,
	) -> Builder {
		self.format = Box::new(format);
		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]
	/// 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 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 last_flush = Instant::now();
				let timeout = Duration::from_millis(200);
				loop {
					match receiver.recv_timeout(timeout) {
						Ok(LoggerInput::LogMsg(msg)) => {
							msg.write();
						}
						Ok(LoggerInput::Flush) => {
							let max = receiver.len();
							'queue: for _ in 1..=max {
								if let Ok(LoggerInput::LogMsg(msg)) = receiver.try_recv() {
									msg.write();
								} 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}");
						}
					}
				}
			})?;
		Ok(Logger {
			format: self.format,
			level: global_level,
			queue: sync_sender,
			notification: notification_receiver,
			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()
	}
}