-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.moon
143 lines (120 loc) · 5.3 KB
/
log.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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
-- Copyright (c) 2014 by Adam Hellberg <[email protected]>.
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
NAME, T = ...
import decorate from T.misc
{localization: L} = T
import format, upper from string
local db
logging_enabled = true
logging_level = 1
logging_color_enabled = true
db_loaded = false
debug_enabled = false
class T.Logger
@levels:
DEBUG: 0
INFO: 1
WARN: 2
ERROR: 3
FATAL: 4
NOTICE: 5
@level_to_prefix: (level) =>
@prefixes[level] or L.unknown\upper!
@prefix_to_level: (prefix) =>
prefix = prefix\upper!
@levels[prefix] or -1
new: (@name, @color) =>
log: (level, ...) =>
return unless logging_enabled -- db 'log', true
return if level == @@levels.DEBUG and not debug_enabled
return if level < logging_level and not debug_enabled
prefix = @@level_to_prefix level
docolor = logging_color_enabled
name = docolor and decorate(NAME, @@colors.name) or NAME
logname = (docolor and @color) and decorate(@name, @color) or @name
prefix_color = docolor and @@colors.levels[level] or nil
prefix = (docolor and prefix_color) and decorate(prefix, prefix_color) or prefix
-- Tostring any LocaleStrings
args = {...}
for i = 1, #args
arg = args[i]
args[i] = tostring(arg) if type(arg) == 'table' and type(arg.__class) == 'table' and arg.__class.__name == 'LocaleString'
msg = '%s.%s: [%s] %s'\format name, logname, prefix, format unpack args
DEFAULT_CHAT_FRAME\AddMessage msg
import Logger from T
Logger.prefixes = {v, k for k, v in pairs Logger.levels}
with Logger
.colors =
name: '00FF00' -- -> db 'log.color.name', '00FF00'
levels:
[.levels.DEBUG]: '008000' -- -> db 'log.color.level.debug', '008000' -- Green
[.levels.INFO]: 'FFFFFF' -- -> db 'log.color.level.info', 'FFFFFF' -- White
[.levels.WARN]: 'FFD700' -- -> db 'log.color.level.warn', 'FFD700' -- Gold
[.levels.ERROR]: 'FF0000' -- -> db 'log.color.level.error', 'FF0000' -- Red
[.levels.FATAL]: 'FF0000' -- -> db 'log.color.level.fatal', 'FF0000' -- Red
[.levels.NOTICE]: '00FFFF' -- -> db 'log.color.level.notice', '00FFFF' -- Cyan
for id, level in pairs Logger.levels
Logger.__base[id\lower!] = (...) => @log level, ...
log = Logger('main')
T.log = log
load_db = (event, database) ->
T.SHAREXP_DB_CREATED = (event, database) ->
db = database if database.global_name == 'ShareXPDB'
T.SHAREXP_DB_LOADED = (event, database) ->
return unless database.global_name == db.global_name
logging_enabled = db 'log', logging_enabled
logging_level = db 'log.level', logging_level
logging_color_enabled = db 'log.color', logging_color_enabled
db_loaded = true
debug_enabled = db 'debug', debug_enabled
for level, color in pairs Logger.colors.levels
prefix = Logger\level_to_prefix(level)\lower!
Logger.colors.levels[level] = db "log.color.level.#{prefix}", color
T.SHAREXP_DB_UPDATED = (event, db, key) ->
return unless db.global_name == 'ShareXPDB'
switch key
when 'debug'
debug_enabled = db 'debug'
when 'log'
value = db 'log'
logging_enabled = value
log\notice L.log_status_changed, value and L.enabled or L.disabled
when 'log.color'
value = db 'log.color'
logging_color_enabled = value
log\notice L.log_coloring_changed, value and L.enabled or L.disabled
when 'log.level'
value = db 'log.level'
logging_level = value
log\notice L.log_level_changed, Logger\level_to_prefix value
when 'log.color.name'
color = db 'log.color.name'
Logger.colors.name = color
msg = L.log_color_changed L.name\gsub('^.', upper), color
log\notice decorate(msg, color)
else
prefix = key\match '^log%.color%.level%.(%w+)$'
if prefix
prefix = prefix\upper!
level = Logger\prefix_to_level prefix
color = db key
Logger.colors.levels[level] = color
msg = L.log_color_changed prefix, color
log\notice decorate(msg, color)