-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
69 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
lib/modules/admin/presentation/resources/logs_resource.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'dart:async'; | ||
import 'dart:convert'; | ||
|
||
import 'package:echidna_dto/echidna_dto.dart'; | ||
import 'package:logging/logging.dart'; | ||
import 'package:shelf_modular/shelf_modular.dart'; | ||
|
||
/// Logs endpoint that broadcasts logs to connected clients. | ||
class LogsResource extends WebSocketResource { | ||
final List<ServerLog> _logs = []; | ||
|
||
/// Logs endpoint that broadcasts logs to connected clients. | ||
LogsResource() { | ||
Logger.root.onRecord.listen(listener); | ||
|
||
Timer.periodic( | ||
const Duration(hours: 1), | ||
(_) { | ||
_logs.removeWhere((log) => log.time.isBefore(DateTime.now().subtract(const Duration(days: 1)))); | ||
}, | ||
); | ||
} | ||
|
||
@override | ||
void connect(WebSocket socket) { | ||
socket.sink.add(jsonEncode(_logs)); | ||
} | ||
|
||
@override | ||
void disconnect(WebSocket socket) {} | ||
|
||
@override | ||
void onMessage(dynamic data, WebSocket socket) {} | ||
|
||
void listener(LogRecord record) { | ||
_logs.add(ServerLog.fromLogRecord(record)); | ||
|
||
broadcast(jsonEncode(_logs)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters