diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2018-06-12 15:13:09 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2018-06-13 16:34:56 +0200 |
| commit | 37845664043499446a7a4508ae2ef9eb1f487309 (patch) | |
| tree | 837943d278dadf0a9a59868e8fe3b3c3ea3e0489 | |
| parent | c3965d6715d7a7c5048866dbefbb44b838f7773b (diff) | |
| download | mullvadvpn-37845664043499446a7a4508ae2ef9eb1f487309.tar.xz mullvadvpn-37845664043499446a7a4508ae2ef9eb1f487309.zip | |
Rotate daemon log on startup
| -rw-r--r-- | mullvad-daemon/src/logging.rs | 19 | ||||
| -rw-r--r-- | mullvad-daemon/src/main.rs | 18 |
2 files changed, 16 insertions, 21 deletions
diff --git a/mullvad-daemon/src/logging.rs b/mullvad-daemon/src/logging.rs index 89154ff20e..958d5321e6 100644 --- a/mullvad-daemon/src/logging.rs +++ b/mullvad-daemon/src/logging.rs @@ -8,7 +8,7 @@ use log; use std::fmt; use std::fs; use std::io; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; error_chain! { errors { @@ -19,6 +19,7 @@ error_chain! { } foreign_links { SetLoggerError(log::SetLoggerError); + Io(io::Error); } } @@ -71,9 +72,7 @@ pub fn init_logger( top_dispatcher = top_dispatcher.chain(stdout_dispatcher); if let Some(ref log_file) = log_file { - if let Some(parent) = log_file.parent() { - fs::create_dir_all(parent).chain_err(|| ErrorKind::CreateDirError(parent.to_owned()))?; - } + rotate_log(log_file)?; let file_formatter = Formatter { output_timestamp: true, output_color: false, @@ -139,3 +138,15 @@ fn escape_newlines(text: String) -> String { fn escape_newlines(text: String) -> String { text.replace("\n", LINE_SEPARATOR) } + +pub fn rotate_log(file: &Path) -> Result<()> { + let backup = file.with_extension("old.log"); + fs::rename(file, backup).unwrap_or_else(|error| { + if error.kind() != io::ErrorKind::NotFound { + warn!("Failed to rotate log file ({})", error); + } + }); + + fs::File::create(file).chain_err(|| "Unable to create new log file")?; + Ok(()) +} diff --git a/mullvad-daemon/src/main.rs b/mullvad-daemon/src/main.rs index cc0bf33a0d..5c18584b1f 100644 --- a/mullvad-daemon/src/main.rs +++ b/mullvad-daemon/src/main.rs @@ -734,7 +734,7 @@ impl Daemon { TunnelEndpointData::Wireguard(_) => WIREGUARD_LOG_FILENAME, }; let tunnel_log = log_dir.join(filename); - Self::prepare_tunnel_log_file(&tunnel_log)?; + logging::rotate_log(&tunnel_log).chain_err(|| "Unable to rotate tunnel log")?; Some(tunnel_log) } else { None @@ -751,22 +751,6 @@ impl Daemon { ).chain_err(|| ErrorKind::TunnelError("Unable to start tunnel monitor")) } - fn prepare_tunnel_log_file(file: &PathBuf) -> Result<()> { - let mut backup = file.clone(); - backup.set_extension("old.log"); - fs::rename(file, backup).unwrap_or_else(|error| { - if error.kind() != io::ErrorKind::NotFound { - warn!( - "Failed to create backup of previous tunnel log file ({})", - error - ); - } - }); - - fs::File::create(file).chain_err(|| "Unable to create the tunnel log file")?; - Ok(()) - } - fn spawn_tunnel_monitor_wait_thread(&self, tunnel_monitor: TunnelMonitor) { let error_tx = self.tx.clone(); thread::spawn(move || { |
