|
| 1 | +{Stacktrace, Frame} = require './stacktrace' |
| 2 | +fs = require 'fs' |
| 3 | +path = require 'path' |
| 4 | +util = require 'util' |
| 5 | + |
| 6 | +# Internal: Build a Frame instance with a simple DSL. |
| 7 | +# |
| 8 | +class FrameBuilder |
| 9 | + |
| 10 | + constructor: (@_rawLine) -> |
| 11 | + [@_path, @_lineNumber, @_functionName] = [] |
| 12 | + |
| 13 | + path: (@_path) -> |
| 14 | + |
| 15 | + lineNumber: (@_lineNumber) -> |
| 16 | + |
| 17 | + functionName: (@_functionName) -> |
| 18 | + |
| 19 | +# Internal: Use the collected information from a FrameBuilder to instantiate a Frame. |
| 20 | +# |
| 21 | +asFrame = (fb) -> |
| 22 | + required = [ |
| 23 | + { name: 'rawLine', ok: fb._rawLine? } |
| 24 | + { name: 'path', ok: fb._path? } |
| 25 | + { name: 'lineNumber', ok: fb._lineNumber? } |
| 26 | + { name: 'functionName', ok: fb._functionName? } |
| 27 | + ] |
| 28 | + missing = (r.name for r in required when not r.ok) |
| 29 | + |
| 30 | + unless missing.length is 0 |
| 31 | + e = new Error("Missing required frame attributes: #{missing.join ', '}") |
| 32 | + e.missing = missing |
| 33 | + e.rawLine = fb.rawLine |
| 34 | + throw e |
| 35 | + |
| 36 | + new Frame(fb._rawLine, fb._path, fb._lineNumber, fb._functionName) |
| 37 | + |
| 38 | +allTracers = null |
| 39 | + |
| 40 | +# Internal: Load stacktrace parsers from the parsers/ directory. |
| 41 | +# |
| 42 | +loadTracers = -> |
| 43 | + allTracers = [] |
| 44 | + parsersPath = path.resolve(__dirname, 'parsers') |
| 45 | + for parserFile in fs.readdirSync(parsersPath) |
| 46 | + allTracers.push require(path.join parsersPath, parserFile) |
| 47 | + |
| 48 | +# Internal: Parse zero or more stacktraces from a sample of text. |
| 49 | +# |
| 50 | +# text - String output sample that may contain one or more stacktraces from a |
| 51 | +# supported language. |
| 52 | +# tracers - If provided, use only the provided tracer objects. Otherwise, everything in parsers/ |
| 53 | +# will be loaded and used. |
| 54 | +# |
| 55 | +# Returns: An Array of Stacktrace objects, in the order in which they occurred |
| 56 | +# in the original sample. |
| 57 | +# |
| 58 | +traceParser = (text, tracers = null) -> |
| 59 | + unless tracers? |
| 60 | + loadTracers() unless allTracers? |
| 61 | + tracers = allTracers |
| 62 | + |
| 63 | + stacks = [] |
| 64 | + frames = [] |
| 65 | + message = null |
| 66 | + activeTracer = null |
| 67 | + |
| 68 | + finishStacktrace = -> |
| 69 | + s = new Stacktrace(frames, message) |
| 70 | + stacks.push s |
| 71 | + |
| 72 | + frames = [] |
| 73 | + message = null |
| 74 | + activeTracer = null |
| 75 | + |
| 76 | + for rawLine in text.split(/\r?\n/) |
| 77 | + trimmed = rawLine.trim() |
| 78 | + |
| 79 | + # Mid-stack frame. |
| 80 | + if activeTracer? |
| 81 | + fb = new FrameBuilder(trimmed) |
| 82 | + activeTracer.consume trimmed, fb, |
| 83 | + emitMessage: (m) -> message = m |
| 84 | + emitFrame: -> frames.push asFrame fb |
| 85 | + emitStack: finishStacktrace |
| 86 | + |
| 87 | + # Outside of a frame. Attempt to recognize the next trace by emitting at least one frame. |
| 88 | + unless activeTracer? |
| 89 | + for t in tracers |
| 90 | + fb = new FrameBuilder(trimmed) |
| 91 | + t.recognize trimmed, fb, |
| 92 | + emitMessage: (m) -> message = m |
| 93 | + emitFrame: -> frames = [asFrame(fb)] |
| 94 | + emitStack: finishStacktrace |
| 95 | + if message? or frames.length > 0 |
| 96 | + activeTracer = t |
| 97 | + break |
| 98 | + |
| 99 | + # Finalize the last Stacktrace. |
| 100 | + finishStacktrace() if frames.length > 0 |
| 101 | + |
| 102 | + stacks |
| 103 | + |
| 104 | +module.exports = |
| 105 | + traceParser: traceParser |
0 commit comments