Skip to content

Commit

Permalink
record latest 1000 messages in RAM
Browse files Browse the repository at this point in the history
Press F12 and type D.bb() for "black box".
The latest messages are returned as a '\n'-separated string.
  • Loading branch information
ngn committed Feb 27, 2015
1 parent 0bf198e commit 163a9c3
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion proxy.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ os = require 'os'
path = require 'path'
{spawn} = require 'child_process'

window.D ?= {}

bbRecord = null
do -> # black box: store latest n log messages in RAM
n = 1000; i = 0; a = Array n
window.D.bb = (m) -> # m: limit
if typeof m != 'number' || !(0 < m <= n) || m != ~~m then m = n
(if m <= n - i then a[i...i + m] else a[i..].concat a[...i + m - n]).join ''
bbRecord = (s) -> a[i++] = s; i %= n; return
return

log = do ->
t0 = +new Date # log timestamps will be number of milliseconds since t0
N = 50; T = 1000 # log no more than N log messages per T milliseconds
Expand All @@ -15,7 +26,7 @@ log = do ->
(s) -> # the actual log() function
if (t1 = +new Date) - t > T then t = t1; n = 1 # if last message was too long ago, start counting afresh
m = if ++n < N then "#{t1 - t0}: #{s}\n" else if n == N then '... logging temporarily suppressed\n'
if m then process.stdout?.write? m; if fd then mb = Buffer m; fs.writeSync fd, mb, 0, mb.length
if m then process.stdout?.write? m; bbRecord m; if fd then mb = Buffer m; fs.writeSync fd, mb, 0, mb.length
return
log new Date().toISOString()

Expand Down

0 comments on commit 163a9c3

Please sign in to comment.