super_logging.dart 11 KB

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