-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtracing_util.tl
54 lines (42 loc) · 1.49 KB
/
tracing_util.tl
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
local inspect <const> = require("inspect")
local record tracing_util
end
-- we use our own custom formatting convention here which is:
-- :l = do not add quotes around value
-- @ = serialize the value to string somehow
-- 0*.0* (eg. 0.00, 00.0) = pad zeros and choose decimal amount
function tracing_util.custom_tostring(value:any, formatting:string):string
local value_type = type(value)
if formatting == nil or formatting == "" or formatting == "l" then
if value_type == "thread" then
return "<thread>"
elseif value_type == "function" then
return "<function>"
elseif value_type == "string" then
if formatting == "l" then
return value as string
else
return "'" .. (value as string) .. "'"
end
else
return tostring(value)
end
end
if formatting == "@" then
return inspect(value, { indent = "", newline = " " })
end
-- Otherwise, we assume the format string is meant for string.format
return string.format(formatting, value)
end
function tracing_util.custom_format(message:string, message_args:{any}):string
local count = 0
-- non greedy match for {} pairs
local pattern = "{(.-)}"
local expanded_message = string.gsub(message, pattern, function(formatting:string):string
count = count + 1
local field_value = message_args[count]
return tracing_util.custom_tostring(field_value, formatting)
end)
return expanded_message
end
return tracing_util