Document with-intercepted-logging.

This commit is contained in:
Vincent St-Amour 2011-06-01 16:10:06 -04:00
parent b71d3cf40c
commit c32efa727c
2 changed files with 36 additions and 5 deletions

View File

@ -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?]{ @defproc[(log-receiver? [v any/c]) boolean?]{

View File

@ -4,6 +4,9 @@
@title{Logging} @title{Logging}
@(define the-eval (make-base-eval))
@(the-eval '(require unstable/logging))
@defmodule[unstable/logging] @defmodule[unstable/logging]
This module provides tools for 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. @racket[port]. Returns whatever @racket[proc] returns.
@defexamples[ @defexamples[
#:eval (eval/require 'unstable/logging) #:eval the-eval
(let ([my-log (open-output-string)]) (let ([my-log (open-output-string)])
(with-logging-to-port my-log (with-logging-to-port my-log
(lambda () (lambda ()
(log-warning "Warning World!") (log-warning "Warning World!")
(+ 2 2)) (+ 2 2))
#:level 'warning) #: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)]}