factor/basis/logging/logging.factor

148 lines
3.9 KiB
Factor
Raw Normal View History

2008-02-07 18:07:43 -05:00
! Copyright (C) 2003, 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2008-02-18 08:30:16 -05:00
USING: logging.server sequences namespaces concurrency.messaging
2008-02-07 18:07:43 -05:00
words kernel arrays shuffle tools.annotations
prettyprint.config prettyprint debugger io.streams.string
splitting continuations effects generalizations parser strings
quotations fry accessors math assocs math.order ;
2008-02-07 18:07:43 -05:00
IN: logging
2008-05-30 19:16:51 -04:00
SYMBOLS: DEBUG NOTICE WARNING ERROR CRITICAL ;
2008-02-07 18:07:43 -05:00
SYMBOL: log-level
2009-02-18 17:59:23 -05:00
log-level [ DEBUG ] initialize
: log-levels ( -- assoc )
H{
{ DEBUG 0 }
{ NOTICE 10 }
{ WARNING 20 }
{ ERROR 30 }
{ CRITICAL 40 }
} ;
ERROR: undefined-log-level ;
: log-level<=> ( log-level log-level -- ? )
[ log-levels at* [ undefined-log-level ] unless ] bi@ <=> ;
: log? ( log-level -- ? )
log-level get log-level<=> +lt+ = not ;
2008-02-07 18:07:43 -05:00
: send-to-log-server ( array string -- )
prefix "log-server" get send ;
2008-02-07 18:07:43 -05:00
SYMBOL: log-service
2009-02-18 14:35:55 -05:00
ERROR: bad-log-message-parameters msg word level ;
2008-05-30 19:16:51 -04:00
: check-log-message ( msg word level -- msg word level )
3dup [ string? ] [ word? ] [ word? ] tri* and and
2009-02-18 14:35:55 -05:00
[ bad-log-message-parameters ] unless ; inline
2008-02-07 18:07:43 -05:00
: log-message ( msg word level -- )
check-log-message
2009-02-18 14:35:55 -05:00
log-service get
2dup [ log? ] [ ] bi* and [
[ [ string-lines ] [ name>> ] [ name>> ] tri* ] dip
2008-02-07 18:07:43 -05:00
4array "log-message" send-to-log-server
] [
4drop
] if ;
: rotate-logs ( -- )
{ } "rotate-logs" send-to-log-server ;
2008-02-07 20:51:37 -05:00
: close-logs ( -- )
{ } "close-logs" send-to-log-server ;
2008-02-07 18:07:43 -05:00
: with-logging ( service quot -- )
[ log-service ] dip with-variable ; inline
2008-02-07 18:07:43 -05:00
! Aspect-oriented programming idioms
<PRIVATE
2008-02-07 20:51:37 -05:00
: stack>message ( obj -- inputs>message )
2008-06-14 03:45:26 -04:00
dup array? [ dup length 1 = [ first ] when ] when
dup string? [
2008-06-11 03:58:38 -04:00
[
2008-09-07 19:05:45 -04:00
boa-tuples? on
string-limit? off
2008-06-11 03:58:38 -04:00
1 line-limit set
3 nesting-limit set
0 margin set
unparse
] with-scope
2008-06-14 03:45:26 -04:00
] unless ;
2008-02-07 18:07:43 -05:00
PRIVATE>
: (define-logging) ( word level quot -- )
[ dup ] 2dip 2curry annotate ; inline
2008-02-07 18:07:43 -05:00
: call-logging-quot ( quot word level -- quot' )
2008-12-22 06:41:01 -05:00
[ "called" ] 2dip [ log-message ] 3curry prepose ;
2008-02-07 18:07:43 -05:00
: add-logging ( word level -- )
[ call-logging-quot ] (define-logging) ;
2008-02-07 20:51:37 -05:00
: log-stack ( n word level -- )
2008-02-07 18:07:43 -05:00
log-service get [
2008-05-30 19:16:51 -04:00
[ [ ndup ] keep narray stack>message ] 2dip log-message
2008-02-07 18:07:43 -05:00
] [
3drop
] if ; inline
: input# ( word -- n ) stack-effect in>> length ;
2008-02-07 18:07:43 -05:00
: input-logging-quot ( quot word level -- quot' )
2008-09-10 23:11:40 -04:00
rot [ [ input# ] keep ] 2dip '[ _ _ _ log-stack @ ] ;
2008-02-07 18:07:43 -05:00
: add-input-logging ( word level -- )
[ input-logging-quot ] (define-logging) ;
: output# ( word -- n ) stack-effect out>> length ;
2008-02-07 20:51:37 -05:00
: output-logging-quot ( quot word level -- quot' )
2008-09-10 23:11:40 -04:00
[ [ output# ] keep ] dip '[ @ _ _ _ log-stack ] ;
2008-02-07 20:51:37 -05:00
: add-output-logging ( word level -- )
[ output-logging-quot ] (define-logging) ;
2008-02-07 18:07:43 -05:00
: (log-error) ( object word level -- )
log-service get [
2008-05-30 19:16:51 -04:00
[ [ print-error ] with-string-writer ] 2dip log-message
2008-02-07 18:07:43 -05:00
] [
2drop rethrow
] if ;
2008-02-07 20:51:37 -05:00
: log-error ( error word -- ) ERROR (log-error) ;
2008-02-07 18:07:43 -05:00
2008-02-07 20:51:37 -05:00
: log-critical ( error word -- ) CRITICAL (log-error) ;
2008-02-07 18:07:43 -05:00
2008-05-30 19:16:51 -04:00
: stack-balancer ( effect -- quot )
[ in>> length [ ndrop ] curry ]
[ out>> length f <repetition> >quotation ]
bi append ;
2008-02-10 02:39:37 -05:00
2008-02-07 18:07:43 -05:00
: error-logging-quot ( quot word -- quot' )
2008-05-30 19:16:51 -04:00
dup stack-effect stack-balancer
2008-09-10 23:11:40 -04:00
'[ _ [ _ log-error @ ] recover ] ;
2008-02-07 18:07:43 -05:00
: add-error-logging ( word level -- )
2008-05-30 19:16:51 -04:00
[ [ input-logging-quot ] 2keep drop error-logging-quot ]
2008-02-07 18:07:43 -05:00
(define-logging) ;
SYNTAX: LOG:
2008-02-07 18:07:43 -05:00
#! Syntax: name level
2008-05-30 19:16:51 -04:00
CREATE-WORD dup scan-word
2008-09-10 23:11:40 -04:00
'[ 1array stack>message _ _ log-message ]
(( message -- )) define-declared ;
USE: vocabs.loader
"logging.parser" require
"logging.analysis" require