Chain.current constructor

Chain.current(
  1. [int level = 0]
)

Returns the current stack chain.

By default, the first frame of the first trace will be the line where Chain.current is called. If level is passed, the first trace will start that many frames up instead.

If this is called outside of a capture zone, it just returns a single-trace chain.

Implementation

factory Chain.current([int level = 0]) {
  if (_currentSpec != null) return _currentSpec!.currentChain(level + 1);

  var chain = Chain.forTrace(StackTrace.current);
  return LazyChain(() {
    // JS includes a frame for the call to StackTrace.current, but the VM
    // doesn't, so we skip an extra frame in a JS context.
    var first = Trace(chain.traces.first.frames.skip(level + (inJS ? 2 : 1)),
        original: chain.traces.first.original.toString());
    return Chain([first, ...chain.traces.skip(1)]);
  });
}