-
-
Notifications
You must be signed in to change notification settings - Fork 639
Expand file tree
/
Copy pathlog.lua
More file actions
129 lines (111 loc) · 3.56 KB
/
Copy pathlog.lua
File metadata and controls
129 lines (111 loc) · 3.56 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
local config = require("nvim-tree.config")
---@alias LogTypes "all" | "config" | "copy_paste" | "dev" | "diagnostics" | "git" | "profile" | "watcher"
---@type table<LogTypes, boolean>
local types = {}
---@type string
local file_path
local M = {}
--- Write to log file
---@param typ string as per log.types config
---@param fmt string for string.format
---@param ... any arguments for string.format
function M.raw(typ, fmt, ...)
if not M.enabled(typ) then
return
end
local line = string.format(fmt, ...)
local file = io.open(file_path, "a")
if file then
io.output(file)
io.write(line)
io.close(file)
end
end
--- Write to a new file
---@param typ LogTypes as per log.types config
---@param path string absolute path
---@param fmt string for string.format
---@param ... any arguments for string.format
function M.file(typ, path, fmt, ...)
if not M.enabled(typ) then
return
end
local line = string.format(fmt, ...)
local file = io.open(path, "w")
if file then
io.output(file)
io.write(line)
io.close(file)
end
end
---@class Profile
---@field start number nanos
---@field tag string
--- Write profile start to log file
--- START is prefixed
---@param fmt string for string.format
---@param ... any arguments for string.format
---@return Profile to pass to profile_end
function M.profile_start(fmt, ...)
local profile = {}
if M.enabled("profile") then
profile.start = vim.uv.hrtime()
profile.tag = string.format((fmt or "???"), ...)
M.line("profile", "START %s", profile.tag)
end
return profile
end
--- Write profile end to log file
--- END is prefixed and duration in seconds is suffixed
---@param profile Profile returned from profile_start
function M.profile_end(profile)
if M.enabled("profile") and type(profile) == "table" then
local millis = profile.start and math.modf((vim.uv.hrtime() - profile.start) / 1000000) or -1
M.line("profile", "END %s %dms", profile.tag or "", millis)
end
end
--- Write to log file
--- time and typ are prefixed and a trailing newline is added
---@param typ LogTypes as per log.types config
---@param fmt string for string.format
---@param ... any arguments for string.format
function M.line(typ, fmt, ...)
if M.enabled(typ) then
M.raw(typ, string.format("[%s] [%s] %s\n", os.date("%Y-%m-%d %H:%M:%S"), typ, (fmt or "???")), ...)
end
end
local inspect_opts = {}
---@param opts table
function M.set_inspect_opts(opts)
inspect_opts = opts
end
--- Write to log file the inspection of a node
---@param typ LogTypes as per log.types config
---@param node Node node to be inspected
---@param fmt string for string.format
---@param ... any arguments for string.format
function M.node(typ, node, fmt, ...)
if M.enabled(typ) then
M.raw(typ, string.format("[%s] [%s] %s\n%s\n", os.date("%Y-%m-%d %H:%M:%S"), typ, (fmt or "???"), vim.inspect(node, inspect_opts)), ...)
end
end
--- Logging is enabled for typ or all
---@param typ LogTypes as per log.types config
---@return boolean
function M.enabled(typ)
return file_path ~= nil and (types[typ] or types.all)
end
--- Create the log file and enable logging, if globally configured
function M.start()
if config.g.log and config.g.log.enable and config.g.log.types then
types = config.g.log.types
file_path = string.format("%s/nvim-tree.log", vim.fn.stdpath("log"), os.date("%H:%M:%S"), vim.env.USER)
if config.g.log.truncate then
os.remove(file_path)
end
if config.g.notify.threshold <= vim.log.levels.DEBUG then
require("nvim-tree.notify").debug("nvim-tree.lua logging to " .. file_path)
end
end
end
return M