-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.moon
80 lines (64 loc) · 1.51 KB
/
logger.moon
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
table = table
math = math
concat = table.concat
local _tostring
_tostring = tostring
moon = assert require "moon"
dump = moon.p
debug = debug
conf = {
color: true
outputToFile: true
logLevel: "print"
modes: {
{n: "print", c: "\27[34m"}
{n: "debug", c: "\27[36m"}
{n: "info", c: "\27[32m"}
{n: "warn", c: "\27[33m"}
{n: "error", c: "\27[31m"}
{n: "fatal", c: "\27[35m"}
}
}
local logger
logger = {}
logger.outFile = nil
levels = {}
for key, val in ipairs conf.modes
levels[val.n] = key
round = (x, inc) ->
inc = inc or 1
x = x/inc
return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * inc
tostring = (...) ->
t = {}
for i = 1, select '#', ...
x = select(i, ...)
if type(x) == "number"
x = round x, .01
t[#t + 1] = _tostring x
return concat t, " "
with logger
.logFileName = (name) ->
logger.outFile = name
for i, mode in ipairs conf.modes
nameUpper = mode.n\upper!
logger[mode.n] = (...) ->
if i < levels[conf.logLevel] then return
msg = tostring ...
info = debug.getinfo 2, "Sl"
line = info.short_src .. ":" .. info.currentline
print string.format(
"%s[%-6s%s]%s %s: %s",
conf.color and mode.c or "",
nameUpper,
os.date("%H:%M:%S"),
conf.color and "\27[0m" or "",
line, msg
)
if conf.outputToFile
print logger.outFile
fp = io.open logger.outFile, "a"
str = string.format("[%-6s%s] %s: %s\n", nameUpper, os.date(), line, msg)
fp\write str
fp\close!
logger