[cocos2dx_Lua]打印用户数据

本文详细介绍了在Cocos2d-x使用Lua时,如何有效地打印和调试用户数据,帮助开发者更好地理解和解决问题。
摘要由CSDN通过智能技术生成
local m = {}
function m:printTT(content, ...)
    local tab = 0
    local out_list = {}

    local function printk(value, key, tab)
        if key == nil then
            return
        end
        if type(key) ~= "number" then
            key = tostring(key)
        else
            key = string.format("[%d]", key)
        end
        if type(value) == "table" then
            if key ~= nil then
                table.insert(out_list, tab .. key .. " =")
            end
            table.insert(out_list, tab .. "{")
            for k, v in pairs(value) do
                printk(v, k, tab .. "|  ")
            end
            table.insert(out_list, tab .. "},")
        else
            local content
            if type(value) == "nil" or value == "^&nil" then
                value = "nil"
            elseif type(value) == "string" then
                value = string.format("\"%s\"", tostring(value))
            else
                value = tostring(value)
            end
            content = string.format("%s%s = %s,", tab, key, value)
            table.insert(out_list, tostring(content))
        end
    end
    local value = type(content) == "string" and string.format(content, ...) or content
    local key = os.date("[\"%X\"]", os.time())
    printk(value, key, "")

    local out_str = table.concat(out_list, "\n")
    print(out_str .. "\n")

    -- local logFileName = os.date("print_tab_%Y_%m_%d.log", os.time())
    -- local logFileName = "a.log"
    -- local file = assert(io.open(logFileName, "a+"))
    -- file:write(out_str .. "\n")
    -- file:close()
end

--打印用户数据
function m:logUD(ud)
    local function tmp(metaT,dSet)  
        if metaT then
            for _val, _val_type in pairs(metaT) do
                if type(_val_type) ~= "userdata" then   
                    if not string.find(_val,"_") then                  
                        table.insert(dSet,_val)  
                    end        
                end  
            end
            table.sort(dSet)
            dSet[tostring(metaT)] = {}
            tmp(getmetatable(metaT),dSet[tostring(metaT)])      
        end  
    end
    local resTb = {}
    tmp(getmetatable(ud),resTb)
    m:printTT(resTb)
end
return m


打印结果

[LUA-print] ["21:06:18"] =
{
|  [1] = ".backup",
|  [2] = "create",
|  [3] = "createInstance",
|  [4] = "disableEffect",
|  [5] = "enableGlow",
|  [6] = "enableOutline",
|  [7] = "enableShadow",
|  [8] = "getAutoRenderSize",
|  [9] = "getEffectColor",
|  [10] = "getFontName",
|  [11] = "getFontSize",
|  [12] = "getLabelEffectType",
|  [13] = "getOutlineSize",
|  [14] = "getShadowBlurRadius",
|  [15] = "getShadowColor",
|  [16] = "getShadowOffset",
|  [17] = "getString",
|  [18] = "getStringLength",
|  [19] = "getStringValue",
|  [20] = "getTextAreaSize",
|  [21] = "getTe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值