From 0fe383e538211679dd730b882ae86583609dcaa3 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Wed, 10 Jul 2024 13:55:40 +0800 Subject: [PATCH] feat: enable debug logs in internal build (#5713) --- frontend/appflowy_flutter/lib/env/env.dart | 7 ++++ .../document/application/document_bloc.dart | 7 ++++ .../document_data_pb_extension.dart | 8 +++++ .../editor_transaction_adapter.dart | 36 ++++++++++++------- .../home/menu/sidebar/sidebar.dart | 6 ---- .../widgets/float_bubble/question_bubble.dart | 24 +++++++++++-- 6 files changed, 66 insertions(+), 22 deletions(-) diff --git a/frontend/appflowy_flutter/lib/env/env.dart b/frontend/appflowy_flutter/lib/env/env.dart index a0786e4d5b1da..b861b4cfb87b0 100644 --- a/frontend/appflowy_flutter/lib/env/env.dart +++ b/frontend/appflowy_flutter/lib/env/env.dart @@ -29,4 +29,11 @@ abstract class Env { defaultValue: '', ) static const String afCloudUrl = _Env.afCloudUrl; + + @EnviedField( + obfuscate: false, + varName: 'INTERNAL_BUILD', + defaultValue: '', + ) + static const String internalBuild = _Env.internalBuild; } diff --git a/frontend/appflowy_flutter/lib/plugins/document/application/document_bloc.dart b/frontend/appflowy_flutter/lib/plugins/document/application/document_bloc.dart index a50c7cac2d2a9..cc99dd60c5dfd 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/application/document_bloc.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/application/document_bloc.dart @@ -18,6 +18,7 @@ import 'package:appflowy/util/color_to_hex_string.dart'; import 'package:appflowy/util/debounce.dart'; import 'package:appflowy/util/throttle.dart'; import 'package:appflowy/workspace/application/view/view_listener.dart'; +import 'package:appflowy_backend/log.dart'; import 'package:appflowy_backend/protobuf/flowy-document/entities.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-document/protobuf.dart'; import 'package:appflowy_backend/protobuf/flowy-error/errors.pb.dart'; @@ -37,6 +38,8 @@ import 'package:freezed_annotation/freezed_annotation.dart'; part 'document_bloc.freezed.dart'; +bool enableDocumentInternalLog = false; + class DocumentBloc extends Bloc { DocumentBloc({ required this.documentId, @@ -212,6 +215,10 @@ class DocumentBloc extends Bloc { } Future _initAppFlowyEditorState(DocumentDataPB data) async { + if (enableDocumentInternalLog) { + Log.info('document data: ${data.toProto3Json()}'); + } + final document = data.toDocument(); if (document == null) { assert(false, 'document is null'); diff --git a/frontend/appflowy_flutter/lib/plugins/document/application/document_data_pb_extension.dart b/frontend/appflowy_flutter/lib/plugins/document/application/document_data_pb_extension.dart index da998860146d8..91faccf484dc2 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/application/document_data_pb_extension.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/application/document_data_pb_extension.dart @@ -180,6 +180,8 @@ extension NodeToBlock on Node { String? parentId, String? childrenId, Attributes? attributes, + String? externalId, + String? externalType, }) { assert(id.isNotEmpty); final block = BlockPB.create() @@ -192,6 +194,12 @@ extension NodeToBlock on Node { if (parentId != null && parentId.isNotEmpty) { block.parentId = parentId; } + if (externalId != null && externalId.isNotEmpty) { + block.externalId = externalId; + } + if (externalType != null && externalType.isNotEmpty) { + block.externalType = externalType; + } return block; } diff --git a/frontend/appflowy_flutter/lib/plugins/document/application/editor_transaction_adapter.dart b/frontend/appflowy_flutter/lib/plugins/document/application/editor_transaction_adapter.dart index 666dea3f004f0..f69d116df5185 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/application/editor_transaction_adapter.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/application/editor_transaction_adapter.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'dart:convert'; +import 'package:appflowy/plugins/document/application/document_bloc.dart'; import 'package:appflowy/plugins/document/application/document_data_pb_extension.dart'; import 'package:appflowy/plugins/document/application/document_service.dart'; import 'package:appflowy_backend/log.dart'; @@ -21,6 +22,8 @@ import 'package:appflowy_editor/appflowy_editor.dart' import 'package:collection/collection.dart'; import 'package:nanoid/nanoid.dart'; +const _kExternalTextType = 'text'; + /// Uses to adjust the data structure between the editor and the backend. /// /// The editor uses a tree structure to represent the document, while the backend uses a flat structure. @@ -34,11 +37,9 @@ class TransactionAdapter { final DocumentService documentService; final String documentId; - final bool _enableDebug = false; - Future apply(Transaction transaction, EditorState editorState) async { final stopwatch = Stopwatch()..start(); - if (_enableDebug) { + if (enableDocumentInternalLog) { Log.debug('transaction => ${transaction.toJson()}'); } final actions = transaction.operations @@ -60,8 +61,10 @@ class TransactionAdapter { textId: payload.textId, delta: payload.delta, ); - if (_enableDebug) { - Log.debug('create external text: ${payload.delta}'); + if (enableDocumentInternalLog) { + Log.debug( + '[editor_transaction_adapter] create external text: ${payload.delta}', + ); } } else if (type == TextDeltaType.update) { await documentService.updateExternalText( @@ -69,8 +72,10 @@ class TransactionAdapter { textId: payload.textId, delta: payload.delta, ); - if (_enableDebug) { - Log.debug('update external text: ${payload.delta}'); + if (enableDocumentInternalLog) { + Log.debug( + '[editor_transaction_adapter] update external text: ${payload.delta}', + ); } } } @@ -82,9 +87,9 @@ class TransactionAdapter { ); final elapsed = stopwatch.elapsedMilliseconds; stopwatch.stop(); - if (_enableDebug) { + if (enableDocumentInternalLog) { Log.debug( - 'apply transaction cost: total $elapsed ms, converter action $actionCostTime ms, apply action ${elapsed - actionCostTime} ms', + '[editor_transaction_adapter] apply transaction cost: total $elapsed ms, converter action $actionCostTime ms, apply action ${elapsed - actionCostTime} ms', ); } } @@ -136,8 +141,9 @@ extension on InsertOperation { // create the external text if the node contains the delta in its data. final delta = node.delta; TextDeltaPayloadPB? textDeltaPayloadPB; + String? textId; if (delta != null) { - final textId = nanoid(6); + textId = nanoid(6); textDeltaPayloadPB = TextDeltaPayloadPB( documentId: documentId, @@ -148,13 +154,17 @@ extension on InsertOperation { // sync the text id to the node node.externalValues = ExternalValues( externalId: textId, - externalType: 'text', + externalType: _kExternalTextType, ); } // remove the delta from the data when the incremental update is stable. final payload = BlockActionPayloadPB() - ..block = node.toBlock(childrenId: nanoid(6)) + ..block = node.toBlock( + childrenId: nanoid(6), + externalId: textId, + externalType: textId != null ? _kExternalTextType : null, + ) ..parentId = parentId ..prevId = prevId; @@ -248,7 +258,7 @@ extension on UpdateOperation { node.externalValues = ExternalValues( externalId: textId, - externalType: 'text', + externalType: _kExternalTextType, ); actions.add( diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar.dart b/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar.dart index 75936f42d5152..47bfc237d84b3 100644 --- a/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar.dart +++ b/frontend/appflowy_flutter/lib/workspace/presentation/home/menu/sidebar/sidebar.dart @@ -27,7 +27,6 @@ import 'package:appflowy/workspace/presentation/home/menu/sidebar/shared/sidebar import 'package:appflowy/workspace/presentation/home/menu/sidebar/space/sidebar_space.dart'; import 'package:appflowy/workspace/presentation/home/menu/sidebar/space/space_migration.dart'; import 'package:appflowy/workspace/presentation/home/menu/sidebar/workspace/sidebar_workspace.dart'; -import 'package:appflowy_backend/log.dart'; import 'package:appflowy_backend/protobuf/flowy-folder/workspace.pb.dart'; import 'package:appflowy_backend/protobuf/flowy-user/protobuf.dart' show UserProfilePB; @@ -371,11 +370,6 @@ class _SidebarState extends State<_Sidebar> { final sidebarSectionBloc = context.watch(); final containsSpace = sidebarSectionBloc.state.containsSpace; - Log.info('fetch the space info from sidebar section: $containsSpace'); - Log.info( - 'fetch the space info from space: ${spaceState.spaces.isNotEmpty}', - ); - if (containsSpace && spaceState.spaces.isEmpty) { context.read().add(const SpaceEvent.didReceiveSpaceUpdate()); } diff --git a/frontend/appflowy_flutter/lib/workspace/presentation/widgets/float_bubble/question_bubble.dart b/frontend/appflowy_flutter/lib/workspace/presentation/widgets/float_bubble/question_bubble.dart index fde71f9150616..6ff84a9adca1a 100644 --- a/frontend/appflowy_flutter/lib/workspace/presentation/widgets/float_bubble/question_bubble.dart +++ b/frontend/appflowy_flutter/lib/workspace/presentation/widgets/float_bubble/question_bubble.dart @@ -1,9 +1,12 @@ import 'package:appflowy/core/helpers/url_launcher.dart'; +import 'package:appflowy/env/env.dart'; import 'package:appflowy/generated/flowy_svgs.g.dart'; import 'package:appflowy/generated/locale_keys.g.dart'; +import 'package:appflowy/plugins/document/application/document_bloc.dart'; import 'package:appflowy/startup/tasks/rust_sdk.dart'; import 'package:appflowy/util/theme_extension.dart'; import 'package:appflowy/workspace/presentation/home/toast.dart'; +import 'package:appflowy/workspace/presentation/widgets/dialogs.dart'; import 'package:appflowy/workspace/presentation/widgets/pop_up_action.dart'; import 'package:appflowy_popover/appflowy_popover.dart'; import 'package:device_info_plus/device_info_plus.dart'; @@ -203,9 +206,24 @@ class FlowyVersionDescription extends CustomActionCell { thickness: 1.0, ), const VSpace(6), - FlowyText( - "$appName $version", - color: Theme.of(context).hintColor, + GestureDetector( + behavior: HitTestBehavior.opaque, + onDoubleTap: () { + if (Env.internalBuild != '1') { + return; + } + enableDocumentInternalLog = !enableDocumentInternalLog; + showToastNotification( + context, + message: enableDocumentInternalLog + ? 'Enabled Internal Log' + : 'Disabled Internal Log', + ); + }, + child: FlowyText( + '$appName $version', + color: Theme.of(context).hintColor, + ), ), ], ).padding(