syndicate-2017/prospect/trace.rkt

55 lines
1.7 KiB
Racket
Raw Normal View History

2015-03-04 16:16:18 +00:00
#lang racket/base
(provide trace-logger
trace-pid-stack
call-in-trace-context
trace-process-step
trace-internal-step
2015-03-16 14:38:32 +00:00
exn->string ;; required from web-server/private/util
string-indent
indented-port-output)
2015-03-04 16:16:18 +00:00
(require (only-in web-server/private/util exn->string))
2015-03-16 14:38:32 +00:00
(require (only-in racket/string string-join string-split))
2015-03-04 16:16:18 +00:00
(define trace-logger (make-logger 'minimart-trace))
;; (Parameterof (Listof PID))
;; Path to the active leaf in the process tree. The car end is the
;; leaf; the cdr end, the root. Used for debugging and tracing purposes.
(define trace-pid-stack (make-parameter '()))
;; PID (-> Any) -> Any
;; Pushes pid on trace-pid-stack for the duration of the call to thunk.
(define (call-in-trace-context pid thunk)
(parameterize ((trace-pid-stack (cons pid (trace-pid-stack))))
(thunk)))
(define-syntax-rule (record-trace-event name r)
(when (log-level? trace-logger 'info)
(log-message trace-logger 'info name "" r #f)))
;; Event PID Process (Option Exception) (Option Transition) -> Void
2015-03-16 14:38:32 +00:00
(define (trace-process-step e pid beh st exn t)
2015-03-04 16:16:18 +00:00
(when exn
(log-error "Process ~a died with exception:\n~a"
(cons pid (trace-pid-stack))
(exn->string exn)))
2015-03-16 14:38:32 +00:00
(record-trace-event 'process-step (list (cons pid (trace-pid-stack)) e beh st exn t)))
2015-03-04 16:16:18 +00:00
;; PID Action World Transition -> Void
(define (trace-internal-step pid a w t)
(record-trace-event 'internal-step (list (cons pid (trace-pid-stack)) a w t)))
2015-03-16 14:38:32 +00:00
(define (string-indent amount s)
(define pad (make-string amount #\space))
(string-join (for/list [(line (string-split s "\n"))] (string-append pad line)) "\n"))
(define (indented-port-output amount f)
(define p (open-output-string))
(f p)
(string-indent amount (get-output-string p)))