summaryrefslogtreecommitdiffhomepage
path: root/talpid-core/src
diff options
context:
space:
mode:
Diffstat (limited to 'talpid-core/src')
-rw-r--r--talpid-core/src/lib.rs3
-rw-r--r--talpid-core/src/logging.rs20
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(())
+}