debug method
RxDart debug operator - Port from RxSwift Debug Operator
Prints received events for all listeners on standard output.
The identifier
is printed together with event description to standard output.
If identifier
is null, it will be current stacktrace, including location, line and member.
If log
is null, this Stream is returned without any transformations.
This is useful for disabling logging in release mode of an application.
If trimOutput
is true, event text will be trimmed to max 40 characters.
Implementation
Stream<T> debug({
String? identifier,
void Function(String)? log = print,
bool trimOutput = false,
}) {
if (log == null) {
// logging is disabled.
return this;
}
identifier ??= Trace.current(1).frames.first.formatted;
@pragma('vm:prefer-inline')
@pragma('dart2js:tryInline')
void logEvent(String content) =>
log('${DateTime.now()}: $identifier -> $content');
@pragma('vm:prefer-inline')
@pragma('dart2js:tryInline')
void logDataErrorDoneEvent(String description) {
const maxEventTextLength = 40;
final descriptionNormalized = description.length > maxEventTextLength &&
trimOutput
? '${description.take(maxEventTextLength ~/ 2)}...${description.takeLast(maxEventTextLength ~/ 2)}'
: description;
logEvent('Event $descriptionNormalized');
}
return transform<T>(
DoStreamTransformer<T>(
onData: (data) => logDataErrorDoneEvent('data($data)'),
onError: (e, s) => logDataErrorDoneEvent('error($e, $s)'),
onDone: () => logDataErrorDoneEvent('done'),
onListen: () => logEvent('Listened'),
onCancel: () => logEvent('Cancelled'),
onPause: () => logEvent('Paused'),
onResume: () => logEvent('Resumed'),
),
);
}