cocos 模仿dump写了一个写错误日志的函数


--require("src.app.utils.FileUtil") 可能会需要包含这个文件
ErrorPath = "error.log"     --记录错误日志路径 默认与exe 同级目录
--写错误日志 表
function dumpError(value, desciption, nesting)

    io.writefile(ErrorPath, "===================================\n", "a")
    local osData = os.date("%Y年%m月%d日_%H时%M分%S秒",os.time())
    io.writefile(ErrorPath, osData .. "\n", "a")

    if type(nesting) ~= "number" then nesting = 3 end

    local lookupTable = {}
    local result = {}

    local function _v(v)
        if type(v) == "string" then
            v = "\"" .. v .. "\""
        end
        return tostring(v)
    end

    local traceback = string.split(debug.traceback("", 2), "\n")
    print("dump from: " .. string.trim(traceback[3]))

    local function _dump(value, desciption, indent, nest, keylen)
        desciption = desciption or ""
        spc = ""
        if type(keylen) == "number" then
            spc = string.rep(" ", keylen - string.len(_v(desciption)))
        end
        if type(value) ~= "table" then
            result[#result +1 ] = string.format("%s%s%s = %s", indent, _v(desciption), spc, _v(value))
        elseif lookupTable[value] then
            result[#result +1 ] = string.format("%s%s%s = *REF*", indent, desciption, spc)
        else
            lookupTable[value] = true
            if nest > nesting then
                result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, desciption)
            else
                result[#result +1 ] = string.format("%s%s = {", indent, _v(desciption))
                local indent2 = indent.."    "
                local keys = {}
                local keylen = 0
                local values = {}
                for k, v in pairs(value) do
                    keys[#keys + 1] = k
                    local vk = _v(k)
                    local vkl = string.len(vk)
                    if vkl > keylen then keylen = vkl end
                    values[k] = v
                end
                table.sort(keys, function(a, b)
                    if type(a) == "number" and type(b) == "number" then
                        return a < b
                    else
                        return tostring(a) < tostring(b)
                    end
                end)
                for i, k in ipairs(keys) do
                    _dump(values[k], k, indent2, nest + 1, keylen)
                end
                result[#result +1] = string.format("%s}", indent)
            end
        end
    end
    _dump(value, desciption, "- ", 1)

    for i, line in ipairs(result) do
        --print(line)
        io.writefile(ErrorPath, line .. "\n", "a")
    end

    io.writefile(ErrorPath, "\n", "a")
end

--写错误日志
function g_writeErrorLog(data, desciption, isClearWrite)

    if not desciption then
        desciption = "var"
    end

    if isClearWrite then            --是否清空历史log
        io.writefile(ErrorPath, "", "w")
    end

    if type(data) ~= "table" then
        io.writefile(ErrorPath, "===================================\n", "a")
        local osData = os.date("%Y年%m月%d日_%H时%M分%S秒",os.time())
        io.writefile(ErrorPath, osData .. "\n", "a")
    end

    local str = ""
    local function arrayToString(array)
        str = str .. "{\n"
        for k, v in pairs(array) do
            if type(v) == "number" then
                str = str .. k .. "=" .. v .. ",\n"
            elseif type(v) == "string" then
                str = str .. k .. "=" .. "\"" .. v .. "\"" .. ",\n"
            elseif type(v) == "table" then
                arrayToString(v)
            end
        end
        str = str .. "},\n"
        return str
    end

    if type(data) == "number" then

        str = desciption .. " = " .. data .. "\n"
        io.writefile(ErrorPath, str, "a")

    elseif type(data) == "string" then

        str = desciption .. " = " .. "\"" .. data .. "\"" .. "\n"
        io.writefile(ErrorPath, str, "a")

    elseif type(data) == "table" then

        dumpError(data, desciption, 10)

    end

end

你可能感兴趣的:(cocos 模仿dump写了一个写错误日志的函数)