-
Notifications
You must be signed in to change notification settings - Fork 2
/
Logger.spad
129 lines (109 loc) · 3.75 KB
/
Logger.spad
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
)abbrev category LOGCAT LoggerCategory
LoggerCategory() : Category == with
++ Info useful to developers for debugging the application.
debug : PrintableForm -> Void
debug : List(PrintableForm) -> Void
++ Normal operational messages.
info : PrintableForm -> Void
info : List(PrintableForm) -> Void
++ Events that are unusual but not error conditions (default level).
notice : PrintableForm -> Void
notice : List(PrintableForm) -> Void
++ Warning messages.
warn : PrintableForm -> Void
warn : List(PrintableForm) -> Void
++ Error messages.
fail : PrintableForm -> Void
fail : List(PrintableForm) -> Void
++ Set logger level.
loggerLevel : Union("debug", "info", "notice", "warn", "fail") -> Void
)abbrev domain LOGMAIN MainLogger
MainLogger() : Exports == Implementation where
LVL ==> Union("debug", "info", "notice", "warn", "fail")
PF ==> PrintableForm
NNI ==> NonNegativeInteger
CONF ==> Table(Symbol, LVL)
Exports ==> with
debug : (Symbol, PF) -> Void
info : (Symbol, PF) -> Void
notice : (Symbol, PF) -> Void
warn : (Symbol, PF) -> Void
fail : (Symbol, PF) -> Void
log : (Symbol, LVL, PF) -> Void
loggerLevel : (Symbol, LVL) -> Void
loggerDefaultLevel : LVL -> Void
getTime : () -> NNI
resetTime : () -> Void
Implementation ==> add
import Printer
debug (c, f) == log(c, "debug", f)
info (c, f) == log(c, "info", f)
notice (c, f) == log(c, "notice", f)
warn (c, f) == log(c, "warn", f)
fail (c, f) == log(c, "fail", f)
config : CONF := [[]]
default : LVL := "notice"
timeBegin : NNI := 0
num : LVL -> NNI
num l ==
l case "debug" => 0
l case "info" => 1
l case "notice" => 2
l case "warn" => 3
l case "fail" => 4
getTime () ==
t : NNI := GET_-INTERNAL_-REAL_-TIME()$Lisp
resetTime () ==
timeBegin := getTime()
log (component, level, form) ==
l :=
lc := search(component, config)
lc case "failed" => default
lc :: LVL
if num l <= num level then
time := (getTime() - timeBegin) :: Float / 1000
println spaces [paren float(time, 3), form]
loggerLevel (component, level) ==
config(component) := level
loggerDefaultLevel level ==
default := level
)abbrev domain LOG Logger
Logger(C : Symbol) : LoggerCategory == Implementation where
LVL ==> Union("debug", "info", "notice", "warn", "fail")
PF ==> PrintableForm
Implementation ==> add
import MainLogger
debug (f : PF) ==
debug(C, spaces [magenta bold bracket (C :: PF), f])
info (f : PF) ==
info(C, spaces [cyan bold bracket (C :: PF), f])
notice (f : PF) ==
notice(C, spaces [green bold bracket (C :: PF), f])
warn (f : PF) ==
warn(C, spaces [yellow bold bracket (C :: PF), f])
fail (f : PF) ==
fail(C, spaces [red bold bracket (C :: PF), f])
debug (l : List(PF)) == debug spaces l
info (l : List(PF)) == info spaces l
notice (l : List(PF)) == notice spaces l
warn (l : List(PF)) == warn spaces l
fail (l : List(PF)) == fail spaces l
loggerLevel l == loggerLevel(C, l)
)abbrev package LOGTEST LoggerTest
LoggerTest() : Exports == Implementation where
PF ==> PrintableForm
Exports ==> with
test : () -> Void
Implementation ==> add
import MainLogger
import Logger('L1)
import Logger('L2)
test () ==
loggerLevel("debug")$Logger('L1)
loggerLevel("info")$Logger('L2)
fail("this is an error" :: PF)$Logger('L1)
warn("this is a warning" :: PF)$Logger('L2)
notice("this is a notice" :: PF)$Logger('L1)
info("this is an information message" :: PF)$Logger('L2)
debug("this is a debug message" :: PF)$Logger('L1)
debug("this is a debug message" :: PF)$Logger('L2)