diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-08-27 12:54:44 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-08-29 10:33:45 -0300 |
| commit | cac52b89b72bde856ca2667696f769bc5652ba9a (patch) | |
| tree | 643c8623284a1e2fb5fc701d4a7905ecd66a2103 /talpid-core | |
| parent | f2dbae265bc70554cc37f2d50fa9c96af9e6c59b (diff) | |
| download | mullvadvpn-cac52b89b72bde856ca2667696f769bc5652ba9a.tar.xz mullvadvpn-cac52b89b72bde856ca2667696f769bc5652ba9a.zip | |
Move `rotate_log` function to `talpid-core`
Diffstat (limited to 'talpid-core')
| -rw-r--r-- | talpid-core/src/lib.rs | 3 | ||||
| -rw-r--r-- | talpid-core/src/logging.rs | 20 |
2 files changed, 23 insertions, 0 deletions
diff --git a/talpid-core/src/lib.rs b/talpid-core/src/lib.rs index 1ee4d3b6ad..b96764848f 100644 --- a/talpid-core/src/lib.rs +++ b/talpid-core/src/lib.rs @@ -47,6 +47,9 @@ pub mod process; /// Abstracts over different VPN tunnel technologies pub mod tunnel; +/// Helper function to preserve previous log files. +pub mod logging; + /// Abstractions and extra features on `std::mpsc` pub mod mpsc; diff --git a/talpid-core/src/logging.rs b/talpid-core/src/logging.rs new file mode 100644 index 0000000000..fe77297b84 --- /dev/null +++ b/talpid-core/src/logging.rs @@ -0,0 +1,20 @@ +use std::path::Path; +use std::{fs, io}; + +error_chain!{} + +/// Create a new log file while backing up a previous version of it. +/// +/// A new log file is created with the given file name, but if a file with that name already exists +/// it is backed up with the extension changed to `.old.log`. +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(()) +} |
