summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-07-05 22:10:47 +0800
committerGitHub <[email protected]>2019-07-05 22:10:47 +0800
commitcf8ff12bc05fde4cb94344d1ca1bce31a4778527 (patch)
treec17e7b563d8a4bcd3fac50ffbf8ae78864efac41
parent789e43df6a5430db61fae2c2b268ab42376616e6 (diff)
parentdae10753450c75af10c300dbaf00653b15a27e87 (diff)
Merge pull request #477 from OpportunityLiu/improve-dump
improve dump
-rw-r--r--core/.clang-format4
-rw-r--r--xmake/core/base/dump.lua123
-rw-r--r--xmake/core/base/utils.lua39
-rw-r--r--xmake/core/sandbox/modules/utils.lua5
4 files changed, 135 insertions, 36 deletions
diff --git a/core/.clang-format b/core/.clang-format
index 70b14e93c..92782d2e0 100644
--- a/core/.clang-format
+++ b/core/.clang-format
@@ -90,10 +90,6 @@ PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
-RawStringFormats:
- - Delimiter: pb
- Language: TextProto
- BasedOnStyle: google
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
diff --git a/xmake/core/base/dump.lua b/xmake/core/base/dump.lua
index 56b98f2ef..fc67749ad 100644
--- a/xmake/core/base/dump.lua
+++ b/xmake/core/base/dump.lua
@@ -107,53 +107,124 @@ function dump._print_reference(value)
io.write(colors.translate("${reset}", { patch_reset = false }))
end
+function dump._print_table_anchor(value, printed_set)
+ printed_set.len = printed_set.len + 1
+ io.write(" ")
+ dump._print_anchor(printed_set.len)
+ io.write("\n")
+ printed_set[value] = printed_set.len
+end
+
-- print table
function dump._print_table(value, first_indent, remain_indent, printed_set)
local first_level = not printed_set
io.write(first_indent)
- local strrep = rawget(getmetatable(value) or {}, "__tostring") and tostring(value)
- if not first_level and strrep then
- return dump._print_default(strrep)
+ local metatable = getmetatable(value)
+ local tostringmethod = metatable and rawget(metatable, "__tostring")
+ if not first_level and tostringmethod then
+ local ok, strrep = pcall(tostringmethod, value, value)
+ if ok then
+ return dump._print_default(strrep)
+ end
end
printed_set = printed_set or { len = 0 }
- io.write(colors.translate("${dim}{"))
local inner_indent = remain_indent .. " "
- local is_arr = table.is_array(value)
+ local index_table = metatable and rawget(metatable, "__index")
+ if type(index_table) ~="table" then index_table = nil end
local first_value = true
- for k, v in pairs(value) do
+ -- print open brackets
+ io.write(colors.translate("${dim}{"))
+
+ local function print_newline()
if first_value then
- printed_set.len = printed_set.len + 1
- io.write(" ")
- dump._print_anchor(printed_set.len)
- io.write("\n")
- printed_set[value] = printed_set.len
+ dump._print_table_anchor(value, printed_set)
first_value = false
- if strrep then
- io.write(inner_indent)
- dump._print_keyword("(tostring)")
- io.write(colors.translate("${dim} = "))
- dump._print_scalar(strrep)
- io.write(",\n")
- end
else
io.write(",\n")
end
io.write(inner_indent)
+ end
+
+ if first_level then
+ -- print metamethods
+ for k, v in pairs(metatable or {}) do
+ if k:startswith("__") and not (k == "__index" and type(v) == "table") then
+ print_newline()
+ local funcname = k:sub(3)
+ dump._print_keyword(funcname)
+ io.write(colors.translate("${dim} = "))
+ if funcname == "tostring" or funcname == "len" then
+ local ok, result = pcall(v, value, value)
+ if ok then
+ dump._print_scalar(result)
+ io.write(" (evaluated)")
+ else
+ dump._print_scalar(v)
+ end
+ elseif v and printed_set[v] then
+ dump._print_reference(printed_set[v])
+ else
+ dump._print_scalar(v)
+ end
+ end
+ end
+
+ -- print index methods
+ for k, v in pairs(index_table or {}) do
+ -- hide private interfaces
+ if type(k) ~= "string" or not k:startswith("_") then
+ print_newline()
+ dump._print_keyword("(")
+ dump._print_scalar(k, true)
+ dump._print_keyword(")")
+ io.write(colors.translate("${dim} = "))
+ if v and printed_set[v] then
+ dump._print_reference(printed_set[v])
+ else
+ dump._print_scalar(v)
+ end
+ end
+ end
+ end
+
+ -- print array items
+ local is_arr = table.is_array(value) and (table.maxn(value) < 2 * #value)
+ if is_arr then
+ for i = 1,table.maxn(value) do
+ print_newline()
+ local v = value[i]
+ if type(v) == "table" then
+ if printed_set[v] then
+ dump._print_reference(printed_set[v])
+ else
+ dump._print_table(v, "", inner_indent, printed_set)
+ end
+ else
+ dump._print_scalar(v)
+ end
+ end
+ end
+
+ -- print data
+ for k, v in pairs(value) do
if not is_arr or type(k) ~= "number" then
+ print_newline()
dump._print_scalar(k, true)
io.write(colors.translate("${dim} = "))
- end
- if type(v) == "table" then
- if printed_set[v] then
- dump._print_reference(printed_set[v])
+ if type(v) == "table" then
+ if printed_set[v] then
+ dump._print_reference(printed_set[v])
+ else
+ dump._print_table(v, "", inner_indent, printed_set)
+ end
else
- dump._print_table(v, "", inner_indent, printed_set)
+ dump._print_scalar(v)
end
- else
- dump._print_scalar(v)
end
end
+
+ -- print close brackets
if first_value then
io.write(colors.translate(" ${dim}}"))
else
@@ -163,7 +234,7 @@ end
-- print value
function dump._print(value, indent)
- indent = indent or ""
+ indent = tostring(indent or "")
if type(value) == "table" then
dump._print_table(value, indent, indent:gsub(".", " "), nil)
else
diff --git a/xmake/core/base/utils.lua b/xmake/core/base/utils.lua
index ec7c63bcc..d8f1ec7c2 100644
--- a/xmake/core/base/utils.lua
+++ b/xmake/core/base/utils.lua
@@ -30,11 +30,44 @@ local io = require("base/io")
local dump = require("base/dump")
-- dump value
-function utils.dump(value, indent)
- if not option.get("quiet") then
- dump(value, indent or "")
+function utils.dump(...)
+ if option.get("quiet") then
+ return ...
+ end
+
+ -- show caller info
+ if option.get("diagnosis") then
+ local info = debug.getinfo(2)
+ local line = info.currentline
+ if not line or line < 0 then line = info.linedefined end
+ io.write(string.format("dump form %s %s:%s\n", info.name or "<anonymous>", info.source, line))
+ end
+
+ local values = table.pack(...)
+ if values.n == 0 then
+ return
+ end
+ local indent = nil
+ local values_count = values.n
+ values.n = nil
+ -- use last input as indent if it is a string
+ if values_count > 1 and type(values[values_count]) == "string" then
+ indent = values[values_count]
+ values[values_count] = nil
+ values_count = values_count - 1
+ end
+
+ if values_count == 1 then
+ dump(values[1], indent or "")
io.write("\n")
+ else
+ for i = 1, values_count do
+ dump(values[i], indent or string.format("%2d: ", i))
+ io.write("\n")
+ end
end
+
+ return table.unpack(values, 1, values_count)
end
-- print string with newline
diff --git a/xmake/core/sandbox/modules/utils.lua b/xmake/core/sandbox/modules/utils.lua
index 7e3129fcb..a81c84ded 100644
--- a/xmake/core/sandbox/modules/utils.lua
+++ b/xmake/core/sandbox/modules/utils.lua
@@ -189,9 +189,8 @@ function sandbox_utils.confirm(opt)
end
-- dump value
-function sandbox_utils.dump(value, indent)
- return utils.dump(value, indent)
-end
+-- do not change to a function call to utils.dump since debug.getinfo is called in utils.dump to get caller info
+sandbox_utils.dump = utils.dump
-- return module
return sandbox_utils