Skip to content

Commit

Permalink
feat: add Log directory (#2736)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
  • Loading branch information
JonasKruckenberg and lucasfernog committed Oct 16, 2021
1 parent 2c1af90 commit acbb3ae
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changes/api-add-log-dir.md
@@ -0,0 +1,6 @@
---
"api": minor
---

Add `logDir` function to the `path` module to access the sugested log directory.
Add `BaseDirectory.Log` to the `fs` module.
5 changes: 5 additions & 0 deletions .changes/core-add-log-dir.md
@@ -0,0 +1,5 @@
---
"tauri": minor
---

Add `tauri::api::path::log_dir` function to access the sugested log directory path.
21 changes: 21 additions & 0 deletions core/tauri/src/api/path.rs
Expand Up @@ -62,6 +62,10 @@ pub enum BaseDirectory {
App,
/// The current working directory.
Current,
/// The Log directory.
/// Resolves to [`BaseDirectory::Home/Library/Logs/{bundle_identifier}`] on macOS
/// and [`BaseDirectory::Config/{bundle_identifier}/logs`] on linux and windows.
Log,
}

/// Resolves the path with the optional base directory.
Expand Down Expand Up @@ -110,6 +114,7 @@ pub fn resolve_path<P: AsRef<Path>>(
BaseDirectory::Resource => resource_dir(package_info),
BaseDirectory::App => app_dir(config),
BaseDirectory::Current => Some(env::current_dir()?),
BaseDirectory::Log => log_dir(config),
};
if let Some(mut base_dir_path_value) = base_dir_path {
// use the same path resolution mechanism as the bundler's resource injection algorithm
Expand Down Expand Up @@ -230,3 +235,19 @@ pub fn resource_dir(package_info: &PackageInfo) -> Option<PathBuf> {
pub fn app_dir(config: &Config) -> Option<PathBuf> {
dirs_next::config_dir().map(|dir| dir.join(&config.tauri.bundle.identifier))
}

/// Returns the path to the log directory.
pub fn log_dir(config: &Config) -> Option<PathBuf> {
#[cfg(target_os = "macos")]
let path = dirs_next::home_dir().map(|dir| {
dir
.join("Library/Logs")
.join(&config.tauri.bundle.identifier)
});

#[cfg(not(target_os = "macos"))]
let path =
dirs_next::config_dir().map(|dir| dir.join(&config.tauri.bundle.identifier).join("logs"));

path
}
3 changes: 2 additions & 1 deletion tooling/api/src/fs.ts
Expand Up @@ -54,7 +54,8 @@ export enum BaseDirectory {
Video,
Resource,
App,
Current
Current,
Log
}

interface FsOptions {
Expand Down
23 changes: 23 additions & 0 deletions tooling/api/src/path.ts
Expand Up @@ -428,6 +428,28 @@ async function currentDir(): Promise<string> {
})
}

/**
* Returns the path to the log directory.
*
* ### Platform-specific
*
* - **Linux:** Resolves to `${configDir}/${bundleIdentifier}`.
* - **macOS:** Resolves to `${homeDir}//Library/Logs/{bundleIdentifier}`
* - **Windows:** Resolves to `${configDir}/${bundleIdentifier}`.
*
* @returns
*/
async function logDir(): Promise<string> {
return invokeTauriCommand<string>({
__tauriModule: 'Path',
message: {
cmd: 'resolvePath',
path: '',
directory: BaseDirectory.Log
}
})
}

/**
* Provides the platform-specific path segment separator:
* - `\` on Windows
Expand Down Expand Up @@ -557,6 +579,7 @@ export {
templateDir,
videoDir,
currentDir,
logDir,
BaseDirectory,
sep,
delimiter,
Expand Down

0 comments on commit acbb3ae

Please sign in to comment.