Skip to content

Commit 144884a

Browse files
authored
thread safe log fix (#6)
* fix: not thread safe writing to log file * docs: replacing cpu with system in cargo.tml
1 parent ab775ef commit 144884a

File tree

3 files changed

+60
-53
lines changed

3 files changed

+60
-53
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "ffzap"
33
version = "0.2.2"
44
edition = "2021"
5-
description = "⚡ A multithreaded CLI for digital media processing using ffmpeg. If ffmpeg can do it, ffzap can do it - as many files in parallel as your CPU can handle."
5+
description = "⚡ A multithreaded CLI for digital media processing using ffmpeg. If ffmpeg can do it, ffzap can do it - as many files in parallel as your system can handle."
66
license-file = "license.md"
77
keywords = ["ffmpeg", "media-processing", "cli", "audio-conversion", "video-conversion"]
88
repository = "https://github.com/CodeF0x/ffzap"

src/logger.rs

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,57 @@
11
use crate::progress::Progress;
2-
use chrono;
32
use std::fs;
4-
use std::fs::{File, OpenOptions};
3+
use std::fs::File;
54
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};
87

98
pub(crate) struct Logger {
10-
pub(crate) current_log: PathBuf,
119
progress: Arc<Progress>,
10+
log_file: Arc<Mutex<File>>,
11+
log_path: PathBuf,
1212
}
1313

1414
impl Logger {
1515
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>>) {
1753
let log_path;
54+
let app_name = "ffzap";
1855

1956
#[cfg(target_os = "windows")]
2057
{
@@ -48,52 +85,24 @@ impl Logger {
4885
.join("logs")
4986
}
5087

51-
Self::setup_log_dir(&log_path);
88+
fs::create_dir_all(&log_path).unwrap();
5289

5390
let locale_time = chrono::Local::now().format("%d-%m-%YT%H-%M-%S");
5491
let mut current_log = log_path.join(locale_time.to_string());
5592
current_log.set_extension("log");
5693

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(&current_log).unwrap())),
97+
)
8998
}
9099

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()
96103
.unwrap()
104+
.write_all(line.as_bytes())
105+
.unwrap();
97106
}
98107

99108
fn print(&self, line: String) {

src/main.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,11 @@ fn main() {
181181

182182
progress.finish();
183183

184-
println!(
185-
"{}",
186-
format!(
187-
"{} out of {} files have been successful. A detailed log has been written to {}",
188-
progress.value(),
189-
progress.len(),
190-
logger.current_log.display()
191-
)
184+
let final_output = format!(
185+
"{} out of {} files have been successful. A detailed log has been written to {}",
186+
progress.value(),
187+
progress.len(),
188+
logger.get_log_path()
192189
);
190+
println!("{final_output}");
193191
}

0 commit comments

Comments
 (0)