Skip to content

Commit

Permalink
refactor: refactored logger
Browse files Browse the repository at this point in the history
  • Loading branch information
jhomlala committed Jul 3, 2024
1 parent 111cb8a commit b2a7df2
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 88 deletions.
1 change: 1 addition & 0 deletions packages/alice/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* [BREAKING_CHANGE] Replaced configuration parameter of Alice with `AliceConfiguration`.
* Fixed issue with invalid count of calls in notification.
* Added payload to notification.
* General refactor of code base.
* Updated documentation.

# 1.0.0-dev.9
Expand Down
1 change: 1 addition & 0 deletions packages/alice/lib/core/alice_adapter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import 'package:alice/core/alice_core.dart';
mixin AliceAdapter {
late final AliceCore aliceCore;

/// Injects [AliceCore] into adapter.
void injectCore(AliceCore aliceCore) => this.aliceCore = aliceCore;
}
73 changes: 22 additions & 51 deletions packages/alice/lib/core/alice_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,48 @@ import 'dart:io' show Process, ProcessResult;

import 'package:alice/helper/operating_system.dart';
import 'package:alice/model/alice_log.dart';
import 'package:alice/utils/num_comparison.dart';
import 'package:flutter/foundation.dart';
import 'package:rxdart/rxdart.dart';

/// Logger used to handle logs from application.
class AliceLogger {
AliceLogger({int? maximumSize = 1000}) : _maximumSize = maximumSize;
/// Maximum logs size. If null, logs will be not rotated.
final int? maximumSize;

final ValueNotifier<List<AliceLog>> _logs = ValueNotifier<List<AliceLog>>([]);
/// Subject which keeps logs.
final BehaviorSubject<List<AliceLog>> _logsSubject =
BehaviorSubject.seeded([]);

ValueListenable<List<AliceLog>> get listenable => _logs;
AliceLogger({required this.maximumSize});

List<AliceLog> get logs => listenable.value;
/// Getter of stream of logs
Stream<List<AliceLog>> get logsStream => _logsSubject.stream;

int? _maximumSize;

/// The maximum number of logs to store or `null` for unlimited storage.
///
/// If more logs arrive, the oldest ones (based on their [
/// AliceLog.timestamp]) will be removed.
int? get maximumSize => _maximumSize;

set maximumSize(int? value) {
_maximumSize = maximumSize;

if (value != null && logs.length > value) {
_logs.value = logs.sublist(logs.length - value, logs.length);
}
}
/// Getter of all logs
List<AliceLog> get logs => _logsSubject.value;

/// Adds all logs.
void addAll(List<AliceLog> logs) {
for (var log in logs) {
add(log);
}
}

/// Add one log. It sorts logs after adding new element. If [maximumSize] is
/// set and max size is reached, first log will be deleted.
void add(AliceLog log) {
late final int index;
if (logs.isEmpty || !log.timestamp.isBefore(logs.last.timestamp)) {
// Quick path as new logs are usually more recent.
index = logs.length;
} else {
// Binary search to find the insertion index.
int min = 0;
int max = logs.length;
while (min < max) {
final int mid = min + ((max - min) >> 1);
final AliceLog item = logs[mid];
if (log.timestamp.isBefore(item.timestamp)) {
max = mid;
} else {
min = mid + 1;
}
}
assert(min == max, '');
index = min;
final values = _logsSubject.value;
final count = values.length;
if (maximumSize != null && count >= maximumSize!) {
values.removeAt(0);
}

int startIndex = 0;
if (maximumSize != null && logs.length.gte(maximumSize)) {
if (index == 0) return;
startIndex = logs.length - maximumSize! + 1;
}
_logs.value = <AliceLog>[
...logs.sublist(startIndex, index),
log,
...logs.sublist(index, logs.length),
];
values.add(log);
values.sort((log1, log2) => log1.timestamp.compareTo(log2.timestamp));
_logsSubject.add(values);
}

/// Clears all logs.
void clearLogs() => _logs.value.clear();
void clearLogs() => _logsSubject.add([]);

/// Returns raw logs from Android via ADB.
Future<String> getAndroidRawLogs() async {
Expand Down
12 changes: 2 additions & 10 deletions packages/alice/lib/model/alice_http_call.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ import 'package:equatable/equatable.dart';

/// Definition of http calls data holder.
class AliceHttpCall with EquatableMixin {
AliceHttpCall(this.id) {
loading = true;
createdTime = DateTime.now();
}
AliceHttpCall(this.id);

final int id;
late DateTime createdTime;
final DateTime createdTime = DateTime.now();
String client = '';
bool loading = true;
bool secure = false;
Expand All @@ -25,11 +22,6 @@ class AliceHttpCall with EquatableMixin {
AliceHttpResponse? response;
AliceHttpError? error;

void setResponse(AliceHttpResponse response) {
this.response = response;
loading = false;
}

@override
List<Object?> get props => [
id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import 'package:flutter/services.dart';
/// Widget which renders log list for calls list page.
class AliceLogListWidget extends StatefulWidget {
const AliceLogListWidget({
required this.logsListenable,
required this.logsStream,
required this.scrollController,
required this.emptyWidget,
super.key,
});

final ValueListenable<List<AliceLog>> logsListenable;
final Stream<List<AliceLog>>? logsStream;
final ScrollController? scrollController;
final Widget emptyWidget;

Expand All @@ -32,9 +32,10 @@ class _AliceLogListWidgetState extends State<AliceLogListWidget> {

@override
Widget build(BuildContext context) {
return ValueListenableBuilder<List<AliceLog>>(
valueListenable: widget.logsListenable,
builder: (_, List<AliceLog> logs, __) {
return StreamBuilder<List<AliceLog>>(
stream: widget.logsStream,
builder: (context, snapshot) {
final logs = snapshot.data ?? [];
if (logs.isEmpty) {
return widget.emptyWidget;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AliceLogsScreen extends StatelessWidget {
emptyWidget: const AliceEmptyLogsWidget(),
)
: AliceLogListWidget(
logsListenable: aliceLogger!.listenable,
logsStream: aliceLogger?.logsStream,
scrollController: scrollController,
emptyWidget: const AliceEmptyLogsWidget(),
)
Expand Down
15 changes: 0 additions & 15 deletions packages/alice/test/core/alice_logger_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,5 @@ void main() {

expect(aliceLogger.logs.isEmpty, true);
});

test("should set maximum size", () {
final logs = [
AliceLog(message: "test"),
AliceLog(message: "test2"),
];

aliceLogger.addAll(logs);

expect(aliceLogger.logs.length, 2);

aliceLogger.maximumSize = 1;

expect(aliceLogger.logs.length, 1);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ class CachedAliceHttpCall implements AliceHttpCall {
@internal
final ToOne<CachedAliceHttpError> errorRel = ToOne<CachedAliceHttpError>();

@override
void setResponse(AliceHttpResponse response) {
this.response = response;
loading = false;
}

@override
List<Object?> get props => [
id,
Expand Down

0 comments on commit b2a7df2

Please sign in to comment.