log static method

Future<void> log(
  1. String level,
  2. String? tag,
  3. String msg,
  4. int stackDepth,
  5. int logType,
  6. bool writeFile,
)

Implementation

static Future<void> log(String level, String? tag, String msg, int stackDepth, int logType, bool writeFile) async {
  var args = { 'level': level, 'tag': tag ?? _tag, 'msg': msg, 'write': writeFile, 'logType': logType };
  var stackTrace = '';
  if(stackDepth > 0) { // stackDepth > 0 means we want to print the stack trace
    final chain = Chain.forTrace(StackTrace.current);
    final frames = chain.toTrace().frames;
    if(frames.isNotEmpty) {
      StringBuffer sb = StringBuffer('stack trace:\n');
      int realDepth = min(stackDepth, frames.length);
      int counter = 0;
      for(var i = 0; i < frames.length; i++) {
        var frame = frames[i];
        // Ignore Log4f frames
        if(frame.uri.toString() == 'package:log4f/src/log4f.dart') {
          continue;
        }

        // add prefix
        if(++counter < realDepth) {
          sb.write('|- ');
        } else {
          sb.write('|_ ');
        }
        // add the frame
        sb.write('${frame.member} at: (${frame.uri}:${frame.line}:${frame.column})');
        sb.write('\n');
        if(counter >= realDepth ) {
          break;
        }
      }
      stackTrace = sb.toString();
    }
  }

  if(stackTrace.isNotEmpty) {
    args['trace'] = stackTrace;
  }
  var log = Log(DateTime.now(), level, tag ?? _tag, msg, stackTrace);
  logContainer._addLog(log);
  // notify listeners
  _listeners.forEach((listener) { listener.onLog(log); });
  await _channel.invokeMethod('doLog', args);
}