main function

void main()

Implementation

void main() async {
  Logger.root.level = Level.ALL;

  DeclarativeEdgeInsets.defaultPadding = Spacing.mediumSpacing;
  NetworkService.timeout = const Duration(seconds: 30);
  CoreModule.isWeb = kIsWeb;
  CoreModule.debugMode = kDebugMode;

  setPathUrlStrategy();

  Logger.root.onRecord.listen((record) {
    final scrubbed = LogRecord(
      record.level,
      scrubSensitiveData(record.message),
      record.loggerName,
      record.error,
      record.stackTrace,
      record.zone,
      record.object,
    );

    final handler = Modular.tryGet<LogHandlerService>();

    handler?.call(scrubbed);

    if (record.level >= Level.SEVERE && record.error != null) {
      final logs = (handler?.flush() ?? []).join('\n');

      final bytes = utf8.encode(logs);
      final byteData = ByteData.view(bytes.buffer, bytes.offsetInBytes, bytes.length);

      Sentry.captureException(
        record.error,
        stackTrace: record.stackTrace,
        hint: Hint.withAttachment(
          SentryAttachment.fromByteData(
            byteData,
            '.log',
          ),
        ),
        withScope: (scope) async {
          await scope.setContexts('Logger', record.loggerName);
        },
      );
    }
  });

  Modular
    ..setInitialRoute('/dashboard/')
    ..to.setObservers([
      LogObserver(),
      SentryNavigatorObserver(),
      kRouteObserver,
    ]);

  setPrintResolver((msg) {
    final logger = Logger('Modular');
    final parts = msg.split(' ');

    final log = logger.info;

    if (parts.length == 3 && parts[0] == '--') {
      final module = parts[1].replaceAll('Module', ' Module').toLowerCase();
      final action = parts[2];

      if (action == 'DISPOSED') {
        log('Disposed $module');
        return;
      }

      if (action == 'INITIALIZED') {
        log('Initilaized $module');
        return;
      }
    }

    log(msg);
  });

  if (await FlutterSingleInstance().isFirstInstance()) {
    await Sentry.init(
      (options) => options
        ..dsn = kDebugMode ? '' : kSentryDSN
        ..environment = kInstalledRelease.channel.name
        ..release = kInstalledRelease.version.toString()
        ..debug = kDebugMode,
      appRunner: () async {
        WidgetsFlutterBinding.ensureInitialized();
        if (!kIsWeb) await windowManager.ensureInitialized();

        await PostHog.init(
          apiKey: kPostHogAPIkey,
          host: kPostHogHost,
          debug: kDebugMode,
          version: kInstalledRelease.toString(),
        );

        Modular.to.addListener(() {
          Logger('Modular').finest('Route changed to ${Modular.to.path}');

          PostHog().screen(Modular.to.path);
        });

        runApp(
          ModularApp(
            module: AppModule(),
            debugMode: false,
            child: const SentryScreenshotWidget(child: AppWidget()),
          ),
        );
      },
    );
  } else {
    Logger(kAppName).info('App is already running');
  }
}