From 502623ad31f7726b4ddb13ccc9dc2af579301146 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Wed, 12 Jun 2019 13:43:14 +0200 Subject: [PATCH] Add debug logs --- lib/src/Client.dart | 5 ++++- lib/src/Connection.dart | 31 ++++++++++++++++++------------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/src/Client.dart b/lib/src/Client.dart index 583244d..5cdc7fa 100644 --- a/lib/src/Client.dart +++ b/lib/src/Client.dart @@ -38,7 +38,7 @@ class Client { /// Optional persistent store for all data. Store store; - Client(this.clientName) { + Client(this.clientName, {this.debug = false}) { connection = Connection(this); if (this.clientName != "testclient") store = Store(this); @@ -47,6 +47,9 @@ class Client { }); } + /// Whether debug prints should be displayed. + final bool debug; + /// The required name for this client. final String clientName; diff --git a/lib/src/Connection.dart b/lib/src/Connection.dart index 6cf57c5..a2f5c20 100644 --- a/lib/src/Connection.dart +++ b/lib/src/Connection.dart @@ -65,7 +65,7 @@ class Connection { /// Outside of rooms there are account updates like account_data or presences. final StreamController onUserEvent = - new StreamController.broadcast(); + new StreamController.broadcast(); /// Called when the login state e.g. user gets logged out. final StreamController onLoginStateChanged = @@ -187,6 +187,8 @@ class Connection { if (client.isLogged()) headers["Authorization"] = "Bearer ${client.accessToken}"; + if (client.debug) print("[REQUEST $type] Action: $action, Data: $data"); + http.Response resp; try { switch (type) { @@ -212,12 +214,15 @@ class Connection { break; } } on TimeoutException catch (_) { - return ErrorResponse( - error: "No connection possible...", errcode: "TIMEOUT", request: resp?.request); + error: "No connection possible...", + errcode: "TIMEOUT", + request: resp?.request); } catch (e) { return ErrorResponse( - error: "No connection possible...", errcode: "NO_CONNECTION", request: resp.request); + error: "No connection possible...", + errcode: "NO_CONNECTION", + request: resp.request); } Map jsonResp; @@ -225,13 +230,17 @@ class Connection { jsonResp = jsonDecode(resp.body) as Map; } catch (e) { return ErrorResponse( - error: "No connection possible...", errcode: "MALFORMED", request: resp.request); + error: "No connection possible...", + errcode: "MALFORMED", + request: resp.request); } if (jsonResp.containsKey("errcode") && jsonResp["errcode"] is String) { if (jsonResp["errcode"] == "M_UNKNOWN_TOKEN") clear(); return ErrorResponse.fromJson(jsonResp, resp.request); } + if (client.debug) print("[RESPONSE] ${jsonResp.toString()}"); + return jsonResp; } @@ -339,8 +348,7 @@ class Connection { if (room["invite_state"] is Map && room["invite_state"]["events"] is List) - _handleRoomEvents( - id, room["invite_state"]["events"], "invite_state"); + _handleRoomEvents(id, room["invite_state"]["events"], "invite_state"); if (room["timeline"] is Map && room["timeline"]["events"] is List) @@ -352,8 +360,7 @@ class Connection { if (room["account_data"] is Map && room["account_data"]["events"] is List) - _handleRoomEvents( - id, room["account_data"]["events"], "account_data"); + _handleRoomEvents(id, room["account_data"]["events"], "account_data"); }); } @@ -391,8 +398,7 @@ class Connection { } } - void _handleRoomEvents( - String chat_id, List events, String type) { + void _handleRoomEvents(String chat_id, List events, String type) { for (num i = 0; i < events.length; i++) { _handleEvent(events[i], chat_id, type); } @@ -411,8 +417,7 @@ class Connection { } } - void _handleEvent( - Map event, String roomID, String type) { + void _handleEvent(Map event, String roomID, String type) { if (event["type"] is String && event["content"] is dynamic) { EventUpdate update = EventUpdate( eventType: event["type"],