super_logging.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. library super_logging;
  2. import 'dart:async';
  3. import 'dart:collection';
  4. import 'dart:core';
  5. import 'dart:io';
  6. import 'package:flutter/foundation.dart';
  7. import 'package:flutter/widgets.dart';
  8. import 'package:http/http.dart' as http;
  9. import 'package:intl/intl.dart';
  10. import 'package:logging/logging.dart';
  11. import 'package:package_info_plus/package_info_plus.dart';
  12. import 'package:path/path.dart';
  13. import 'package:path_provider/path_provider.dart';
  14. import 'package:photos/core/error-reporting/tunneled_transport.dart';
  15. import 'package:photos/models/typedefs.dart';
  16. import 'package:sentry_flutter/sentry_flutter.dart';
  17. import 'package:shared_preferences/shared_preferences.dart';
  18. extension SuperString on String {
  19. Iterable<String> chunked(int chunkSize) sync* {
  20. var start = 0;
  21. while (true) {
  22. final stop = start + chunkSize;
  23. if (stop > length) break;
  24. yield substring(start, stop);
  25. start = stop;
  26. }
  27. if (start < length) {
  28. yield substring(start);
  29. }
  30. }
  31. }
  32. extension SuperLogRecord on LogRecord {
  33. String toPrettyString([String? extraLines]) {
  34. final header = "[$loggerName] [$level] [$time]";
  35. var msg = "$header $message";
  36. if (error != null) {
  37. msg += "\n⤷ type: ${error.runtimeType}\n⤷ error: $error";
  38. }
  39. if (stackTrace != null) {
  40. msg += "\n⤷ trace: $stackTrace";
  41. }
  42. for (var line in extraLines?.split('\n') ?? []) {
  43. msg += '\n$header $line';
  44. }
  45. return msg;
  46. }
  47. }
  48. class LogConfig {
  49. /// The DSN for a Sentry app.
  50. /// This can be obtained from the Sentry apps's "settings > Client Keys (DSN)" page.
  51. ///
  52. /// Only logs containing errors are sent to sentry.
  53. /// Errors can be caught using a try-catch block, like so:
  54. ///
  55. /// ```
  56. /// final logger = Logger("main");
  57. ///
  58. /// try {
  59. /// // do something dangerous here
  60. /// } catch(e, trace) {
  61. /// logger.info("Huston, we have a problem", e, trace);
  62. /// }
  63. /// ```
  64. ///
  65. /// If this is [null], Sentry logger is completely disabled (default).
  66. String? sentryDsn;
  67. String? tunnel;
  68. /// A built-in retry mechanism for sending errors to sentry.
  69. ///
  70. /// This parameter defines the time to wait for, before retrying.
  71. Duration sentryRetryDelay;
  72. /// Path of the directory where log files will be stored.
  73. ///
  74. /// If this is [null], file logging is completely disabled (default).
  75. ///
  76. /// If this is an empty string (['']),
  77. /// then a 'logs' directory will be created in [getTemporaryDirectory()].
  78. ///
  79. /// A non-empty string will be treated as an explicit path to a directory.
  80. ///
  81. /// The chosen directory can be accessed using [SuperLogging.logFile.parent].
  82. String? logDirPath;
  83. /// The maximum number of log files inside [logDirPath].
  84. ///
  85. /// One log file is created per day.
  86. /// Older log files are deleted automatically.
  87. int maxLogFiles;
  88. /// Whether to enable super logging features in debug mode.
  89. ///
  90. /// Sentry and file logging are typically not needed in debug mode,
  91. /// where a complete logcat is available.
  92. bool enableInDebugMode;
  93. /// If provided, super logging will invoke this function, and
  94. /// any uncaught errors during its execution will be reported.
  95. ///
  96. /// Works by using [FlutterError.onError] and [runZoned].
  97. FutureOrVoidCallback? body;
  98. /// The date format for storing log files.
  99. ///
  100. /// `DateFormat('y-M-d')` by default.
  101. DateFormat? dateFmt;
  102. String prefix;
  103. LogConfig({
  104. this.sentryDsn,
  105. this.tunnel,
  106. this.sentryRetryDelay = const Duration(seconds: 30),
  107. this.logDirPath,
  108. this.maxLogFiles = 10,
  109. this.enableInDebugMode = false,
  110. this.body,
  111. this.dateFmt,
  112. this.prefix = "",
  113. }) {
  114. dateFmt ??= DateFormat("y-M-d");
  115. }
  116. }
  117. class SuperLogging {
  118. /// The logger for SuperLogging
  119. static final $ = Logger('ente_logging');
  120. /// The current super logging configuration
  121. static late LogConfig config;
  122. static late SharedPreferences _preferences;
  123. static const keyShouldReportCrashes = "should_report_crashes";
  124. static Future<void> main([LogConfig? appConfig]) async {
  125. appConfig ??= LogConfig();
  126. SuperLogging.config = appConfig;
  127. WidgetsFlutterBinding.ensureInitialized();
  128. _preferences = await SharedPreferences.getInstance();
  129. appVersion ??= await getAppVersion();
  130. final isFDroidClient = await isFDroidBuild();
  131. if (isFDroidClient) {
  132. appConfig.sentryDsn = null;
  133. appConfig.tunnel = null;
  134. }
  135. final enable = appConfig.enableInDebugMode || kReleaseMode;
  136. sentryIsEnabled = enable &&
  137. appConfig.sentryDsn != null &&
  138. !isFDroidClient &&
  139. shouldReportCrashes();
  140. fileIsEnabled = enable && appConfig.logDirPath != null;
  141. if (fileIsEnabled) {
  142. await setupLogDir();
  143. }
  144. if (sentryIsEnabled) {
  145. setupSentry();
  146. }
  147. Logger.root.level = Level.ALL;
  148. Logger.root.onRecord.listen(onLogRecord);
  149. if (isFDroidClient) {
  150. assert(
  151. sentryIsEnabled == false,
  152. "sentry dsn should be disabled for "
  153. "f-droid config ${appConfig.sentryDsn} & ${appConfig.tunnel}",
  154. );
  155. }
  156. if (!enable) {
  157. $.info("detected debug mode; sentry & file logging disabled.");
  158. }
  159. if (fileIsEnabled) {
  160. $.info("log file for today: $logFile with prefix ${appConfig.prefix}");
  161. }
  162. if (sentryIsEnabled) {
  163. $.info("sentry uploader started");
  164. }
  165. if (appConfig.body == null) return;
  166. if (enable && sentryIsEnabled) {
  167. await SentryFlutter.init(
  168. (options) {
  169. options.dsn = appConfig!.sentryDsn;
  170. options.httpClient = http.Client();
  171. if (appConfig.tunnel != null) {
  172. options.transport =
  173. TunneledTransport(Uri.parse(appConfig.tunnel!), options);
  174. }
  175. },
  176. appRunner: () => appConfig!.body!(),
  177. );
  178. } else {
  179. await appConfig.body!();
  180. }
  181. }
  182. static Future<void> setUserID(String userID) async {
  183. if (config.sentryDsn != null) {
  184. $.finest("setting sentry user ID to: $userID");
  185. Sentry.configureScope(
  186. (scope) => scope.setUser(SentryUser(id: userID)).onError(
  187. (e, s) => $.warning("failed to configure scope user", e, s),
  188. ),
  189. );
  190. }
  191. }
  192. static Future<void> _sendErrorToSentry(
  193. Object error,
  194. StackTrace? stack,
  195. ) async {
  196. try {
  197. await Sentry.captureException(
  198. error,
  199. stackTrace: stack,
  200. );
  201. } catch (e) {
  202. $.info('Sending report to sentry.io failed: $e');
  203. $.info('Original error: $error');
  204. }
  205. }
  206. static String _lastExtraLines = '';
  207. static Future onLogRecord(LogRecord rec) async {
  208. // log misc info if it changed
  209. String? extraLines = "app version: '$appVersion'\n";
  210. if (extraLines != _lastExtraLines) {
  211. _lastExtraLines = extraLines;
  212. } else {
  213. extraLines = null;
  214. }
  215. final str = (config.prefix) + " " + rec.toPrettyString(extraLines);
  216. // write to stdout
  217. printLog(str);
  218. // push to log queue
  219. if (fileIsEnabled) {
  220. fileQueueEntries.add(str + '\n');
  221. if (fileQueueEntries.length == 1) {
  222. flushQueue();
  223. }
  224. }
  225. // add error to sentry queue
  226. if (sentryIsEnabled && rec.error != null) {
  227. _sendErrorToSentry(rec.error!, null);
  228. }
  229. }
  230. static final Queue<String> fileQueueEntries = Queue();
  231. static bool isFlushing = false;
  232. static void flushQueue() async {
  233. if (isFlushing || logFile == null) {
  234. return;
  235. }
  236. isFlushing = true;
  237. final entry = fileQueueEntries.removeFirst();
  238. await logFile!.writeAsString(entry, mode: FileMode.append, flush: true);
  239. isFlushing = false;
  240. if (fileQueueEntries.isNotEmpty) {
  241. flushQueue();
  242. }
  243. }
  244. // Logs on must be chunked or they get truncated otherwise
  245. // See https://github.com/flutter/flutter/issues/22665
  246. static var logChunkSize = 800;
  247. static void printLog(String text) {
  248. text.chunked(logChunkSize).forEach(print);
  249. }
  250. /// A queue to be consumed by [setupSentry].
  251. static final sentryQueueControl = StreamController<Error>();
  252. /// Whether sentry logging is currently enabled or not.
  253. static bool sentryIsEnabled = false;
  254. static Future<void> setupSentry() async {
  255. await for (final error in sentryQueueControl.stream.asBroadcastStream()) {
  256. try {
  257. Sentry.captureException(
  258. error,
  259. );
  260. } catch (e) {
  261. $.fine(
  262. "sentry upload failed; will retry after ${config.sentryRetryDelay}",
  263. );
  264. doSentryRetry(error);
  265. }
  266. }
  267. }
  268. static void doSentryRetry(Error error) async {
  269. await Future.delayed(config.sentryRetryDelay);
  270. sentryQueueControl.add(error);
  271. }
  272. static bool shouldReportCrashes() {
  273. if (_preferences.containsKey(keyShouldReportCrashes)) {
  274. return _preferences.getBool(keyShouldReportCrashes)!;
  275. } else {
  276. return true; // Report crashes by default
  277. }
  278. }
  279. static Future<void> setShouldReportCrashes(bool value) {
  280. return _preferences.setBool(keyShouldReportCrashes, value);
  281. }
  282. /// The log file currently in use.
  283. static File? logFile;
  284. /// Whether file logging is currently enabled or not.
  285. static bool fileIsEnabled = false;
  286. static Future<void> setupLogDir() async {
  287. var dirPath = config.logDirPath;
  288. // choose [logDir]
  289. if (dirPath == null || dirPath.isEmpty) {
  290. final root = await getExternalStorageDirectory();
  291. dirPath = '${root!.path}/logs';
  292. }
  293. // create [logDir]
  294. final dir = Directory(dirPath);
  295. await dir.create(recursive: true);
  296. final files = <File>[];
  297. final dates = <File, DateTime>{};
  298. // collect all log files with valid names
  299. await for (final file in dir.list()) {
  300. try {
  301. final date = config.dateFmt!.parse(basename(file.path));
  302. dates[file as File] = date;
  303. files.add(file);
  304. } on FormatException {}
  305. }
  306. final nowTime = DateTime.now();
  307. // delete old log files, if [maxLogFiles] is exceeded.
  308. if (files.length > config.maxLogFiles) {
  309. // sort files based on ascending order of date (older first)
  310. files.sort(
  311. (a, b) => (dates[a] ?? nowTime).compareTo((dates[b] ?? nowTime)),
  312. );
  313. final extra = files.length - config.maxLogFiles;
  314. final toDelete = files.sublist(0, extra);
  315. for (final file in toDelete) {
  316. try {
  317. $.fine(
  318. "deleting log file ${file.path}",
  319. );
  320. await file.delete();
  321. } catch (ignore) {}
  322. }
  323. }
  324. logFile = File("$dirPath/${config.dateFmt!.format(DateTime.now())}.txt");
  325. }
  326. /// Current app version, obtained from package_info plugin.
  327. ///
  328. /// See: [getAppVersion]
  329. static String? appVersion;
  330. static Future<String> getAppVersion() async {
  331. final pkgInfo = await PackageInfo.fromPlatform();
  332. return "${pkgInfo.version}+${pkgInfo.buildNumber}";
  333. }
  334. // disable sentry on f-droid. We need to make it opt-in preference
  335. static Future<bool> isFDroidBuild() async {
  336. if (!Platform.isAndroid) {
  337. return false;
  338. }
  339. final pkgName = (await PackageInfo.fromPlatform()).packageName;
  340. return pkgName.startsWith("io.ente.photos.fdroid");
  341. }
  342. }