From e9479b1dcf788e28e63224cb45ba938a762b6550 Mon Sep 17 00:00:00 2001 From: Zhang Hua Date: Sun, 7 Jul 2024 14:13:01 +0800 Subject: [PATCH] feat(linux): Use XDG_STATE_HOME to storage logs --- lib/services/logger/logger.dart | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/services/logger/logger.dart b/lib/services/logger/logger.dart index 6ba76ea1..16fda618 100644 --- a/lib/services/logger/logger.dart +++ b/lib/services/logger/logger.dart @@ -64,6 +64,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); @@ -87,4 +92,18 @@ 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 (bool.hasEnvironment("XDG_STATE_HOME")) { + String xdgStateHomeRaw = String.fromEnvironment("XDG_STATE_HOME"); + if (!xdgStateHomeRaw.isEmpty) { + return xdgStateHomeRaw; + } + } + return join(String.fromEnvironment("HOME"), ".local", "state"); + } }