diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-04-30 22:45:45 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-05-07 08:30:45 -0300 |
| commit | 938664406c262fbe1c393291d3f0c08d1620393d (patch) | |
| tree | d41b9aead2c089b698efc184569ee947f0b04b30 | |
| parent | 16a7ebc3f572f7c2aeb4fe1cc207815bd58edee2 (diff) | |
| download | mullvadvpn-938664406c262fbe1c393291d3f0c08d1620393d.tar.xz mullvadvpn-938664406c262fbe1c393291d3f0c08d1620393d.zip | |
Use platform specific newlines in problem-report
| -rw-r--r-- | mullvad-daemon/src/bin/problem-report.rs | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/mullvad-daemon/src/bin/problem-report.rs b/mullvad-daemon/src/bin/problem-report.rs index a5e8ac0577..aace9d86cf 100644 --- a/mullvad-daemon/src/bin/problem-report.rs +++ b/mullvad-daemon/src/bin/problem-report.rs @@ -23,9 +23,8 @@ use std::borrow::Cow; use std::cmp::min; use std::collections::HashMap; use std::env; -use std::fmt; use std::fs::File; -use std::io::{self, Read, Seek, SeekFrom, Write}; +use std::io::{self, BufWriter, Read, Seek, SeekFrom, Write}; use std::path::{Path, PathBuf}; use std::process::Command; @@ -38,6 +37,23 @@ const REPORT_MAX_SIZE: usize = 2 * LOG_MAX_READ_BYTES + 16 * 1024; /// Field delimeter in generated problem report const LOG_DELIMITER: &'static str = "===================="; +/// Line separator character sequence +#[cfg(not(windows))] +const LINE_SEPARATOR: &str = "\n"; + +#[cfg(windows)] +const LINE_SEPARATOR: &str = "\r\n"; + +/// Custom macro to write a line to an output formatter that uses platform-specific newline +/// character sequences. +macro_rules! write_line { + ($fmt:expr $(,)*) => { write!($fmt, "{}", LINE_SEPARATOR) }; + ($fmt:expr, $pattern:expr $(, $arg:expr)* $(,)*) => { + write!($fmt, $pattern, $( $arg ),*) + .and_then(|_| write!($fmt, "{}", LINE_SEPARATOR)) + }; +} + error_chain!{ errors { WriteReportError(path: PathBuf) { @@ -169,11 +185,11 @@ fn send_problem_report(user_email: &str, user_message: &str, report_path: &Path) } fn write_problem_report(path: &Path, problem_report: ProblemReport) -> io::Result<()> { - let mut file = File::create(path)?; + let file = File::create(path)?; let mut permissions = file.metadata()?.permissions(); permissions.set_readonly(true); file.set_permissions(permissions)?; - file.write(problem_report.to_string().as_bytes())?; + problem_report.write_to(BufWriter::new(file))?; Ok(()) } @@ -250,21 +266,19 @@ impl ProblemReport { } out } -} -impl fmt::Display for ProblemReport { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - writeln!(fmt, "System information:")?; + fn write_to<W: Write>(&self, mut output: W) -> io::Result<()> { + write_line!(output, "System information:")?; for (key, value) in &self.metadata { - writeln!(fmt, "{}: {}", key, value)?; + write_line!(output, "{}: {}", key, value)?; } - writeln!(fmt, "")?; + write_line!(output)?; for &(ref label, ref content) in &self.logs { - writeln!(fmt, "{}", LOG_DELIMITER)?; - writeln!(fmt, "Log: {}", label)?; - writeln!(fmt, "{}", LOG_DELIMITER)?; - fmt.write_str(content)?; - writeln!(fmt)?; + write_line!(output, "{}", LOG_DELIMITER)?; + write_line!(output, "Log: {}", label)?; + write_line!(output, "{}", LOG_DELIMITER)?; + output.write_all(content.as_bytes())?; + write_line!(output)?; } Ok(()) } |
