1 ! Copyright (C) 2003, 2008 Slava Pestov.
\r
2 ! See http://factorcode.org/license.txt for BSD license.
\r
3 USING: logging.server sequences namespaces concurrency.messaging
\r
4 words kernel arrays shuffle tools.annotations
\r
5 prettyprint.config prettyprint debugger io.streams.string
\r
6 splitting continuations effects generalizations parser strings
\r
7 quotations fry accessors math assocs math.order ;
\r
10 SYMBOLS: DEBUG NOTICE WARNING ERROR CRITICAL ;
\r
14 log-level [ DEBUG ] initialize
\r
16 : log-levels ( -- assoc )
\r
25 ERROR: undefined-log-level ;
\r
27 : log-level<=> ( log-level log-level -- ? )
\r
28 [ log-levels at* [ undefined-log-level ] unless ] bi@ <=> ;
\r
30 : log? ( log-level -- ? )
\r
31 log-level get log-level<=> +lt+ = not ;
\r
33 : send-to-log-server ( array string -- )
\r
34 prefix "log-server" get send ;
\r
38 ERROR: bad-log-message-parameters msg word level ;
\r
40 : check-log-message ( msg word level -- msg word level )
\r
41 3dup [ string? ] [ word? ] [ word? ] tri* and and
\r
42 [ bad-log-message-parameters ] unless ; inline
\r
44 : log-message ( msg word level -- )
\r
47 2dup [ log? ] [ ] bi* and [
\r
48 [ [ string-lines ] [ name>> ] [ name>> ] tri* ] dip
\r
49 4array "log-message" send-to-log-server
\r
54 : rotate-logs ( -- )
\r
55 { } "rotate-logs" send-to-log-server ;
\r
58 { } "close-logs" send-to-log-server ;
\r
60 : with-logging ( service quot -- )
\r
61 [ log-service ] dip with-variable ; inline
\r
63 ! Aspect-oriented programming idioms
\r
67 : stack>message ( obj -- inputs>message )
\r
68 dup array? [ dup length 1 = [ first ] when ] when
\r
82 : (define-logging) ( word level quot -- )
\r
83 [ dup ] 2dip 2curry annotate ; inline
\r
85 : call-logging-quot ( quot word level -- quot' )
\r
86 [ "called" ] 2dip [ log-message ] 3curry prepose ;
\r
88 : add-logging ( word level -- )
\r
89 [ call-logging-quot ] (define-logging) ;
\r
91 : log-stack ( n word level -- )
\r
93 [ [ ndup ] keep narray stack>message ] 2dip log-message
\r
98 : input# ( word -- n ) stack-effect in>> length ;
\r
100 : input-logging-quot ( quot word level -- quot' )
\r
101 rot [ [ input# ] keep ] 2dip '[ _ _ _ log-stack @ ] ;
\r
103 : add-input-logging ( word level -- )
\r
104 [ input-logging-quot ] (define-logging) ;
\r
106 : output# ( word -- n ) stack-effect out>> length ;
\r
108 : output-logging-quot ( quot word level -- quot' )
\r
109 [ [ output# ] keep ] dip '[ @ _ _ _ log-stack ] ;
\r
111 : add-output-logging ( word level -- )
\r
112 [ output-logging-quot ] (define-logging) ;
\r
114 : (log-error) ( object word level -- )
\r
116 [ [ print-error ] with-string-writer ] 2dip log-message
\r
121 : log-error ( error word -- ) ERROR (log-error) ;
\r
123 : log-critical ( error word -- ) CRITICAL (log-error) ;
\r
125 : stack-balancer ( effect -- quot )
\r
126 [ in>> length [ ndrop ] curry ]
\r
127 [ out>> length f <repetition> >quotation ]
\r
130 : error-logging-quot ( quot word -- quot' )
\r
131 dup stack-effect stack-balancer
\r
132 '[ _ [ _ log-error @ ] recover ] ;
\r
134 : add-error-logging ( word level -- )
\r
135 [ [ input-logging-quot ] 2keep drop error-logging-quot ]
\r
139 #! Syntax: name level
\r
140 CREATE-WORD dup scan-word
\r
141 '[ 1array stack>message _ _ log-message ]
\r
142 (( message -- )) define-declared ;
\r
146 "logging.parser" require
\r
147 "logging.analysis" require
\r