printf function

String printf(
  1. dynamic msg, {
  2. LogMode mode = LogMode.debug,
})

Implementation

String printf(dynamic msg, {LogMode mode = LogMode.debug}) {
  if (kReleaseMode) {
    // release mode does not print
    return "";
  }
  // Chain.forTrace(StackTrace.current);
  var chain = Chain.current();
  // Combine the stacks of core and fluent packages (there is only one piece of relevant data left)
  chain =
      chain.foldFrames((frame) => frame.isCore || frame.package == "flutter");
  // Take out all information frames
  final frames = chain.toTrace().frames;
  // Find the information frame of the current function
  final idx = frames.indexWhere((element) => element.member == "printf");
  if (idx == -1 || idx + 1 >= frames.length) {
    return "";
  }
  // Function information frame for calling the current function
  final frame = frames[idx + 1];

  var modeStr = "";
  switch (mode) {
    case LogMode.debug:
      modeStr = "💚 DEBUG";
      break;
    case LogMode.warning:
      modeStr = "💛 WARNING";
      break;
    case LogMode.info:
      modeStr = "💙 INFO";
      break;
    case LogMode.error:
      modeStr = "❤️ ERROR";
      break;
  }

  final msgInfo =
      "$modeStr ${frame.uri.toString().split("/").last}(${frame.line}-${frame.column}) - $msg";

  ///As flutter would like, it is currently only printed in debug mode
  if (kDebugMode) {
    print(msgInfo);
  }
  return msgInfo;
}