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")

你可能感兴趣的:(cocos2dx,lua)