Skip to content

Commit 6074a51

Browse files
committed
feat: more logging for wallet groups
1 parent 3c3663f commit 6074a51

File tree

10 files changed

+162
-4
lines changed

10 files changed

+162
-4
lines changed

cw_core/lib/utils/print_verbose.dart

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
import 'dart:io';
12
import 'dart:math';
23
import 'package:flutter/foundation.dart';
34

5+
String? printVLogFilePath;
6+
47
void printV(dynamic content) {
58
CustomTrace programInfo = CustomTrace(StackTrace.current);
6-
print("${programInfo.fileName}#${programInfo.lineNumber}:${programInfo.columnNumber} ${programInfo.callerFunctionName}: $content");
9+
final logMsg = "${programInfo.fileName}#${programInfo.lineNumber}:${programInfo.columnNumber} ${programInfo.callerFunctionName}: $content";
10+
if (printVLogFilePath != null) {
11+
try {
12+
File(printVLogFilePath!).writeAsStringSync("$logMsg\n", mode: FileMode.append);
13+
} catch (e) {
14+
print("Unable to write to log file (printV): $e");
15+
}
16+
}
17+
print(logMsg);
718
}
819

920
// https://stackoverflow.com/a/59386101

lib/core/wallet_loading_service.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class WalletLoadingService {
8585

8686
// try opening another wallet that is not corrupted to give user access to the app
8787
final walletInfoSource = await CakeHive.openBox<WalletInfo>(WalletInfo.boxName);
88+
printV("WalletInfoSource length (wallet loading service): ${walletInfoSource.length}");
8889
WalletBase? wallet;
8990
for (var walletInfo in walletInfoSource.values) {
9091
try {

lib/main.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import 'package:cake_wallet/store/authentication_store.dart';
2828
import 'package:cake_wallet/themes/theme_base.dart';
2929
import 'package:cake_wallet/utils/device_info.dart';
3030
import 'package:cake_wallet/utils/exception_handler.dart';
31+
import 'package:cake_wallet/utils/feature_flag.dart';
3132
import 'package:cake_wallet/view_model/link_view_model.dart';
3233
import 'package:cake_wallet/utils/responsive_layout_util.dart';
3334
import 'package:cw_core/address_info.dart';
@@ -72,6 +73,12 @@ Future<void> runAppWithZone({Key? topLevelKey}) async {
7273

7374
return true;
7475
};
76+
final date = DateTime.now().toIso8601String().replaceAll(':', '-');
77+
final dir = '${(await getAppDir()).path}/print_v';
78+
if (!Directory(dir).existsSync()) {
79+
Directory(dir).createSync(recursive: true);
80+
}
81+
printVLogFilePath = FeatureFlag.hasDevOptions ? '$dir/$date.log' : null;
7582
await FlutterDaemon().unmarkBackgroundSync();
7683
await initializeAppAtRoot();
7784

@@ -193,6 +200,7 @@ Future<void> initializeAppConfigs({bool loadWallet = true}) async {
193200
final trades = await CakeHive.openBox<Trade>(Trade.boxName, encryptionKey: tradesBoxKey);
194201
final orders = await CakeHive.openBox<Order>(Order.boxName, encryptionKey: ordersBoxKey);
195202
final walletInfoSource = await CakeHive.openBox<WalletInfo>(WalletInfo.boxName);
203+
printV("WalletInfoSource length (initializeAppConfigs): ${walletInfoSource.length}");
196204
final templates = await CakeHive.openBox<Template>(Template.boxName);
197205
final exchangeTemplates = await CakeHive.openBox<ExchangeTemplate>(ExchangeTemplate.boxName);
198206
final anonpayInvoiceInfo = await CakeHive.openBox<AnonpayInvoiceInfo>(AnonpayInvoiceInfo.boxName);
@@ -407,6 +415,12 @@ Future<void> backgroundSync() async {
407415
WidgetsFlutterBinding.ensureInitialized();
408416
printV("- DartPluginRegistrant.ensureInitialized()");
409417
DartPluginRegistrant.ensureInitialized();
418+
final date = DateTime.now().toIso8601String().replaceAll(':', '-');
419+
final dir = '${(await getAppDir()).path}/print_v';
420+
if (!Directory(dir).existsSync()) {
421+
Directory(dir).createSync(recursive: true);
422+
}
423+
printVLogFilePath = FeatureFlag.hasDevOptions ? '$dir/$date.log' : null;
410424
printV("- FlutterDaemon.markBackgroundSync()");
411425
final val = await FlutterDaemon().markBackgroundSync();
412426
if (val) {

lib/router.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import 'package:cake_wallet/src/screens/dashboard/sign_page.dart';
3737
import 'package:cake_wallet/src/screens/dev/hash_change_logs_page.dart';
3838
import 'package:cake_wallet/src/screens/dev/monero_background_sync.dart';
3939
import 'package:cake_wallet/src/screens/dev/moneroc_call_profiler.dart';
40+
import 'package:cake_wallet/src/screens/dev/print_verbose_logs_page.dart';
4041
import 'package:cake_wallet/src/screens/dev/secure_preferences_page.dart';
4142
import 'package:cake_wallet/src/screens/dev/shared_preferences_page.dart';
4243
import 'package:cake_wallet/src/screens/dev/background_sync_logs_page.dart';
@@ -863,6 +864,9 @@ Route<dynamic> createRoute(RouteSettings settings) {
863864
case Routes.devHashChangeLogs:
864865
return MaterialPageRoute<void>(builder: (_) => HashChangeLogsPage());
865866

867+
case Routes.devPrintVerbose:
868+
return MaterialPageRoute<void>(builder: (_) => PrintVerboseLogsPage());
869+
866870
default:
867871
return MaterialPageRoute<void>(
868872
builder: (_) => Scaffold(

lib/routes.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ class Routes {
118118
static const devSecurePreferences = '/dev/secure_preferences';
119119
static const devBackgroundSyncLogs = '/dev/background_sync_logs';
120120
static const devHashChangeLogs = '/dev/hash_change_logs';
121-
121+
static const devPrintVerbose = '/dev/print_verbose';
122+
122123
static const signPage = '/sign_page';
123124
static const connectDevices = '/device/connect';
124125
static const urqrAnimatedPage = '/urqr/animated_page';

lib/src/screens/dev/hash_change_logs_page.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class HashChangeLogsPage extends BasePage {
5353
}
5454
return SingleChildScrollView(
5555
padding: EdgeInsets.all(16),
56-
child: Text(
56+
child: SelectableText(
5757
text,
5858
style: TextStyle(fontFamily: 'monospace', fontSize: 14),
5959
),
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import 'dart:io';
2+
3+
import 'package:cake_wallet/src/screens/base_page.dart';
4+
import 'package:cake_wallet/utils/share_util.dart';
5+
import 'package:flutter/material.dart';
6+
import 'package:flutter_mobx/flutter_mobx.dart';
7+
import 'package:cake_wallet/view_model/dev/print_verbose_view_model.dart';
8+
9+
class PrintVerboseLogsPage extends BasePage {
10+
11+
final PrintVerboseViewModel viewModel = PrintVerboseViewModel();
12+
13+
PrintVerboseLogsPage();
14+
15+
@override
16+
String? get title => "[dev] print verbose logs";
17+
18+
@override
19+
Widget? trailing(BuildContext context) {
20+
return IconButton(
21+
icon: Icon(Icons.download, size: 20),
22+
onPressed: () => _shareLog(context),
23+
);
24+
}
25+
26+
Future<void> _shareLog(BuildContext context) async {
27+
if (viewModel.logFilePath == null) {
28+
return;
29+
}
30+
final file = File(viewModel.logFilePath!);
31+
if (await file.exists()) {
32+
await ShareUtil.shareFile(
33+
filePath: file.path,
34+
fileName: 'Print verbose log',
35+
context: context,
36+
);
37+
}
38+
}
39+
40+
Future<String?> _loadLog() async {
41+
if (viewModel.logFilePath == null) {
42+
return null;
43+
}
44+
final file = File(viewModel.logFilePath!);
45+
if (!await file.exists()) return null;
46+
return await file.readAsString();
47+
}
48+
49+
List<String> _logFiles() {
50+
final dir = Directory(viewModel.logDirecoryPath);
51+
if (!dir.existsSync()) {
52+
return [];
53+
}
54+
return dir.listSync().map((e) => e.path).toList();
55+
}
56+
57+
Widget logSelector() {
58+
return ListView.builder(
59+
itemCount: _logFiles().length,
60+
itemBuilder: (context, index) {
61+
return ListTile(
62+
title: Text(_logFiles()[index]),
63+
onTap: () {
64+
viewModel.logFilePath = _logFiles()[index];
65+
},
66+
);
67+
},
68+
);
69+
}
70+
71+
@override
72+
Widget body(BuildContext context) {
73+
return Observer(builder: (context) {
74+
return actualBody(context);
75+
});
76+
}
77+
78+
Widget actualBody(BuildContext context) {
79+
if (viewModel.logFilePath == null) {
80+
return logSelector();
81+
}
82+
return Scaffold(
83+
body: FutureBuilder<String?>(
84+
future: _loadLog(),
85+
builder: (context, snap) {
86+
if (snap.connectionState != ConnectionState.done) {
87+
return Center(child: CircularProgressIndicator());
88+
}
89+
final text = snap.data;
90+
if (text == null || text.isEmpty) {
91+
return Center(child: Text('No log records found.'));
92+
}
93+
return SingleChildScrollView(
94+
padding: EdgeInsets.all(16),
95+
child: SelectableText(
96+
text,
97+
style: TextStyle(fontFamily: 'monospace', fontSize: 8),
98+
),
99+
);
100+
},
101+
),
102+
);
103+
}
104+
}

lib/src/screens/settings/other_settings_page.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ class OtherSettingsPage extends BasePage {
9999
handler: (BuildContext context) =>
100100
Navigator.of(context).pushNamed(Routes.devHashChangeLogs),
101101
),
102+
if (FeatureFlag.hasDevOptions)
103+
SettingsCellWithArrow(
104+
title: '[dev] print verbose',
105+
handler: (BuildContext context) =>
106+
Navigator.of(context).pushNamed(Routes.devPrintVerbose),
107+
),
102108
Spacer(),
103109
SettingsVersionCell(
104110
title: S.of(context).version(_otherSettingsViewModel.currentVersion)),

lib/view_model/dashboard/dashboard_view_model.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ abstract class DashboardViewModelBase with Store {
984984

985985
Future<List<String>> checkForHavenWallets() async {
986986
final walletInfoSource = await CakeHive.openBox<WalletInfo>(WalletInfo.boxName);
987+
printV("WalletInfoSource length (checkForHavenWallets): ${walletInfoSource.length}");
987988
return walletInfoSource.values
988989
.where((element) => element.type == WalletType.haven)
989990
.map((e) => e.name)
@@ -998,7 +999,8 @@ abstract class DashboardViewModelBase with Store {
998999
final vulnerableSeeds = vulnerableSeedsString.split("\n");
9991000

10001001
final walletInfoSource = await CakeHive.openBox<WalletInfo>(WalletInfo.boxName);
1001-
1002+
printV("WalletInfoSource length (checkAffectedWallets): ${walletInfoSource.length}");
1003+
10021004
List<String> affectedWallets = [];
10031005
for (var walletInfo in walletInfoSource.values) {
10041006
if (walletInfo.type == WalletType.bitcoin) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:cw_core/utils/print_verbose.dart';
2+
import 'package:mobx/mobx.dart';
3+
import 'package:path/path.dart' as p;
4+
5+
part 'print_verbose_view_model.g.dart';
6+
class PrintVerboseViewModel = PrintVerboseViewModelBase with _$PrintVerboseViewModel;
7+
8+
abstract class PrintVerboseViewModelBase with Store {
9+
PrintVerboseViewModelBase();
10+
11+
@observable
12+
String? logFilePath;
13+
14+
final logDirecoryPath = p.dirname(printVLogFilePath!);
15+
}

0 commit comments

Comments
 (0)