“This is the 9th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Functional view

So today we’re going to look at how logger works, and we’re going to look at what it looks like. When we click on the debugPrint, we’re going to print out what we’re printing, right

Code to view

The code for today is in the flutter_ume_kit_console package, where a method swap is done, assigning the debugPrint method to _originalDebugPrint, and then rewriting the debugPrint to send message in the new debugPrint Add to stream, and then call _originalDebugPrint

static redirectDebugPrint() { if (_originalDebugPrint ! = null) return; _originalDebugPrint = debugPrint; debugPrint = (String? message, {int? wrapWidth}) { ConsoleManager.streamController! .sink.add(message); if (_originalDebugPrint ! = null) { _originalDebugPrint! (message, wrapWidth: wrapWidth); }}; }Copy the code

Now, what we do in the stream, we say in the stream if the number of data does not exceed the maximum number we add it, if it does exceed the maximum number we delete the last one, so we have all the data that we printed,

_logStreamController!.stream.transform(transformer).listen((value) {
  if (_logData.length < maxLine) {
    _logData.addFirst(value);
  } else {
    _logData.removeLast();
  }
});
Copy the code

Streaming data

Value = [2021-11-09 20:21:48.215855, Item1 = 2021-11-09 20:21:48.215855 item2 = StatementCopy the code

Now let’s go back to console_panel.dart and see how the data is displayed, with rich text showing the time and printing the data

RichText(
  text: TextSpan(children: [
    TextSpan(
        text: _dateTimeString(index),
        style: TextStyle(
          color: Colors.white60,
          fontFamily: 'Courier',
          fontSize: 16,
          fontWeight: FontWeight.w400,
        )),
    TextSpan(
        text:
            '${_logList[_logList.length - index - 1].item2}',
        style: TextStyle(
          color: Colors.white,
          fontFamily: 'Courier',
          fontSize: 16,
          fontWeight: FontWeight.w400,
        )),
  ]),
)
Copy the code

As for the time format display of multiple styles, an enumeration is written to determine which format of the current time, to return the format to display, the code does not show

The tool can only print through the debugPrint, not print, because it is to plug the debugPrint, so it can only record the debugPrint data

In addition, the debugPrint can customize the print information, and print can not customize the print information, if you have time to write a print output small tool, please look forward to, hey hey

conclusion

Ok, today’s source view is over here, as a student of Flutter, I hope you can give me more advice and discuss with me where there are problems