|
1 | 1 | use crate::progress::Progress;
|
2 |
| -use chrono; |
3 | 2 | use std::fs;
|
4 |
| -use std::fs::{File, OpenOptions}; |
| 3 | +use std::fs::File; |
5 | 4 | use std::io::Write;
|
6 |
| -use std::path::PathBuf; |
7 |
| -use std::sync::Arc; |
| 5 | +use std::path::{Display, PathBuf}; |
| 6 | +use std::sync::{Arc, Mutex}; |
8 | 7 |
|
9 | 8 | pub(crate) struct Logger {
|
10 |
| - pub(crate) current_log: PathBuf, |
11 | 9 | progress: Arc<Progress>,
|
| 10 | + log_file: Arc<Mutex<File>>, |
| 11 | + log_path: PathBuf, |
12 | 12 | }
|
13 | 13 |
|
14 | 14 | impl Logger {
|
15 | 15 | pub(crate) fn new(progress: Arc<Progress>) -> Self {
|
16 |
| - let app_name = "ffzap"; |
| 16 | + let path_file_tuple = Self::setup_log_dir_and_create_log_file(); |
| 17 | + |
| 18 | + let log_path = path_file_tuple.0; |
| 19 | + let log_file = path_file_tuple.1; |
| 20 | + |
| 21 | + Logger { |
| 22 | + log_path, |
| 23 | + log_file, |
| 24 | + progress, |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + pub(crate) fn log_info(&self, line: String, thread: u16, print: bool) { |
| 29 | + let line = format!("[INFO in THREAD {thread}] -- {line}\n"); |
| 30 | + |
| 31 | + self.write_to_log(&line); |
| 32 | + |
| 33 | + if print { |
| 34 | + self.print(line); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + pub(crate) fn log_error(&self, line: String, thread: u16, print: bool) { |
| 39 | + let line = format!("[ERROR in THREAD {thread} -- {line}\n"); |
| 40 | + |
| 41 | + self.write_to_log(&line); |
| 42 | + |
| 43 | + if print { |
| 44 | + self.print(line); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + pub(crate) fn get_log_path(&self) -> Display { |
| 49 | + self.log_path.display() |
| 50 | + } |
| 51 | + |
| 52 | + fn setup_log_dir_and_create_log_file() -> (PathBuf, Arc<Mutex<File>>) { |
17 | 53 | let log_path;
|
| 54 | + let app_name = "ffzap"; |
18 | 55 |
|
19 | 56 | #[cfg(target_os = "windows")]
|
20 | 57 | {
|
@@ -48,52 +85,24 @@ impl Logger {
|
48 | 85 | .join("logs")
|
49 | 86 | }
|
50 | 87 |
|
51 |
| - Self::setup_log_dir(&log_path); |
| 88 | + fs::create_dir_all(&log_path).unwrap(); |
52 | 89 |
|
53 | 90 | let locale_time = chrono::Local::now().format("%d-%m-%YT%H-%M-%S");
|
54 | 91 | let mut current_log = log_path.join(locale_time.to_string());
|
55 | 92 | current_log.set_extension("log");
|
56 | 93 |
|
57 |
| - Logger { |
58 |
| - current_log, |
59 |
| - progress, |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| - pub(crate) fn log_info(&self, line: String, thread: u16, print: bool) { |
64 |
| - let line = format!("[INFO in THREAD {thread}] -- {line}"); |
65 |
| - |
66 |
| - let mut log_file = self.get_log_file(); |
67 |
| - |
68 |
| - writeln!(&mut log_file, "{}", line).unwrap(); |
69 |
| - |
70 |
| - if print { |
71 |
| - self.print(line); |
72 |
| - } |
73 |
| - } |
74 |
| - |
75 |
| - pub(crate) fn log_error(&self, line: String, thread: u16, print: bool) { |
76 |
| - let line = format!("[ERROR in THREAD {thread} -- {line}"); |
77 |
| - |
78 |
| - let mut log_file = self.get_log_file(); |
79 |
| - |
80 |
| - writeln!(&mut log_file, "{}", line).unwrap(); |
81 |
| - |
82 |
| - if print { |
83 |
| - self.print(line); |
84 |
| - } |
85 |
| - } |
86 |
| - |
87 |
| - fn setup_log_dir(path: &PathBuf) { |
88 |
| - fs::create_dir_all(path).unwrap(); |
| 94 | + ( |
| 95 | + current_log.clone(), |
| 96 | + Arc::new(Mutex::new(File::create(¤t_log).unwrap())), |
| 97 | + ) |
89 | 98 | }
|
90 | 99 |
|
91 |
| - fn get_log_file(&self) -> File { |
92 |
| - OpenOptions::new() |
93 |
| - .append(true) |
94 |
| - .create(true) |
95 |
| - .open(&self.current_log) |
| 100 | + fn write_to_log(&self, line: &str) { |
| 101 | + self.log_file |
| 102 | + .lock() |
96 | 103 | .unwrap()
|
| 104 | + .write_all(line.as_bytes()) |
| 105 | + .unwrap(); |
97 | 106 | }
|
98 | 107 |
|
99 | 108 | fn print(&self, line: String) {
|
|
0 commit comments