Skip to content

Commit

Permalink
bitwindow: navigate to logs correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
octobocto committed Jan 30, 2025
1 parent b6ab749 commit 188c6f0
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 67 deletions.
2 changes: 1 addition & 1 deletion clients/Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ gen-router:
if [ -d "$dir" ]; then
(cd "$dir" && dart run build_runner build --delete-conflicting-outputs)
fi
done
done
7 changes: 6 additions & 1 deletion clients/bitwindow/Justfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
fix:
dart fix --apply

format:
find . -name "*.dart" -not -path "./lib/gen/*" | xargs dart format -l 120
find . -name "*.dart" -not -path "./lib/gen/*" | xargs dart format -l 120

lint: format fix

gen-enforcer:
buf generate --template buf.gen.yaml https://github.com/LayerTwo-Labs/cusf_sidechain_proto.git
Expand Down
8 changes: 8 additions & 0 deletions clients/bitwindow/lib/pages/root_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ class _StatusBarState extends State<StatusBar> {
rpc: bitwindow,
name: 'BitWindow',
),
navigateToLogs: (name, logPath) {
GetIt.I.get<AppRouter>().push(
LogRoute(
name: name,
logPath: logPath,
),
);
},
mainchainInfo: true,
endWidgets: [
Tooltip(
Expand Down
4 changes: 2 additions & 2 deletions clients/bitwindow/lib/routing/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:bitwindow/pages/sidechain_activation_management_page.dart';
import 'package:bitwindow/pages/sidechain_proposal_page.dart';
import 'package:bitwindow/pages/sidechains_page.dart';
import 'package:bitwindow/pages/wallet_page.dart';
import 'package:sail_ui/sail_ui.dart';
import 'package:sail_ui/pages/router.gr.dart';

part 'router.gr.dart';

Expand Down Expand Up @@ -58,7 +58,7 @@ class AppRouter extends RootStackRouter {
],
),
AutoRoute(
page: SailLogRoute.page,
page: LogRoute.page,
),
AutoRoute(
page: ShuttingDownRoute.page,
Expand Down
2 changes: 1 addition & 1 deletion clients/launcher/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Future<void> initDependencies(Logger log) async {
host: '127.0.0.1',
port: binary.port,
binary: binary,
logPath: path.join(binary.datadir(), 'bitwindow.log'),
logPath: path.join(binary.datadir(), 'debug.log'),
);
GetIt.I.registerSingleton<BitwindowRPC>(bitwindow);

Expand Down
12 changes: 8 additions & 4 deletions clients/launcher/lib/routing/router.gr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ part of 'router.dart';
/// generated route for
/// [OverviewPage]
class OverviewRoute extends PageRouteInfo<void> {
const OverviewRoute({List<PageRouteInfo>? children}) : super(OverviewRoute.name, initialChildren: children);
const OverviewRoute({List<PageRouteInfo>? children})
: super(OverviewRoute.name, initialChildren: children);

static const String name = 'OverviewRoute';

Expand All @@ -28,7 +29,8 @@ class OverviewRoute extends PageRouteInfo<void> {
/// generated route for
/// [RootPage]
class RootRoute extends PageRouteInfo<void> {
const RootRoute({List<PageRouteInfo>? children}) : super(RootRoute.name, initialChildren: children);
const RootRoute({List<PageRouteInfo>? children})
: super(RootRoute.name, initialChildren: children);

static const String name = 'RootRoute';

Expand All @@ -43,7 +45,8 @@ class RootRoute extends PageRouteInfo<void> {
/// generated route for
/// [SettingsPage]
class SettingsRoute extends PageRouteInfo<void> {
const SettingsRoute({List<PageRouteInfo>? children}) : super(SettingsRoute.name, initialChildren: children);
const SettingsRoute({List<PageRouteInfo>? children})
: super(SettingsRoute.name, initialChildren: children);

static const String name = 'SettingsRoute';

Expand All @@ -58,7 +61,8 @@ class SettingsRoute extends PageRouteInfo<void> {
/// generated route for
/// [ToolsPage]
class ToolsRoute extends PageRouteInfo<void> {
const ToolsRoute({List<PageRouteInfo>? children}) : super(ToolsRoute.name, initialChildren: children);
const ToolsRoute({List<PageRouteInfo>? children})
: super(ToolsRoute.name, initialChildren: children);

static const String name = 'ToolsRoute';

Expand Down
13 changes: 8 additions & 5 deletions clients/sail_ui/lib/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';

LogPrinter _printer() {
LogPrinter _consolePrinter() {
if (!kReleaseMode) {
return PrettyPrinter(
dateTimeFormat: DateTimeFormat.onlyTimeAndSinceStart,
printEmojis: false,
);
}

return LogfmtPrinter();
}

Future<LogOutput> _logoutput(File? logFile) async {
List<LogOutput> outputs = [];

if (logFile != null) {
outputs.add(FileOutput(file: logFile));
outputs.add(
FileOutput(
file: logFile,
overrideExisting: true,
),
);
}

if (logFile == null || kDebugMode) {
// always print to console in debug mode
outputs.add(ConsoleOutput());
}

Expand All @@ -32,6 +35,6 @@ Future<LogOutput> _logoutput(File? logFile) async {
Future<Logger> logger(bool fileLog, bool consoleLog, File? logFile) async => Logger(
level: Level.debug,
filter: ProductionFilter(),
printer: _printer(),
printer: logFile != null ? LogfmtPrinter() : _consolePrinter(), // Use LogfmtPrinter for file output
output: await _logoutput(logFile),
);
56 changes: 39 additions & 17 deletions clients/sail_ui/lib/pages/log_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import 'dart:io';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get_it/get_it.dart';
import 'package:logger/logger.dart';
import 'package:sail_ui/sail_ui.dart';
import 'package:stacked/stacked.dart';

@RoutePage()
class SailLogPage extends StatelessWidget {
class LogPage extends StatelessWidget {
final String name;
final String logPath;

const SailLogPage({
super.key,
const LogPage({
required this.name,
required this.logPath,
super.key,
});

@override
Expand All @@ -26,7 +28,7 @@ class SailLogPage extends StatelessWidget {
onViewModelReady: (model) => model.init(),
builder: (context, model, child) {
return SailPage(
widgetTitle: SailText.primary15('$name logs'),
widgetTitle: SailText.primary20('$name logs'),
body: KeyboardListener(
focusNode: model.focusNode,
onKeyEvent: (KeyEvent event) {
Expand Down Expand Up @@ -72,6 +74,8 @@ class SailLogPage extends StatelessWidget {
}

class LogPageViewModel extends BaseViewModel {
final Logger log = GetIt.I.get<Logger>();

final String logPath;
final List<String> _logLines = [];
List<String> get logLines => _logLines;
Expand Down Expand Up @@ -116,6 +120,11 @@ class LogPageViewModel extends BaseViewModel {
notifyListeners();
}

String _stripAnsiCodes(String text) {
// This regex matches ANSI escape codes
return text.replaceAll(RegExp(r'\x1B\[[0-9;]*[a-zA-Z]'), '');
}

Stream<String> watchLogFile(String logFilePath) async* {
final file = File(logFilePath);
if (!await file.exists()) {
Expand All @@ -124,23 +133,19 @@ class LogPageViewModel extends BaseViewModel {
}

_raf = await file.open(mode: FileMode.read);

// Read initial lines
final initialLines = await _readLastNLines(_raf!, 100);
for (var line in initialLines) {
yield line;
}

var doneInitialScroll = false;

while (true) {
// Watch for file changes
await for (final _ in file.watch(events: FileSystemEvent.modify)) {
final newLines = await _readNewLines(_raf!);
for (var line in newLines) {
yield line;
}
await Future.delayed(const Duration(milliseconds: 100));
if (!doneInitialScroll) {
_scrollToBottom(overrideBottom: true);
doneInitialScroll = true;
}
}
}

Expand All @@ -155,8 +160,17 @@ class LogPageViewModel extends BaseViewModel {
while (lines.length < n && start >= 0) {
raf.setPositionSync(start);
final buffer = raf.readSync(end - start);
final chunk = utf8.decode(buffer.toList()).split('\n').reversed;
lines.insertAll(0, chunk);
try {
final chunk = utf8
.decode(buffer.toList(), allowMalformed: true)
.split('\n')
.map(_stripAnsiCodes) // Strip ANSI codes from each line
.toList() // Convert to List before reversing
.reversed;
lines.insertAll(0, chunk);
} catch (e) {
log.e('Error decoding log file chunk: $e');
}
end = start;
start -= bufferSize;
if (start < 0) start = 0;
Expand All @@ -172,8 +186,17 @@ class LogPageViewModel extends BaseViewModel {

if (fileLength > currentPosition) {
final buffer = await raf.read(fileLength - currentPosition);
final chunk = utf8.decode(buffer);
newLines.addAll(chunk.split('\n').where((line) => line.isNotEmpty));
try {
final chunk = utf8.decode(buffer, allowMalformed: true);
newLines.addAll(
chunk
.split('\n')
.map(_stripAnsiCodes) // Strip ANSI codes from each line
.where((line) => line.isNotEmpty),
);
} catch (e) {
log.e('Error decoding new log lines: $e');
}
await raf.setPosition(fileLength);
}

Expand All @@ -193,7 +216,6 @@ class LogPageViewModel extends BaseViewModel {
@override
void dispose() {
_logSubscription?.cancel();
_raf?.close();
scrollController.dispose();
focusNode.dispose();
super.dispose();
Expand Down
2 changes: 1 addition & 1 deletion clients/sail_ui/lib/pages/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class AppRouter extends RootStackRouter {
List<AutoRoute> get routes => [
// HomeScreen is generated as HomeRoute because
// of the replaceInRouteName property
AutoRoute(page: SailLogRoute.page),
AutoRoute(page: LogRoute.page),
AutoRoute(page: ShuttingDownRoute.page),
];

Expand Down
38 changes: 17 additions & 21 deletions clients/sail_ui/lib/pages/router.gr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,50 @@ import 'package:sail_ui/pages/log_page.dart' as _i1;
import 'package:sail_ui/pages/shutdown_page.dart' as _i2;

/// generated route for
/// [_i1.SailLogPage]
class SailLogRoute extends _i3.PageRouteInfo<SailLogRouteArgs> {
SailLogRoute({
_i4.Key? key,
/// [_i1.LogPage]
class LogRoute extends _i3.PageRouteInfo<LogRouteArgs> {
LogRoute({
required String name,
required String logPath,
_i4.Key? key,
List<_i3.PageRouteInfo>? children,
}) : super(
SailLogRoute.name,
args: SailLogRouteArgs(key: key, name: name, logPath: logPath),
initialChildren: children,
);
LogRoute.name,
args: LogRouteArgs(name: name, logPath: logPath, key: key),
initialChildren: children,
);

static const String name = 'SailLogRoute';
static const String name = 'LogRoute';

static _i3.PageInfo page = _i3.PageInfo(
name,
builder: (data) {
final args = data.argsAs<SailLogRouteArgs>();
return _i1.SailLogPage(
key: args.key,
name: args.name,
logPath: args.logPath,
);
final args = data.argsAs<LogRouteArgs>();
return _i1.LogPage(name: args.name, logPath: args.logPath, key: args.key);
},
);
}

class SailLogRouteArgs {
const SailLogRouteArgs({this.key, required this.name, required this.logPath});

final _i4.Key? key;
class LogRouteArgs {
const LogRouteArgs({required this.name, required this.logPath, this.key});

final String name;

final String logPath;

final _i4.Key? key;

@override
String toString() {
return 'SailLogRouteArgs{key: $key, name: $name, logPath: $logPath}';
return 'LogRouteArgs{name: $name, logPath: $logPath, key: $key}';
}
}

/// generated route for
/// [_i2.ShuttingDownPage]
class ShuttingDownRoute extends _i3.PageRouteInfo<void> {
const ShuttingDownRoute({List<_i3.PageRouteInfo>? children})
: super(ShuttingDownRoute.name, initialChildren: children);
: super(ShuttingDownRoute.name, initialChildren: children);

static const String name = 'ShuttingDownRoute';

Expand Down
4 changes: 3 additions & 1 deletion clients/sail_ui/lib/sail_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export './extensions/build_context.dart';
export './extensions/formatting.dart';
export './logger.dart';
export './mocks/mocks.dart';
export './pages/router.gr.dart';
export './providers/blockchain_provider.dart';
export './providers/process_provider.dart';
export './sail_app.dart';
Expand Down Expand Up @@ -69,3 +68,6 @@ export './widgets/nav/nav_components.dart';
export './widgets/optional_builder.dart';
export './widgets/spacing.dart';
export './widgets/static/static_field.dart';
export 'pages/log_page.dart';
export 'pages/router.gr.dart';
export 'pages/shutdown_page.dart';
Loading

0 comments on commit 188c6f0

Please sign in to comment.