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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
local levels = require("99.logger.level")
local time = require("99.time")
local MAX_REQUEST_DEFAULT = 5
--- @type table<number, _99.Logger.RequestLogs>
local logger_cache = {}
local logger_list = {}
local max_requests_in_logger_cache = MAX_REQUEST_DEFAULT
--- @class _99.Logger.Options
--- @docs included
--- @field level number?
--- @field type? "print" | "void" | "file"
--- @field path string?
--- @field print_on_error? boolean
--- @field max_requests_cached? number
--- @param ... any
--- @return table<string, any>
local function to_args(...)
local count = select("#", ...)
local out = {}
assert(
count % 2 == 0,
"you cannot call logging with an odd number of args. e.g: msg, [k, v]..."
)
for i = 1, count, 2 do
local key = select(i, ...)
local value = select(i + 1, ...)
assert(type(key) == "string", "keys in logging must be strings")
assert(out[key] == nil, "key collision in logs: " .. key)
out[key] = value
end
return out
end
--- @param log_statement table<string, any>
--- @param args table<string, any>
local function put_args(log_statement, args)
for k, v in pairs(args) do
assert(log_statement[k] == nil, "key collision in logs: " .. k)
log_statement[k] = v
end
end
--- @class LoggerSink
--- @field write_line fun(LoggerSink, string): nil
--- @class VoidLogger : LoggerSink
local VoidSink = {}
VoidSink.__index = VoidSink
function VoidSink.new()
return setmetatable({}, VoidSink)
end
--- @param _ string
function VoidSink:write_line(_)
_ = self
end
--- @class FileSink : LoggerSink
--- @field fd number
local FileSink = {}
FileSink.__index = FileSink
--- @param path string
--- @return LoggerSink
function FileSink:new(path)
-- Ensure the directory is already there (*thanks Windows*)
vim.fn.mkdir(vim.fn.fnamemodify(path, ":h"), "p")
-- 420 decimal == 644 octal (rw-r--r--)
local fd, err = vim.uv.fs_open(path, "w", 420)
if not fd then
error("unable to file sink: " .. err)
end
return setmetatable({
fd = fd,
}, self)
end
--- @param str string
function FileSink:write_line(str)
local success, err = vim.uv.fs_write(self.fd, str .. "\n")
if not success then
error("unable to write to file sink", err)
end
vim.uv.fs_fsync(self.fd)
end
--- @class PrintSink : LoggerSink
local PrintSink = {}
PrintSink.__index = PrintSink
--- @return LoggerSink
function PrintSink:new()
return setmetatable({}, self)
end
--- @param str string
function PrintSink:write_line(str)
local _ = self
print(str)
end
--- @class _99.Logger.RequestLogs
--- @field last_access number
--- @field logs string[]
--- @class _99.Logger
--- @field level number
--- @field sink LoggerSink
--- @field print_on_error boolean
--- @field extra_params table<string, any>
local Logger = {}
Logger.__index = Logger
--- @param level number?
--- @return _99.Logger
function Logger:new(level)
level = level or levels.FATAL
return setmetatable({
sink = VoidSink:new(),
level = level,
print_on_error = false,
extra_params = {},
}, self)
end
--- @return _99.Logger
function Logger:clone()
local params = {}
for k, v in pairs(self.extra_params) do
params[k] = v
end
return setmetatable({
sink = self.sink,
level = self.level,
print_on_error = self.print_on_error,
extra_params = params,
}, Logger)
end
--- @param path string
--- @return _99.Logger
function Logger:file_sink(path)
self.sink = FileSink:new(path)
return self
end
--- @return _99.Logger
function Logger:void_sink()
self.sink = VoidSink:new()
return self
end
--- @return _99.Logger
function Logger:print_sink()
self.sink = PrintSink:new()
return self
end
--- @param area string
--- @return _99.Logger
function Logger:set_area(area)
local new_logger = self:clone()
new_logger.extra_params["Area"] = area
return new_logger
end
--- @param xid number
--- @return _99.Logger
function Logger:set_id(xid)
local new_logger = self:clone()
new_logger.extra_params["id"] = xid
return new_logger
end
--- @param level number
--- @return _99.Logger
function Logger:set_level(level)
self.level = level
return self
end
--- @return _99.Logger
function Logger:on_error_print_message()
self.print_on_error = true
return self
end
--- @param opts _99.Logger.Options?
function Logger:configure(opts)
if not opts then
return
end
if opts.level then
self:set_level(opts.level)
end
if opts.type == "print" then
self:print_sink()
elseif opts.type == "file" then
assert(
opts.path,
"if you choose file for logger, you must have a path specified"
)
self:file_sink(opts.path)
else
self:void_sink()
end
if opts.print_on_error then
self:on_error_print_message()
end
max_requests_in_logger_cache = opts.max_requests_cached or MAX_REQUEST_DEFAULT
end
--- @param line string
function Logger:_cache_log(line)
local id = self.extra_params.id
if not id then
return
end
local cache = logger_cache[id]
local new_cache = false
if not cache then
cache = {
last_access = time.now(),
logs = {},
}
logger_cache[id] = cache
table.insert(logger_list, id)
new_cache = true
end
cache.last_access = time.now()
table.insert(cache.logs, line)
table.sort(logger_list, function(a, b)
assert(
logger_cache[a] and logger_cache[b],
"logger list is out of sync with logger cache: "
.. tostring(a)
.. " and "
.. tostring(b)
)
local a_time = logger_cache[a].last_access
local b_time = logger_cache[b].last_access
return a_time > b_time
end)
if not new_cache then
return
end
Logger._trim_cache()
end
--- This is a _TEST ONLY_ function. you should not call this function outside
--- of unit tests
function Logger.reset()
logger_cache = {}
max_requests_in_logger_cache = MAX_REQUEST_DEFAULT
end
--- @return string[][]
function Logger.logs()
local out = {}
for _, id in ipairs(logger_list) do
local request_logs = logger_cache[id]
table.insert(out, request_logs.logs)
end
return out
end
--- @param level number
---@param msg string
---@param ... any
function Logger:_log(level, msg, ...)
if self.level > level then
return
end
local log_statement = {
level = levels.levelToString(level),
msg = msg,
}
put_args(log_statement, to_args(...))
put_args(log_statement, self.extra_params)
assert(log_statement["id"], "every log must have an id associated with it")
local json_string = vim.json.encode(log_statement)
if self.print_on_error and level == levels.ERROR then
print(json_string)
end
self:_cache_log(json_string)
self.sink:write_line(json_string)
end
--- @param msg string
--- @param ... any
function Logger:info(msg, ...)
self:_log(levels.INFO, msg, ...)
end
--- @param msg string
--- @param ... any
function Logger:warn(msg, ...)
self:_log(levels.WARN, msg, ...)
end
--- @param msg string
--- @param ... any
function Logger:debug(msg, ...)
self:_log(levels.DEBUG, msg, ...)
end
--- @param msg string
--- @param ... any
function Logger:error(msg, ...)
self:_log(levels.ERROR, msg, ...)
end
--- @param msg string
--- @param ... any
function Logger:fatal(msg, ...)
self:_log(levels.FATAL, msg, ...)
assert(false, "fatal msg recieved: " .. msg, ...)
end
--- @param test any
---@param msg string
---@param ... any
function Logger:assert(test, msg, ...)
if not test then
self:fatal(msg, ...)
end
end
function Logger._trim_cache()
local count = 0
local oldest = nil
local oldest_key = nil
for k, log in pairs(logger_cache) do
if oldest == nil or log.last_access < oldest.last_access then
oldest = log
oldest_key = k
end
count = count + 1
end
if count > max_requests_in_logger_cache then
assert(oldest_key, "oldest key must exist")
logger_cache[oldest_key] = nil
for i, id in ipairs(logger_list) do
if id == oldest_key then
table.remove(logger_list, i)
break
end
end
end
end
function Logger.set_max_cached_requests(count)
max_requests_in_logger_cache = count
Logger._trim_cache()
end
local module_logger = Logger:new(levels.DEBUG)
return module_logger
|