Commit 0a97273b authored by Leon Tappe's avatar Leon Tappe 🔥
Browse files

refactor logging

parent 6d117e6b
......@@ -7,6 +7,7 @@ import 'package:logging/logging.dart';
import '../../admins.dart';
import '../models/enums.dart';
import '../network_exception.dart';
import '../util.dart';
class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
final Logger _log = Logger('AdminsBloc');
......@@ -117,7 +118,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
'X-Api-Key': _token,
},
).then((http.Response response) {
_log.finer('_deleteAdmin: ${response.statusCode}');
_log.finer(formatResponse(response));
if (response.statusCode == 205) {
return;
} else {
......@@ -135,7 +136,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
'Accept': 'application/json',
},
).then((http.Response response) {
_log.finer('_getAdmins: ${response.body}');
_log.finer(formatResponse(response));
if (response.statusCode == 200) {
_admins = List.from((json.decode(response.body) as Iterable)
.map<Admin>((dynamic admin) => Admin.fromMap(admin as Map<String, dynamic>)));
......@@ -170,7 +171,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
}),
)
.then((http.Response response) {
_log.finer('_postAdmin: ${response.body}');
_log.finer(formatResponse(response));
if (response.statusCode == 204) {
return;
} else {
......@@ -192,7 +193,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
body: json.encode(password),
)
.then((http.Response response) {
_log.finer('_setAdminPassword: ${response.body}');
_log.finer(formatResponse(response));
if (response.statusCode == 204) {
return;
} else {
......@@ -256,7 +257,7 @@ class AdminsBloc extends Bloc<AdminsEvent, AdminsState> {
}),
)
.then((http.Response response) {
_log.finer('_updateAdmin: ${response.body}');
_log.finer(formatResponse(response));
if (response.statusCode == 205) {
return;
} else {
......
......@@ -5,17 +5,18 @@ import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import '../network_exception.dart';
import '../util.dart';
import 'auth_events.dart';
import 'auth_state.dart';
class AuthBloc extends Bloc<AuthEvent, AuthState> {
final log = Logger('AuthBloc');
final _log = Logger('AuthBloc');
final http.Client client;
String _token;
AuthBloc(this.client) : super(AuthState.unauthorized()) {
log.fine('$this started');
_log.fine('$this started');
on<Login>((Login event, Emitter<AuthState> emit) async {
try {
......@@ -39,7 +40,7 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
@override
Future<void> close() {
log.fine('closing $this');
_log.fine('closing $this');
return super.close();
}
......@@ -49,24 +50,23 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
@override
void onTransition(Transition<AuthEvent, AuthState> transition) {
log.fine('State: ${transition.nextState}');
_log.fine('State: ${transition.nextState}');
super.onTransition(transition);
}
void tokenLogin(String token) => add(TokenLogin(token));
Future<void> _postLogin(String user, String password) async {
log.finer('_postLogin: $user, $password');
_log.finer('_postLogin: $user, $password');
return await client.post(Uri.parse('https://astaprint.upb.de/api/v1/admin/tokens'), headers: {
'Authorization': 'Basic ' + base64.encode(utf8.encode('$user:$password')),
}).then(
(http.Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 200) {
log.finer('_postLogin: ${response.body}');
_token = json.decode(response.body) as String;
return;
} else {
log.severe('_postLogin: ${response.body}');
throw NetworkException('status code other than 200 received', response.statusCode);
}
},
......
......@@ -47,6 +47,11 @@ abstract class ResultState<T> extends CommonState {
...super.toMap,
};
Map<String, dynamic> get toShortMap => {
'value': value is Iterable ? (value as Iterable).last : value,
...super.toMap,
};
@override
String toString() => '[ResultState<$T> $toMap]';
}
......@@ -7,37 +7,38 @@ import 'package:logging/logging.dart';
import '../../exceptions.dart';
import '../models/dispatcher_task.dart';
import '../util.dart';
import 'dispatcher_events.dart';
import 'dispatcher_state.dart';
class DispatcherBloc extends Bloc<DispatcherEvent, DispatcherState> {
final _l = Logger('DispatcherBloc');
final _log = Logger('DispatcherBloc');
final Client _client;
String _token;
DispatcherBloc(this._client) : super(DispatcherState.init()) {
_l.fine('$this started');
_log.fine('$this started');
on<GetTasks>((GetTasks event, Emitter<DispatcherState> emit) async {
try {
emit(DispatcherState.busy());
emit(DispatcherState.result(await _getQueue()));
} on NetworkException catch (e) {
_l.severe(e.toString());
_log.severe(e.toString());
emit(DispatcherState.exception(e));
}
});
on<InitDispatcher>((InitDispatcher event, Emitter<DispatcherState> emit) async {
_token = event.token;
_l.fine('token initialized');
_log.fine('token initialized');
});
}
@override
Future<void> close() {
_l.fine('closing $this');
_log.fine('closing $this');
return super.close();
}
......@@ -47,12 +48,12 @@ class DispatcherBloc extends Bloc<DispatcherEvent, DispatcherState> {
@override
void onTransition(Transition<DispatcherEvent, DispatcherState> transition) {
_l.fine('Transition from ${transition.currentState} to ${transition.nextState}');
_log.fine('Transition from ${transition.currentState} to ${transition.nextState}');
super.onTransition(transition);
}
Future<List<DispatcherTask>> _getQueue() async {
_l.finer('_getQueue');
_log.finer('_getQueue');
return await _client.get(
Uri.parse('https://astaprint.upb.de/api/v1/admin/jobs/queue'),
headers: {
......@@ -60,7 +61,7 @@ class DispatcherBloc extends Bloc<DispatcherEvent, DispatcherState> {
},
).then(
(Response response) {
_l.finer(response.body);
_log.finer(formatResponse(response));
if (response.statusCode == 200) {
final body = json.decode(utf8.decode(response.bodyBytes)) as Iterable;
return List.from(body.map<DispatcherTask>(
......
......@@ -7,11 +7,12 @@ import 'package:logging/logging.dart';
import '../models/enums.dart';
import '../models/transaction.dart';
import '../network_exception.dart';
import '../util.dart';
import 'journal_events.dart';
import 'journal_state.dart';
class JournalBloc extends Bloc<JournalEvent, JournalState> {
final log = Logger('JournalBloc');
final _log = Logger('JournalBloc');
final Client client;
int chunkSize = 200;
......@@ -22,7 +23,7 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
List<Transaction> _journalFiltered = [];
JournalBloc(this.client) : super(JournalState.init()) {
log.fine('$this started');
_log.fine('$this started');
on<InitJournal>((InitJournal event, Emitter<JournalState> emit) async {
_token = event.token;
......@@ -83,7 +84,7 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
@override
Future<void> close() {
log.fine('closing $this');
_log.fine('closing $this');
return super.close();
}
......@@ -101,7 +102,7 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
@override
void onTransition(Transition<JournalEvent, JournalState> transition) {
log.fine('State: ${transition.nextState}');
_log.fine('State: ${transition.nextState}');
super.onTransition(transition);
}
......@@ -161,7 +162,7 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
}
Future<void> _getJournal({int limit = 50, int offset = 0}) async {
log.finer('_getJournal:');
_log.finer('_getJournal:');
return await client.get(
Uri.parse(
'https://astaprint.upb.de/api/v1/admin/journal?limit=${limit?.toString()}&offset=${offset?.toString()}'),
......@@ -171,13 +172,12 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
},
).then(
(Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 200) {
log.finer('_getJournal: ${response.body}');
_journal = List<Transaction>.from((json.decode(response.body) as Iterable)
.map<Transaction>((dynamic job) => Transaction.fromMap(job as Map)));
return;
} else {
log.severe('_getJournal: ${response.body}');
throw NetworkException('status code other than 200 received', response.statusCode);
}
},
......@@ -185,7 +185,7 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
}
Future<void> _postJournal(int userId, int value, String annotation, bool withoutReceipt) async {
log.finer('_postJournal:');
_log.finer('_postJournal:');
return await client
.post(
Uri.parse('https://astaprint.upb.de/api/v1/admin/journal'),
......@@ -203,11 +203,10 @@ class JournalBloc extends Bloc<JournalEvent, JournalState> {
)
.then(
(Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 204) {
log.finer('_getJournal: ${response.body}');
return;
} else {
log.severe('_getJournal: ${response.body}');
throw NetworkException('status code other than 204 received', response.statusCode);
}
},
......
......@@ -28,5 +28,5 @@ class JournalState extends ResultState<List<Transaction>> {
JournalState(isResult: true, journal: journal);
@override
String toString() => '[JournalState $toMap]';
String toString() => '[JournalState $toShortMap]';
}
......@@ -6,11 +6,12 @@ import 'package:logging/logging.dart';
import '../models/journal_token.dart';
import '../network_exception.dart';
import '../util.dart';
import 'journal_token_events.dart';
import 'journal_token_state.dart';
class JournalTokenBloc extends Bloc<JournalTokenEvent, JournalTokenState> {
final log = Logger('JournalBloc');
final _log = Logger('JournalBloc');
final Client client;
int chunkSize = 200;
......@@ -20,7 +21,7 @@ class JournalTokenBloc extends Bloc<JournalTokenEvent, JournalTokenState> {
List<JournalToken> _journalTokens;
JournalTokenBloc(this.client) : super(JournalTokenState.init()) {
log.fine('$this started');
_log.fine('$this started');
on<InitJournalToken>((InitJournalToken event, Emitter<JournalTokenState> emit) async {
_token = event.token;
......@@ -52,7 +53,7 @@ class JournalTokenBloc extends Bloc<JournalTokenEvent, JournalTokenState> {
@override
Future<void> close() {
log.fine('closing $this');
_log.fine('closing $this');
return super.close();
}
......@@ -64,12 +65,12 @@ class JournalTokenBloc extends Bloc<JournalTokenEvent, JournalTokenState> {
@override
void onTransition(Transition<JournalTokenEvent, JournalTokenState> transition) {
log.fine('State: ${transition.nextState}');
_log.fine('State: ${transition.nextState}');
super.onTransition(transition);
}
Future<void> _getJournalTokens() async {
log.finer('_getJournalTokens:');
_log.finer('_getJournalTokens:');
return await client.get(
Uri.parse('https://astaprint.upb.de/api/v1/admin/journal/tokens'),
headers: {
......@@ -78,14 +79,13 @@ class JournalTokenBloc extends Bloc<JournalTokenEvent, JournalTokenState> {
},
).then(
(Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 200) {
log.finer('_getJournalTokens: ${response.body}');
_journalTokens = List<JournalToken>.from((json.decode(response.body) as Iterable)
.map<JournalToken>(
(dynamic job) => JournalToken.fromMap(job as Map<String, dynamic>)));
return;
} else {
log.severe('_getJournalTokens: ${response.body}');
throw NetworkException('status code other than 200 received', response.statusCode);
}
},
......@@ -93,7 +93,7 @@ class JournalTokenBloc extends Bloc<JournalTokenEvent, JournalTokenState> {
}
Future<void> _postJournalToken(int value) async {
log.finer('_postJournalToken:');
_log.finer('_postJournalToken:');
return await client.post(
Uri.parse('https://astaprint.upb.de/api/v1/admin/journal/tokens?value=$value'),
headers: {
......@@ -103,11 +103,10 @@ class JournalTokenBloc extends Bloc<JournalTokenEvent, JournalTokenState> {
},
).then(
(Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 200) {
log.finer('_getJournal: ${response.body}');
return;
} else {
log.severe('_getJournal: ${response.body}');
throw NetworkException('status code other than 204 received', response.statusCode);
}
},
......
......@@ -7,18 +7,19 @@ import 'package:logging/logging.dart';
import '../../exceptions.dart';
import '../models/print_queue_task.dart';
import '../util.dart';
import 'print_queue_events.dart';
import 'print_queue_state.dart';
class PrintQueueBloc extends Bloc<PrintQueueEvent, PrintQueueState> {
final log = Logger('PrintQueueBloc');
final _log = Logger('PrintQueueBloc');
final Client _client;
String _token;
int _printer;
PrintQueueBloc(this._client) : super(PrintQueueState.init()) {
log.fine('$this started');
_log.fine('$this started');
on<InitPrintQueue>((InitPrintQueue event, Emitter<PrintQueueState> emit) async {
_token = event.token;
......@@ -46,7 +47,7 @@ class PrintQueueBloc extends Bloc<PrintQueueEvent, PrintQueueState> {
@override
Future<void> close() {
log.fine('closing $this with device id $_printer');
_log.fine('closing $this with device id $_printer');
return super.close();
}
......@@ -56,26 +57,26 @@ class PrintQueueBloc extends Bloc<PrintQueueEvent, PrintQueueState> {
void onSetPrinter(int deviceId) {
_printer = deviceId;
log.fine('printer set to $_printer');
_log.fine('printer set to $_printer');
}
void onStart(String token) => add(InitPrintQueue(token));
@override
void onTransition(Transition<PrintQueueEvent, PrintQueueState> transition) {
log.fine('Transition from ${transition.currentState} to ${transition.nextState}');
_log.fine('Transition from ${transition.currentState} to ${transition.nextState}');
super.onTransition(transition);
}
Future<void> _deleteQueue(int deviceId) async {
log.finer('_deleteQueue $deviceId');
_log.finer('_deleteQueue $deviceId');
return await _client.delete(
Uri.parse('https://astaprint.upb.de/api/v1/admin/printers/$deviceId/queue'),
headers: {
'X-Api-Key': _token,
},
).then((Response response) {
log.finer('_deleteQueue: ${response.statusCode}');
_log.finer(formatResponse(response));
if (response.statusCode == 202 || response.statusCode == 404) {
return;
} else {
......@@ -85,7 +86,7 @@ class PrintQueueBloc extends Bloc<PrintQueueEvent, PrintQueueState> {
}
Future<List<PrintQueueTask>> _getQueue(int deviceId) async {
log.finer('_getQueue $deviceId');
_log.finer('_getQueue $deviceId');
return await _client.get(
Uri.parse('https://astaprint.upb.de/api/v1/admin/printers/$deviceId/queue'),
headers: {
......@@ -93,7 +94,7 @@ class PrintQueueBloc extends Bloc<PrintQueueEvent, PrintQueueState> {
'Accept': 'application/json',
},
).then((response) {
log.finer('_getQueue: ${response.statusCode} ${response.body}');
_log.finer(formatResponse(response));
if (response.statusCode == 200) {
if (response.body != null) {
return List.from(
......
......@@ -10,11 +10,12 @@ import '../models/printer.dart';
import '../models/printer_details.dart';
import '../models/printer_watch_options.dart';
import '../network_exception.dart';
import '../util.dart';
import 'printers_events.dart';
import 'printers_state.dart';
class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
final log = Logger('PrintersBloc');
final _log = Logger('PrintersBloc');
final Client client;
int chunkSize = 100;
......@@ -25,7 +26,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
List<Printer> _printersFiltered = [];
PrintersBloc(this.client) : super(PrintersState.init()) {
log.fine('$this started');
_log.fine('$this started');
on<InitPrinters>((InitPrinters event, Emitter<PrintersState> emit) async {
_token = event.token;
......@@ -104,7 +105,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
@override
Future<void> close() {
log.fine('closing $this');
_log.fine('closing $this');
return super.close();
}
......@@ -151,7 +152,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
@override
void onTransition(Transition<PrintersEvent, PrintersState> transition) {
log.fine('State: ${transition.nextState}');
_log.fine('State: ${transition.nextState}');
super.onTransition(transition);
}
......@@ -193,7 +194,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
Future<void> _addPrinter(int deviceId, PrinterDetails details) async {
log.finer('_addPrinter:');
_log.finer('_addPrinter:');
return await client
.post(
Uri.parse('https://astaprint.upb.de/api/v1/admin/printers'),
......@@ -205,11 +206,11 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
)
.then(
(Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 205) {
log.finer('_addPrinter: success');
return;
} else {
log.severe('_addPrinter: ${response.body}');
_log.severe('_addPrinter: ${response.body}');
throw NetworkException('status code other than 205 received', response.statusCode);
}
},
......@@ -217,18 +218,17 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
Future<void> _deletePrinter(int deviceId) async {
log.finer('_deletePrinter');
_log.finer('_deletePrinter');
return await client
.delete(Uri.parse('https://astaprint.upb.de/api/v1/admin/printers/$deviceId'), headers: {
'X-Api-Key': _token,
'Accept': 'application/json',
}).then(
(Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 205) {
log.finer('_deletePrinter: success');
return;
} else {
log.severe('_deletePrinter: ${response.body}');
throw NetworkException('status code other than 205 received', response.statusCode);
}
},
......@@ -313,7 +313,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
Future<void> _getPrinterDetails(int deviceId) async {
log.finer('_getPrinterDetails:');
_log.finer('_getPrinterDetails:');
return await client.get(
Uri.parse('https://astaprint.upb.de/api/v1/admin/printers/$deviceId'),
headers: {
......@@ -322,8 +322,8 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
},
).then(
(Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 200) {
log.finer('_getPrinterDetails: ${response.body}');
var index = _printers.indexWhere((Printer printer) => printer.deviceId == deviceId);
_printers[index] = Printer.fromMap(json.decode(response.body) as Map<String, dynamic>);
......@@ -354,7 +354,6 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
return;
} else {
log.severe('_getPrinterDetails: ${response.body}');
throw NetworkException('status code other than 200 received', response.statusCode);
}
},
......@@ -362,7 +361,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
Future<void> _getPrinters() async {
log.finer('_getPrinters:');
_log.finer('_getPrinters:');
return await client.get(
Uri.parse('https://astaprint.upb.de/api/v1/admin/printers'),
headers: {
......@@ -371,13 +370,12 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
},
).then(
(Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 200) {
log.finer('_getPrinters: ${response.statusCode} ${response.body.substring(0, 40)}');
_printers = List.from((json.decode(response.body) as Iterable)
.map<Printer>((dynamic job) => Printer.fromMap(job as Map<String, dynamic>)));
return;
} else {
log.severe('_getPrinters: ${response.body}');
throw NetworkException('status code other than 200 received', response.statusCode);
}
},
......@@ -507,7 +505,7 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
}
Future<void> _updatePrinterDetails(int deviceId, PrinterDetails details) async {
log.finer('_updatePrinterDetails: $deviceId $details');
_log.finer('_updatePrinterDetails: $deviceId $details');
return await client
.put(
Uri.parse('https://astaprint.upb.de/api/v1/admin/printers/$deviceId'),
......@@ -519,11 +517,10 @@ class PrintersBloc extends Bloc<PrintersEvent, PrintersState> {
)
.then(
(Response response) {
_log.finer(formatResponse(response));
if (response.statusCode == 205) {
log.finer('_updatePrinterDetails: success');
return;
} else {
log.severe('_updatePrinterDetails: ${response.body}');
throw NetworkException('status code other than 205 received', response.statusCode);
}
},
......
......@@ -28,5 +28,5 @@ class PrintersState extends ResultState<List<Printer>> {
PrintersState(isResult: true, printers: printers);
@override
String toString() => '[PrinterState $toMap]';
String toString() => '[PrinterState $toShortMap]';
}
......@@ -6,11 +6,12 @@ import 'package:logging/logging.dart';
import '../models/user.dart';
import '../network_exception.dart';
import '../util.dart';
import 'users_events.dart';
import 'users_state.dart';
class UsersBloc extends Bloc<UsersEvent, UsersSt