Skip to content

Commit 55c4218

Browse files
committed
pr#70: fixed --save for new file case
1 parent 94327d9 commit 55c4218

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

src/main.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::sync::Arc;
2626
use std::thread;
2727

2828
struct OutputSink<'a, T: LogFormatter> {
29-
file: Option<File>,
29+
filename: Option<String>,
3030
output_lines: Option<Vec<String>>,
3131
formatter: &'a T,
3232
comments_enabled: bool,
@@ -41,22 +41,16 @@ impl<'a, T: LogFormatter> OutputSink<'a, T> {
4141
comments_in_file_enabled: bool,
4242
) -> Result<Self, io::Error> {
4343
let output = match filename {
44-
Some(filename) => {
45-
let file = std::fs::OpenOptions::new()
46-
.write(true)
47-
.create(false) // only overwrite on success
48-
.open(filename)?;
49-
Self {
50-
formatter,
51-
file: Some(file),
52-
output_lines: Some(Vec::new()),
53-
comments_enabled,
54-
comments_in_file_enabled,
55-
}
56-
}
44+
Some(filename) => Self {
45+
formatter,
46+
filename: Some(filename.to_string()),
47+
output_lines: Some(Vec::new()),
48+
comments_enabled,
49+
comments_in_file_enabled,
50+
},
5751
None => Self {
5852
formatter,
59-
file: None,
53+
filename: None,
6054
output_lines: None,
6155
comments_enabled,
6256
comments_in_file_enabled,
@@ -87,10 +81,10 @@ impl<'a, T: LogFormatter> OutputSink<'a, T> {
8781

8882
pub fn save_to_file(&mut self) -> Result<(), io::Error> {
8983
if let Some(output_lines) = &mut self.output_lines {
90-
if let Some(f) = &mut self.file {
91-
let output_string = output_lines.join("\n") + "\n";
92-
f.set_len(0)?;
93-
f.write_all(output_string.as_bytes())?;
84+
if let Some(filename) = self.filename.as_ref() {
85+
let mut f = File::create(filename)?;
86+
f.write_all(output_lines.join("\n").as_bytes())?;
87+
f.write_all("\n".as_bytes())?;
9488
}
9589
}
9690
return Ok(());

0 commit comments

Comments
 (0)