feat(linux): Use XDG_STATE_HOME to storage logs (#1675)

* feat(linux): Use XDG_STATE_HOME to storage logs

* fix: Clean LSP suggestions.

* fix: Use Platform.environment instead String.fromEnvironment

The latter seems return an empty string.
See: https://github.com/flutter/flutter/issues/55870#issuecomment-936612420
This commit is contained in:
arenekosreal 2024-07-14 21:38:28 +08:00 committed by GitHub
parent 6a500731d6
commit e6fee03c20
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -65,6 +65,11 @@ class AppLogger {
if (kIsMacOS) {
dir = join((await getLibraryDirectory()).path, "Logs");
}
if (kIsLinux) {
dir = join(_getXdgStateHome(), "spotube");
}
final file = File(join(dir, ".spotube_logs"));
if (!await file.exists()) {
await file.create(recursive: true);
@ -88,6 +93,20 @@ class AppLogger {
);
}
}
static String _getXdgStateHome() {
// path_provider seems does not support XDG_STATE_HOME,
// which is the specification to store application logs on Linux.
// See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
// TODO: Use path_provider once it supports XDG_STATE_HOME
if (const bool.hasEnvironment("XDG_STATE_HOME")) {
String xdgStateHomeRaw = Platform.environment["XDG_STATE_HOME"] ?? "";
if (xdgStateHomeRaw.isNotEmpty) {
return xdgStateHomeRaw;
}
}
return join(Platform.environment["HOME"] ?? "", ".local", "state");
}
}
class AppLoggerProviderObserver extends ProviderObserver {