Skip to content

Commit acbb3ae

Browse files
feat: add Log directory (#2736)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 2c1af90 commit acbb3ae

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

.changes/api-add-log-dir.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"api": minor
3+
---
4+
5+
Add `logDir` function to the `path` module to access the sugested log directory.
6+
Add `BaseDirectory.Log` to the `fs` module.

.changes/core-add-log-dir.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": minor
3+
---
4+
5+
Add `tauri::api::path::log_dir` function to access the sugested log directory path.

core/tauri/src/api/path.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ pub enum BaseDirectory {
6262
App,
6363
/// The current working directory.
6464
Current,
65+
/// The Log directory.
66+
/// Resolves to [`BaseDirectory::Home/Library/Logs/{bundle_identifier}`] on macOS
67+
/// and [`BaseDirectory::Config/{bundle_identifier}/logs`] on linux and windows.
68+
Log,
6569
}
6670

6771
/// Resolves the path with the optional base directory.
@@ -110,6 +114,7 @@ pub fn resolve_path<P: AsRef<Path>>(
110114
BaseDirectory::Resource => resource_dir(package_info),
111115
BaseDirectory::App => app_dir(config),
112116
BaseDirectory::Current => Some(env::current_dir()?),
117+
BaseDirectory::Log => log_dir(config),
113118
};
114119
if let Some(mut base_dir_path_value) = base_dir_path {
115120
// 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> {
230235
pub fn app_dir(config: &Config) -> Option<PathBuf> {
231236
dirs_next::config_dir().map(|dir| dir.join(&config.tauri.bundle.identifier))
232237
}
238+
239+
/// Returns the path to the log directory.
240+
pub fn log_dir(config: &Config) -> Option<PathBuf> {
241+
#[cfg(target_os = "macos")]
242+
let path = dirs_next::home_dir().map(|dir| {
243+
dir
244+
.join("Library/Logs")
245+
.join(&config.tauri.bundle.identifier)
246+
});
247+
248+
#[cfg(not(target_os = "macos"))]
249+
let path =
250+
dirs_next::config_dir().map(|dir| dir.join(&config.tauri.bundle.identifier).join("logs"));
251+
252+
path
253+
}

tooling/api/src/fs.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export enum BaseDirectory {
5454
Video,
5555
Resource,
5656
App,
57-
Current
57+
Current,
58+
Log
5859
}
5960

6061
interface FsOptions {

tooling/api/src/path.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,28 @@ async function currentDir(): Promise<string> {
428428
})
429429
}
430430

431+
/**
432+
* Returns the path to the log directory.
433+
*
434+
* ### Platform-specific
435+
*
436+
* - **Linux:** Resolves to `${configDir}/${bundleIdentifier}`.
437+
* - **macOS:** Resolves to `${homeDir}//Library/Logs/{bundleIdentifier}`
438+
* - **Windows:** Resolves to `${configDir}/${bundleIdentifier}`.
439+
*
440+
* @returns
441+
*/
442+
async function logDir(): Promise<string> {
443+
return invokeTauriCommand<string>({
444+
__tauriModule: 'Path',
445+
message: {
446+
cmd: 'resolvePath',
447+
path: '',
448+
directory: BaseDirectory.Log
449+
}
450+
})
451+
}
452+
431453
/**
432454
* Provides the platform-specific path segment separator:
433455
* - `\` on Windows
@@ -557,6 +579,7 @@ export {
557579
templateDir,
558580
videoDir,
559581
currentDir,
582+
logDir,
560583
BaseDirectory,
561584
sep,
562585
delimiter,

0 commit comments

Comments
 (0)