cocos2dx lua开发调试打log方式

local platform = cc.Application:getInstance():getTargetPlatform()

local plats = {
    WINDOWS      = 0,
    LINUX        = 1,
    MAC          = 2,
    ANDROID      = 3,
    IPHONE       = 4,
    IPAD         = 5,
    BLACKBERRY   = 6,
    NACL         = 7,
    EMSCRIPTEN   = 8,
    TIZEN        = 9,
    WINRT        = 10,
    WP8          = 11,
}

local count = 0

local LEVEL =
{
    ["DEBUG"] = 1, 
    ["INFO" ] = 2, 
    ["WARN" ] = 3, 
    ["ERROR"] = 4, 
}

local IS_OPEN = {
    [LEVEL.DEBUG] = platform == plats.WINDOWS,
    [LEVEL.INFO]  = platform == plats.WINDOWS,
    [LEVEL.WARN]  = platform == plats.WINDOWS,
    [LEVEL.ERROR] = platform == plats.WINDOWS,
}

local function Filter(t, name, indent) 
    local tableList = {}   
    local function table_r (t, name, indent, full)   
        local id = not full and name or type(name)~="number" and tostring(name) or '['..name..']'   
        local tag = indent .. id .. ' = '   
        local out = {}
        if type(t) == "table" then   
            if tableList[t] ~= nil then   
                table.insert(out, tag .. '{} -- ' .. tableList[t] .. ' (self reference)')   
            else  
                tableList[t]= full and (full .. '.' .. id) or id  
                if next(t) then
                    table.insert(out, tag .. '{')   
                    for key,value in pairs(t) do   
                        table.insert(out,table_r(value,key,indent .. '   ',tableList[t]))   
                    end   
                    table.insert(out,indent .. '}')   
                else table.insert(out,tag .. '{}') end   
            end   
        else  
            local val = type(t)~="number" and type(t)~="boolean" and '"'..tostring(t)..'"' or tostring(t)   
            table.insert(out, tag .. val)   
        end   
        return table.concat(out, '\n')   
    end   
    return table_r(t,name or 'Value',indent or '')  
end

local function Dump(obj)
    local getIndent, quoteStr, wrapKey, wrapVal, dumpObj
    getIndent = function(level)
        return string.rep("\t", level)
    end
    quoteStr = function(str)
        return '"' .. string.gsub(str, '"', '\\"') .. '"'
    end
    wrapKey = function(val)
        if type(val) == "number" then
            return "[" .. val .. "]"
        elseif type(val) == "string" then
            return "[" .. quoteStr(val) .. "]"
        else
            return "[" .. tostring(val) .. "]"
        end
    end
    wrapVal = function(val, level)
        if type(val) == "table" then
            return dumpObj(val, level)
        elseif type(val) == "number" then
            return val
        elseif type(val) == "string" then
            return quoteStr(val)
        else
            return tostring(val)
        end
    end
    dumpObj = function(obj, level)
        if type(obj) ~= "table" then
            return wrapVal(obj)
        end
        level = level + 1
        local tokens = {}
        tokens[#tokens + 1] = "{"
        for k, v in pairs(obj) do
            tokens[#tokens + 1] = getIndent(level) .. wrapKey(k) .. " = " .. wrapVal(v, level) .. ","
        end
        tokens[#tokens + 1] = getIndent(level - 1) .. "}"
        return table.concat(tokens, "\n")
    end
    return dumpObj(obj, 0)
end

local function Write(t, level, name, indent)
    if not IS_OPEN[LEVEL[level]] then
        return
    end

    local info = Filter(t, name, indent)
    local file = assert(io.open("a.log", "a+"))
    file:write(info.."\n")
    file:close()
end

local function Log(name, level, t)
    count = count + 1

    local time = os.date("*t", os.time())
    local date  = string.format("%d:%d:%d", time.hour, time.min, time.sec)

    Write(t, level, string.format("[%s %s %s]#", level, date, count))
end

local function CreateLog(name, level)
    _G["log" .. name] = function(t)
        Log(name, level, t)
    end
end

CreateLog("d", "DEBUG")
CreateLog("i", "INFO")
CreateLog("w", "WARN")
CreateLog("e", "ERROR")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值