2023-03-23 02:36:44 +01:00
|
|
|
import 'dart:async';
|
2022-11-27 14:34:19 -06:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/widgets.dart';
|
2025-02-28 01:48:49 +05:30
|
|
|
import 'package:immich_mobile/domain/services/log.service.dart';
|
2022-11-27 14:34:19 -06:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:share_plus/share_plus.dart';
|
|
|
|
|
|
|
|
/// [ImmichLogger] is a custom logger that is built on top of the [logging] package.
|
2023-03-23 02:36:44 +01:00
|
|
|
/// The logs are written to the database and onto console, using `debugPrint` method.
|
2022-11-27 14:34:19 -06:00
|
|
|
///
|
2024-02-24 04:38:57 +01:00
|
|
|
/// The logs are deleted when exceeding the `maxLogEntries` (default 500) property
|
2022-11-27 14:34:19 -06:00
|
|
|
/// in the class.
|
|
|
|
///
|
|
|
|
/// Logs can be shared by calling the `shareLogs` method, which will open a share dialog
|
|
|
|
/// and generate a csv file.
|
2025-02-28 01:48:49 +05:30
|
|
|
abstract final class ImmichLogger {
|
|
|
|
const ImmichLogger();
|
2022-11-27 14:34:19 -06:00
|
|
|
|
2025-02-28 01:48:49 +05:30
|
|
|
static Future<void> shareLogs(BuildContext context) async {
|
2022-11-28 17:17:27 +01:00
|
|
|
final tempDir = await getTemporaryDirectory();
|
|
|
|
final dateTime = DateTime.now().toIso8601String();
|
2024-04-12 01:16:40 +00:00
|
|
|
final filePath = '${tempDir.path}/Immich_log_$dateTime.log';
|
2022-11-28 17:17:27 +01:00
|
|
|
final logFile = await File(filePath).create();
|
|
|
|
final io = logFile.openWrite();
|
|
|
|
try {
|
|
|
|
// Write messages
|
2025-02-28 01:48:49 +05:30
|
|
|
for (final m in await LogService.I.getMessages()) {
|
2024-04-12 01:16:40 +00:00
|
|
|
final created = m.createdAt;
|
|
|
|
final level = m.level.name.padRight(8);
|
2025-02-28 01:48:49 +05:30
|
|
|
final logger = (m.logger ?? "<UNKNOWN_LOGGER>").padRight(20);
|
2024-04-12 01:16:40 +00:00
|
|
|
final message = m.message;
|
2025-02-28 01:48:49 +05:30
|
|
|
final error = m.error == null ? "" : " ${m.error} |";
|
|
|
|
final stack = m.stack == null ? "" : "\n${m.stack!}";
|
2024-04-12 01:16:40 +00:00
|
|
|
io.write('$created | $level | $logger | $message |$error$stack\n');
|
2022-11-28 17:17:27 +01:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
await io.flush();
|
|
|
|
await io.close();
|
2022-11-27 14:34:19 -06:00
|
|
|
}
|
|
|
|
|
2024-07-26 15:43:59 +02:00
|
|
|
final box = context.findRenderObject() as RenderBox?;
|
|
|
|
|
2022-11-27 14:34:19 -06:00
|
|
|
// Share file
|
2023-04-03 16:43:46 -05:00
|
|
|
await Share.shareXFiles(
|
|
|
|
[XFile(filePath)],
|
2022-11-28 17:17:27 +01:00
|
|
|
subject: "Immich logs $dateTime",
|
2024-07-26 15:43:59 +02:00
|
|
|
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
|
2025-02-28 01:48:49 +05:30
|
|
|
).then((value) => logFile.delete());
|
2023-03-23 02:36:44 +01:00
|
|
|
}
|
2022-11-27 14:34:19 -06:00
|
|
|
}
|