-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathlog.lua
More file actions
291 lines (253 loc) · 6.76 KB
/
log.lua
File metadata and controls
291 lines (253 loc) · 6.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
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
-- stylua: ignore start
local uv = vim.uv or vim.loop
local MODSTR = 'project.utils.log'
local TRACE = vim.log.levels.TRACE -- `0`
local DEBUG = vim.log.levels.DEBUG -- `1`
local INFO = vim.log.levels.INFO -- `2`
local WARN = vim.log.levels.WARN -- `3`
local ERROR = vim.log.levels.ERROR -- `4`
-- stylua: ignore end
---@class Project.LogLoc: Project.HistoryLoc
local Util = require('project.utils.util')
local Config = require('project.config')
---@class Project.Log
---@field logfile? string
---@field log_loc? Project.LogLoc
local Log = {}
---@param lvl vim.log.levels
---@return fun(...: any): output: string|nil
local function gen_log(lvl)
---@param ... any
---@return string|nil output
return function(...)
if not Config.options.log.enabled then
return
end
local msg = ''
for i = 1, select('#', ...) do
local sel = select(i, ...)
if sel then
if type(sel) == 'number' or type(sel) == 'boolean' then
sel = tostring(sel)
elseif not type(sel) == 'string' then
sel = vim.inspect(sel)
end
msg = ('%s %s'):format(msg, sel)
end
end
return Log.write(('%s\n'):format(msg), lvl)
end
end
Log.trace = gen_log(TRACE)
Log.warn = gen_log(WARN)
Log.error = gen_log(ERROR)
Log.info = gen_log(INFO)
Log.debug = gen_log(DEBUG)
---@return string|nil data
function Log.read_log()
if not Log.logfile then
return
end
local stat = uv.fs_stat(Log.logfile)
if not stat then
return
end
local fd = Log.open('r')
if not fd then
return
end
Log.setup_watch()
local data = uv.fs_read(fd, stat.size, -1)
return data
end
function Log.clear_log()
local success = uv.fs_unlink(Log.logfile)
if success then
vim.notify('(project.nvim): Log cleared successfully', INFO)
vim.g.project_log_cleared = 1
end
end
---Only runs once.
--- ---
function Log.setup_watch()
if Log.has_watch_setup then
return
end
local event = uv.new_fs_event()
if not event then
return
end
event:start(Log.logpath, {}, function(err, _, events)
if err or not events.change then
return
end
Log.read_log()
end)
Log.has_watch_setup = true
end
---@param data string
---@param lvl vim.log.levels
---@return string|nil written_data
function Log.write(data, lvl)
if not Config.options.log.enabled or vim.g.project_log_cleared == 1 then
return
end
Util.validate({
data = { data, { 'string' } },
lvl = { lvl, { 'number' } },
})
local fd = Log.open('a')
if not fd then
return
end
-- stylua: ignore start
local PFX = {
[TRACE] = '[TRACE] ',
[DEBUG] = '[DEBUG] ',
[INFO] = '[INFO] ',
[WARN] = '[WARN] ',
[ERROR] = '[ERROR] ',
}
-- stylua: ignore end
local msg = os.date(('%s ==> %s%s'):format('%H:%M:%S', PFX[lvl], data))
uv.fs_write(fd, msg, -1)
uv.fs_close(fd)
return msg
end
function Log.create_commands()
local Commands = require('project.commands')
Commands.new({
{
name = 'ProjectLog',
callback = function()
Log.toggle_win()
end,
desc = 'Opens the `project.nvim` log in a new tab',
},
{
name = 'ProjectLogClear',
with_ctx = false,
callback = function()
if Log.log_loc then
Log.close_win()
end
Log.clear_log()
end,
desc = 'Clears the `project.nvim` log',
},
})
end
---@param mode OpenMode
---@return integer|nil fd
function Log.open(mode)
require('project.utils.path').create_path(Log.logpath)
local dir_stat = uv.fs_stat(Log.logpath)
if not dir_stat or dir_stat.type ~= 'directory' then
error(('(%s.open): Projectpath stat is not valid!'):format(MODSTR), ERROR)
end
local fd = uv.fs_open(Log.logfile, mode, tonumber('644', 8))
return fd
end
function Log.init()
local log_cfg = Config.options.log
if not log_cfg.enabled then
return
end
Log.logpath = log_cfg.logpath
Log.logfile = Log.logpath .. '/project.log'
require('project.utils.path').create_path(Log.logpath)
local fd
local stat = uv.fs_stat(Log.logfile)
if not stat then
fd = Log.open('w')
uv.fs_close(fd)
fd = nil
end
stat = uv.fs_stat(Log.logfile) ---@type uv.fs_stat.result
local max_size = log_cfg.max_size
if (stat.size / 1024) / 1024 >= max_size then
fd = Log.open('w')
uv.fs_ftruncate(fd, 0)
uv.fs_close(fd)
fd = nil
end
fd = Log.open('a')
local head = ('='):rep(45)
uv.fs_write(
fd,
(stat.size >= 1 and '\n' or '')
.. os.date(('%s %s %s\n'):format(head, '%x (%H:%M:%S)', head))
)
Log.create_commands()
end
function Log.open_win()
local enabled = Config.options.log.enabled
if not (Log.logfile and enabled) then
return
end
if vim.g.project_log_cleared == 1 then
vim.notify(('(%s.open_win): Log has been cleared. Try restarting.'):format(MODSTR), WARN)
return
end
if not require('project.utils.path').exists(Log.logfile) then
error(('(%s.open_win): Bad logfile path!'):format(MODSTR), ERROR)
end
if Log.log_loc then -- Log window appears to be open
return
end
local stat = uv.fs_stat(Log.logfile)
if not stat then
return
end
local fd = uv.fs_open(Log.logfile, 'r', tonumber('644', 8))
if not fd then
return
end
local data = uv.fs_read(fd, stat.size)
if not data then
return
end
vim.cmd.tabnew()
vim.schedule(function()
local bufnr = vim.api.nvim_get_current_buf()
local win = vim.api.nvim_get_current_win()
vim.api.nvim_buf_set_name(bufnr, 'Project Log')
vim.api.nvim_buf_set_lines(
bufnr,
0,
-1,
true,
vim.split(data, '\n', { plain = true, trimempty = false })
)
---@type vim.api.keyset.option, vim.api.keyset.option
local win_opts, buf_opts = { win = win }, { buf = bufnr }
vim.api.nvim_set_option_value('signcolumn', 'no', win_opts)
vim.api.nvim_set_option_value('list', false, win_opts)
vim.api.nvim_set_option_value('number', false, win_opts)
vim.api.nvim_set_option_value('wrap', false, win_opts)
vim.api.nvim_set_option_value('colorcolumn', '', win_opts)
vim.api.nvim_set_option_value('filetype', 'log', buf_opts)
vim.api.nvim_set_option_value('fileencoding', 'utf-8', buf_opts)
vim.api.nvim_set_option_value('buftype', 'nowrite', buf_opts)
vim.api.nvim_set_option_value('modifiable', false, buf_opts)
vim.keymap.set('n', 'q', Log.close_win, { buffer = bufnr })
Log.log_loc = { win = win, bufnr = bufnr }
end)
end
function Log.close_win()
if not Log.log_loc then
return
end
pcall(vim.api.nvim_buf_delete, Log.log_loc.bufnr, { force = true })
pcall(vim.cmd.tabclose)
Log.log_loc = nil
end
function Log.toggle_win()
if not Log.log_loc then
Log.open_win()
return
end
Log.close_win()
end
return Log
-- vim: set ts=2 sts=2 sw=2 et ai si sta: