Explorar el Código

feat: add Log directory (#2736)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Jonas Kruckenberg hace 3 años
padre
commit
acbb3ae7bb

+ 6 - 0
.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 - 0
.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 - 0
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<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
+}

+ 2 - 1
tooling/api/src/fs.ts

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

+ 23 - 0
tooling/api/src/path.ts

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