summaryrefslogtreecommitdiffhomepage
path: root/mullvad-paths/src/logs.rs
blob: d7f2c58f95d73499d1a97bfc9647ca7107e94a4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::Result;

#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::{env, path::PathBuf};

/// Creates and returns the logging directory pointed to by `MULLVAD_LOG_DIR`, or the default
/// one if that variable is unset.
pub fn log_dir() -> Result<PathBuf> {
    #[cfg(unix)]
    {
        let permissions = Some(PermissionsExt::from_mode(0o755));
        crate::create_and_return(get_log_dir, permissions)
    }
    #[cfg(target_os = "windows")]
    {
        crate::create_and_return(get_log_dir, true)
    }
}

/// Get the logging directory, but don't try to create it.
pub fn get_log_dir() -> Result<PathBuf> {
    match env::var_os("MULLVAD_LOG_DIR") {
        Some(path) => Ok(PathBuf::from(path)),
        None => get_default_log_dir(),
    }
}

pub fn get_default_log_dir() -> Result<PathBuf> {
    #[cfg(not(target_os = "android"))]
    {
        let dir;
        #[cfg(unix)]
        {
            dir = Ok(PathBuf::from("/var/log"));
        }
        #[cfg(windows)]
        {
            dir = crate::get_allusersprofile_dir();
        }
        dir.map(|dir| dir.join(crate::PRODUCT_NAME))
    }
    #[cfg(target_os = "android")]
    {
        Ok(PathBuf::from(crate::APP_PATH))
    }
}