| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2008-07-07 20:36:33 -04:00
										 |  |  | splitting continuations effects generalizations parser strings | 
					
						
							| 
									
										
										
										
											2010-05-18 18:36:47 -04:00
										 |  |  | quotations fry accessors math assocs math.order | 
					
						
							|  |  |  | sequences.generalizations ;
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-17 12:30:28 -05:00
										 |  |  | SYMBOL: log-level | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-18 17:59:23 -05:00
										 |  |  | log-level [ DEBUG ] initialize
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-17 12:30:28 -05:00
										 |  |  | : log-levels ( -- assoc )
 | 
					
						
							|  |  |  |     H{ | 
					
						
							|  |  |  |         { DEBUG 0 } | 
					
						
							|  |  |  |         { NOTICE 10 } | 
					
						
							|  |  |  |         { WARNING 20 } | 
					
						
							|  |  |  |         { ERROR 30 } | 
					
						
							|  |  |  |         { CRITICAL 40 } | 
					
						
							|  |  |  |     } ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ERROR: undefined-log-level ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : log-level<=> ( log-level log-level -- ? )
 | 
					
						
							| 
									
										
										
										
											2011-10-14 15:31:06 -04:00
										 |  |  |     [ log-levels at* [ undefined-log-level ] unless ] compare ;
 | 
					
						
							| 
									
										
										
										
											2009-02-17 12:30:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : log? ( log-level -- ? )
 | 
					
						
							|  |  |  |     log-level get log-level<=> +lt+ = not ;
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : send-to-log-server ( array string -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-31 20:18:05 -04:00
										 |  |  |     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 [ | 
					
						
							| 
									
										
										
										
											2008-06-28 03:36:20 -04:00
										 |  |  |         [ [ 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-18 17:01:53 -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
 | 
					
						
							| 
									
										
										
										
											2008-09-06 04:23:54 -04:00
										 |  |  |             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 -- )
 | 
					
						
							| 
									
										
										
										
											2009-03-15 19:19:29 -04:00
										 |  |  |     [ 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
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  | : 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) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  | : 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) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-21 02:27:50 -04:00
										 |  |  | SYNTAX: LOG: | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  |     #! Syntax: name level | 
					
						
							| 
									
										
										
										
											2011-09-27 16:20:07 -04:00
										 |  |  |     scan-new-word dup scan-word | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  |     '[ 1array stack>message _ _ log-message ] | 
					
						
							| 
									
										
										
										
											2011-10-18 16:18:42 -04:00
										 |  |  |     ( message -- ) define-declared ;
 | 
					
						
							| 
									
										
										
										
											2008-07-03 02:39:45 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-11-02 14:23:41 -04:00
										 |  |  | USE: vocabs | 
					
						
							| 
									
										
										
										
											2008-07-03 02:39:45 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | "logging.parser" require | 
					
						
							|  |  |  | "logging.analysis" require |