-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathLog.lua
138 lines (115 loc) · 2.77 KB
/
Log.lua
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
local Event
local ansicolors = require("thirdparty.ansicolors")
--- @module Log
local Log = {}
local levels = {
trace = 5,
debug = 4,
info = 3,
warn = 2,
error = 1
}
local level = levels.info
local filter = nil
-- copied from internal.env
local PATH_CACHE = {}
local function convert_to_require_path(path)
if PATH_CACHE[path] then
return PATH_CACHE[path]
end
local old_path = path
local path = path
path = string.strip_suffix(path, ".lua")
path = string.strip_suffix(path, ".fnl")
path = string.gsub(path, "/", ".")
path = string.gsub(path, "\\", ".")
path = string.strip_suffix(path, ".init")
path = string.gsub(path, "^%.+", "")
PATH_CACHE[old_path] = path
return path
end
local function format(kind, color, s, ...)
local out = string.format(s, ...)
local trace = debug.getinfo(3, "S")
local req_paths = true
local source = "<...>"
if trace then
local file = trace.source:sub(2)
local line = trace.linedefined
local req_path = convert_to_require_path(file)
if req_paths then
source = req_path
else
source = string.format("%s:%d", file, line)
end
end
if filter and not string.match(source, filter) then
return
end
local str = string.format("[%s][%s] %s", kind, source, out)
local formatted = ansicolors(("%%{%s}%s%%{reset}"):format(color, str))
print(formatted)
Event = Event or require("api.Event")
Event.trigger("base.on_log_message", {message=out,level=kind,source=source})
end
--- @tparam string l
function Log.set_level(l)
if type(l) == "string" then
l = levels[l] or 3
end
level = l
end
--- @tparam string l
function Log.has_level(l)
if type(l) == "string" then
l = levels[l] or 3
end
return level >= l
end
function Log.set_filter(tag)
if tag ~= nil and type(tag) ~= "string" then
return
end
filter = tag
end
--- @tparam string s Format string for string.format
--- @param ...
function Log.trace(s, ...)
if level < 5 then
return
end
format("trace", "magenta", s, ...)
end
--- @tparam string s Format string for string.format
--- @param ...
function Log.debug(s, ...)
if level < 4 then
return
end
format("debug", "bright black", s, ...)
end
--- @tparam string s Format string for string.format
--- @param ...
function Log.info(s, ...)
if level < 3 then
return
end
format("info", "white", s, ...)
end
--- @tparam string s Format string for string.format
--- @param ...
function Log.warn(s, ...)
if level < 2 then
return
end
format("warn", "yellow", s, ...)
end
--- @tparam string s Format string for string.format
--- @param ...
function Log.error(s, ...)
if level < 1 then
return
end
format("error", "red", s, ...)
end
return Log