如何转储所有_G表内容

时间:2017-02-22 07:01:11

标签: lua

我想转储_G表。在_G表中还有其他表也转储(内联表)。我希望有一个好的格式。我有一个例子,但使用它转储_G表有一些问题

function print_table(node)
-- to make output beautiful
local function tab(amt)
    local str = ""
    for i=1,amt do
        str = str .. "\t"
    end
    return str
end

local cache, stack, output = {},{},{}
local depth = 1
local output_str = "{\n"

while true do
    local size = 0
    for k,v in pairs(node) do
        size = size + 1
    end

    local cur_index = 1
    for k,v in pairs(node) do
        if (cache[node] == nil) or (cur_index >= cache[node]) then

            if (string.find(output_str,"}",output_str:len())) then
                output_str = output_str .. ",\n"
            elseif not (string.find(output_str,"\n",output_str:len())) then
                output_str = output_str .. "\n"
            end

            -- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
            table.insert(output,output_str)
            output_str = ""

            local key
            if (type(k) == "number" or type(k) == "boolean") then
                key = "[" ..type(k).. ":"..tostring(k).."]"
            else
                key = "['"..type(k).. ":"..tostring(k).."']"
            end

            if (type(v) == "number" or type(v) == "boolean") then
                output_str = output_str .. tab(depth) .. key .. " = "..tostring(v)
            elseif (type(v) == "table") then
                output_str = output_str .. tab(depth) .. key .. " = {\n"
                table.insert(stack,node)
                table.insert(stack,v)
                cache[node] = cur_index+1
                break
            else
                output_str = output_str .. tab(depth) .. key ..  " = '"..tostring(v).."'"
            end

            if (cur_index == size) then
                output_str = output_str .. "\n" .. tab(depth-1) .. "}"
            else
                output_str = output_str .. ","
            end
        else
            -- close the table
            if (cur_index == size) then
                output_str = output_str .. "\n" .. tab(depth-1) .. "}"
            end
        end

        cur_index = cur_index + 1
    end

    if (size == 0) then
        output_str = output_str .. "\n" .. tab(depth-1) .. "}"
    end

    if (#stack > 0) then
        node = stack[#stack]
        stack[#stack] = nil
        depth = cache[node] == nil and depth + 1 or depth - 1
    else
        break
    end
end

-- This is necessary for working with HUGE tables otherwise we run out of memory using concat on huge strings
table.insert(output,output_str)
output_str = table.concat(output)

print(output_str) end

这是结果日志

------start-------
['function:add_msg_define'] = 'function: 0x7c6baa98',
['number:BLEND_DST_RGB'] = 32968,
['function:_attachShader'] = 'function: 0x7c626cf0',
['number:INVALID_OPERATION'] = 1282,
['function:drawArrays'] = 'function: 0x7c627480',
['number:SRGB8_ALPHA8_EXT'] = 35907,
['number:CCW'] = 2
------end-------

我不知道如何修复此功能

1 个答案:

答案 0 :(得分:1)

我用这个:

-- deep dumps the contents of the table and it's contents' contents
function deepdump( tbl )
    local checklist = {}
    local function innerdump( tbl, indent )
        checklist[ tostring(tbl) ] = true
        for k,v in pairs(tbl) do
            print(indent..k,v,type(v),checklist[ tostring(tbl) ])
            if (type(v) == "table" and not checklist[ tostring(v) ]) then innerdump(v,indent.."    ") end
        end
    end
    print("=== DEEPDUMP -----")
    checklist[ tostring(tbl) ] = true
    innerdump( tbl, "" )
    print("------------------")
end

https://gist.github.com/HoraceBury/9321964