diff --git a/collects/scribblings/reference/logging.scrbl b/collects/scribblings/reference/logging.scrbl index 89ab247ae0..e8aa05898d 100644 --- a/collects/scribblings/reference/logging.scrbl +++ b/collects/scribblings/reference/logging.scrbl @@ -150,7 +150,7 @@ is equivalent to ]} @; ---------------------------------------- -@section{Receiving Logged Events} +@section[#:tag "receiving-logged-events"]{Receiving Logged Events} @defproc[(log-receiver? [v any/c]) boolean?]{ diff --git a/collects/unstable/scribblings/logging.scrbl b/collects/unstable/scribblings/logging.scrbl index 9069afd6fd..ab72c4356a 100644 --- a/collects/unstable/scribblings/logging.scrbl +++ b/collects/unstable/scribblings/logging.scrbl @@ -4,6 +4,9 @@ @title{Logging} +@(define the-eval (make-base-eval)) +@(the-eval '(require unstable/logging)) + @defmodule[unstable/logging] This module provides tools for logging. @@ -19,14 +22,42 @@ Runs @racket[proc], outputting any logging of level @racket[level] or higher to @racket[port]. Returns whatever @racket[proc] returns. @defexamples[ -#:eval (eval/require 'unstable/logging) +#:eval the-eval (let ([my-log (open-output-string)]) (with-logging-to-port my-log (lambda () (log-warning "Warning World!") (+ 2 2)) #:level 'warning) - (display (get-output-string my-log))) -] + (get-output-string my-log))]} -} + +@defproc[(with-intercepted-logging + [interceptor (-> (vector/c + (or/c 'fatal 'error 'warning 'info 'debug) + string? + any/c) + any)] + [proc (-> any)] + [#:level level (or/c 'fatal 'error 'warning 'info 'debug) 'info]) + any]{ + +Runs @racket[proc], calling @racket[interceptor] on any log message of level +@racket[level] or higher. @racket[interceptor] receives the entire log vectors +(see @secref["receiving-logged-events" #:doc '(lib "scribblings/reference/reference.scrbl")]) +as arguments. Returns whatever @racket[proc] returns. + +@defexamples[ +#:eval the-eval +(let ([warning-counter 0]) + (with-intercepted-logging + (lambda (l) + (when (eq? (vector-ref l 0) ; actual level + 'warning) + (set! warning-counter (add1 warning-counter)))) + (lambda () + (log-warning "Warning!") + (log-warning "Warning again!") + (+ 2 2)) + #:level 'warning) + warning-counter)]}