forked from sudo-tee/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.lua
More file actions
76 lines (68 loc) · 1.76 KB
/
Copy pathlog.lua
File metadata and controls
76 lines (68 loc) · 1.76 KB
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
local M = {}
local config = require('opencode.config')
local log_path = (config.logging and config.logging.outfile) or vim.fn.stdpath('log') .. '/opencode.log'
local log_levels = { debug = 1, info = 2, warn = 3, error = 4 }
local current_level = log_levels[(config.logging and config.logging.level or 'warn'):lower()] or 3
local function caller_info()
local level = 2
while true do
local info = debug.getinfo(level, 'Sl')
if not info then return '' end
local file = info.source:gsub('^@', '')
if not file:match('/log%.lua$') then
return string.format(' [%s:%d]', file, info.currentline)
end
level = level + 1
end
end
local function log(level, msg, ...)
if not config.logging.enabled then
return
end
if log_levels[level] < current_level then
return
end
local line = string.format(
'[%s] [opencode] [%s]%s %s\n',
os.date('%Y-%m-%d %H:%M:%S'),
level:upper(),
caller_info(),
string.format(msg, ...)
)
local file = io.open(log_path, 'a')
if file then
file:write(line)
file:close()
end
end
function M.debug(msg, ...)
log('debug', msg, ...)
end
function M.info(msg, ...)
log('info', msg, ...)
end
function M.warn(msg, ...)
log('warn', msg, ...)
end
function M.error(msg, ...)
log('error', msg, ...)
end
function M.get_path()
return config.logging.enabled and log_path or nil
end
--- Emit a user-visible notification and write the same message to the log.
--- @param msg string
--- @param level integer vim.log.levels.*
function M.notify(msg, level)
if level == vim.log.levels.ERROR then
log('error', msg)
elseif level == vim.log.levels.WARN then
log('warn', msg)
else
log('info', msg)
end
vim.schedule(function()
vim.notify('[opencode.nvim] ' .. msg, level)
end)
end
return M