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
8 sequences.generalizations ;
\r
11 SYMBOLS: DEBUG NOTICE WARNING ERROR CRITICAL ;
\r
15 log-level [ DEBUG ] initialize
\r
17 : log-levels ( -- assoc )
\r
26 ERROR: undefined-log-level ;
\r
28 : log-level<=> ( log-level log-level -- <=> )
\r
29 [ log-levels at* [ undefined-log-level ] unless ] compare ;
\r
31 : log? ( log-level -- ? )
\r
32 log-level get log-level<=> +lt+ = not ;
\r
34 : send-to-log-server ( array string -- )
\r
35 prefix "log-server" get send ;
\r
39 ERROR: bad-log-message-parameters msg word level ;
\r
41 : check-log-message ( msg word level -- msg word level )
\r
42 3dup [ string? ] [ word? ] [ word? ] tri* and and
\r
43 [ bad-log-message-parameters ] unless ; inline
\r
45 : log-message ( msg word level -- )
\r
48 2dup [ log? ] [ ] bi* and [
\r
49 [ [ string-lines ] [ name>> ] [ name>> ] tri* ] dip
\r
50 4array "log-message" send-to-log-server
\r
55 : rotate-logs ( -- )
\r
56 { } "rotate-logs" send-to-log-server ;
\r
59 { } "close-logs" send-to-log-server ;
\r
61 : with-logging ( service quot -- )
\r
62 [ log-service ] dip with-variable ; inline
\r
64 ! Aspect-oriented programming idioms
\r
68 : stack>message ( obj -- inputs>message )
\r
69 dup array? [ dup length 1 = [ first ] when ] when
\r
83 : (define-logging) ( word level quot -- )
\r
84 [ dup ] 2dip 2curry annotate ; inline
\r
86 : call-logging-quot ( quot word level -- quot' )
\r
87 [ "called" ] 2dip [ log-message ] 3curry prepose ;
\r
89 : add-logging ( word level -- )
\r
90 [ call-logging-quot ] (define-logging) ;
\r
92 : log-stack ( n word level -- )
\r
94 [ [ ndup ] keep narray stack>message ] 2dip log-message
\r
99 : input# ( word -- n ) stack-effect in>> length ;
\r
101 : input-logging-quot ( quot word level -- quot' )
\r
102 rot [ [ input# ] keep ] 2dip '[ _ _ _ log-stack @ ] ;
\r
104 : add-input-logging ( word level -- )
\r
105 [ input-logging-quot ] (define-logging) ;
\r
107 : output# ( word -- n ) stack-effect out>> length ;
\r
109 : output-logging-quot ( quot word level -- quot' )
\r
110 [ [ output# ] keep ] dip '[ @ _ _ _ log-stack ] ;
\r
112 : add-output-logging ( word level -- )
\r
113 [ output-logging-quot ] (define-logging) ;
\r
115 : (log-error) ( object word level -- )
\r
117 [ [ print-error ] with-string-writer ] 2dip log-message
\r
122 : log-error ( error word -- ) ERROR (log-error) ;
\r
124 : log-critical ( error word -- ) CRITICAL (log-error) ;
\r
126 : stack-balancer ( effect -- quot )
\r
127 [ in>> length [ ndrop ] curry ]
\r
128 [ out>> length f <repetition> >quotation ]
\r
131 : error-logging-quot ( quot word -- quot' )
\r
132 dup stack-effect stack-balancer
\r
133 '[ _ [ _ log-error @ ] recover ] ;
\r
135 : add-error-logging ( word level -- )
\r
136 [ [ input-logging-quot ] 2keep drop error-logging-quot ]
\r
140 #! Syntax: name level
\r
141 scan-new-word dup scan-word
\r
142 '[ 1array stack>message _ _ log-message ]
\r
143 ( message -- ) define-declared ;
\r
147 "logging.parser" require
\r
148 "logging.analysis" require
\r