diff --git a/.changes/api-add-log-dir.md b/.changes/api-add-log-dir.md new file mode 100644 index 00000000000..b0586cbd633 --- /dev/null +++ b/.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. diff --git a/.changes/core-add-log-dir.md b/.changes/core-add-log-dir.md new file mode 100644 index 00000000000..d338b59f349 --- /dev/null +++ b/.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. diff --git a/core/tauri/src/api/path.rs b/core/tauri/src/api/path.rs index 2fc00735357..04814881459 100644 --- a/core/tauri/src/api/path.rs +++ b/core/tauri/src/api/path.rs @@ -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>( 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 { pub fn app_dir(config: &Config) -> Option { 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 { + #[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 +} diff --git a/tooling/api/src/fs.ts b/tooling/api/src/fs.ts index 3c7acc990e9..895c747b6aa 100644 --- a/tooling/api/src/fs.ts +++ b/tooling/api/src/fs.ts @@ -54,7 +54,8 @@ export enum BaseDirectory { Video, Resource, App, - Current + Current, + Log } interface FsOptions { diff --git a/tooling/api/src/path.ts b/tooling/api/src/path.ts index 8abb20dd07d..3073457bbbf 100644 --- a/tooling/api/src/path.ts +++ b/tooling/api/src/path.ts @@ -428,6 +428,28 @@ async function currentDir(): Promise { }) } +/** + * 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 { + return invokeTauriCommand({ + __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,