|
- --[[
-
- Copyright (c) 2011-2012 qeeplay.com
-
- http://dualface.github.com/quick-cocos2d-x/
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
-
- ]]
-
- if CCLuaLog then
- io.output():setvbuf('no')
- elseif ngx and ngx.log then
- print = function(...)
- local arg = {...}
- for k,v in pairs(arg) do
- arg[k] = tostring(v)
- end
- ngx.log(ngx.ERR, table.concat(arg, "\t"))
- end
- end
-
- echo = print
-
- function printf(fmt, ...)
- echo(string.format(tostring(fmt), ...))
- end
-
- function echoError(fmt, ...)
- echoLog("ERR", fmt, ...)
- print(debug.traceback("", 2))
- end
-
- function echoInfo(fmt, ...)
- echoLog("INFO", fmt, ...)
- end
-
- function echoLog(tag, fmt, ...)
- echo(string.format("[%s] %s", string.upper(tostring(tag)), string.format(tostring(fmt), ...)))
- end
-
- function throw(errorType, fmt, ...)
- local arg = {...}
- for k,v in pairs(arg) do
- arg[k] = tostring(v)
- end
- local msg = string.format(tostring(fmt), unpack(arg))
- error(string.format("<<%s>> - %s", tostring(errorType), msg), 2)
- end
-
- function dump(object, label, isReturnContents, nesting)
- if type(nesting) ~= "number" then nesting = 99 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")
- echo("dump from: " .. string.trim(traceback[3]))
-
- local function _dump(object, label, indent, nest, keylen)
- label = label or "<var>"
- spc = ""
- if type(keylen) == "number" then
- spc = string.rep(" ", keylen - string.len(_v(label)))
- end
- if type(object) ~= "table" then
- result[#result +1 ] = string.format("%s%s%s = %s", indent, _v(label), spc, _v(object))
- elseif lookupTable[object] then
- result[#result +1 ] = string.format("%s%s%s = *REF*", indent, label, spc)
- else
- lookupTable[object] = true
- if nest > nesting then
- result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, label)
- else
- result[#result +1 ] = string.format("%s%s = {", indent, _v(label))
- local indent2 = indent.." "
- local keys = {}
- local keylen = 0
- local values = {}
- for k, v in pairs(object) 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(object, label, "- ", 1)
-
- if isReturnContents then
- return table.concat(result, "\n")
- end
-
- for i, line in ipairs(result) do
- echo(line)
- end
- end
-
- function vardump(object, label)
- local lookupTable = {}
- local result = {}
-
- local function _v(v)
- if type(v) == "string" then
- v = v:toLuaString()
- end
- return tostring(v)
- end
-
- local function _key(k)
- if type(k) == "string" then
- return '["' .. k .. '"]';
- else
- return '[' .. k .. ']';
- end
- end
-
- local function _vardump(object, label, indent, nest)
- label = label or "<var>"
- local postfix = ""
- if nest > 1 then postfix = "," end
- if type(object) ~= "table" then
- result[#result +1] = string.format("%s%s = %s%s", indent, _key(label), _v(object), postfix)
- elseif not lookupTable[object] then
- lookupTable[object] = true
-
- result[#result +1 ] = string.format("%s%s = {", indent, _key(label))
- local indent2 = indent .. " "
- local keys = {}
- local values = {}
- for k, v in pairs(object) do
- keys[#keys + 1] = k
- 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
- _vardump(values[k], k, indent2, nest + 1)
- end
- result[#result +1] = string.format("%s}%s", indent, postfix)
- end
- end
- _vardump(object, label, "", 1)
-
- return table.concat(result, "\n")
- end
|