-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogging.lua
242 lines (202 loc) · 6.82 KB
/
logging.lua
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
--- Logging library to aid with logging things to files and whatnot.
local expect = require "cc.expect".expect
local init_time = os.epoch "utc"
---@class logging
local logging = {
---@enum logging-log_level
LOG_LEVEL = {
DEBUG = 0,
INFO = 1,
WARN = 2,
ERROR = 3,
FATAL = 4
}
}
local errored = false
local warned = false
local log_level = logging.LOG_LEVEL.INFO
local log_window = term.current()
local log_lines = {
n = 0
}
local log_file_formatter = "[%d][%s][%s] %s"
local log_formatter = "[%s][%s] "
local level_text_formatter = "0%s00%s00"
local function blit_print(text, text_color, back_color, printed, level)
log_window.blit(text, text_color, back_color)
local old = term.redirect(log_window)
local old_c = term.getTextColor()
term.setTextColor(
level == logging.LOG_LEVEL.DEBUG and colors.lightGray
or level == logging.LOG_LEVEL.WARN and colors.orange
or level == logging.LOG_LEVEL.ERROR and colors.red
or colors.white
)
print(printed)
term.setTextColor(old_c)
term.redirect(old)
end
--- Log information to the window and file.
---@param context string The context name.
---@param level logging-log_level The log level to use.
---@param level_name string The name of the log level.
---@param ... any The values to be included in the log. Concatenated with a space.
local function log(context, level, level_name, ...)
if level >= log_level then
local args = table.pack(...)
if args.n == 0 then
args = {n = 1, "Nothing."}
end
for i = 1, args.n do
args[i] = tostring(args[i])
end
local combined = table.concat(args, ' ')
log_lines.n = log_lines.n + 1
log_lines[log_lines.n] = log_file_formatter:format(
os.epoch "utc" - init_time,
level_name,
context,
combined
)
local text = log_formatter:format(level_name, context)
local t_color = level == logging.LOG_LEVEL.DEBUG and '8' -- light gray
or level == logging.LOG_LEVEL.INFO and '0' -- white
or level == logging.LOG_LEVEL.WARN and '4' -- yellow
or level == logging.LOG_LEVEL.ERROR and '1' -- orange
or level == logging.LOG_LEVEL.FATAL and 'e' -- red
or '6' -- pink
blit_print(
text,
level_text_formatter:format(
t_color:rep(#level_name), t_color:rep(#context)
),
('f'):rep(#text),
combined,
level
)
end
end
--- Create a new logging context.
---@param name string The name of the context.
---@return logging-log_context context The context object.
function logging.create_context(name)
expect(1, name, "string")
---@class logging-log_context
local context = {}
--- Log something with a custom level and level name.
---@param level logging-log_level The level to log at.
---@param level_name string The name of the level.
---@param ... any The values to be included in the log. Concatenated with a space.
function context.log(level, level_name, ...)
log(name, level, level_name, ...)
if level == logging.LOG_LEVEL.WARN then
warned = true
elseif level >= logging.LOG_LEVEL.ERROR then
errored = true
end
end
--- Send a debug message to the log.
---@param ... any The values to be included in the log. Concatenated with a space.
function context.debug(...)
log(name, logging.LOG_LEVEL.DEBUG, "DEBUG", ...)
end
--- Send a debug message, formatted, to the log.
---@param fmt string The format string.
---@param ... any The values to be included in the log. Concatenated with a space.
function context.debugf(fmt, ...)
context.debug(fmt:format(...))
end
--- Send an informational message to the log.
---@param ... any The values to be included in the log. Concatenated with a space.
function context.info(...)
log(name, logging.LOG_LEVEL.INFO, "INFO", ...)
end
--- Send an informational message, formatted, to the log.
---@param fmt string The format string.
---@param ... any The values to be included in the log. Concatenated with a space.
function context.infof(fmt, ...)
context.info(fmt:format(...))
end
--- Send a warning to the log.
---@param ... any The values to be included in the log. Concatenated with a space.
function context.warn(...)
log(name, logging.LOG_LEVEL.WARN, "WARN", ...)
warned = true
end
--- Send a warning, formatted, to the log.
---@param fmt string The format string.
---@param ... any The values to be included in the log. Concatenated with a space.
function context.warnf(fmt, ...)
context.warn(fmt:format(...))
end
--- Send an error to the log.
---@param ... any The values to be included in the log. Concatenated with a space.
function context.error(...)
log(name, logging.LOG_LEVEL.ERROR, "ERROR", ...)
errored = true
end
--- Send an error, formatted, to the log.
---@param fmt string The format string.
---@param ... any The values to be included in the log. Concatenated with a space.
function context.errorf(fmt, ...)
context.error(fmt:format(...))
end
--- Send a fatal error to the log.
---@param ... any The values to be included in the log. Concatenated with a space.
function context.fatal(...)
log(name, logging.LOG_LEVEL.FATAL, "FATAL", ...)
errored = true
end
--- Send a fatal error, formatted, to the log.
---@param fmt string The format string.
---@param ... any The values to be included in the log. Concatenated with a space.
function context.fatalf(fmt, ...)
context.fatal(fmt:format(...))
end
return setmetatable(context, logging)
end
--- Set the display level of the log. Log entries that are below this value will not be logged.
---@param level logging-log_level The log level.
function logging.set_level(level)
expect(1, level, "number")
log_level = level
end
--- Set the window that the logger uses. Otherwise will just output to whatever `term.current()` is at the time of requiring.
---@param win Redirect The window object to log to.
function logging.set_window(win)
expect(1, win, "table")
log_window = win
end
--- Check if an error has been thrown since the last time errors were cleared.
---@return boolean errored
function logging.has_errored()
return errored
end
--- Check if a warn has occurred since the last time warns were cleared.
---@return boolean warned
function logging.has_warned()
return warned
end
--- Clear the errors.
function logging.clear_error()
errored = false
end
--- Clear the warns.
function logging.clear_warn()
warned = false
end
--- Dump the log to a file.
---@param filename string The file to dump to.
---@param dont_clear boolean? If true, does not clear the log after dumping.
function logging.dump_log(filename, dont_clear)
local h, err = fs.open(filename, 'w') --[[@as WriteHandle]]
if not h then error(err, 0) end
for _, line in ipairs(log_lines) do
h.writeLine(line)
end
h.close()
if not dont_clear then
log_lines = {n = 0}
end
end
return logging