]> gitweb.factorcode.org Git - factor.git/blob - extra/logging/analysis/analysis.factor
cd1429ac53485d9f332c6c2cc0e626026eac1c5c
[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 IN: logging.analysis\r
6 \r
7 SYMBOL: word-names\r
8 SYMBOL: errors\r
9 SYMBOL: word-histogram\r
10 SYMBOL: message-histogram\r
11 \r
12 : analyze-entry ( entry -- )\r
13     dup second ERROR eq? [ dup errors get push ] when\r
14     dup second CRITICAL eq? [ dup errors get push ] when\r
15     1 over third word-histogram get at+\r
16     dup third word-names get member? [\r
17         1 over rest message-histogram get at+\r
18     ] when\r
19     drop ;\r
20 \r
21 : analyze-entries ( entries word-names -- errors word-histogram message-histogram )\r
22     [\r
23         word-names set\r
24         V{ } clone errors set\r
25         H{ } clone word-histogram set\r
26         H{ } clone message-histogram set\r
27 \r
28         [\r
29             analyze-entry\r
30         ] each\r
31 \r
32         errors get\r
33         word-histogram get\r
34         message-histogram get\r
35     ] with-scope ;\r
36 \r
37 : histogram. ( assoc quot -- )\r
38     standard-table-style [\r
39         >r >alist sort-values <reversed> r> [\r
40             [ >r swap r> with-cell pprint-cell ] with-row\r
41         ] curry assoc-each\r
42     ] tabular-output ;\r
43 \r
44 : log-entry.\r
45     "====== " write\r
46     dup first (timestamp>string) bl\r
47     dup second pprint bl\r
48     dup third write nl\r
49     fourth "\n" join print ;\r
50 \r
51 : errors. ( errors -- )\r
52     [ log-entry. ] each ;\r
53 \r
54 : analysis. ( errors word-histogram message-histogram -- )\r
55     "==== INTERESTING MESSAGES:" print nl\r
56     "Total: " write dup values sum . nl\r
57     [\r
58         dup second write ": " write third "\n" join write\r
59     ] histogram.\r
60     nl\r
61     "==== WORDS:" print nl\r
62     [ write ] histogram.\r
63     nl\r
64     "==== ERRORS:" print nl\r
65     errors. ;\r
66 \r
67 : analyze-log ( lines word-names -- )\r
68     >r parse-log r> analyze-entries analysis. ;\r