| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | ! Copyright (C) 2008 Slava Pestov. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  | USING: accessors peg peg.parsers memoize kernel sequences | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  | logging arrays words strings vectors io io.files | 
					
						
							|  |  |  | io.encodings.utf8 namespaces make combinators logging.server | 
					
						
							|  |  |  | calendar calendar.format ;
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | IN: logging.parser | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  | TUPLE: log-entry date level word-name message ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : string-of ( quot -- parser )
 | 
					
						
							|  |  |  |     satisfy repeat0 [ >string ] action ; inline
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-02-08 22:15:29 -05:00
										 |  |  | SYMBOL: multiline | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  | : 'date' ( -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-02-10 21:32:10 -05:00
										 |  |  |     [ "]" member? not ] string-of [ | 
					
						
							|  |  |  |         dup multiline-header =
 | 
					
						
							|  |  |  |         [ drop multiline ] [ rfc3339>timestamp ] if
 | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  |     ] action | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  |     "[" "]" surrounded-by ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  | : 'log-level' ( -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  |     log-levels [ | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  |         [ name>> token ] keep [ nip ] curry action | 
					
						
							|  |  |  |     ] map choice ;
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  | : 'word-name' ( -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  |     [ " :" member? not ] string-of ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SYMBOL: malformed | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  | : 'malformed-line' ( -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  |     [ drop t ] string-of | 
					
						
							|  |  |  |     [ log-entry new swap >>message malformed >>level ] action ;
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  | : 'log-message' ( -- parser )
 | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  |     [ drop t ] string-of | 
					
						
							|  |  |  |     [ 1vector ] action ;
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  | : 'log-line' ( -- parser )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         'date' , | 
					
						
							|  |  |  |         " " token hide , | 
					
						
							|  |  |  |         'log-level' , | 
					
						
							|  |  |  |         " " token hide , | 
					
						
							|  |  |  |         'word-name' , | 
					
						
							|  |  |  |         ": " token hide , | 
					
						
							|  |  |  |         'log-message' , | 
					
						
							|  |  |  |     ] seq* [ first4 log-entry boa ] action | 
					
						
							|  |  |  |     'malformed-line' 2choice ;
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  | PEG: parse-log-line ( string -- entry ) 'log-line' ;
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : malformed? ( line -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  |     level>> malformed eq? ;
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : multiline? ( line -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  |     level>> multiline eq? ;
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-09 03:14:14 -04:00
										 |  |  | : malformed-line ( line -- )
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  |     "Warning: malformed log line:" print
 | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  |     message>> print ;
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : add-multiline ( line -- )
 | 
					
						
							|  |  |  |     building get empty? [ | 
					
						
							|  |  |  |         "Warning: log begins with multiline entry" print drop
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  |         message>> first building get peek message>> push
 | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : parse-log ( lines -- entries )
 | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             parse-log-line { | 
					
						
							|  |  |  |                 { [ dup malformed? ] [ malformed-line ] } | 
					
						
							|  |  |  |                 { [ dup multiline? ] [ add-multiline ] } | 
					
						
							| 
									
										
										
										
											2008-04-11 13:56:48 -04:00
										 |  |  |                 [ , ] | 
					
						
							| 
									
										
										
										
											2008-02-07 18:07:43 -05:00
										 |  |  |             } cond
 | 
					
						
							|  |  |  |         ] each
 | 
					
						
							|  |  |  |     ] { } make ;
 | 
					
						
							| 
									
										
										
										
											2008-07-09 17:34:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : parse-log-file ( service -- entries )
 | 
					
						
							|  |  |  |     log-path 1 log# dup exists? | 
					
						
							|  |  |  |     [ utf8 file-lines parse-log ] [ drop f ] if ;
 |