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
use log::{ Level, Record };

use std::path::Path;
use std::fs::{ File, OpenOptions };
use std::io::{ self, Write };
use std::sync::Mutex;

use super::{ Target, IgnoreList };

pub struct FileTarget {
	level: Level,
	ignore_list: IgnoreList,

	file: Mutex<File>
}
impl FileTarget {
	pub fn new(level: Level, ignore_list: IgnoreList, path: &Path) -> io::Result<Self> {
		let file = Mutex::new(
			OpenOptions::new()
				.write(true)
				.truncate(true)
				.create(true)
			.open(path)?
		);


		Ok(FileTarget {
			level,
			ignore_list,

			file
		})
	}
}
impl Target for FileTarget {
	fn level(&self) -> Level {
		self.level
	}

	fn ignore(&self, record: &Record) -> bool {
		self.ignore_list.ignore(record)
	}
	
	fn write(&self, string: &str) -> io::Result<()> {
		match self.file.lock() {
			Err(_) => {
				Err(io::Error::new(io::ErrorKind::Other, "mutex poison error"))
			},
			Ok(mut lock) => writeln!(&mut lock, "{}", string)
		}
	}

	fn flush(&self) -> io::Result<()> {
		match self.file.lock() {
			Err(_) => {
				Err(io::Error::new(io::ErrorKind::Other, "mutex poison error"))
			},
			Ok(mut lock) => lock.flush()
		}
	}
}