super_logging.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 void setUserID(String userID) async {
  183. if (config.sentryDsn != null) {
  184. Sentry.configureScope((scope) => scope.user = SentryUser(id: userID));
  185. $.info("setting sentry user ID to: $userID");
  186. }
  187. }
  188. static Future<void> _sendErrorToSentry(
  189. Object error,
  190. StackTrace? stack,
  191. ) async {
  192. try {
  193. await Sentry.captureException(
  194. error,
  195. stackTrace: stack,
  196. );
  197. } catch (e) {
  198. $.info('Sending report to sentry.io failed: $e');
  199. $.info('Original error: $error');
  200. }
  201. }
  202. static String _lastExtraLines = '';
  203. static Future onLogRecord(LogRecord rec) async {
  204. // log misc info if it changed
  205. String? extraLines = "app version: '$appVersion'\n";
  206. if (extraLines != _lastExtraLines) {
  207. _lastExtraLines = extraLines;
  208. } else {
  209. extraLines = null;
  210. }
  211. final str = (config.prefix) + " " + rec.toPrettyString(extraLines);
  212. // write to stdout
  213. printLog(str);
  214. // push to log queue
  215. if (fileIsEnabled) {
  216. fileQueueEntries.add(str + '\n');
  217. if (fileQueueEntries.length == 1) {
  218. flushQueue();
  219. }
  220. }
  221. // add error to sentry queue
  222. if (sentryIsEnabled && rec.error != null) {
  223. _sendErrorToSentry(rec.error!, null);
  224. }
  225. }
  226. static final Queue<String> fileQueueEntries = Queue();
  227. static bool isFlushing = false;
  228. static void flushQueue() async {
  229. if (isFlushing || logFile == null) {
  230. return;
  231. }
  232. isFlushing = true;
  233. final entry = fileQueueEntries.removeFirst();
  234. await logFile!.writeAsString(entry, mode: FileMode.append, flush: true);
  235. isFlushing = false;
  236. if (fileQueueEntries.isNotEmpty) {
  237. flushQueue();
  238. }
  239. }
  240. // Logs on must be chunked or they get truncated otherwise
  241. // See https://github.com/flutter/flutter/issues/22665
  242. static var logChunkSize = 800;
  243. static void printLog(String text) {
  244. text.chunked(logChunkSize).forEach(print);
  245. }
  246. /// A queue to be consumed by [setupSentry].
  247. static final sentryQueueControl = StreamController<Error>();
  248. /// Whether sentry logging is currently enabled or not.
  249. static bool sentryIsEnabled = false;
  250. static Future<void> setupSentry() async {
  251. await for (final error in sentryQueueControl.stream.asBroadcastStream()) {
  252. try {
  253. Sentry.captureException(
  254. error,
  255. );
  256. } catch (e) {
  257. $.fine(
  258. "sentry upload failed; will retry after ${config.sentryRetryDelay}",
  259. );
  260. doSentryRetry(error);
  261. }
  262. }
  263. }
  264. static void doSentryRetry(Error error) async {
  265. await Future.delayed(config.sentryRetryDelay);
  266. sentryQueueControl.add(error);
  267. }
  268. static bool shouldReportCrashes() {
  269. if (_preferences.containsKey(keyShouldReportCrashes)) {
  270. return _preferences.getBool(keyShouldReportCrashes)!;
  271. } else {
  272. return true; // Report crashes by default
  273. }
  274. }
  275. static Future<void> setShouldReportCrashes(bool value) {
  276. return _preferences.setBool(keyShouldReportCrashes, value);
  277. }
  278. /// The log file currently in use.
  279. static File? logFile;
  280. /// Whether file logging is currently enabled or not.
  281. static bool fileIsEnabled = false;
  282. static Future<void> setupLogDir() async {
  283. var dirPath = config.logDirPath;
  284. // choose [logDir]
  285. if (dirPath == null || dirPath.isEmpty) {
  286. final root = await getExternalStorageDirectory();
  287. dirPath = '${root!.path}/logs';
  288. }
  289. // create [logDir]
  290. final dir = Directory(dirPath);
  291. await dir.create(recursive: true);
  292. final files = <File>[];
  293. final dates = <File, DateTime>{};
  294. // collect all log files with valid names
  295. await for (final file in dir.list()) {
  296. try {
  297. final date = config.dateFmt!.parse(basename(file.path));
  298. dates[file as File] = date;
  299. files.add(file);
  300. } on FormatException {}
  301. }
  302. final nowTime = DateTime.now();
  303. // delete old log files, if [maxLogFiles] is exceeded.
  304. if (files.length > config.maxLogFiles) {
  305. // sort files based on ascending order of date (older first)
  306. files.sort(
  307. (a, b) => (dates[a] ?? nowTime).compareTo((dates[b] ?? nowTime)),
  308. );
  309. final extra = files.length - config.maxLogFiles;
  310. final toDelete = files.sublist(0, extra);
  311. for (final file in toDelete) {
  312. try {
  313. $.fine(
  314. "deleting log file ${file.path}",
  315. );
  316. await file.delete();
  317. } catch (ignore) {}
  318. }
  319. }
  320. logFile = File("$dirPath/${config.dateFmt!.format(DateTime.now())}.txt");
  321. }
  322. /// Current app version, obtained from package_info plugin.
  323. ///
  324. /// See: [getAppVersion]
  325. static String? appVersion;
  326. static Future<String> getAppVersion() async {
  327. final pkgInfo = await PackageInfo.fromPlatform();
  328. return "${pkgInfo.version}+${pkgInfo.buildNumber}";
  329. }
  330. // disable sentry on f-droid. We need to make it opt-in preference
  331. static Future<bool> isFDroidBuild() async {
  332. if (!Platform.isAndroid) {
  333. return false;
  334. }
  335. final pkgName = (await PackageInfo.fromPlatform()).packageName;
  336. return pkgName.startsWith("io.ente.photos.fdroid");
  337. }
  338. }