forked from zbirenbaum/copilot.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.lua
More file actions
152 lines (127 loc) · 3.97 KB
/
logger.lua
File metadata and controls
152 lines (127 loc) · 3.97 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
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
local uv = vim.uv
---@class logger
local mod = {
log_to_file = false,
log_file = vim.fn.stdpath("log") .. "/copilot-lua.log",
file_log_level = vim.log.levels.WARN,
print_log = true,
print_log_level = vim.log.levels.WARN,
}
local log_level_names = {
[vim.log.levels.ERROR] = "ERROR", --4
[vim.log.levels.WARN] = "WARN", --3
[vim.log.levels.INFO] = "INFO", --2
[vim.log.levels.DEBUG] = "DEBUG", --1
[vim.log.levels.TRACE] = "TRACE", --0
}
---@return string timestamp
local function get_timestamp_with_ms()
local seconds = os.time()
local milliseconds = math.floor((os.clock() % 1) * 1000)
return string.format("%s.%03d", os.date("%Y-%m-%d %H:%M:%S", seconds), milliseconds)
end
---@param log_level integer --vim.log.levels
---@param msg string
---@param data any
---@return string log_msg
local function format_log(log_level, msg, data)
local log_level_name = log_level_names[log_level]
local log_msg = string.format("%s [%s]: %s", get_timestamp_with_ms(), log_level_name, msg)
if data then
log_msg = string.format("%s\n%s", log_msg, vim.inspect(data))
end
return log_msg
end
---@param log_level integer -- one of the vim.log.levels
---@param msg string
---@param data any
local function notify_log(log_level, msg, data)
local log_msg = format_log(log_level, msg, data)
vim.notify(log_msg, log_level)
end
---@param log_level integer -- one of the vim.log.levels
---@param log_file string
---@param msg string
---@param data any
local function write_log(log_level, log_file, msg, data)
local log_msg = format_log(log_level, msg, data) .. "\n"
uv.fs_open(log_file, "a", tonumber("644", 8), function(err, fd)
if err then
notify_log(vim.log.levels.ERROR, "Failed to open log file: " .. err)
return
end
uv.fs_write(fd, log_msg, -1, function(write_err)
if write_err then
notify_log(vim.log.levels.ERROR, "Failed to write to log file: " .. write_err)
end
uv.fs_close(fd)
end)
end)
end
---@param log_level integer -- one of the vim.log.levels
---@param msg string
---@param data any
---@param force_print boolean
function mod.log(log_level, msg, data, force_print)
if mod.log_to_file and (mod.file_log_level <= log_level) then
write_log(log_level, mod.log_file, msg, data)
end
if force_print or (mod.print_log and (mod.print_log_level <= log_level)) then
notify_log(log_level, msg, data)
end
end
---@param msg string
---@param data any
function mod.debug(msg, data)
mod.log(vim.log.levels.DEBUG, msg, data, false)
end
---@param msg string
---@param data any
function mod.trace(msg, data)
mod.log(vim.log.levels.TRACE, msg, data, false)
end
---@param msg string
---@param data any
function mod.error(msg, data)
mod.log(vim.log.levels.ERROR, msg, data, false)
end
---@param msg string
---@param data any
function mod.warn(msg, data)
mod.log(vim.log.levels.WARN, msg, data, false)
end
---@param msg string
---@param data any
function mod.info(msg, data)
mod.log(vim.log.levels.INFO, msg, data, false)
end
---@param msg string
---@param data any
function mod.notify(msg, data)
mod.log(vim.log.levels.INFO, msg, data, true)
end
---@param conf copilot_config_logging
function mod.setup(conf)
mod.log_file = conf.file
mod.file_log_level = conf.file_log_level
mod.print_log_level = conf.print_log_level
mod.log_to_file = conf.log_to_file
mod.print_log = conf.print_log
if conf.trace_lsp ~= "off" then
vim.lsp.handlers["$/logTrace"] = function(_, result, _)
if not result then
return
end
mod.trace(string.format("LSP trace - %s", result.message), result.verbose)
end
end
if conf.trace_lsp_progress then
vim.lsp.handlers["$/progress"] = function(_, result, _)
if not result then
return
end
mod.trace(string.format("LSP progress - token %s", result.token), result.value)
end
end
end
return mod