feat: add Log directory (#2736)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
This commit is contained in:
Jonas Kruckenberg 2021-10-16 14:56:23 +02:00 committed by GitHub
parent 2c1af90c35
commit acbb3ae7bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 1 deletions

View File

@ -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.

View File

@ -0,0 +1,5 @@
---
"tauri": minor
---
Add `tauri::api::path::log_dir` function to access the sugested log directory path.

View File

@ -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.
@ -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
@ -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
}

View File

@ -54,7 +54,8 @@ export enum BaseDirectory {
Video,
Resource,
App,
Current
Current,
Log
}
interface FsOptions {

View File

@ -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
@ -557,6 +579,7 @@ export {
templateDir,
videoDir,
currentDir,
logDir,
BaseDirectory,
sep,
delimiter,