]> gitweb.factorcode.org Git - factor.git/blob - extra/logging/analysis/analysis.factor
Fixing everything for mandatory stack effects
[factor.git] / extra / logging / analysis / analysis.factor
1 ! Copyright (C) 2008 Slava Pestov.\r
2 ! See http://factorcode.org/license.txt for BSD license.\r
3 USING: kernel sequences namespaces words assocs logging sorting\r
4 prettyprint io io.styles strings logging.parser calendar.format\r
5 combinators ;\r
6 IN: logging.analysis\r
7 \r
8 SYMBOL: word-names\r
9 SYMBOL: errors\r
10 SYMBOL: word-histogram\r
11 SYMBOL: message-histogram\r
12 \r
13 : analyze-entry ( entry -- )\r
14     dup second ERROR eq? [ dup errors get push ] when\r
15     dup second CRITICAL eq? [ dup errors get push ] when\r
16     1 over third word-histogram get at+\r
17     dup third word-names get member? [\r
18         1 over rest message-histogram get at+\r
19     ] when\r
20     drop ;\r
21 \r
22 : analyze-entries ( entries word-names -- errors word-histogram message-histogram )\r
23     [\r
24         word-names set\r
25         V{ } clone errors set\r
26         H{ } clone word-histogram set\r
27         H{ } clone message-histogram set\r
28 \r
29         [\r
30             analyze-entry\r
31         ] each\r
32 \r
33         errors get\r
34         word-histogram get\r
35         message-histogram get\r
36     ] with-scope ;\r
37 \r
38 : histogram. ( assoc quot -- )\r
39     standard-table-style [\r
40         >r >alist sort-values <reversed> r> [\r
41             [ >r swap r> with-cell pprint-cell ] with-row\r
42         ] curry assoc-each\r
43     ] tabular-output ;\r
44 \r
45 : log-entry. ( entry -- )\r
46     "====== " write\r
47     {\r
48         [ first (timestamp>string) bl ]\r
49         [ second pprint bl ]\r
50         [ third write nl ]\r
51         [ fourth "\n" join print ]\r
52     } cleave ;\r
53 \r
54 : errors. ( errors -- )\r
55     [ log-entry. ] each ;\r
56 \r
57 : analysis. ( errors word-histogram message-histogram -- )\r
58     "==== INTERESTING MESSAGES:" print nl\r
59     "Total: " write dup values sum . nl\r
60     [\r
61         dup second write ": " write third "\n" join write\r
62     ] histogram.\r
63     nl\r
64     "==== WORDS:" print nl\r
65     [ write ] histogram.\r
66     nl\r
67     "==== ERRORS:" print nl\r
68     errors. ;\r
69 \r
70 : analyze-log ( lines word-names -- )\r
71     >r parse-log r> analyze-entries analysis. ;\r